📄 popupmenueditor.cpp
字号:
/************************************************************************ Copyright (C) 2003 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.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** 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.** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include <qapplication.h>#include <qcstring.h>#include <qdatastream.h>#include <qdragobject.h>#include <qlineedit.h>#include <qobjectlist.h>#include <qpainter.h>#include <qpopupmenu.h>#include <qrect.h>#include <qsize.h>#include <qstyle.h>#include <qtimer.h>#include "actiondnd.h"#include "actioneditorimpl.h"#include "command.h"#include "formfile.h"#include "formwindow.h"#include "mainwindow.h"#include "metadatabase.h"#include "pixmapchooser.h"#include "popupmenueditor.h"#include "menubareditor.h"// Drag Object Declaration -------------------------------------------class PopupMenuEditorItemPtrDrag : public QStoredDrag{public: PopupMenuEditorItemPtrDrag( PopupMenuEditorItem * item, QWidget * parent = 0, const char * name = 0 ); ~PopupMenuEditorItemPtrDrag() {}; static bool canDecode( QDragMoveEvent * e ); static bool decode( QDropEvent * e, PopupMenuEditorItem ** i );};// Drag Object Implementation ---------------------------------------PopupMenuEditorItemPtrDrag::PopupMenuEditorItemPtrDrag( PopupMenuEditorItem * item, QWidget * parent, const char * name ) : QStoredDrag( "qt/popupmenueditoritemptr", parent, name ){ QByteArray data( sizeof( Q_LONG ) ); QDataStream stream( data, IO_WriteOnly ); stream << ( Q_LONG ) item; setEncodedData( data );}bool PopupMenuEditorItemPtrDrag::canDecode( QDragMoveEvent * e ){ return e->provides( "qt/popupmenueditoritemptr" );}bool PopupMenuEditorItemPtrDrag::decode( QDropEvent * e, PopupMenuEditorItem ** i ){ QByteArray data = e->encodedData( "qt/popupmenueditoritemptr" ); QDataStream stream( data, IO_ReadOnly ); if ( !data.size() ) return FALSE; Q_LONG p = 0; stream >> p; *i = ( PopupMenuEditorItem *) p; return TRUE;}// PopupMenuEditorItem Implementation -----------------------------------PopupMenuEditorItem::PopupMenuEditorItem( PopupMenuEditor * menu, QObject * parent, const char * name ) : QObject( parent, name ), a( 0 ), s( 0 ), m( menu ), separator( FALSE ), removable( FALSE ){ init(); a = new QAction( this ); QObject::connect( a, SIGNAL( destroyed() ), this, SLOT( selfDestruct() ) );}PopupMenuEditorItem::PopupMenuEditorItem( QAction * action, PopupMenuEditor * menu, QObject * parent, const char * name ) : QObject( parent, name ), a( action ), s( 0 ), m( menu ), separator( FALSE ), removable( TRUE ){ init(); if ( /*a->name() == "qt_separator_action" ||*/ ::qt_cast<QSeparatorAction*>(a) ) separator = TRUE; if ( a && a->children() ) a->installEventFilter( this );}PopupMenuEditorItem::PopupMenuEditorItem( PopupMenuEditorItem * item, PopupMenuEditor * menu, QObject * parent, const char * name ) : QObject( parent, name ), a( item->a ), s( 0 ), m( menu ), separator( item->separator ), removable( item->removable ){ init(); if ( ::qt_cast<QActionGroup*>(a) ) a->installEventFilter( this );}PopupMenuEditorItem::~PopupMenuEditorItem(){}void PopupMenuEditorItem::init(){ if ( a ) { QObject::connect( a, SIGNAL( destroyed() ), this, SLOT( selfDestruct() ) ); if ( m && !isSeparator() ) { s = new PopupMenuEditor( m->formWindow(), m ); QString n = "popupMenu"; m->formWindow()->unify( s, n, TRUE ); s->setName( n ); MetaDataBase::addEntry( s ); } }}PopupMenuEditorItem::ItemType PopupMenuEditorItem::type() const{ if ( separator ) return Separator; else if ( a ) return Action; return Unknown;}void PopupMenuEditorItem::setVisible( bool enable ){ if ( a ) a->setVisible( enable );}bool PopupMenuEditorItem::isVisible() const{ QActionGroup *g = ::qt_cast<QActionGroup*>(a); if ( g ) return ( g->isVisible() && g->usesDropDown() ); else if ( a ) return a->isVisible(); return FALSE;}void PopupMenuEditorItem::showMenu( int x, int y ){ if ( ( !separator ) && s ) { s->move( x, y ); s->show(); s->raise(); }}void PopupMenuEditorItem::hideMenu(){ if ( s ) { s->hideSubMenu(); s->hide(); }}void PopupMenuEditorItem::focusOnMenu(){ if ( s ) { s->showSubMenu(); s->setFocus(); }}int PopupMenuEditorItem::count() const{ if ( s ) { return s->count(); } else if ( ::qt_cast<QActionGroup*>(a) ) { const QObjectList * l = a->children(); if ( l ) return l->count(); } return 0;}bool PopupMenuEditorItem::eventFilter( QObject * o, QEvent * event ){ if ( ! ::qt_cast<QActionGroup*>( o ) ) return FALSE; if ( event->type() == QEvent::ChildInserted ) { QChildEvent * ce = ( QChildEvent * ) event; QObject * c = ce->child(); QAction * action = ::qt_cast<QAction*>( c ); if ( s->find( action ) != -1 ) // avoid duplicates return FALSE; QActionGroup * actionGroup = ::qt_cast<QActionGroup*>( c ); if ( actionGroup ) s->insert( actionGroup ); else if ( action ) s->insert( action ); } return FALSE;}void PopupMenuEditorItem::selfDestruct(){ hideMenu(); int i = m->find( s ); if ( i != -1 && i < m->count() ) m->remove( i ); // remove this item a = 0; // the selfDestruct call was caused by the deletion of the action delete this;}// PopupMenuEditor Implementation -----------------------------------PopupMenuEditorItem * PopupMenuEditor::draggedItem = 0;int PopupMenuEditor::clipboardOperation = 0;PopupMenuEditorItem * PopupMenuEditor::clipboardItem = 0;PopupMenuEditor::PopupMenuEditor( FormWindow * fw, QWidget * parent, const char * name ) : QWidget( 0, name, WStyle_Customize | WStyle_NoBorder | WRepaintNoErase | WResizeNoErase ), formWnd( fw ), parentMenu( parent ), iconWidth( 0 ), textWidth( 0 ), accelWidth( 0 ), arrowWidth( 30 ), borderSize( 2 ), currentField( 1 ), currentIndex( 0 ){ init();}PopupMenuEditor::PopupMenuEditor( FormWindow * fw, PopupMenuEditor * menu, QWidget * parent, const char * name ) : QWidget( 0, name, WStyle_Customize | WStyle_NoBorder | WRepaintNoErase ), formWnd( fw ), parentMenu( parent ), iconWidth( menu->iconWidth ), textWidth( menu->textWidth ), accelWidth( menu->accelWidth ), arrowWidth( menu->arrowWidth ), borderSize( menu->borderSize ), currentField( menu->currentField ), currentIndex( menu->currentIndex ){ init(); PopupMenuEditorItem * i; for ( i = menu->itemList.first(); i; i = menu->itemList.next() ) { PopupMenuEditorItem * n = new PopupMenuEditorItem( i, this ); itemList.append( n ); }}PopupMenuEditor::~PopupMenuEditor(){ itemList.setAutoDelete( TRUE );}void PopupMenuEditor::init(){ reparent( ( QMainWindow * ) formWnd->mainContainer(), pos() ); addItem.action()->setMenuText( tr("new item") ); addSeparator.action()->setMenuText( tr("new separator") ); setAcceptDrops( TRUE ); setFocusPolicy( StrongFocus ); lineEdit = new QLineEdit( this ); lineEdit->hide(); lineEdit->setFrameStyle(QFrame::Plain | QFrame::NoFrame); lineEdit->polish(); lineEdit->setBackgroundOrigin(ParentOrigin); lineEdit->setBackgroundMode(PaletteButton); lineEdit->installEventFilter( this ); dropLine = new QWidget( this, 0, Qt::WStyle_NoBorder | WStyle_StaysOnTop ); dropLine->setBackgroundColor( Qt::red ); dropLine->hide(); hide();}void PopupMenuEditor::insert( PopupMenuEditorItem * item, int index ){ if ( !item ) return; if ( index == -1 ) { itemList.append( item ); if ( isVisible() ) currentIndex = itemList.count() - 1; } else { itemList.insert( index, item ); if ( isVisible() ) currentIndex = index; } item->m = this; item->s->parentMenu = this; resizeToContents(); if ( isVisible() && parentMenu ) parentMenu->update(); // draw arrow in parent menu emit inserted( item->action() );}void PopupMenuEditor::insert( QAction * action, int index ){ if ( !action ) return; insert( new PopupMenuEditorItem( action, this, 0, action->name() ), index );}void PopupMenuEditor::insert( QActionGroup * actionGroup, int index ){ if ( !actionGroup ) return; bool dropdown = actionGroup->usesDropDown(); PopupMenuEditorItem *i = new PopupMenuEditorItem( (QAction *)actionGroup, this, 0, QString( actionGroup->name() ) + "Menu" ); QActionGroup *g = 0; QObjectList *l = actionGroup->queryList( "QAction", 0, FALSE, FALSE ); QObjectListIterator it( *l ); insert( i, index ); for ( ; it.current(); ++it ) { g = ::qt_cast<QActionGroup*>(it.current()); if ( g ) { if ( dropdown ) i->s->insert( g ); else insert( g ); } else { i->s->insert( (QAction*)it.current() ); } } delete l;}int PopupMenuEditor::find( const QAction * action ){ PopupMenuEditorItem * i = itemList.first(); while ( i ) { if ( i->action() == action ) return itemList.at(); i = itemList.next(); } return -1;}int PopupMenuEditor::find( PopupMenuEditor * menu ){ PopupMenuEditorItem * i = itemList.first(); while ( i ) { if ( i->subMenu() == menu ) return itemList.at(); i = itemList.next(); } return -1;}int PopupMenuEditor::count(){ return itemList.count();}PopupMenuEditorItem * PopupMenuEditor::at( int index ){ return itemList.at( index );}void PopupMenuEditor::exchange( int a, int b ){ PopupMenuEditorItem * ia = itemList.at( a ); PopupMenuEditorItem * ib = itemList.at( b ); if ( !ia || !ib || ia == &addItem || ia == &addSeparator || ib == &addItem || ib == &addSeparator ) return; // do nothing itemList.replace( b, ia ); itemList.replace( a, ib );}void PopupMenuEditor::cut( int index ){ int idx = ( index == -1 ? currentIndex : index ); if ( clipboardItem && clipboardOperation == Cut ) delete clipboardItem; clipboardOperation = Cut; clipboardItem = itemList.at( idx ); if ( clipboardItem == &addItem || clipboardItem == &addSeparator ) { clipboardOperation = None; clipboardItem = 0; return; // do nothing } RemoveActionFromPopupCommand * cmd = new RemoveActionFromPopupCommand( "Cut Item", formWnd, this, idx ); formWnd->commandHistory()->addCommand( cmd ); cmd->execute();}void PopupMenuEditor::copy( int index ){ int idx = ( index == -1 ? currentIndex : index ); if ( clipboardItem && clipboardOperation == Cut ) delete clipboardItem; clipboardOperation = Copy; clipboardItem = itemList.at( idx ); if ( clipboardItem == &addItem || clipboardItem == &addSeparator ) { clipboardOperation = None; clipboardItem = 0; }}void PopupMenuEditor::paste( int index ){ int idx = ( index == -1 ? currentIndex : index ); if ( clipboardItem && clipboardOperation ) { PopupMenuEditorItem * n = new PopupMenuEditorItem( clipboardItem, this ); AddActionToPopupCommand * cmd = new AddActionToPopupCommand( "Paste Item", formWnd, this, n, idx ); formWnd->commandHistory()->addCommand( cmd ); cmd->execute(); }}void PopupMenuEditor::insertedActions( QPtrList<QAction> & list ){ QAction * a = 0; PopupMenuEditorItem * i = itemList.first(); while ( i ) { a = i->action(); if ( a ) list.append( a ); i = itemList.next(); }}void PopupMenuEditor::show(){ resizeToContents(); QWidget::show();}void PopupMenuEditor::choosePixmap( int index ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -