📄 actiondnd.cpp
字号:
{ widgetInserting = FALSE; if ( e->button() == LeftButton && MainWindow::self->currentTool() != POINTER_TOOL && MainWindow::self->currentTool() != ORDER_TOOL && MainWindow::self->currentTool() != CONNECT_TOOL && MainWindow::self->currentTool() != BUDDY_TOOL ) widgetInserting = TRUE;}void QDesignerToolBar::mouseReleaseEvent( QMouseEvent *e ){ if ( widgetInserting ) doInsertWidget( mapFromGlobal( e->globalPos() ) ); widgetInserting = FALSE;}void QDesignerToolBar::buttonMouseReleaseEvent( QMouseEvent *e, QObject *w ){ if ( widgetInserting ) doInsertWidget( mapFromGlobal( e->globalPos() ) ); else if ( w->isWidgetType() && formWindow->widgets()->find( w ) ) { formWindow->clearSelection( FALSE ); formWindow->selectWidget( w ); } widgetInserting = FALSE;}void QDesignerToolBar::buttonContextMenuEvent( QContextMenuEvent *e, QObject *o ){ e->accept(); QPopupMenu menu( 0 ); const int ID_DELETE = 1; const int ID_SEP = 2; const int ID_DELTOOLBAR = 3; QMap<QWidget*, QAction*>::Iterator it = actionMap.find( (QWidget*)o ); if ( it != actionMap.end() && ::qt_cast<QSeparatorAction*>(*it) ) menu.insertItem( tr( "Delete Separator" ), ID_DELETE ); else menu.insertItem( tr( "Delete Item" ), ID_DELETE ); menu.insertItem( tr( "Insert Separator" ), ID_SEP ); menu.insertSeparator(); menu.insertItem( tr( "Delete Toolbar" ), ID_DELTOOLBAR ); int res = menu.exec( e->globalPos() ); if ( res == ID_DELETE ) { QMap<QWidget*, QAction*>::Iterator it = actionMap.find( (QWidget*)o ); if ( it == actionMap.end() ) return; QAction *a = *it; int index = actionList.find( a ); RemoveActionFromToolBarCommand *cmd = new RemoveActionFromToolBarCommand( tr( "Delete Action '%1' from Toolbar '%2'" ). arg( a->name() ).arg( caption() ), formWindow, a, this, index ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); } else if ( res == ID_SEP ) { calcIndicatorPos( mapFromGlobal( e->globalPos() ) ); QAction *a = new QSeparatorAction( 0 ); int index = actionList.findRef( *actionMap.find( insertAnchor ) ); if ( index != -1 && afterAnchor ) ++index; if ( !insertAnchor ) index = 0; AddActionToToolBarCommand *cmd = new AddActionToToolBarCommand( tr( "Add Separator to Toolbar '%1'" ). arg( a->name() ), formWindow, a, this, index ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); } else if ( res == ID_DELTOOLBAR ) { RemoveToolBarCommand *cmd = new RemoveToolBarCommand( tr( "Delete Toolbar '%1'" ).arg( name() ), formWindow, 0, this ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); }}void QDesignerToolBar::buttonMousePressEvent( QMouseEvent *e, QObject * ){ widgetInserting = FALSE; if ( e->button() == MidButton ) return; if ( e->button() == LeftButton && MainWindow::self->currentTool() != POINTER_TOOL && MainWindow::self->currentTool() != ORDER_TOOL && MainWindow::self->currentTool() != CONNECT_TOOL && MainWindow::self->currentTool() != BUDDY_TOOL ) { widgetInserting = TRUE; return; } dragStartPos = e->pos();}void QDesignerToolBar::removeWidget( QWidget *w ){ QMap<QWidget*, QAction*>::Iterator it = actionMap.find( w ); if ( it == actionMap.end() ) return; QAction *a = *it; int index = actionList.find( a ); RemoveActionFromToolBarCommand *cmd = new RemoveActionFromToolBarCommand( tr( "Delete Action '%1' from Toolbar '%2'" ). arg( a->name() ).arg( caption() ), formWindow, a, this, index ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); QApplication::sendPostedEvents(); adjustSize();}void QDesignerToolBar::buttonMouseMoveEvent( QMouseEvent *e, QObject *o ){ if ( widgetInserting || ( e->state() & LeftButton ) == 0 ) return; if ( QABS( QPoint( dragStartPos - e->pos() ).manhattanLength() ) < QApplication::startDragDistance() ) return; QMap<QWidget*, QAction*>::Iterator it = actionMap.find( (QWidget*)o ); if ( it == actionMap.end() ) return; QAction *a = *it; if ( !a ) return; int index = actionList.find( a ); RemoveActionFromToolBarCommand *cmd = new RemoveActionFromToolBarCommand( tr( "Delete Action '%1' from Toolbar '%2'" ). arg( a->name() ).arg( caption() ), formWindow, a, this, index ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); QApplication::sendPostedEvents(); adjustSize(); QString type = ::qt_cast<QActionGroup*>(a) ? QString( "application/x-designer-actiongroup" ) : ::qt_cast<QSeparatorAction*>(a) ? QString( "application/x-designer-separator" ) : QString( "application/x-designer-actions" ); QStoredDrag *drag = new ActionDrag( type, a, this ); drag->setPixmap( a->iconSet().pixmap() ); if ( ::qt_cast<QDesignerAction*>(a) ) { if ( formWindow->widgets()->find( ( (QDesignerAction*)a )->widget() ) ) formWindow->selectWidget( ( (QDesignerAction*)a )->widget(), FALSE ); } if ( !drag->drag() ) { AddActionToToolBarCommand *cmd = new AddActionToToolBarCommand( tr( "Add Action '%1' to Toolbar '%2'" ). arg( a->name() ).arg( caption() ), formWindow, a, this, index ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); } lastIndicatorPos = QPoint( -1, -1 ); indicator->hide();}#ifndef QT_NO_DRAGANDDROPvoid QDesignerToolBar::dragEnterEvent( QDragEnterEvent *e ){ widgetInserting = FALSE; lastIndicatorPos = QPoint( -1, -1 ); if (ActionDrag::canDecode(e)) e->accept();}void QDesignerToolBar::dragMoveEvent( QDragMoveEvent *e ){ if (ActionDrag::canDecode(e)) { e->accept(); drawIndicator( calcIndicatorPos( e->pos() ) ); }}void QDesignerToolBar::dragLeaveEvent( QDragLeaveEvent * ){ indicator->hide(); insertAnchor = 0; afterAnchor = TRUE;}void QDesignerToolBar::dropEvent( QDropEvent *e ){ if (!ActionDrag::canDecode(e)) return; e->accept(); indicator->hide(); QAction *a = 0; int index = actionList.findRef( *actionMap.find( insertAnchor ) ); if ( index != -1 && afterAnchor ) ++index; if ( !insertAnchor ) index = 0; if ( e->provides( "application/x-designer-actions" ) || e->provides( "application/x-designer-separator" ) ) { if ( e->provides( "application/x-designer-actions" ) ) a = ::qt_cast<QDesignerAction*>(ActionDrag::action()); else a = ::qt_cast<QSeparatorAction*>(ActionDrag::action()); } else { a = ::qt_cast<QDesignerActionGroup*>(ActionDrag::action()); } if ( actionList.findRef( a ) != -1 ) { QMessageBox::warning( MainWindow::self, tr( "Insert/Move Action" ), tr( "Action '%1' has already been added to this toolbar.\n" "An Action may only occur once in a given toolbar." ). arg( a->name() ) ); return; } AddActionToToolBarCommand *cmd = new AddActionToToolBarCommand( tr( "Add Action '%1' to Toolbar '%2'" ). arg( a->name() ).arg( caption() ), formWindow, a, this, index ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); lastIndicatorPos = QPoint( -1, -1 );}#endifvoid QDesignerToolBar::reInsert(){ QAction *a = 0; actionMap.clear(); clear(); for ( a = actionList.first(); a; a = actionList.next() ) { a->addTo( this ); if ( ::qt_cast<QActionGroup*>(a) ) { actionMap.insert( ( (QDesignerActionGroup*)a )->widget(), a ); if ( ( (QDesignerActionGroup*)a )->widget() ) ( (QDesignerActionGroup*)a )->widget()->installEventFilter( this ); } else if ( ::qt_cast<QDesignerAction*>(a) ) { actionMap.insert( ( (QDesignerAction*)a )->widget(), a ); ( (QDesignerAction*)a )->widget()->installEventFilter( this ); } else if ( ::qt_cast<QSeparatorAction*>(a) ) { actionMap.insert( ( (QSeparatorAction*)a )->widget(), a ); ( (QSeparatorAction*)a )->widget()->installEventFilter( this ); } } QApplication::sendPostedEvents(); adjustSize();}void QDesignerToolBar::actionRemoved(){ actionList.removeRef( (QAction*)sender() );}QPoint QDesignerToolBar::calcIndicatorPos( const QPoint &pos ){ if ( orientation() == Horizontal ) { QPoint pnt( width() - 2, 0 ); insertAnchor = 0; afterAnchor = TRUE; if ( !children() ) return pnt; pnt = QPoint( 13, 0 ); QObjectListIt it( *children() ); QObject * obj; while( (obj=it.current()) != 0 ) { ++it; if ( obj->isWidgetType() && qstrcmp( "qt_dockwidget_internal", obj->name() ) != 0 ) { QWidget *w = (QWidget*)obj; if ( w->x() < pos.x() ) { pnt.setX( w->x() + w->width() + 1 ); insertAnchor = w; afterAnchor = TRUE; } } } return pnt; } else { QPoint pnt( 0, height() - 2 ); insertAnchor = 0; afterAnchor = TRUE; if ( !children() ) return pnt; pnt = QPoint( 0, 13 ); QObjectListIt it( *children() ); QObject * obj; while( (obj=it.current()) != 0 ) { ++it; if ( obj->isWidgetType() && qstrcmp( "qt_dockwidget_internal", obj->name() ) != 0 ) { QWidget *w = (QWidget*)obj; if ( w->y() < pos.y() ) { pnt.setY( w->y() + w->height() + 1 ); insertAnchor = w; afterAnchor = TRUE; } } } return pnt; }}void QDesignerToolBar::drawIndicator( const QPoint &pos ){ if ( lastIndicatorPos == pos ) return; bool wasVsisible = indicator->isVisible(); if ( orientation() == Horizontal ) { indicator->resize( 3, height() ); if ( pos != QPoint( -1, -1 ) ) indicator->move( pos.x() - 1, 0 ); indicator->show(); indicator->raise(); lastIndicatorPos = pos; } else { indicator->resize( width(), 3 ); if ( pos != QPoint( -1, -1 ) ) indicator->move( 0, pos.y() - 1 ); indicator->show(); indicator->raise(); lastIndicatorPos = pos; } if ( !wasVsisible ) QApplication::sendPostedEvents();}void QDesignerToolBar::doInsertWidget( const QPoint &p ){ if ( formWindow != MainWindow::self->formWindow() ) return; calcIndicatorPos( p ); QWidget *w = WidgetFactory::create( MainWindow::self->currentTool(), this, 0, TRUE ); installEventFilters( w ); MainWindow::self->formWindow()->insertWidget( w, TRUE ); QDesignerAction *a = new QDesignerAction( w, parent() ); int index = actionList.findRef( *actionMap.find( insertAnchor ) ); if ( index != -1 && afterAnchor ) ++index; if ( !insertAnchor ) index = 0; AddActionToToolBarCommand *cmd = new AddActionToToolBarCommand( tr( "Add Widget '%1' to Toolbar '%2'" ). arg( w->name() ).arg( caption() ), formWindow, a, this, index ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); MainWindow::self->resetTool();}void QDesignerToolBar::clear(){ for ( QAction *a = actionList.first(); a; a = actionList.next() ) { if ( ::qt_cast<QDesignerAction*>(a) ) ( (QDesignerAction*)a )->remove(); } QToolBar::clear();}void QDesignerToolBar::installEventFilters( QWidget *w ){ if ( !w ) return; QObjectList *l = w->queryList( "QWidget" ); for ( QObject *o = l->first(); o; o = l->next() ) o->installEventFilter( this ); delete l;}#include "actiondnd.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -