You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1470 lines
37 KiB
1470 lines
37 KiB
15 years ago
|
/**********************************************************************
|
||
|
** Copyright (C) 2003 Trolltech AS. All rights reserved.
|
||
|
**
|
||
14 years ago
|
** This file is part of TQt Designer.
|
||
15 years ago
|
**
|
||
|
** 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.
|
||
|
**
|
||
14 years ago
|
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
|
||
|
** licenses may use this file in accordance with the TQt Commercial License
|
||
15 years ago
|
** 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
|
||
14 years ago
|
** information about TQt Commercial License Agreements.
|
||
15 years ago
|
**
|
||
|
** Contact info@trolltech.com if any conditions of this licensing are
|
||
|
** not clear to you.
|
||
|
**
|
||
|
**********************************************************************/
|
||
|
|
||
15 years ago
|
#include <tqapplication.h>
|
||
|
#include <tqcstring.h>
|
||
|
#include <tqdatastream.h>
|
||
|
#include <tqdragobject.h>
|
||
|
#include <tqlineedit.h>
|
||
|
#include <tqobjectlist.h>
|
||
|
#include <tqpainter.h>
|
||
|
#include <tqpopupmenu.h>
|
||
|
#include <tqrect.h>
|
||
|
#include <tqsize.h>
|
||
|
#include <tqstyle.h>
|
||
|
#include <tqtimer.h>
|
||
15 years ago
|
#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"
|
||
|
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
|
||
|
// Drag Object Declaration -------------------------------------------
|
||
|
|
||
14 years ago
|
class PopupMenuEditorItemPtrDrag : public TQStoredDrag
|
||
15 years ago
|
{
|
||
|
public:
|
||
14 years ago
|
PopupMenuEditorItemPtrDrag( PopupMenuEditorItem * item, TQWidget * parent = 0,
|
||
15 years ago
|
const char * name = 0 );
|
||
|
~PopupMenuEditorItemPtrDrag() {};
|
||
15 years ago
|
static bool canDecode( TQDragMoveEvent * e );
|
||
|
static bool decode( TQDropEvent * e, PopupMenuEditorItem ** i );
|
||
15 years ago
|
};
|
||
|
|
||
|
// Drag Object Implementation ---------------------------------------
|
||
|
|
||
|
PopupMenuEditorItemPtrDrag::PopupMenuEditorItemPtrDrag( PopupMenuEditorItem * item,
|
||
14 years ago
|
TQWidget * parent, const char * name )
|
||
|
: TQStoredDrag( "qt/popupmenueditoritemptr", parent, name )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQByteArray data( sizeof( TQ_LONG ) );
|
||
15 years ago
|
TQDataStream stream( data, IO_WriteOnly );
|
||
14 years ago
|
stream << ( TQ_LONG ) item;
|
||
15 years ago
|
setEncodedData( data );
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PopupMenuEditorItemPtrDrag::canDecode( TQDragMoveEvent * e )
|
||
15 years ago
|
{
|
||
|
return e->provides( "qt/popupmenueditoritemptr" );
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PopupMenuEditorItemPtrDrag::decode( TQDropEvent * e, PopupMenuEditorItem ** i )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQByteArray data = e->encodedData( "qt/popupmenueditoritemptr" );
|
||
15 years ago
|
TQDataStream stream( data, IO_ReadOnly );
|
||
15 years ago
|
|
||
|
if ( !data.size() )
|
||
|
return FALSE;
|
||
|
|
||
14 years ago
|
TQ_LONG p = 0;
|
||
15 years ago
|
stream >> p;
|
||
|
*i = ( PopupMenuEditorItem *) p;
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
// PopupMenuEditorItem Implementation -----------------------------------
|
||
|
|
||
14 years ago
|
PopupMenuEditorItem::PopupMenuEditorItem( PopupMenuEditor * menu, TQObject * parent, const char * name )
|
||
|
: TQObject( parent, name ),
|
||
15 years ago
|
a( 0 ),
|
||
|
s( 0 ),
|
||
|
m( menu ),
|
||
|
separator( FALSE ),
|
||
|
removable( FALSE )
|
||
|
{
|
||
|
init();
|
||
15 years ago
|
a = new TQAction( this );
|
||
|
TQObject::connect( a, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( selfDestruct() ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
15 years ago
|
PopupMenuEditorItem::PopupMenuEditorItem( TQAction * action, PopupMenuEditor * menu,
|
||
14 years ago
|
TQObject * parent, const char * name )
|
||
|
: TQObject( parent, name ),
|
||
15 years ago
|
a( action ),
|
||
|
s( 0 ),
|
||
|
m( menu ),
|
||
|
separator( FALSE ),
|
||
|
removable( TRUE )
|
||
|
{
|
||
|
init();
|
||
14 years ago
|
if ( /*a->name() == "qt_separator_action" ||*/ ::tqqt_cast<QSeparatorAction*>(a) )
|
||
15 years ago
|
separator = TRUE;
|
||
14 years ago
|
if ( a && !a->childrenListObject().isEmpty() )
|
||
15 years ago
|
a->installEventFilter( this );
|
||
|
}
|
||
|
|
||
|
PopupMenuEditorItem::PopupMenuEditorItem( PopupMenuEditorItem * item, PopupMenuEditor * menu,
|
||
14 years ago
|
TQObject * parent, const char * name )
|
||
|
: TQObject( parent, name ),
|
||
15 years ago
|
a( item->a ),
|
||
|
s( 0 ),
|
||
|
m( menu ),
|
||
|
separator( item->separator ),
|
||
|
removable( item->removable )
|
||
|
{
|
||
|
init();
|
||
14 years ago
|
if ( ::tqqt_cast<TQActionGroup*>(a) )
|
||
15 years ago
|
a->installEventFilter( this );
|
||
|
}
|
||
|
|
||
|
PopupMenuEditorItem::~PopupMenuEditorItem()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditorItem::init()
|
||
|
{
|
||
|
if ( a ) {
|
||
15 years ago
|
TQObject::connect( a, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( selfDestruct() ) );
|
||
15 years ago
|
if ( m && !isSeparator() ) {
|
||
|
s = new PopupMenuEditor( m->formWindow(), m );
|
||
15 years ago
|
TQString n = "PopupMenuEditor";
|
||
14 years ago
|
m->formWindow()->unify( TQT_TQOBJECT(s), n, TRUE );
|
||
15 years ago
|
s->setName( n );
|
||
14 years ago
|
MetaDataBase::addEntry( TQT_TQOBJECT(s) );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
PopupMenuEditorItem::ItemType PopupMenuEditorItem::type() const
|
||
|
{
|
||
|
if ( separator )
|
||
|
return Separator;
|
||
|
else if ( a )
|
||
|
return Action;
|
||
|
return Unknown;
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditorItem::setVisible( bool enable )
|
||
|
{
|
||
|
if ( a )
|
||
13 years ago
|
a->setVisible( enable );
|
||
15 years ago
|
}
|
||
|
|
||
|
bool PopupMenuEditorItem::isVisible() const
|
||
|
{
|
||
14 years ago
|
TQActionGroup *g = ::tqqt_cast<TQActionGroup*>(a);
|
||
15 years ago
|
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();
|
||
14 years ago
|
} else if ( ::tqqt_cast<TQActionGroup*>(a) ) {
|
||
|
const TQObjectList l = a->childrenListObject();
|
||
|
if ( !l.isEmpty() )
|
||
|
return l.count();
|
||
15 years ago
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PopupMenuEditorItem::eventFilter( TQObject * o, TQEvent * event )
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( ! ::tqqt_cast<TQActionGroup*>( o ) )
|
||
15 years ago
|
return FALSE;
|
||
15 years ago
|
if ( event->type() == TQEvent::ChildInserted ) {
|
||
|
TQChildEvent * ce = ( TQChildEvent * ) event;
|
||
14 years ago
|
TQObject * c = TQT_TQOBJECT(ce->child());
|
||
|
TQAction * action = ::tqqt_cast<TQAction*>( c );
|
||
14 years ago
|
if ( s->find( action ) != -1 ) // avoid duplicates
|
||
15 years ago
|
return FALSE;
|
||
14 years ago
|
TQActionGroup * actionGroup = ::tqqt_cast<TQActionGroup*>( c );
|
||
15 years ago
|
if ( actionGroup )
|
||
|
s->insert( actionGroup );
|
||
|
else if ( action )
|
||
|
s->insert( action );
|
||
|
}
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditorItem::selfDestruct()
|
||
|
{
|
||
|
hideMenu();
|
||
14 years ago
|
int i = m->find( s );
|
||
15 years ago
|
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;
|
||
|
|
||
14 years ago
|
PopupMenuEditor::PopupMenuEditor( FormWindow * fw, TQWidget * parent, const char * name )
|
||
15 years ago
|
: TQWidget( 0, name, WStyle_Customize | WStyle_NoBorder | WRepaintNoErase | WResizeNoErase ),
|
||
15 years ago
|
formWnd( fw ),
|
||
14 years ago
|
parentMenu( parent ),
|
||
15 years ago
|
iconWidth( 0 ),
|
||
|
textWidth( 0 ),
|
||
|
accelWidth( 0 ),
|
||
|
arrowWidth( 30 ),
|
||
|
borderSize( 2 ),
|
||
|
currentField( 1 ),
|
||
|
currentIndex( 0 )
|
||
|
{
|
||
|
init();
|
||
|
}
|
||
|
|
||
|
PopupMenuEditor::PopupMenuEditor( FormWindow * fw, PopupMenuEditor * menu,
|
||
14 years ago
|
TQWidget * parent, const char * name )
|
||
15 years ago
|
: TQWidget( 0, name, WStyle_Customize | WStyle_NoBorder | WRepaintNoErase ),
|
||
15 years ago
|
formWnd( fw ),
|
||
14 years ago
|
parentMenu( parent ),
|
||
15 years ago
|
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()
|
||
|
{
|
||
15 years ago
|
reparent( ( TQMainWindow * ) formWnd->mainContainer(), pos() );
|
||
15 years ago
|
|
||
|
addItem.action()->setMenuText( i18n("new item") );
|
||
|
addSeparator.action()->setMenuText( i18n("new separator") );
|
||
|
|
||
|
setAcceptDrops( TRUE );
|
||
14 years ago
|
setFocusPolicy( TQ_StrongFocus );
|
||
15 years ago
|
|
||
15 years ago
|
lineEdit = new TQLineEdit( this );
|
||
15 years ago
|
lineEdit->hide();
|
||
15 years ago
|
lineEdit->setFrameStyle(TQFrame::Plain | TQFrame::NoFrame);
|
||
15 years ago
|
lineEdit->polish();
|
||
|
lineEdit->setBackgroundOrigin(ParentOrigin);
|
||
|
lineEdit->setBackgroundMode(PaletteButton);
|
||
|
lineEdit->installEventFilter( this );
|
||
|
|
||
14 years ago
|
dropLine = new TQWidget( this, 0, TQt::WStyle_NoBorder | WStyle_StaysOnTop );
|
||
|
dropLine->setBackgroundColor( TQt::red );
|
||
15 years ago
|
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;
|
||
14 years ago
|
item->s->parentMenu = this;
|
||
15 years ago
|
resizeToContents();
|
||
14 years ago
|
if ( isVisible() && parentMenu )
|
||
14 years ago
|
parentMenu->update(); // draw arrow in parent menu
|
||
15 years ago
|
emit inserted( item->action() );
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::insert( TQAction * action, int index )
|
||
15 years ago
|
{
|
||
|
if ( !action )
|
||
|
return;
|
||
|
insert( new PopupMenuEditorItem( action, this, 0, action->name() ), index );
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::insert( TQActionGroup * actionGroup, int index )
|
||
15 years ago
|
{
|
||
|
if ( !actionGroup )
|
||
|
return;
|
||
|
bool dropdown = actionGroup->usesDropDown();
|
||
15 years ago
|
PopupMenuEditorItem *i = new PopupMenuEditorItem( (TQAction *)actionGroup, this, 0,
|
||
|
TQString( actionGroup->name() ) + "Menu" );
|
||
|
TQActionGroup *g = 0;
|
||
14 years ago
|
TQObjectList *l = actionGroup->queryList( TQACTION_OBJECT_NAME_STRING, 0, FALSE, FALSE );
|
||
15 years ago
|
TQObjectListIterator it( *l );
|
||
15 years ago
|
insert( i, index );
|
||
|
for ( ; it.current(); ++it ) {
|
||
14 years ago
|
g = ::tqqt_cast<TQActionGroup*>(it.current());
|
||
15 years ago
|
if ( g ) {
|
||
|
if ( dropdown )
|
||
|
i->s->insert( g );
|
||
|
else
|
||
|
insert( g );
|
||
|
} else {
|
||
15 years ago
|
i->s->insert( (TQAction*)it.current() );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
delete l;
|
||
|
}
|
||
|
|
||
14 years ago
|
int PopupMenuEditor::find( const TQAction * action )
|
||
15 years ago
|
{
|
||
|
PopupMenuEditorItem * i = itemList.first();
|
||
|
while ( i ) {
|
||
|
if ( i->action() == action )
|
||
|
return itemList.at();
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
14 years ago
|
int PopupMenuEditor::find( PopupMenuEditor * menu )
|
||
15 years ago
|
{
|
||
|
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
|
||
14 years ago
|
itemList.replace( b, ia );
|
||
|
itemList.replace( a, ib );
|
||
15 years ago
|
}
|
||
|
|
||
|
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( i18n( "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( i18n( "Paste Item" ), formWnd, this, n, idx );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::insertedActions( TQPtrList<TQAction> & list )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQAction * a = 0;
|
||
15 years ago
|
PopupMenuEditorItem * i = itemList.first();
|
||
|
|
||
|
while ( i ) {
|
||
|
a = i->action();
|
||
|
if ( a )
|
||
|
list.append( a );
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::show()
|
||
|
{
|
||
|
resizeToContents();
|
||
15 years ago
|
TQWidget::show();
|
||
15 years ago
|
}
|
||
|
|
||
|
void PopupMenuEditor::choosePixmap( int index )
|
||
|
{
|
||
|
int idx = ( index == -1 ? currentIndex : index );
|
||
|
|
||
|
PopupMenuEditorItem * i = 0;
|
||
15 years ago
|
TQAction * a = 0;
|
||
15 years ago
|
|
||
|
if ( idx < (int)itemList.count() ) {
|
||
|
i = itemList.at( idx );
|
||
|
a = i->action();
|
||
|
} else {
|
||
|
createItem();
|
||
|
}
|
||
|
|
||
|
hide(); // qChoosePixmap hides the menu
|
||
15 years ago
|
TQIconSet icons( qChoosePixmap( 0, formWnd, 0, 0 ) );
|
||
15 years ago
|
SetActionIconsCommand * cmd =
|
||
|
new SetActionIconsCommand( i18n( "Set Icon" ), formWnd, a, this, icons );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
show();
|
||
|
setFocus();
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::showLineEdit( int index )
|
||
|
{
|
||
|
int idx = ( index == -1 ? currentIndex : index );
|
||
|
|
||
|
PopupMenuEditorItem * i = 0;
|
||
|
|
||
|
if ( idx >= (int)itemList.count() )
|
||
|
i = &addItem;
|
||
|
else
|
||
|
i = itemList.at( idx );
|
||
|
|
||
|
// open edit currentField for item name
|
||
|
lineEdit->setText( i->action()->menuText() );
|
||
|
lineEdit->selectAll();
|
||
|
lineEdit->setGeometry( borderSize + iconWidth, borderSize + itemPos( i ),
|
||
|
textWidth, itemHeight( i ) );
|
||
|
lineEdit->show();
|
||
|
lineEdit->setFocus();
|
||
|
}
|
||
|
|
||
14 years ago
|
void PopupMenuEditor::setAccelerator( int key, TQt::ButtonState state, int index )
|
||
15 years ago
|
{
|
||
|
// FIXME: make this a command
|
||
|
|
||
|
int idx = ( index == -1 ? currentIndex : index );
|
||
|
|
||
14 years ago
|
if ( key == TQt::Key_Shift ||
|
||
|
key == TQt::Key_Control ||
|
||
|
key == TQt::Key_Alt ||
|
||
|
key == TQt::Key_Meta ||
|
||
|
key == TQt::Key_unknown )
|
||
15 years ago
|
return; // ignore these keys when they are pressed
|
||
|
|
||
|
PopupMenuEditorItem * i = 0;
|
||
|
|
||
|
if ( idx >= (int)itemList.count() )
|
||
|
i = createItem();
|
||
|
else
|
||
|
i = itemList.at( idx );
|
||
|
|
||
14 years ago
|
int shift = ( state & TQt::ShiftButton ? TQt::SHIFT : 0 );
|
||
|
int ctrl = ( state & TQt::ControlButton ? TQt::CTRL : 0 );
|
||
|
int alt = ( state & TQt::AltButton ? TQt::ALT : 0 );
|
||
|
int meta = ( state & TQt::MetaButton ? TQt::META : 0 );
|
||
15 years ago
|
|
||
15 years ago
|
TQAction * a = i->action();
|
||
|
TQKeySequence ks = a->accel();
|
||
15 years ago
|
int keys[4] = { ks[0], ks[1], ks[2], ks[3] };
|
||
|
int n = 0;
|
||
|
while ( n < 4 && ks[n++] );
|
||
|
n--;
|
||
|
if ( n < 4 )
|
||
|
keys[n] = key | shift | ctrl | alt | meta;
|
||
15 years ago
|
a->setAccel( TQKeySequence( keys[0], keys[1], keys[2], keys[3] ) );
|
||
15 years ago
|
MetaDataBase::setPropertyChanged( a, "accel", TRUE );
|
||
|
resizeToContents();
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::resizeToContents()
|
||
|
{
|
||
15 years ago
|
TQSize s = contentsSize();
|
||
15 years ago
|
dropLine->resize( s.width(), 2 );
|
||
|
s.rwidth() += borderSize * 2;
|
||
|
s.rheight() += borderSize * 2;
|
||
|
resize( s );
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::showSubMenu()
|
||
|
{
|
||
|
if ( currentIndex < (int)itemList.count() ) {
|
||
|
itemList.at( currentIndex )->showMenu( pos().x() + width() - borderSize * 3,
|
||
|
pos().y() + itemPos( at( currentIndex ) ) +
|
||
|
borderSize * 2 );
|
||
|
setFocus(); // Keep focus in this widget
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::hideSubMenu()
|
||
|
{
|
||
|
if ( currentIndex < (int)itemList.count() )
|
||
|
itemList.at( currentIndex )->hideMenu();
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::focusOnSubMenu()
|
||
|
{
|
||
|
if ( currentIndex < (int)itemList.count() )
|
||
|
itemList.at( currentIndex )->focusOnMenu();
|
||
|
}
|
||
|
|
||
|
// This function has no undo. It is only here to remove an item when its action was
|
||
|
// removed from the action editor.
|
||
|
// Use removeItem to put the command on the command stack.
|
||
|
void PopupMenuEditor::remove( int index )
|
||
|
{
|
||
|
int idx = ( index == -1 ? currentIndex : index );
|
||
|
PopupMenuEditorItem * i = itemList.at( idx );
|
||
|
if ( i && i->isRemovable() ) {
|
||
|
itemList.remove( idx );
|
||
|
int n = itemList.count() + 1;
|
||
|
if ( currentIndex >= n )
|
||
|
currentIndex = itemList.count() + 1;
|
||
|
emit removed( i->action() );
|
||
|
resizeToContents();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
PopupMenuEditorItem * PopupMenuEditor::createItem( TQAction * a )
|
||
15 years ago
|
{
|
||
|
ActionEditor * ae = (ActionEditor *) formWindow()->mainWindow()->child( 0, "ActionEditor" );
|
||
|
if ( !a )
|
||
|
a = ae->newActionEx();
|
||
|
PopupMenuEditorItem * i = new PopupMenuEditorItem( a, this );
|
||
15 years ago
|
TQString n = TQString( a->name() ) + "Item";
|
||
15 years ago
|
formWindow()->unify( i, n, FALSE );
|
||
|
i->setName( n );
|
||
|
AddActionToPopupCommand * cmd =
|
||
|
new AddActionToPopupCommand( i18n( "Add Item" ), formWnd, this, i );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
return i;
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::removeItem( int index )
|
||
|
{
|
||
|
int idx = ( index == -1 ? currentIndex : index );
|
||
|
if ( idx < (int)itemList.count() ) {
|
||
|
RemoveActionFromPopupCommand * cmd = new RemoveActionFromPopupCommand( i18n( "Remove Item" ),
|
||
|
formWnd,
|
||
|
this,
|
||
|
idx );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
14 years ago
|
if ( itemList.count() == 0 && parentMenu )
|
||
|
parentMenu->update();
|
||
15 years ago
|
resizeToContents();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
PopupMenuEditorItem * PopupMenuEditor::currentItem()
|
||
|
{
|
||
|
int count = itemList.count();
|
||
|
if ( currentIndex < count )
|
||
|
return itemList.at( currentIndex );
|
||
|
else if ( currentIndex == count )
|
||
|
return &addItem;
|
||
|
return &addSeparator;
|
||
|
}
|
||
|
|
||
|
PopupMenuEditorItem * PopupMenuEditor::itemAt( int y )
|
||
|
{
|
||
|
PopupMenuEditorItem * i = itemList.first();
|
||
|
int iy = 0;
|
||
|
|
||
|
while ( i ) {
|
||
|
iy += itemHeight( i );
|
||
|
if ( iy > y )
|
||
|
return i;
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
iy += itemHeight( &addItem );
|
||
|
if ( iy > y )
|
||
|
return &addItem;
|
||
|
return &addSeparator;
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::setFocusAt( const TQPoint & pos )
|
||
15 years ago
|
{
|
||
|
hideSubMenu();
|
||
|
lineEdit->hide();
|
||
|
|
||
|
currentIndex = 0;
|
||
|
int iy = 0;
|
||
|
PopupMenuEditorItem * i = itemList.first();
|
||
|
|
||
|
while ( i ) {
|
||
|
iy += itemHeight( i );
|
||
|
if ( iy > pos.y() )
|
||
|
break;
|
||
|
i = itemList.next();
|
||
|
currentIndex++;
|
||
|
}
|
||
|
|
||
|
iy += itemHeight( &addItem );
|
||
|
if ( iy <= pos.y() )
|
||
|
currentIndex++;
|
||
|
|
||
|
if ( currentIndex < (int)itemList.count() ) {
|
||
|
if ( pos.x() < iconWidth )
|
||
|
currentField = 0;
|
||
|
else if ( pos.x() < iconWidth + textWidth )
|
||
|
currentField = 1;
|
||
|
else
|
||
|
currentField = 2;
|
||
|
} else {
|
||
|
currentField = 1;
|
||
|
}
|
||
|
|
||
|
showSubMenu();
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PopupMenuEditor::eventFilter( TQObject * o, TQEvent * e )
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(lineEdit) && e->type() == TQEvent::FocusOut ) {
|
||
15 years ago
|
leaveEditMode( 0 );
|
||
|
update();
|
||
|
}
|
||
15 years ago
|
return TQWidget::eventFilter( o, e );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::paintEvent( TQPaintEvent * )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQPainter p( this );
|
||
15 years ago
|
p.save();
|
||
15 years ago
|
TQRegion reg( rect() );
|
||
|
TQRegion mid( borderSize, borderSize,
|
||
15 years ago
|
rect().width() - borderSize * 2, rect().height() - borderSize * 2 );
|
||
|
reg -= mid;
|
||
|
p.setClipRegion( reg );
|
||
13 years ago
|
style().tqdrawPrimitive( TQStyle::PE_PanelPopup, &p, rect(), colorGroup() );
|
||
15 years ago
|
p.restore();
|
||
|
drawItems( &p );
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::mousePressEvent( TQMouseEvent * e )
|
||
15 years ago
|
{
|
||
|
mousePressPos = e->pos();
|
||
|
setFocusAt( mousePressPos );
|
||
|
e->accept();
|
||
|
update();
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::mouseDoubleClickEvent( TQMouseEvent * )
|
||
15 years ago
|
{
|
||
|
setFocusAt( mousePressPos );
|
||
|
if ( currentItem() == &addSeparator ) {
|
||
14 years ago
|
PopupMenuEditorItem * i = createItem( new QSeparatorAction( 0 ) );
|
||
15 years ago
|
i->setSeparator( TRUE );
|
||
|
return;
|
||
|
}
|
||
|
if ( currentField == 0 ) {
|
||
|
choosePixmap();
|
||
|
resizeToContents();
|
||
|
} else if ( currentField == 1 ) {
|
||
|
showLineEdit();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::mouseMoveEvent( TQMouseEvent * e )
|
||
15 years ago
|
{
|
||
|
if ( e->state() & Qt::LeftButton ) {
|
||
|
if ( ( e->pos() - mousePressPos ).manhattanLength() > 3 ) {
|
||
|
draggedItem = itemAt( mousePressPos.y() );
|
||
|
if ( draggedItem == &addItem ) {
|
||
|
draggedItem = createItem();
|
||
|
RenameActionCommand cmd( i18n( "Rename Item" ), formWnd, draggedItem->action(),
|
||
|
this, "Unnamed" );
|
||
|
cmd.execute();
|
||
|
// FIXME: start rename after drop
|
||
|
} else if ( draggedItem == &addSeparator ) {
|
||
14 years ago
|
draggedItem = createItem( new QSeparatorAction( 0 ) );
|
||
15 years ago
|
draggedItem->setSeparator( TRUE );
|
||
|
}
|
||
|
|
||
|
PopupMenuEditorItemPtrDrag * d =
|
||
|
new PopupMenuEditorItemPtrDrag( draggedItem, this );
|
||
|
|
||
|
hideSubMenu();
|
||
|
|
||
|
draggedItem->setVisible( FALSE );
|
||
|
resizeToContents();
|
||
|
|
||
|
// If the item is dropped in the same list,
|
||
|
// we will have two instances of the same pointer
|
||
|
// in the list. We use node instead.
|
||
14 years ago
|
int idx = itemList.find( draggedItem );
|
||
15 years ago
|
TQLNode * node = itemList.currentNode();
|
||
15 years ago
|
|
||
|
d->dragCopy(); // dragevents and stuff happens
|
||
|
|
||
|
if ( draggedItem ) { // item was not dropped
|
||
|
draggedItem->setVisible( TRUE );
|
||
|
draggedItem = 0;
|
||
|
if ( hasFocus() ) {
|
||
|
hideSubMenu();
|
||
|
resizeToContents();
|
||
|
showSubMenu();
|
||
|
}
|
||
|
} else { // item was dropped
|
||
|
itemList.takeNode( node )->setVisible( TRUE );
|
||
|
if ( currentIndex > 0 && currentIndex > idx )
|
||
|
--currentIndex;
|
||
|
// the drop might happen in another menu, so we'll resize
|
||
|
// and show the submenu there
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::dragEnterEvent( TQDragEnterEvent * e )
|
||
15 years ago
|
{
|
||
|
if ( e->provides( "qt/popupmenueditoritemptr" ) ||
|
||
|
e->provides( "application/x-designer-actions" ) ||
|
||
|
e->provides( "application/x-designer-actiongroup" ) ) {
|
||
|
e->accept();
|
||
|
dropLine->show();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::dragLeaveEvent( TQDragLeaveEvent * )
|
||
15 years ago
|
{
|
||
|
dropLine->hide();
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::dragMoveEvent( TQDragMoveEvent * e )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQPoint pos = e->pos();
|
||
15 years ago
|
dropLine->move( borderSize, snapToItem( pos.y() ) );
|
||
|
|
||
|
if ( currentItem() != itemAt( pos.y() ) ) {
|
||
|
hideSubMenu();
|
||
|
setFocusAt( pos );
|
||
|
showSubMenu();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::dropEvent( TQDropEvent * e )
|
||
15 years ago
|
{
|
||
|
if ( !( e->provides( "qt/popupmenueditoritemptr" ) ||
|
||
|
e->provides( "application/x-designer-actions" ) ||
|
||
|
e->provides( "application/x-designer-actiongroup" ) ) )
|
||
|
return;
|
||
|
|
||
|
// Hide the sub menu of the current item, but do it later
|
||
|
if ( currentIndex < (int)itemList.count() ) {
|
||
|
PopupMenuEditor *s = itemList.at( currentIndex )->s;
|
||
15 years ago
|
TQTimer::singleShot( 0, s, TQT_SLOT( hide() ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
draggedItem = 0;
|
||
|
PopupMenuEditorItem * i = 0;
|
||
|
|
||
|
if ( e->provides( "qt/popupmenueditoritemptr" ) ) {
|
||
|
PopupMenuEditorItemPtrDrag::decode( e, &i );
|
||
|
} else {
|
||
|
if ( e->provides( "application/x-designer-actiongroup" ) ) {
|
||
14 years ago
|
TQActionGroup * g = ::tqqt_cast<QDesignerActionGroup*>(ActionDrag::action());
|
||
15 years ago
|
if ( g->usesDropDown() ) {
|
||
|
i = new PopupMenuEditorItem( g, this );
|
||
15 years ago
|
TQString n = TQString( g->name() ) + "Item";
|
||
15 years ago
|
formWindow()->unify( i, n, FALSE );
|
||
|
i->setName( n );
|
||
14 years ago
|
TQObjectList *l = g->queryList( TQACTION_OBJECT_NAME_STRING, 0, FALSE, FALSE );
|
||
15 years ago
|
TQObjectListIterator it( *l );
|
||
15 years ago
|
for ( ; it.current(); ++it ) {
|
||
14 years ago
|
g = ::tqqt_cast<TQActionGroup*>(it.current());
|
||
15 years ago
|
if ( g )
|
||
|
i->s->insert( g );
|
||
|
else
|
||
15 years ago
|
i->s->insert( (TQAction*)it.current() );
|
||
15 years ago
|
}
|
||
|
delete l;
|
||
|
} else {
|
||
|
dropInPlace( g, e->pos().y() );
|
||
|
}
|
||
|
} else if ( e->provides( "application/x-designer-actions" ) ) {
|
||
14 years ago
|
TQAction *a = ::tqqt_cast<QDesignerAction*>(ActionDrag::action());
|
||
15 years ago
|
i = new PopupMenuEditorItem( a, this );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ( i ) {
|
||
|
dropInPlace( i, e->pos().y() );
|
||
15 years ago
|
TQTimer::singleShot( 0, this, TQT_SLOT( resizeToContents() ) );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQTimer::singleShot( 0, this, TQT_SLOT( showSubMenu() ) );
|
||
|
TQTimer::singleShot( 0, this, TQT_SLOT( setFocus() ) );
|
||
15 years ago
|
dropLine->hide();
|
||
|
e->accept();
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::keyPressEvent( TQKeyEvent * e )
|
||
15 years ago
|
{
|
||
|
if ( lineEdit->isHidden() ) { // In navigation mode
|
||
|
|
||
|
switch ( e->key() ) {
|
||
|
|
||
14 years ago
|
case TQt::Key_Delete:
|
||
15 years ago
|
hideSubMenu();
|
||
|
removeItem();
|
||
|
showSubMenu();
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_Backspace:
|
||
15 years ago
|
clearCurrentField();
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_Up:
|
||
|
navigateUp( e->state() & TQt::ControlButton );
|
||
15 years ago
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_Down:
|
||
|
navigateDown( e->state() & TQt::ControlButton );
|
||
15 years ago
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_Left:
|
||
15 years ago
|
navigateLeft();
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_Right:
|
||
15 years ago
|
navigateRight();
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_PageUp:
|
||
15 years ago
|
currentIndex = 0;
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_PageDown:
|
||
15 years ago
|
currentIndex = itemList.count();
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_Enter:
|
||
|
case TQt::Key_Return:
|
||
|
case TQt::Key_F2:
|
||
15 years ago
|
enterEditMode( e );
|
||
|
// move on
|
||
14 years ago
|
case TQt::Key_Alt:
|
||
|
case TQt::Key_Shift:
|
||
|
case TQt::Key_Control:
|
||
15 years ago
|
// do nothing
|
||
|
return;
|
||
|
|
||
14 years ago
|
case TQt::Key_Escape:
|
||
15 years ago
|
currentField = 0;
|
||
|
navigateLeft();
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_C:
|
||
|
if ( e->state() & TQt::ControlButton &&
|
||
15 years ago
|
currentIndex < (int)itemList.count() ) {
|
||
|
copy( currentIndex );
|
||
|
break;
|
||
|
}
|
||
|
|
||
14 years ago
|
case TQt::Key_X:
|
||
|
if ( e->state() & TQt::ControlButton &&
|
||
15 years ago
|
currentIndex < (int)itemList.count() ) {
|
||
|
hideSubMenu();
|
||
|
cut( currentIndex );
|
||
|
showSubMenu();
|
||
|
break;
|
||
|
}
|
||
|
|
||
14 years ago
|
case TQt::Key_V:
|
||
|
if ( e->state() & TQt::ControlButton ) {
|
||
15 years ago
|
hideSubMenu();
|
||
|
paste( currentIndex < (int)itemList.count() ? currentIndex + 1: itemList.count() );
|
||
|
showSubMenu();
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
default:
|
||
|
if ( currentItem()->isSeparator() )
|
||
|
return;
|
||
|
if ( currentField == 1 ) {
|
||
|
showLineEdit();
|
||
15 years ago
|
TQApplication::sendEvent( lineEdit, e );
|
||
15 years ago
|
e->accept();
|
||
|
return;
|
||
|
} else if ( currentField == 2 ) {
|
||
|
setAccelerator( e->key(), e->state() );
|
||
|
showSubMenu();
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
}
|
||
|
|
||
|
} else { // In edit mode
|
||
|
switch ( e->key() ) {
|
||
14 years ago
|
case TQt::Key_Enter:
|
||
|
case TQt::Key_Return:
|
||
|
case TQt::Key_Escape:
|
||
15 years ago
|
leaveEditMode( e );
|
||
|
e->accept();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
update();
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::focusInEvent( TQFocusEvent * )
|
||
15 years ago
|
{
|
||
|
showSubMenu();
|
||
|
update();
|
||
14 years ago
|
parentMenu->update();
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::focusOutEvent( TQFocusEvent * )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQWidget * fw = tqApp->focusWidget();
|
||
14 years ago
|
if ( !fw || ( !::tqqt_cast<PopupMenuEditor*>(fw) && fw != lineEdit ) ) {
|
||
15 years ago
|
hideSubMenu();
|
||
14 years ago
|
if ( fw && ::tqqt_cast<MenuBarEditor*>(fw) )
|
||
15 years ago
|
return;
|
||
15 years ago
|
TQWidget * w = this;
|
||
14 years ago
|
while ( w && w != fw && ::tqqt_cast<PopupMenuEditor*>(w) ) { // hide all popups
|
||
15 years ago
|
w->hide();
|
||
14 years ago
|
w = ((PopupMenuEditor *)w)->parentEditor();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::drawItem( TQPainter * p, PopupMenuEditorItem * i,
|
||
|
const TQRect & r, int f ) const
|
||
15 years ago
|
{
|
||
|
int x = r.x();
|
||
|
int y = r.y();
|
||
|
int h = r.height();
|
||
|
|
||
13 years ago
|
p->fillRect( r, colorGroup().brush( TQColorGroup::Background ) );
|
||
15 years ago
|
|
||
|
if ( i->isSeparator() ) {
|
||
13 years ago
|
style().tqdrawPrimitive( TQStyle::PE_Separator, p,
|
||
15 years ago
|
TQRect( r.x(), r.y() + 2, r.width(), 1 ),
|
||
13 years ago
|
colorGroup(), TQStyle::Style_Sunken | f );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
const TQAction * a = i->action();
|
||
15 years ago
|
if ( a->isToggleAction() && a->isOn() ) {
|
||
13 years ago
|
style().tqdrawPrimitive( TQStyle::PE_CheckMark, p,
|
||
15 years ago
|
TQRect( x , y, iconWidth, h ),
|
||
13 years ago
|
colorGroup(), f );
|
||
15 years ago
|
} else {
|
||
15 years ago
|
TQPixmap icon = a->iconSet().pixmap( TQIconSet::Automatic, TQIconSet::Normal );
|
||
15 years ago
|
p->drawPixmap( x + ( iconWidth - icon.width() ) / 2,
|
||
|
y + ( h - icon.height() ) / 2,
|
||
|
icon );
|
||
|
}
|
||
|
x += iconWidth;
|
||
|
p->drawText( x, y, textWidth, h,
|
||
15 years ago
|
TQPainter::AlignLeft |
|
||
|
TQPainter::AlignVCenter |
|
||
14 years ago
|
TQt::ShowPrefix |
|
||
|
TQt::SingleLine,
|
||
15 years ago
|
a->menuText() );
|
||
|
|
||
|
x += textWidth + borderSize * 3;
|
||
|
p->drawText( x, y, accelWidth, h,
|
||
15 years ago
|
TQPainter::AlignLeft | TQPainter::AlignVCenter,
|
||
15 years ago
|
a->accel() );
|
||
|
if ( i->count() ) // Item has submenu
|
||
13 years ago
|
style().tqdrawPrimitive( TQStyle::PE_ArrowRight, p,
|
||
15 years ago
|
TQRect( r.width() - arrowWidth, r.y(), arrowWidth, r.height() ),
|
||
13 years ago
|
colorGroup(), f );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::drawWinFocusRect( TQPainter * p, const TQRect & r ) const
|
||
15 years ago
|
{
|
||
|
if ( currentIndex < (int)itemList.count() &&
|
||
|
((PopupMenuEditor*)this)->itemList.at( currentIndex )->isSeparator() ) {
|
||
|
p->drawWinFocusRect( borderSize, r.y(), width() - borderSize * 2, r.height() );
|
||
|
return;
|
||
|
}
|
||
|
int y = r.y();
|
||
|
int h = r.height();
|
||
|
if ( currentField == 0 )
|
||
|
p->drawWinFocusRect( borderSize + 1, y, iconWidth - 2, h );
|
||
|
else if ( currentField == 1 )
|
||
|
p->drawWinFocusRect( borderSize + iconWidth, y, textWidth, h );
|
||
|
else if ( currentField == 2 )
|
||
|
p->drawWinFocusRect( borderSize + iconWidth + textWidth +
|
||
|
borderSize * 3, y, accelWidth, h );
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::drawItems( TQPainter * p )
|
||
15 years ago
|
{
|
||
|
int flags = 0;
|
||
|
int idx = 0;
|
||
|
|
||
13 years ago
|
TQColorGroup enabled = colorGroup();
|
||
13 years ago
|
TQColorGroup disabled = palette().disabled();
|
||
15 years ago
|
TQRect focus;
|
||
|
TQRect rect( borderSize, borderSize, width() - borderSize * 2, 0 );
|
||
15 years ago
|
|
||
|
PopupMenuEditorItem * i = itemList.first();
|
||
|
while ( i ) {
|
||
|
if ( i->isVisible() ) {
|
||
|
rect.setHeight( itemHeight( i ) );
|
||
|
if ( idx == currentIndex )
|
||
|
focus = rect;
|
||
|
if ( i->action()->isEnabled() ) {
|
||
15 years ago
|
flags = TQStyle::Style_Enabled;
|
||
15 years ago
|
p->setPen( enabled.buttonText() );
|
||
|
} else {
|
||
15 years ago
|
flags = TQStyle::Style_Default;
|
||
15 years ago
|
p->setPen( disabled.buttonText() );
|
||
|
}
|
||
|
drawItem( p, i, rect, flags );
|
||
|
rect.moveBy( 0, rect.height() );
|
||
|
}
|
||
|
i = itemList.next();
|
||
|
idx++;
|
||
|
}
|
||
|
|
||
|
// Draw the "add item" and "add separator" items
|
||
|
p->setPen( darkBlue );
|
||
|
rect.setHeight( itemHeight( &addItem ) );
|
||
|
if ( idx == currentIndex )
|
||
|
focus = rect;
|
||
15 years ago
|
drawItem( p, &addItem, rect, TQStyle::Style_Default );
|
||
15 years ago
|
rect.moveBy( 0, rect.height() );
|
||
|
idx++;
|
||
|
rect.setHeight( itemHeight( &addSeparator ) );
|
||
|
if ( idx == currentIndex )
|
||
|
focus = rect;
|
||
15 years ago
|
drawItem( p, &addSeparator, rect, TQStyle::Style_Default );
|
||
15 years ago
|
idx++;
|
||
|
|
||
|
if ( hasFocus() && !draggedItem )
|
||
|
drawWinFocusRect( p, focus );
|
||
|
}
|
||
|
|
||
15 years ago
|
TQSize PopupMenuEditor::contentsSize()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQRect textRect = fontMetrics().boundingRect( addSeparator.action()->menuText() );
|
||
15 years ago
|
textWidth = textRect.width();
|
||
|
accelWidth = textRect.height(); // default size
|
||
|
iconWidth = textRect.height();
|
||
|
|
||
|
int w = 0;
|
||
|
int h = itemHeight( &addItem ) + itemHeight( &addSeparator );
|
||
|
PopupMenuEditorItem * i = itemList.first();
|
||
15 years ago
|
TQAction * a = 0;
|
||
15 years ago
|
while ( i ) {
|
||
|
if ( i->isVisible() ) {
|
||
|
if ( !i->isSeparator() ) {
|
||
|
a = i->action();
|
||
15 years ago
|
w = a->iconSet().pixmap( TQIconSet::Automatic, TQIconSet::Normal ).rect().width() +
|
||
15 years ago
|
borderSize; // padding
|
||
14 years ago
|
iconWidth = TQMAX( iconWidth, w );
|
||
15 years ago
|
w = fontMetrics().boundingRect( a->menuText() ).width();
|
||
14 years ago
|
textWidth = TQMAX( textWidth, w );
|
||
15 years ago
|
w = fontMetrics().boundingRect( a->accel() ).width() + 2; // added padding?
|
||
14 years ago
|
accelWidth = TQMAX( accelWidth, w );
|
||
15 years ago
|
}
|
||
|
h += itemHeight( i );
|
||
|
}
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
|
||
|
int width = iconWidth + textWidth + borderSize * 3 + accelWidth + arrowWidth;
|
||
15 years ago
|
return TQSize( width, h );
|
||
15 years ago
|
}
|
||
|
|
||
|
int PopupMenuEditor::itemHeight( const PopupMenuEditorItem * item ) const
|
||
|
{
|
||
|
if ( !item || ( item && !item->isVisible() ) )
|
||
|
return 0;
|
||
|
if ( item->isSeparator() )
|
||
|
return 4; // FIXME: hardcoded ( get from styles )r
|
||
|
int padding = + borderSize * 6;
|
||
15 years ago
|
TQAction * a = item->action();
|
||
|
int h = a->iconSet().pixmap( TQIconSet::Automatic, TQIconSet::Normal ).rect().height();
|
||
14 years ago
|
h = TQMAX( h, fontMetrics().boundingRect( a->menuText() ).height() + padding );
|
||
|
h = TQMAX( h, fontMetrics().boundingRect( a->accel() ).height() + padding );
|
||
15 years ago
|
return h;
|
||
|
}
|
||
|
|
||
|
int PopupMenuEditor::itemPos( const PopupMenuEditorItem * item ) const
|
||
|
{
|
||
|
PopupMenuEditor * that = ( PopupMenuEditor * ) this;
|
||
|
int y = 0;
|
||
|
PopupMenuEditorItem * i = that->itemList.first();
|
||
|
while ( i ) {
|
||
|
if ( i == item )
|
||
|
return y;
|
||
|
y += itemHeight( i );
|
||
|
i = that->itemList.next();
|
||
|
}
|
||
|
return y;
|
||
|
}
|
||
|
|
||
|
int PopupMenuEditor::snapToItem( int y )
|
||
|
{
|
||
|
int iy = 0;
|
||
|
int dy = 0;
|
||
|
|
||
|
PopupMenuEditorItem * i = itemList.first();
|
||
|
|
||
|
while ( i ) {
|
||
|
dy = itemHeight( i );
|
||
|
if ( iy + dy / 2 > y )
|
||
|
return iy;
|
||
|
iy += dy;
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
|
||
|
return iy;
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::dropInPlace( PopupMenuEditorItem * i, int y )
|
||
|
{
|
||
|
int iy = 0;
|
||
|
int dy = 0;
|
||
|
int idx = 0;
|
||
|
|
||
|
PopupMenuEditorItem * n = itemList.first();
|
||
|
|
||
|
while ( n ) {
|
||
|
dy = itemHeight( n );
|
||
|
if ( iy + dy / 2 > y )
|
||
|
break;
|
||
|
iy += dy;
|
||
|
idx++;
|
||
|
n = itemList.next();
|
||
|
}
|
||
14 years ago
|
int same = itemList.findRef( i );
|
||
15 years ago
|
AddActionToPopupCommand * cmd = new AddActionToPopupCommand( i18n( "Drop Item" ), formWnd, this, i, idx );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
currentIndex = ( same >= 0 && same < idx ) ? idx - 1 : idx;
|
||
|
currentField = 1;
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::dropInPlace( TQActionGroup * g, int y )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQObjectList l = g->childrenListObject();
|
||
|
if (l.isEmpty())
|
||
15 years ago
|
return;
|
||
|
for ( int i = 0; i < (int)l.count(); ++i ) {
|
||
14 years ago
|
TQAction *a = ::tqqt_cast<TQAction*>(l.at(i));
|
||
|
TQActionGroup *g = ::tqqt_cast<TQActionGroup*>(l.at(i));
|
||
15 years ago
|
if ( g )
|
||
|
dropInPlace( g, y );
|
||
|
else if ( a )
|
||
|
dropInPlace( new PopupMenuEditorItem( a, this ), y );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::safeDec()
|
||
|
{
|
||
|
do {
|
||
|
currentIndex--;
|
||
|
} while ( currentIndex > 0 && !currentItem()->isVisible() );
|
||
|
if ( currentIndex == 0 &&
|
||
|
!currentItem()->isVisible() &&
|
||
14 years ago
|
parentMenu ) {
|
||
|
parentMenu->setFocus();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::safeInc()
|
||
|
{
|
||
|
int max = itemList.count() + 1;
|
||
|
if ( currentIndex < max ) {
|
||
|
do {
|
||
|
currentIndex++;
|
||
|
} while ( currentIndex < max && !currentItem()->isVisible() ); // skip invisible items
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::clearCurrentField()
|
||
|
{
|
||
|
if ( currentIndex >= (int)itemList.count() )
|
||
|
return; // currentIndex is addItem or addSeparator
|
||
|
PopupMenuEditorItem * i = currentItem();
|
||
|
hideSubMenu();
|
||
|
if ( i->isSeparator() )
|
||
|
return;
|
||
|
if ( currentField == 0 ) {
|
||
15 years ago
|
TQIconSet icons( 0 );
|
||
15 years ago
|
SetActionIconsCommand * cmd = new SetActionIconsCommand( i18n( "Remove Icon" ),
|
||
|
formWnd,
|
||
|
i->action(),
|
||
|
this,
|
||
|
icons );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
} else if ( currentField == 2 ) {
|
||
|
i->action()->setAccel( 0 );
|
||
|
}
|
||
|
resizeToContents();
|
||
|
showSubMenu();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::navigateUp( bool ctrl )
|
||
|
{
|
||
|
if ( currentIndex > 0 ) {
|
||
|
hideSubMenu();
|
||
|
if ( ctrl ) {
|
||
|
ExchangeActionInPopupCommand * cmd =
|
||
|
new ExchangeActionInPopupCommand( i18n( "Move Item Up" ),
|
||
|
formWnd,
|
||
|
this,
|
||
|
currentIndex,
|
||
|
currentIndex - 1 );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
safeDec();
|
||
|
} else {
|
||
|
safeDec();
|
||
|
}
|
||
|
showSubMenu();
|
||
14 years ago
|
} else if ( parentMenu ) {
|
||
|
parentMenu->setFocus();
|
||
|
parentMenu->update();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::navigateDown( bool ctrl )
|
||
|
{
|
||
|
hideSubMenu();
|
||
|
if ( ctrl ) {
|
||
|
if ( currentIndex < ( (int)itemList.count() - 1 ) ) { // safe index
|
||
|
ExchangeActionInPopupCommand * cmd =
|
||
|
new ExchangeActionInPopupCommand( i18n( "Move Item Down" ),
|
||
|
formWnd,
|
||
|
this,
|
||
|
currentIndex,
|
||
|
currentIndex + 1 );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
safeInc();
|
||
|
}
|
||
|
} else { // ! Ctrl
|
||
|
safeInc();
|
||
|
}
|
||
|
if ( currentIndex >= (int)itemList.count() )
|
||
|
currentField = 1;
|
||
|
showSubMenu();
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::navigateLeft()
|
||
|
{
|
||
|
if ( currentItem()->isSeparator() ||
|
||
|
currentIndex >= (int)itemList.count() ||
|
||
|
currentField == 0 ) {
|
||
14 years ago
|
if ( parentMenu ) {
|
||
15 years ago
|
hideSubMenu();
|
||
14 years ago
|
parentMenu->setFocus();
|
||
15 years ago
|
} else if ( !currentItem()->isSeparator() ) {
|
||
|
currentField = 2;
|
||
|
}
|
||
|
} else {
|
||
|
currentField--;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PopupMenuEditor::navigateRight()
|
||
|
{
|
||
|
if ( !currentItem()->isSeparator() &&
|
||
|
currentIndex < (int)itemList.count() ) {
|
||
|
if ( currentField == 2 ) {
|
||
|
focusOnSubMenu();
|
||
|
} else {
|
||
|
currentField++;
|
||
|
currentField %= 3;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::enterEditMode( TQKeyEvent * e )
|
||
15 years ago
|
{
|
||
|
PopupMenuEditorItem * i = currentItem();
|
||
|
|
||
|
if ( i == &addSeparator ) {
|
||
14 years ago
|
i = createItem( new QSeparatorAction( 0 ) );
|
||
15 years ago
|
} else if ( i->isSeparator() ) {
|
||
|
return;
|
||
|
} else if ( currentField == 0 ) {
|
||
|
choosePixmap();
|
||
|
} else if ( currentField == 1 ) {
|
||
|
showLineEdit();
|
||
|
return;
|
||
|
} else {// currentField == 2
|
||
|
setAccelerator( e->key(), e->state() );
|
||
|
}
|
||
|
showSubMenu();
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
void PopupMenuEditor::leaveEditMode( TQKeyEvent * e )
|
||
15 years ago
|
{
|
||
|
setFocus();
|
||
|
lineEdit->hide();
|
||
|
|
||
|
PopupMenuEditorItem * i = 0;
|
||
14 years ago
|
if ( e && e->key() == TQt::Key_Escape ) {
|
||
15 years ago
|
update();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ( currentIndex >= (int)itemList.count() ) {
|
||
|
// new item was created
|
||
15 years ago
|
TQAction * a = formWnd->mainWindow()->actioneditor()->newActionEx();
|
||
|
TQString actionText = lineEdit->text();
|
||
14 years ago
|
actionText.replace("&&", "&");
|
||
15 years ago
|
TQString menuText = lineEdit->text();
|
||
15 years ago
|
a->setText( actionText );
|
||
|
a->setMenuText( menuText );
|
||
|
i = createItem( a );
|
||
15 years ago
|
TQString n = constructName( i );
|
||
15 years ago
|
formWindow()->unify( a, n, TRUE );
|
||
|
a->setName( n );
|
||
|
MetaDataBase::addEntry( a );
|
||
|
MetaDataBase::setPropertyChanged( a, "menuText", TRUE );
|
||
|
ActionEditor *ae = (ActionEditor*)formWindow()->mainWindow()->child( 0, "ActionEditor" );
|
||
|
if ( ae )
|
||
|
ae->updateActionName( a );
|
||
|
} else {
|
||
|
i = itemList.at( currentIndex );
|
||
|
RenameActionCommand * cmd = new RenameActionCommand( i18n( "Rename Item" ),
|
||
|
formWnd,
|
||
|
i->action(),
|
||
|
this,
|
||
|
lineEdit->text() );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
}
|
||
|
resizeToContents();
|
||
|
|
||
|
if ( !i )
|
||
|
return;
|
||
|
|
||
|
if ( i->isSeparator() )
|
||
|
hideSubMenu();
|
||
|
else
|
||
|
showSubMenu();
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString PopupMenuEditor::constructName( PopupMenuEditorItem *item )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString s;
|
||
|
TQString name = item->action()->menuText();
|
||
14 years ago
|
TQWidget *e = parentEditor();
|
||
14 years ago
|
PopupMenuEditor *p = ::tqqt_cast<PopupMenuEditor*>(e);
|
||
15 years ago
|
if ( p ) {
|
||
14 years ago
|
int idx = p->find( item->m );
|
||
15 years ago
|
PopupMenuEditorItem * i = ( idx > -1 ? p->at( idx ) : 0 );
|
||
15 years ago
|
s = ( i ? TQString( i->action()->name() ).remove( "Action" ) : TQString( "" ) );
|
||
15 years ago
|
} else {
|
||
14 years ago
|
MenuBarEditor *b = ::tqqt_cast<MenuBarEditor*>(e);
|
||
15 years ago
|
if ( b ) {
|
||
|
int idx = b->findItem( item->m );
|
||
|
MenuBarEditorItem * i = ( idx > -1 ? b->item( idx ) : 0 );
|
||
15 years ago
|
s = ( i ? i->menuText().lower() : TQString( "" ) );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
// replace illegal characters
|
||
|
|
||
|
return ( RenameMenuCommand::makeLegal( s ) +
|
||
|
RenameMenuCommand::makeLegal( name ) + "Action" );
|
||
|
}
|