📄 actiondnd.cpp
字号:
/************************************************************************ Copyright (C) 2000-2002 Trolltech AS. All rights reserved.**** This file is part of Qt Designer.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "actiondnd.h"#include "command.h"#include "defs.h"#include "formwindow.h"#include "mainwindow.h"#include "metadatabase.h"#include "widgetfactory.h"#include "hierarchyview.h"#include <qaction.h>#include <qapplication.h>#include <qbitmap.h>#include <qdragobject.h>#include <qinputdialog.h>#include <qlayout.h>#include <qmainwindow.h>#include <qmenudata.h>#include <qmessagebox.h>#include <qobjectlist.h>#include <qpainter.h>#include <qstyle.h>#include <qtimer.h>void QDesignerAction::init(){ MetaDataBase::addEntry( this ); int id = WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( this ) ); WidgetFactory::saveDefaultProperties( this, id ); WidgetFactory::saveChangedProperties( this, id );}void QDesignerActionGroup::init(){ MetaDataBase::addEntry( this ); int id = WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( this ) ); WidgetFactory::saveDefaultProperties( this, id ); WidgetFactory::saveChangedProperties( this, id );}bool QDesignerAction::addTo( QWidget *w ){ if ( !widgetToInsert ) return QAction::addTo( w ); if ( w->inherits( "QPopupMenu" ) ) return FALSE; widgetToInsert->reparent( w, QPoint( 0, 0 ), FALSE ); addedTo( widgetToInsert, w ); return TRUE;}bool QDesignerAction::removeFrom( QWidget *w ){ if ( !widgetToInsert ) return QAction::removeFrom( w ); remove(); return TRUE;}void QDesignerAction::remove(){ if ( !widgetToInsert ) return; MainWindow::self->formWindow()->selectWidget( widgetToInsert, FALSE ); widgetToInsert->reparent( 0, QPoint( 0, 0 ), FALSE );}class QDesignerIndicatorWidget : public QWidget{ Q_OBJECTpublic: QDesignerIndicatorWidget( QWidget *p ) : QWidget( p, "qt_dockwidget_internal" ) { setBackgroundColor( red ); }};QDesignerToolBarSeparator::QDesignerToolBarSeparator(Orientation o , QToolBar *parent, const char* name ) : QWidget( parent, name ){ connect( parent, SIGNAL(orientationChanged(Orientation)), this, SLOT(setOrientation(Orientation)) ); setOrientation( o ); setBackgroundMode( parent->backgroundMode() ); setBackgroundOrigin( ParentOrigin ); setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) );}void QDesignerToolBarSeparator::setOrientation( Orientation o ){ orient = o;}void QDesignerToolBarSeparator::styleChange( QStyle& ){ setOrientation( orient );}QSize QDesignerToolBarSeparator::sizeHint() const{ int extent = style().pixelMetric( QStyle::PM_DockWindowSeparatorExtent, this ); if ( orient == Horizontal ) return QSize( extent, 0 ); else return QSize( 0, extent );}void QDesignerToolBarSeparator::paintEvent( QPaintEvent * ){ QPainter p( this ); QStyle::SFlags flags = QStyle::Style_Default; if ( orientation() == Horizontal ) flags |= QStyle::Style_Horizontal; style().drawPrimitive( QStyle::PE_DockWindowSeparator, &p, rect(), colorGroup(), flags );}QSeparatorAction::QSeparatorAction( QObject *parent ) : QAction( parent, "qt_designer_separator" ), wid( 0 ){}bool QSeparatorAction::addTo( QWidget *w ){ if ( w->inherits( "QToolBar" ) ) { QToolBar *tb = (QToolBar*)w; wid = new QDesignerToolBarSeparator( tb->orientation(), tb ); return TRUE; } else if ( w->inherits( "QPopupMenu" ) ) { idx = ( (QPopupMenu*)w )->count(); ( (QPopupMenu*)w )->insertSeparator( idx ); return TRUE; } return FALSE;}bool QSeparatorAction::removeFrom( QWidget *w ){ if ( w->inherits( "QToolBar" ) ) { delete wid; return TRUE; } else if ( w->inherits( "QPopupMenu" ) ) { ( (QPopupMenu*)w )->removeItemAt( idx ); return TRUE; } return FALSE;}QWidget *QSeparatorAction::widget() const{ return wid;}QSubMenuAction::QSubMenuAction( QObject *parent ) : QAction( parent, "qt_designer_separator" ), popup( 0 ){}bool QSubMenuAction::addTo( QWidget *w ){ idx = ( (QPopupMenu*)w )->count(); if ( !popup ) popup = new QDesignerPopupMenu( MainWindow::self ); ( (QPopupMenu*)w )->insertItem( menuText(), popup, -1, idx ); return TRUE;}bool QSubMenuAction::removeFrom( QWidget *w ){ ( (QPopupMenu*)w )->removeItemAt( idx ); return TRUE;}QPopupMenu *QSubMenuAction::popupMenu() const{ return popup;}QDesignerToolBar::QDesignerToolBar( QMainWindow *mw ) : QToolBar( mw ), lastIndicatorPos( -1, -1 ){ insertAnchor = 0; afterAnchor = TRUE; setAcceptDrops( TRUE ); MetaDataBase::addEntry( this ); lastIndicatorPos = QPoint( -1, -1 ); indicator = new QDesignerIndicatorWidget( this ); indicator->hide(); installEventFilter( this ); widgetInserting = FALSE; findFormWindow(); mw->setDockEnabled( DockTornOff, FALSE );}QDesignerToolBar::QDesignerToolBar( QMainWindow *mw, Dock dock ) : QToolBar( QString::null, mw, dock), lastIndicatorPos( -1, -1 ){ insertAnchor = 0; afterAnchor = TRUE; setAcceptDrops( TRUE ); indicator = new QDesignerIndicatorWidget( this ); indicator->hide(); MetaDataBase::addEntry( this ); installEventFilter( this ); widgetInserting = FALSE; findFormWindow(); mw->setDockEnabled( DockTornOff, FALSE );}void QDesignerToolBar::findFormWindow(){ QWidget *w = this; while ( w ) { if ( w->inherits( "FormWindow" ) ) formWindow = (FormWindow*)w; w = w->parentWidget(); }}void QDesignerToolBar::addAction( QAction *a ){ actionList.append( a ); connect( a, SIGNAL( destroyed() ), this, SLOT( actionRemoved() ) ); if ( a->inherits( "QActionGroup" ) ) { ( (QDesignerActionGroup*)a )->widget()->installEventFilter( this ); actionMap.insert( ( (QDesignerActionGroup*)a )->widget(), a ); } else if ( a->inherits( "QSeparatorAction" ) ) { ( (QSeparatorAction*)a )->widget()->installEventFilter( this ); actionMap.insert( ( (QSeparatorAction*)a )->widget(), a ); } else { ( (QDesignerAction*)a )->widget()->installEventFilter( this ); actionMap.insert( ( (QDesignerAction*)a )->widget(), a ); }}static void fixObject( QObject *&o ){ while ( o && o->parent() && !o->parent()->inherits( "QDesignerToolBar" ) ) o = o->parent();}bool QDesignerToolBar::eventFilter( QObject *o, QEvent *e ){ if ( !o || !e || o->inherits( "QDockWindowHandle" ) || o->inherits( "QDockWindowTitleBar" ) ) return QToolBar::eventFilter( o, e ); if ( o == this && e->type() == QEvent::MouseButtonPress && ( ( QMouseEvent*)e )->button() == LeftButton ) { mousePressEvent( (QMouseEvent*)e ); return TRUE; } if ( o == this ) return QToolBar::eventFilter( o, e ); if ( e->type() == QEvent::MouseButtonPress ) { QMouseEvent *ke = (QMouseEvent*)e; fixObject( o ); if ( !o ) return FALSE; buttonMousePressEvent( ke, o ); return TRUE; } else if(e->type() == QEvent::ContextMenu ) { QContextMenuEvent *ce = (QContextMenuEvent*)e; fixObject( o ); if( !o ) return FALSE; buttonContextMenuEvent( ce, o ); return TRUE; } else if ( e->type() == QEvent::MouseMove ) { QMouseEvent *ke = (QMouseEvent*)e; fixObject( o ); if ( !o ) return FALSE; buttonMouseMoveEvent( ke, o ); return TRUE; } else if ( e->type() == QEvent::MouseButtonRelease ) { QMouseEvent *ke = (QMouseEvent*)e; fixObject( o ); if ( !o ) return FALSE; buttonMouseReleaseEvent( ke, o ); return TRUE; } else if ( e->type() == QEvent::DragEnter ) { QDragEnterEvent *de = (QDragEnterEvent*)e; if ( de->provides( "application/x-designer-actions" ) || de->provides( "application/x-designer-actiongroup" ) || de->provides( "application/x-designer-separator" ) ) de->accept(); } else if ( e->type() == QEvent::DragMove ) { QDragMoveEvent *de = (QDragMoveEvent*)e; if ( de->provides( "application/x-designer-actions" ) || de->provides( "application/x-designer-actiongroup" ) || de->provides( "application/x-designer-separator" ) ) de->accept(); } return QToolBar::eventFilter( o, e );}void QDesignerToolBar::paintEvent( QPaintEvent *e ){ QToolBar::paintEvent( e ); if ( e->rect() != rect() ) return; lastIndicatorPos = QPoint( -1, -1 );}void QDesignerToolBar::contextMenuEvent( QContextMenuEvent *e ){ e->accept(); QPopupMenu menu( 0 ); menu.insertItem( tr( "Delete Toolbar" ), 1 ); int res = menu.exec( e->globalPos() ); if ( res != -1 ) { RemoveToolBarCommand *cmd = new RemoveToolBarCommand( tr( "Delete Toolbar '%1'" ).arg( name() ), formWindow, 0, this ); formWindow->commandHistory()->addCommand( cmd ); cmd->execute(); }}void QDesignerToolBar::mousePressEvent( QMouseEvent *e ){ widgetInserting = FALSE; if ( e->button() == LeftButton && MainWindow::self->currentTool() != POINTER_TOOL && MainWindow::self->currentTool() != ORDER_TOOL ) { if ( MainWindow::self->currentTool() == CONNECT_TOOL ) { } else { widgetInserting = TRUE; } return; }}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() && (*it)->inherits( "QSeparatorAction" ) ) 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -