formwindow.cpp

来自「qt-x11-free-3.0.3.tar.gz minigui图形界面工具」· C++ 代码 · 共 2,164 行 · 第 1/5 页

CPP
2,164
字号
	    // tell property editor to update	    if ( propertyWidget && propertyWidget->isWidgetType() && !isMainContainer( propertyWidget ) )		emitUpdateProperties( propertyWidget );	    QMapConstIterator<ulong,QPoint> it = moving.begin();	    QWidget *oldParent = ( (QWidget*)it.key() )->parentWidget();	    QWidget *newParent = oldParent;	    // check whether we have to reparent the selection	    QWidget* wa = containerAt( e->globalPos(), ( (QWidget*)it.key() ) );	    if ( wa ) {		wa = WidgetFactory::containerOfWidget( wa );		// ok, looks like we moved onto a container		// check whether we really have different parents.		if ( wa == ( (QWidget*)it.key() )->parentWidget() )		    goto make_move_command;		// break layout if necessary		if ( WidgetFactory::layoutType( wa ) != WidgetFactory::NoLayout ) {		    if ( QMessageBox::information( mainWindow(), tr( "Inserting a Widget" ),						   tr( "You tried to insert a widget into the "						       "layout Container Widget '%1'.\n"						       "This is not possible. "						       "In order to insert the widget, the layout of '%1'\n"						       "must first be broken.\n"						       "Break the layout or cancel the operation?" ).						   arg( wa->name() ).						   arg( wa->name() ), tr( "&Break Layout" ), tr( "&Cancel" ) ) )			goto make_move_command; // cancel		    breakLayout( wa );		}		// doesn't need to be a command, the MoveCommand does reparenting too		bool emitSelChanged = FALSE;		for ( QMap<ulong, QPoint>::Iterator it = moving.begin(); it != moving.end(); ++it ) {		    QWidget *i = (QWidget*)it.key();		    if ( !emitSelChanged && i->inherits( "QButton" )  ) {			if ( i->parentWidget() && i->parentWidget()->inherits( "QButtonGroup" ) ||			     wa->inherits( "QButtonGroup" ) )			    emitSelChanged = TRUE;			if ( !wa->inherits( "QButtonGroup" ) ) {			    MetaDataBase::setPropertyChanged( i, "buttonGroupId", FALSE );			    if ( i->parentWidget() && i->parentWidget()->inherits( "QButtonGroup" ) )				( (QButtonGroup*)i->parentWidget() )->remove( (QButton*)i );			}		    }		    QPoint pos = wa->mapFromGlobal( i->mapToGlobal( QPoint(0,0) ) );		    i->reparent( wa, pos, TRUE );		    raiseSelection( i );		    raiseChildSelections( i );		    widgetChanged( i );		    mainWindow()->objectHierarchy()->widgetRemoved( i );		    mainWindow()->objectHierarchy()->widgetInserted( i );		}		if ( emitSelChanged ) {		    emit showProperties( wa );		    emit showProperties( propertyWidget );		}		newParent = wa;	    }	make_move_command:	    QWidgetList widgets; // collect the widgets and its old and new positions which have been moved	    QValueList<QPoint> oldPos, newPos;	    for ( it = moving.begin(); it != moving.end(); ++it ) {		widgets.append( (QWidget*)it.key() );		oldPos.append( *it );		newPos.append( ( (QWidget*)it.key() )->pos() );	    }	    // add move command, don't execute it, this is just a summary of the operations we did during the move-event handling	    commandHistory()->addCommand( new MoveCommand( tr( "Move" ),							   this, widgets,							   oldPos, newPos, oldParent, newParent ) );	} else if ( drawRubber ) { // we were drawing a rubber selection	    endRectDraw(); // get rid of the rectangle	    blockSignals( TRUE );	    selectWidgets(); // select widgets which intersect the rect	    blockSignals( FALSE );	    emitSelectionChanged(); // inform about selection changes	    if ( propertyWidget )		emitShowProperties( propertyWidget );	}	break;    case CONNECT_TOOL:	restoreConnectionLine();	if ( connectSender )	    restoreRect( QRect( mapToForm(  ( (QWidget*)connectSender )->parentWidget(),					    ( (QWidget*)connectSender )->pos() ),				( (QWidget*)connectSender )->size() ) );	if ( connectReceiver )	    restoreRect( QRect( mapToForm( ( (QWidget*)connectReceiver )->parentWidget(),					   ( (QWidget*)connectReceiver )->pos() ),				( (QWidget*)connectReceiver )->size() ) );	endUnclippedPainter();	qApp->processEvents();	if ( connectSender && connectReceiver )	    editConnections();	break;    case ORDER_TOOL:	break;    default: // any insert widget tool is active	if ( insertParent ) { // we should insert the new widget now	    endRectDraw();	    if ( WidgetFactory::layoutType( insertParent ) != WidgetFactory::NoLayout ) {		if ( QMessageBox::information( mainWindow(), tr( "Inserting a Widget" ),					       tr( "You tried to insert a widget into the "						   "layout Container Widget '%1'.\n"						   "This is not possible. "						   "In order to insert the widget, the layout of '%1'\n"						   "must first be broken.\n"						   "Break the layout or cancel the operation?" ).					       arg( insertParent->name() ).					       arg( insertParent->name() ), tr( "&Break Layout" ), tr( "&Cancel" ) ) == 0 ) {		    breakLayout( insertParent );		} else {		    if ( !toolFixed )			mainWindow()->resetTool();		    break;		}	    }	    insertWidget(); // so do it	}	break;    }    widgetPressed = FALSE;    drawRubber = FALSE;    insertParent = 0;    delete buffer;    buffer = 0;}void FormWindow::handleKeyPress( QKeyEvent *e, QWidget *w ){    e->ignore();    checkSelectionsTimer->stop();    if ( !checkedSelectionsForMove &&	 ( e->key() == Key_Left ||	   e->key() == Key_Right ||	   e->key() == Key_Up ||	   e->key() == Key_Down ) &&	 propertyWidget->isWidgetType() )	checkSelectionsForMove( (QWidget*)propertyWidget );    checkSelectionsTimer->start( 1000, TRUE );    if ( e->key() == Key_Left || e->key() == Key_Right ||	 e->key() == Key_Up || e->key() == Key_Down ) {	QWidgetList widgets;	QValueList<QPoint> oldPos, newPos;	for ( WidgetSelection *s = selections.first(); s; s = selections.next() ) {	    if ( s->isUsed() ) {		int dx = 0, dy = 0;		bool control = e->state() & ControlButton;		switch ( e->key() ) {		case Key_Left: {		    e->accept();		    if ( control )			dx = -1;		    else			dx = -grid().x();		} break;		case Key_Right: {		    e->accept();		    if ( control )			dx = 1;		    else			dx = grid().x();		} break;		case Key_Up: {		    e->accept();		    if ( control )			dy = -1;		    else			dy = -grid().y();		} break;		case Key_Down: {		    e->accept();		    if ( control )			dy = 1;		    else			dy = grid().y();		} break;		default:		    break;		}		widgets.append( s->widget() );		oldPos.append( s->widget()->pos() );		newPos.append( s->widget()->pos() + QPoint( dx, dy ) );	    }	}	if ( !widgets.isEmpty() ) {	    MoveCommand *cmd = new MoveCommand( tr( "Move" ), this,						widgets, oldPos, newPos, 0, 0 );	    commandHistory()->addCommand( cmd, TRUE );	    cmd->execute();	}    }    if ( !e->isAccepted() ) {	QObjectList *l = queryList( "QWidget" );	if ( !l )	    return;	if ( l->find( w ) != -1 )	    e->accept();	delete l;    }}void FormWindow::handleKeyRelease( QKeyEvent *e, QWidget * ){    e->ignore();}void FormWindow::selectWidget( QObject *o, bool select ){    if ( !o->isWidgetType() ) {	// ########### do QObject stuff	return;    }    QWidget *w = (QWidget*)o;    if ( isMainContainer( w ) ) {	QObject *opw = propertyWidget;	propertyWidget = mainContainer();	if ( opw->isWidgetType() )	    repaintSelection( (QWidget*)opw );	emitShowProperties( propertyWidget );	return;    }    if ( mainContainer()->inherits( "QMainWindow" ) &&	 w == ( (QMainWindow*)mainContainer() )->centralWidget() ) {	QObject *opw = propertyWidget;	propertyWidget = mainContainer();	if ( opw->isWidgetType() )	    repaintSelection( (QWidget*)opw );	emitShowProperties( propertyWidget );	return;    }    if ( o->inherits( "QDesignerToolBar" ) || o->inherits( "QDesignerMenuBar" ) )	return;    if ( select ) {	QObject *opw = propertyWidget;	propertyWidget = w;	if ( opw->isWidgetType() )	    repaintSelection( (QWidget*)opw );	if ( !isPropertyShowingBlocked() )	    emitShowProperties( propertyWidget );	WidgetSelection *s = usedSelections.find( w );	if ( s ) {	    s->show();	    return;	}	for ( WidgetSelection *s2 = selections.first(); s2; s2 = selections.next() ) {	    if ( !s2->isUsed() ) {		s = s2;	    }	}	if ( !s ) {	    s = new WidgetSelection( this, &usedSelections );	    selections.append( s );	}	s->setWidget( w );	emitSelectionChanged();    } else {	WidgetSelection *s = usedSelections.find( w );	if ( s )	    s->setWidget( 0 );	QObject *opw = propertyWidget;	if ( !usedSelections.isEmpty() )	    propertyWidget = QPtrDictIterator<WidgetSelection>( usedSelections ).current()->widget();	else	    propertyWidget = mainContainer();	if ( opw->isWidgetType() )	    repaintSelection( (QWidget*)opw );	if ( !isPropertyShowingBlocked() )	    emitShowProperties( propertyWidget );	emitSelectionChanged();    }}QPoint FormWindow::grid() const{    if ( !mainWindow()->snapGrid() )	return QPoint( 1, 1 );    return mainWindow()->grid();}void FormWindow::updateSelection( QWidget *w ){    WidgetSelection *s = usedSelections.find( w );    if ( !w->isVisibleTo( this ) )	selectWidget( w, FALSE );    else if ( s )	s->updateGeometry();}void FormWindow::raiseSelection( QWidget *w ){    WidgetSelection *s = usedSelections.find( w );    if ( s )	s->show();}void FormWindow::repaintSelection( QWidget *w ){    WidgetSelection *s = usedSelections.find( w );    if ( s )	s->update();}void FormWindow::clearSelection( bool changePropertyDisplay ){    QPtrDictIterator<WidgetSelection> it( usedSelections );    for ( ; it.current(); ++it )	it.current()->setWidget( 0, FALSE );    usedSelections.clear();    if ( changePropertyDisplay ) {	QObject *opw = propertyWidget;	propertyWidget = mainContainer();	if ( opw->isWidgetType() )	    repaintSelection( (QWidget*)opw );	emitShowProperties( propertyWidget );    }    emitSelectionChanged();}void FormWindow::startRectDraw( const QPoint &p, const QPoint &global, QWidget *, RectType t ){    QPoint pos( p );    pos = mapFromGlobal( global );    oldRectValid = FALSE;    beginUnclippedPainter( TRUE );    if ( t == Rubber )	unclippedPainter->setPen( QPen( color0, 1 ) );    if ( t == Insert )	rectAnchor = gridPoint( pos );    else if ( t == Rubber )	rectAnchor = pos;    currRect = QRect( rectAnchor, QPoint( 0, 0 ) );    if ( t == Insert )	drawSizePreview( pos, tr("Use Size Hint") );}void FormWindow::continueRectDraw( const QPoint &p, const QPoint &global, QWidget *, RectType t ){    QPoint pos =p;    pos = mapFromGlobal( global );    QPoint p2;    if ( t == Insert )	p2 = gridPoint( pos );    else if ( t == Rubber )	p2 = pos;    QRect r( rectAnchor, p2 );    r = r.normalize();    if ( currRect == r ) {	QString t = tr( "%1/%2" );	t = t.arg( r.width() - 1 ).arg( r.height() - 1 );	drawSizePreview( pos, t );	return;    }    if ( oldRectValid )	unclippedPainter->drawRect( currRect );    if ( r.width() > 1 || r.height() > 1 ) {	oldRectValid = TRUE;	currRect = r;	if ( t == Insert ) {	    QString t = tr( "%1/%2" );	    t = t.arg( r.width() - 1 ).arg( r.height() - 1 );	    drawSizePreview( pos, t );	}	unclippedPainter->setClipRegion( QRegion( rect() ).subtract( QRect( sizePreviewPos, sizePreviewPixmap.size() ) ) );	unclippedPainter->drawRect( currRect );	unclippedPainter->setClipping( FALSE );    } else {	oldRectValid = FALSE;	if ( t == Insert )	    drawSizePreview( pos, tr("Use Size Hint") );    }}void FormWindow::endRectDraw(){    if ( !unclippedPainter )	return;    if ( oldRectValid )	unclippedPainter->drawRect( currRect );    drawSizePreview( QPoint(-1,-1), QString::null );    endUnclippedPainter();}void FormWindow::selectWidgets(){    QObjectList *l = mainContainer()->queryList( "QWidget" );    if ( l ) {	for ( QObject *o = l->first(); o; o = l->next() ) {	    if ( ( (QWidget*)o )->isVisibleTo( this ) &&		 insertedWidgets[ (void*)o ] ) {		QPoint p = ( (QWidget*)o )->mapToGlobal( QPoint(0,0) );		p = mapFromGlobal( p );		QRect r( p, ( (QWidget*)o )->size() );		if ( r.intersects( currRect ) && !r.contains( currRect ) )		    selectWidget( (QWidget*)o );	    }	}	delete l;    }    emitSelectionChanged();}bool FormWindow::isWidgetSelected( QObject *w ){    if ( w->isWidgetType() )	return usedSelections.find( (QWidget*)w ) != 0;    return FALSE; // #### do stuff for QObjects}void FormWindow::moveSelectedWidgets( int dx, int dy ){    QPtrDictIterator<WidgetSelection> it( usedSelections );    for ( ; it.current(); ++it ) {	WidgetSelection *s = it.current();	QWidget *w = s->widget();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?