📄 menubareditor.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 <qaction.h>#include <qapplication.h>#include <qbitmap.h>#include <qdragobject.h>#include <qlineedit.h>#include <qmainwindow.h>#include <qpainter.h>#include <qstyle.h>#include "command.h"#include "formwindow.h"#include "menubareditor.h"#include "popupmenueditor.h"extern void find_accel( const QString &txt, QMap<QChar, QWidgetList > &accels, QWidget *w );// Drag Object Declaration -------------------------------------------class MenuBarEditorItemPtrDrag : public QStoredDrag{public: MenuBarEditorItemPtrDrag( MenuBarEditorItem * item, QWidget * parent = 0, const char * name = 0 ); ~MenuBarEditorItemPtrDrag() {}; static bool canDecode( QDragMoveEvent * e ); static bool decode( QDropEvent * e, MenuBarEditorItem ** i );};// Drag Object Implementation ---------------------------------------MenuBarEditorItemPtrDrag::MenuBarEditorItemPtrDrag( MenuBarEditorItem * item, QWidget * parent, const char * name ) : QStoredDrag( "qt/menubareditoritemptr", parent, name ){ QByteArray data( sizeof( Q_LONG ) ); QDataStream stream( data, IO_WriteOnly ); stream << ( Q_LONG ) item; setEncodedData( data );}bool MenuBarEditorItemPtrDrag::canDecode( QDragMoveEvent * e ){ return e->provides( "qt/menubareditoritemptr" );}bool MenuBarEditorItemPtrDrag::decode( QDropEvent * e, MenuBarEditorItem ** i ){ QByteArray data = e->encodedData( "qt/menubareditoritemptr" ); QDataStream stream( data, IO_ReadOnly ); if ( !data.size() ) return FALSE; Q_LONG p = 0; stream >> p; *i = ( MenuBarEditorItem *) p; return TRUE;}// MenuBarEditorItem ---------------------------------------------------MenuBarEditorItem::MenuBarEditorItem( MenuBarEditor * bar, QObject * parent, const char * name ) : QObject( parent, name ), menuBar( bar ), popupMenu( 0 ), visible( TRUE ), separator( FALSE ), removable( FALSE ){ }MenuBarEditorItem::MenuBarEditorItem( PopupMenuEditor * menu, MenuBarEditor * bar, QObject * parent, const char * name ) : QObject( parent, name ), menuBar( bar ), popupMenu( menu ), visible( TRUE ), separator( FALSE ), removable( TRUE ){ text = menu->name();}MenuBarEditorItem::MenuBarEditorItem( QActionGroup * actionGroup, MenuBarEditor * bar, QObject * parent, const char * name ) : QObject( parent, name ), menuBar( bar ), popupMenu( 0 ), visible( TRUE ), separator( FALSE ), removable( TRUE ){ text = actionGroup->menuText(); popupMenu = new PopupMenuEditor( menuBar->formWindow(), menuBar ); popupMenu->insert( actionGroup );}MenuBarEditorItem::MenuBarEditorItem( MenuBarEditorItem * item, QObject * parent, const char * name ) : QObject( parent, name ), menuBar( item->menuBar ), popupMenu( 0 ), text( item->text ), visible( item->visible ), separator( item->separator ), removable( item->removable ){ popupMenu = new PopupMenuEditor( menuBar->formWindow(), item->popupMenu, menuBar );}// MenuBarEditor --------------------------------------------------------int MenuBarEditor::clipboardOperation = 0;MenuBarEditorItem * MenuBarEditor::clipboardItem = 0;MenuBarEditor::MenuBarEditor( FormWindow * fw, QWidget * parent, const char * name ) : QMenuBar( parent, name ), formWnd( fw ), draggedItem( 0 ), currentIndex( 0 ), itemHeight( 0 ), separatorWidth( 32 ), hideWhenEmpty( TRUE ), hasSeparator( FALSE ){ setAcceptDrops( TRUE ); setFocusPolicy( StrongFocus ); addItem.setMenuText( tr("new menu") ); addSeparator.setMenuText( tr("new separator") ); lineEdit = new QLineEdit( this, "menubar lineedit" ); lineEdit->hide(); lineEdit->setFrameStyle(QFrame::Plain | QFrame::NoFrame); lineEdit->polish(); lineEdit->setBackgroundMode(PaletteButton); lineEdit->setBackgroundOrigin(ParentOrigin); lineEdit->installEventFilter( this ); dropLine = new QWidget( this, "menubar dropline", Qt::WStyle_NoBorder | WStyle_StaysOnTop ); dropLine->setBackgroundColor( Qt::red ); dropLine->hide(); setMinimumHeight( fontMetrics().height() + 2 * borderSize() );}MenuBarEditor::~MenuBarEditor(){ itemList.setAutoDelete( TRUE );}FormWindow * MenuBarEditor::formWindow(){ return formWnd;}MenuBarEditorItem * MenuBarEditor::createItem( int index, bool addToCmdStack ){ MenuBarEditorItem * i = new MenuBarEditorItem( new PopupMenuEditor( formWnd, ( QWidget * ) parent() ), this ); if ( addToCmdStack ) { AddMenuCommand * cmd = new AddMenuCommand( "Add Menu", formWnd, this, i, index ); formWnd->commandHistory()->addCommand( cmd ); cmd->execute(); } else { AddMenuCommand cmd( "Add Menu", formWnd, this, i, index ); cmd.execute(); } return i;}void MenuBarEditor::insertItem( MenuBarEditorItem * item, int index ){ item->menu()->parentMenu = this; if ( index != -1 ) itemList.insert( index, item ); else itemList.append( item ); if ( hideWhenEmpty && itemList.count() == 1 ) show(); // calls resizeInternals(); else resizeInternals(); if ( isVisible() ) update();}void MenuBarEditor::insertItem( QString text, PopupMenuEditor * menu, int index ){ MenuBarEditorItem * item = new MenuBarEditorItem( menu, this ); if ( !text.isNull() ) item->setMenuText( text ); insertItem( item, index );}void MenuBarEditor::insertItem( QString text, QActionGroup * group, int index ){ MenuBarEditorItem * item = new MenuBarEditorItem( group, this ); if ( !text.isNull() ) item->setMenuText( text ); insertItem( item, index );}void MenuBarEditor::insertSeparator( int index ){ if ( hasSeparator ) return; MenuBarEditorItem * i = createItem( index ); i->setSeparator( TRUE ); i->setMenuText( "separator" ); hasSeparator = TRUE;}void MenuBarEditor::removeItemAt( int index ){ removeItem( item( index ) );}void MenuBarEditor::removeItem( MenuBarEditorItem * item ){ if ( item && item->isRemovable() && itemList.removeRef( item ) ) { if ( item->isSeparator() ) hasSeparator = FALSE; if ( hideWhenEmpty && itemList.count() == 0 ) hide(); else resizeInternals(); int n = count() + 1; if ( currentIndex >= n ) currentIndex = n; if ( isVisible() ) update(); }}int MenuBarEditor::findItem( MenuBarEditorItem * item ){ return itemList.findRef( item );}int MenuBarEditor::findItem( PopupMenuEditor * menu ){ MenuBarEditorItem * i = itemList.first(); while ( i ) { if ( i->menu() == menu ) return itemList.at(); i = itemList.next(); } return -1;}int MenuBarEditor::findItem( QPoint & pos ){ int x = borderSize(); int dx = 0; int y = 0; int w = width(); QSize s; QRect r; MenuBarEditorItem * i = itemList.first(); while ( i ) { if ( i->isVisible() ) { s = itemSize( i ); dx = s.width(); if ( x + dx > w && x > borderSize() ) { y += itemHeight; x = borderSize(); } r = QRect( x, y, s.width(), s.height() ); if ( r.contains( pos ) ) return itemList.at(); addItemSizeToCoords( i, x, y, w ); } i = itemList.next(); } // check add item s = itemSize( &addItem ); dx = s.width(); if ( x + dx > w && x > borderSize() ) { y += itemHeight; x = borderSize(); } r = QRect( x, y, s.width(), s.height() ); if ( r.contains( pos ) ) return itemList.count(); return itemList.count() + 1;}MenuBarEditorItem * MenuBarEditor::item( int index ){ if ( index == -1 ) return itemList.at( currentIndex ); int c = itemList.count(); if ( index == c ) return &addItem; else if ( index > c ) return &addSeparator; return itemList.at( index );}int MenuBarEditor::count(){ return itemList.count();}int MenuBarEditor::current(){ return currentIndex;}void MenuBarEditor::cut( int index ){ if ( clipboardItem && clipboardOperation == Cut ) delete clipboardItem; clipboardOperation = Cut; clipboardItem = itemList.at( index ); if ( clipboardItem == &addItem || clipboardItem == &addSeparator ) { clipboardOperation = None; clipboardItem = 0; return; // do nothing } RemoveMenuCommand * cmd = new RemoveMenuCommand( "Cut Menu", formWnd, this, index ); formWnd->commandHistory()->addCommand( cmd ); cmd->execute();}void MenuBarEditor::copy( int index ){ if ( clipboardItem && clipboardOperation == Cut ) delete clipboardItem; clipboardOperation = Copy; clipboardItem = itemList.at( index ); if ( clipboardItem == &addItem || clipboardItem == &addSeparator ) { clipboardOperation = None; clipboardItem = 0; }}void MenuBarEditor::paste( int index ){ if ( clipboardItem && clipboardOperation ) { MenuBarEditorItem * i = new MenuBarEditorItem( clipboardItem ); AddMenuCommand * cmd = new AddMenuCommand( "Paste Menu", formWnd, this, i, index ); formWnd->commandHistory()->addCommand( cmd ); cmd->execute(); }}void MenuBarEditor::exchange( int a, int b ){ MenuBarEditorItem * ia = itemList.at( a ); MenuBarEditorItem * 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 MenuBarEditor::showLineEdit( int index ){ if ( index == -1 ) index = currentIndex; MenuBarEditorItem * i = 0; if ( (uint) index >= itemList.count() ) i = &addItem; else i = itemList.at( index ); if ( i && i->isSeparator() ) return; // open edit field for item name lineEdit->setText( i->menuText() ); lineEdit->selectAll(); QPoint pos = itemPos( index ); lineEdit->move( pos.x() + borderSize(), pos.y() - ( borderSize() / 2 ) ); lineEdit->resize( itemSize( i ) ); lineEdit->show(); lineEdit->setFocus();}void MenuBarEditor::showItem( int index ){ if ( index == -1 ) index = currentIndex; if ( (uint)index < itemList.count() ) { MenuBarEditorItem * i = itemList.at( index ); if ( i->isSeparator() || draggedItem ) return; PopupMenuEditor * m = i->menu(); QPoint pos = itemPos( index ); m->move( pos.x(), pos.y() + itemHeight - 1 ); m->raise(); m->show(); setFocus(); }}void MenuBarEditor::hideItem( int index ){ if ( index == -1 ) index = currentIndex; if ( (uint)index < itemList.count() ) { PopupMenuEditor * m = itemList.at( index )->menu(); m->hideSubMenu(); m->hide(); }}void MenuBarEditor::focusItem( int index ){ if ( index == -1 ) index = currentIndex; if ( (uint)index < itemList.count() ) { PopupMenuEditor * m = itemList.at( index )->menu(); m->setFocus(); m->update(); update(); }}void MenuBarEditor::deleteItem( int index ){ if ( index == -1 ) index = currentIndex; if ( (uint)index < itemList.count() ) { RemoveMenuCommand * cmd = new RemoveMenuCommand( "Delete Menu", formWnd, this, currentIndex ); formWnd->commandHistory()->addCommand( cmd ); cmd->execute(); }}QSize MenuBarEditor::sizeHint() const{ return QSize( parentWidget()->width(), heightForWidth( parentWidget()->width() ) );}int MenuBarEditor::heightForWidth( int max_width ) const{ MenuBarEditor * that = ( MenuBarEditor * ) this; int x = borderSize(); int y = 0; QPainter p( this ); that->itemHeight = that->itemSize( &(that->addItem) ).height(); MenuBarEditorItem * i = that->itemList.first(); while ( i ) { if ( i->isVisible() ) that->addItemSizeToCoords( i, x, y, max_width ); i = that->itemList.next(); } that->addItemSizeToCoords( &(that->addItem), x, y, max_width ); that->addItemSizeToCoords( &(that->addSeparator), x, y, max_width ); return y + itemHeight;}void MenuBarEditor::show(){ QWidget::show(); resizeInternals(); QResizeEvent e( parentWidget()->size(), parentWidget()->size() ); QApplication::sendEvent( parentWidget(), &e );}void MenuBarEditor::checkAccels( QMap<QChar, QWidgetList > &accels ){ QString t; MenuBarEditorItem * i = itemList.first(); while ( i ) { t = i->menuText(); find_accel( t, accels, this ); // do not check the accelerators in the popup menus i = itemList.next(); }}// public slotsvoid MenuBarEditor::cut(){ cut( currentIndex );}void MenuBarEditor::copy(){ copy( currentIndex );}void MenuBarEditor::paste()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -