📄 formwindow.cpp
字号:
QValueList<QPoint> op, np; for ( QWidget *i = lst.first(); i; i = lst.next() ) { op.append( i->pos() ); QPoint pos = pw->mapFromGlobal( i->mapToGlobal( QPoint( 0, 0 ) ) ); pos -= r.topLeft(); np.append( pos ); } MoveCommand *mv = new MoveCommand( tr( "Reparent Widgets" ), this, lst, op, np, insertParent, pw ); if ( !toolFixed ) mainwindow->resetTool(); else setCursorToAll( CrossCursor, w ); InsertCommand *cmd = new InsertCommand( tr( "Insert %1" ).arg( w->name() ), this, w, r ); QPtrList<Command> commands; commands.append( mv ); commands.append( cmd ); MacroCommand *mc = new MacroCommand( tr( "Insert %1" ).arg( w->name() ), this, commands ); commandHistory()->addCommand( mc ); mc->execute(); } else { if ( !toolFixed ) mainwindow->resetTool(); else setCursorToAll( CrossCursor, w ); InsertCommand *cmd = new InsertCommand( tr( "Insert %1" ).arg( w->name() ), this, w, r ); commandHistory()->addCommand( cmd ); cmd->execute(); } TemplateWizardInterface *iface = mainWindow()->templateWizardInterface( w->className() ); if ( iface ) { iface->setup( w->className(), w, iFace(), mainWindow()->designerInterface() ); iface->release(); }}void FormWindow::insertWidget( QWidget *w, bool checkName ){ if ( checkName ) { QString s = w->name(); unify( w, s, TRUE ); w->setName( s ); } MetaDataBase::addEntry( w ); int id = WidgetDatabase::idFromClassName( WidgetFactory::classNameOf(w) ); if ( WidgetDatabase::isCustomWidget( id ) ) { QWhatsThis::add( w, tr("<b>A %1 (custom widget)</b> " "<p>Click <b>Edit Custom Widgets...</b> in the <b>Tools|Custom</b> " "menu to add and change custom widgets. You can add " "properties as well as signals and slots to integrate custom widgets into " "<i>Qt Designer</i>, and provide a pixmap which will be used to represent " "the widget on the form.</p>") .arg(WidgetDatabase::toolTip( id )) ); QToolTip::add( w, tr("A %1 (custom widget)").arg(WidgetDatabase::toolTip( id )) ); } else { QString tt = WidgetDatabase::toolTip( id ); QString wt = WidgetDatabase::whatsThis( id ); if ( !wt.isEmpty() && !tt.isEmpty() ) QWhatsThis::add( w, QString("<b>A %1</b><p>%2</p>").arg( tt ).arg( wt ) ); } restoreCursors( w, this ); widgets()->insert( w, w ); w->show();}void FormWindow::removeWidget( QWidget *w ){ MetaDataBase::removeEntry( w ); widgets()->take( w );}void FormWindow::handleContextMenu( QContextMenuEvent *e, QWidget *w ){ CHECK_MAINWINDOW; switch ( currTool ) { case POINTER_TOOL: { if ( !isMainContainer( w ) && qstrcmp( w->name(), "central widget" ) != 0 ) { // press on a child widget raiseChildSelections( w ); // raise selections and select widget selectWidget( w ); // if widget is laid out, find the first non-laid out super-widget QWidget *realWidget = w; // but store the original one while ( w->parentWidget() && ( WidgetFactory::layoutType( w->parentWidget()) != WidgetFactory::NoLayout || !insertedWidgets.find(w) ) ) w = w->parentWidget(); if ( mainContainer()->inherits( "QMainWindow" ) && ( (QMainWindow*)mainContainer() )->centralWidget() == realWidget ) { e->accept(); mainwindow->popupFormWindowMenu( e->globalPos(), this ); } else { e->accept(); mainwindow->popupWidgetMenu( e->globalPos(), this, realWidget); } } else { e->accept(); clearSelection(); mainwindow->popupFormWindowMenu( e->globalPos(), this ); } break; } default: break; }}void FormWindow::handleMousePress( QMouseEvent *e, QWidget *w ){ CHECK_MAINWINDOW; checkedSelectionsForMove = FALSE; checkSelectionsTimer->stop(); if ( !sizePreviewLabel ) { sizePreviewLabel = new QLabel( this ); sizePreviewLabel->hide(); sizePreviewLabel->setBackgroundColor( QColor( 255, 255, 128 ) ); sizePreviewLabel->setFrameStyle( QFrame::Plain | QFrame::Box ); } switch ( currTool ) { case POINTER_TOOL: if ( !isMainContainer( w ) && qstrcmp( w->name(), "central widget" ) != 0 ) { // press on a child widget // if the clicked widget is not in a layout, raise it if ( !w->parentWidget() || WidgetFactory::layoutType( w->parentWidget() ) == WidgetFactory::NoLayout ) w->raise(); if ( ( e->state() & ControlButton ) ) { // with control pressed, always start rubber band selection drawRubber = TRUE; currRect = QRect( 0, 0, -1, -1 ); startRectDraw( mapFromGlobal( e->globalPos() ), e->globalPos(), this, Rubber ); break; } bool sel = isWidgetSelected( w ); if ( !( ( e->state() & ControlButton ) || ( e->state() & ShiftButton ) ) ) { // control not pressed... if ( !sel ) { // ...and widget no selectted: unselect all clearSelection( FALSE ); } else { // ...widget selected // only if widget has a layout (it is a layout meta widget or a laid out container!), unselect its childs if ( WidgetFactory::layoutType( w ) != WidgetFactory::NoLayout ) { QObjectList *l = w->queryList( "QWidget" ); setPropertyShowingBlocked( TRUE ); for ( QObject *o = l->first(); o; o = l->next() ) { if ( !o->isWidgetType() ) continue; if ( insertedWidgets.find( (QWidget*)o ) ) selectWidget( (QWidget*)o, FALSE ); } setPropertyShowingBlocked( FALSE ); delete l; } } qApp->processEvents(); } if ( ( ( e->state() & ControlButton ) || ( e->state() & ShiftButton ) ) && sel && e->button() == LeftButton ) { // control pressed and selected, unselect widget selectWidget( w, FALSE ); break; } raiseChildSelections( w ); // raise selections and select widget selectWidget( w ); // if widget is laid out, find the first non-laid out super-widget while ( w->parentWidget() && ( WidgetFactory::layoutType( w->parentWidget()) != WidgetFactory::NoLayout || !insertedWidgets.find(w) ) ) w = w->parentWidget(); if ( e->button() == LeftButton ) { // left button: store original geometry and more as the widget might start moving widgetPressed = TRUE; widgetGeom = QRect( w->pos(), w->size() ); oldPressPos = w->mapFromGlobal( e->globalPos() ); origPressPos = oldPressPos; checkedSelectionsForMove = FALSE; moving.clear(); if ( w->parentWidget() && !isMainContainer( w->parentWidget() ) && !isCentralWidget( w->parentWidget() ) ) { targetContainer = w->parentWidget(); hadOwnPalette = w->parentWidget()->ownPalette(); restorePalette = w->parentWidget()->palette(); } } } else { // press was on the formwindow if ( e->button() == LeftButton ) { // left button: start rubber selection and show formwindow properties drawRubber = TRUE; if ( !( ( e->state() & ControlButton ) || ( e->state() & ShiftButton ) ) ) { clearSelection( FALSE ); QObject *opw = propertyWidget; propertyWidget = mainContainer(); if ( opw->isWidgetType() ) repaintSelection( (QWidget*)opw ); } currRect = QRect( 0, 0, -1, -1 ); startRectDraw( mapFromGlobal( e->globalPos() ), e->globalPos(), this, Rubber ); } } break; case CONNECT_TOOL: case BUDDY_TOOL: if ( e->button() != LeftButton ) break; validForBuddy = FALSE; if ( currTool == BUDDY_TOOL ) { if ( !w->inherits( "QLabel" ) ) break; clearSelection( FALSE ); validForBuddy = TRUE; mainWindow()->statusBar()->message( tr( "Set buddy for '%1' to..." ).arg( w->name() ) ); } else { mainWindow()->statusBar()->message( tr( "Connect '%1' with..." ).arg( w->name() ) ); } saveBackground(); startPos = mapFromGlobal( e->globalPos() ); currentPos = startPos; startWidget = designerWidget( w ); endWidget = startWidget; beginUnclippedPainter( FALSE ); drawConnectionLine(); break; case ORDER_TOOL: if ( !isMainContainer( w ) ) { // press on a child widget orderedWidgets.removeRef( w ); orderedWidgets.append( w ); for ( QWidget *wid = orderedWidgets.last(); wid; wid = orderedWidgets.prev() ) { int i = stackedWidgets.findRef( wid ); if ( i != -1 ) { stackedWidgets.removeRef( wid ); stackedWidgets.insert( 0, wid ); } } QWidgetList oldl = MetaDataBase::tabOrder( this ); TabOrderCommand *cmd = new TabOrderCommand( tr( "Change Tab Order" ), this, oldl, stackedWidgets ); cmd->execute(); commandHistory()->addCommand( cmd, TRUE ); updateOrderIndicators(); } break; default: // any insert widget tool if ( e->button() == LeftButton ) { insertParent = WidgetFactory::containerOfWidget( mainContainer() ); // default parent for new widget is the formwindow if ( !isMainContainer( w ) ) { // press was not on formwindow, check if we can find another parent QWidget *wid = w; for (;;) { int id = WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( wid ) ); if ( ( WidgetDatabase::isContainer( id ) || wid == mainContainer() ) && !wid->inherits( "QLayoutWidget" ) && !wid->inherits( "QSplitter" ) ) { insertParent = WidgetFactory::containerOfWidget( wid ); // found another parent, store it break; } else { wid = wid->parentWidget(); if ( !wid ) break; } } } startRectDraw( w->mapFromGlobal( e->globalPos() ), e->globalPos(), w, Insert ); } break; }}void FormWindow::handleMouseDblClick( QMouseEvent *, QWidget *w ){ CHECK_MAINWINDOW; switch ( currTool ) { case ORDER_TOOL: if ( !isMainContainer( w ) ) { // press on a child widget orderedWidgets.clear(); orderedWidgets.append( w ); for ( QWidget *wid = orderedWidgets.last(); wid; wid = orderedWidgets.prev() ) { int i = stackedWidgets.findRef( wid ); if ( i != -1 ) { stackedWidgets.removeRef( wid ); stackedWidgets.insert( 0, wid ); } } QWidgetList oldl = MetaDataBase::tabOrder( this ); TabOrderCommand *cmd = new TabOrderCommand( tr( "Change Tab Order" ), this, oldl, stackedWidgets ); cmd->execute(); commandHistory()->addCommand( cmd, TRUE ); updateOrderIndicators(); } default: if ( !WidgetFactory::isPassiveInteractor( w ) && ( isMainContainer( w ) || w == this ) ) mainWindow()->editSource(); break; }}void FormWindow::handleMouseMove( QMouseEvent *e, QWidget *w ){ CHECK_MAINWINDOW; if ( ( e->state() & LeftButton ) != LeftButton ) return; QWidget *newendWidget = endWidget, *oldendWidget = endWidget, *wid; bool drawRecRect; switch ( currTool ) { case POINTER_TOOL: if ( widgetPressed && allowMove( w ) ) { // we are prepated for a move // if widget is laid out, find the first non-laid out super-widget while ( w->parentWidget() && ( WidgetFactory::layoutType( w->parentWidget() ) != WidgetFactory::NoLayout || !insertedWidgets.find(w ) ) ) w = w->parentWidget(); // calc correct position QPoint pos = w->mapFromGlobal( e->globalPos() );#if 0 QPoint mpos = w->mapToParent( pos ); // check if we are not outside the visible area, else correct coords if ( mpos.x() < 0 ) pos.setX( w->mapFromParent( QPoint( 0, mpos.y() ) ).x() ); if ( mpos.y() < 0 ) pos.setY( w->mapFromParent( QPoint( mpos.x(), 0 ) ).y() ); if ( mpos.x() > w->parentWidget()->width() ) pos.setX( w->mapFromParent( QPoint( w->parentWidget()->width(), mpos.y() ) ).x() ); if ( mpos.y() > w->parentWidget()->height() ) pos.setY( w->mapFromParent( QPoint( mpos.x(), w->parentWidget()->height() ) ).y() );#endif // calc move distance and store it QPoint d = oldPressPos - pos; if ( QABS( d.x() ) < grid().x() ) d.setX( 0 ); if ( QABS( d.y() ) < grid().y() ) d.setY( 0 ); if ( d.x() == 0 ) pos.setX( oldPressPos.x() ); if ( d.y() == 0 ) pos.setY( oldPressPos.y() ); oldPressPos = pos; // snap to grid int x = widgetGeom.x() - d.x(); widgetGeom.setX( x ); x = ( x / grid().x() ) * grid().x(); int y = widgetGeom.y() - d.y(); widgetGeom.setY( y ); y = ( y / grid().y() ) * grid().y(); QPoint p = w->pos(); if ( x - p.x() != 0 || y - p.y() != 0 ) { // if we actually have to move if ( !checkedSelectionsForMove ) { // if not checked yet, check if the correct widget are selected... if ( !isWidgetSelected( w ) ) { // and unselect others. Only siblings can be moved at the same time setPropertyShowingBlocked( TRUE ); selectWidget( w ); setPropertyShowingBlocked( FALSE ); } checkSelectionsForMove( w ); } // check whether we would have to reparent the selection and highlight the possible new parent container QMapConstIterator<ulong, QPoint> it = moving.begin(); QWidget* wa = containerAt( e->globalPos(), ( (QWidget*)it.key() ) ); if ( wa && !isMainContainer( wa ) && !isCentralWidget( wa ) ) { wa = WidgetFactory::containerOfWidget( wa ); // ok, looks like we moved onto a container if ( wa != targetContainer ) { if ( targetContainer ) { if ( hadOwnPalette ) targetContainer->setPalette( restorePalette ); else targetContainer->unsetPalette(); } targetContainer = wa; hadOwnPalette = wa->ownPalette(); restorePalette = wa->palette(); wa->setPaletteBackgroundColor( wa->colorGroup().midlight() ); } } else if ( targetContainer ) { if( hadOwnPalette ) targetContainer->setPalette( restorePalette ); else targetContainer->unsetPalette(); targetContainer = 0; } // finally move the selected widgets and show/update preview label moveSelectedWidgets( x - p.x(), y - p.y() ); sizePreviewLabel->setText( tr( "%1/%2" ).arg( w->pos().x() ).arg( w->pos().y() ) ); sizePreviewLabel->adjustSize(); QRect lg( mapFromGlobal( e->globalPos() ) + QPoint( 16, 16 ), sizePreviewLabel->size() ); checkPreviewGeometry( lg ); sizePreviewLabel->setGeometry( lg ); sizePreviewLabel->raise(); sizePreviewLabel->show();#if defined(Q_WS_WIN32) windowsRepaintWorkaroundTimer->start( 100, TRUE );#endif } else { // if we don't need to move, do some indication QRect lg( mapFromGlobal( e->globalPos() ) + QPoint( 16, 16 ), sizePreviewLabel->size() ); checkPreviewGeometry( lg ); sizePreviewLabel->move( lg.x(), lg.y() ); } oldPressPos += ( p - w->pos() ); } else if ( drawRubber ) { // draw rubber if we are in rubber-selection mode continueRectDraw( mapFromGlobal( e->globalPos() ), e->globalPos(), this, Rubber ); } break; case CONNECT_TOOL: restoreConnectionLine(); wid = qApp->widgetAt( e->globalPos(), TRUE ); if ( wid ) wid = designerWidget( wid ); if ( wid && ( isMainContainer( wid ) || insertedWidgets.find( wid ) ) && wid->isVisibleTo( this ) ) newendWidget = wid; if ( newendWidget && ( newendWidget->inherits( "QLayoutWidget" ) || newendWidget->inherits( "Spacer" ) ) ) newendWidget = (QWidget*)endWidget; drawRecRect = newendWidget != endWidget; if ( newendWidget && ( isMainContainer( newendWidget ) || insertedWidgets.find( newendWidget ) ) && !isCentralWidget( newendWidget ) ) endWidget = newendWidget; mainWindow()->statusBar()->message( tr( "Connect '%1' to '%2'" ).arg( startWidget->name() ). arg( endWidget->name() ) ); currentPos = mapFromGlobal( e->globalPos() ); qApp->processEvents(); if ( drawRecRect ) restoreRect( QRect( mapToForm( ( (QWidget*)oldendWidget )->parentWidget(), ( (QWidget*)oldendWidget )->pos() ),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -