#include "QtxAction.h"
-
-#include <qpopupmenu.h>
#include <qmenubar.h>
+#include <qpopupmenu.h>
/*!
Name: QtxAction [public]
*/
QtxAction::QtxAction( QObject* parent, const char* name, bool toggle )
- : QAction( parent, name, toggle )
+: QAction( parent, name, toggle )
{
}
QtxAction::QtxAction( const QString& text, const QIconSet& icon,
const QString& menuText, int accel,
QObject* parent, const char* name, bool toggle )
- : QAction( text, icon, menuText, accel, parent, name, toggle )
+: QAction( text, icon, menuText, accel, parent, name, toggle )
{
}
QtxAction::QtxAction( const QString& text, const QString& menuText, int accel,
QObject* parent, const char* name, bool toggle )
- : QAction( text, menuText, accel, parent, name, toggle )
+: QAction( text, menuText, accel, parent, name, toggle )
{
}
bool QtxAction::addTo( QWidget* w )
{
- if ( w->inherits( "QMenuBar" ) ) {
-
-
- // --- Add action to the QMenuBar ---
- // n.b. currently for the actions inserted to the menu bar
- // the following properties are not supported:
- // * tooltips
- // * what's this info
- // * toggle mode
- QMenuBar* mb = (QMenuBar*)w;
- if ( myMenuIds.find( w ) != myMenuIds.end() )
+ if ( !w->inherits( "QMenuBar" ) )
+ return QAction::addTo( w );
+
+ // --- Add action to the QMenuBar ---
+ // n.b. currently for the actions inserted to the menu bar
+ // the following properties are not supported:
+ // * tooltips
+ // * what's this info
+ // * toggle mode
+ QMenuBar* mb = (QMenuBar*)w;
+ if ( myMenuIds.contains( w ) )
return false; // already added
-
-
-
-
-
-
-
-
-
-
-
-
- if ( name() == "qt_separator_action" ) // separator
- myMenuIds[ w ] = mb->insertSeparator();
- else if ( iconSet().isNull() ) // has no icon
- myMenuIds[ w ] = mb->insertItem( menuText(), this, SIGNAL( activated() ), accel() );
- else // has icon
- myMenuIds[ w ] = mb->insertItem( iconSet(), menuText(), this, SIGNAL( activated() ), accel() );
- mb->setItemEnabled( myMenuIds[ w ], isEnabled() );
- mb->setItemVisible( myMenuIds[ w ], isVisible() );
-
- return true;
+ QMenuData* mData = mb;
+ int idx = mData->count();
+ while ( idx > 0 )
+ {
+ QMenuData* md = 0;
+ QMenuItem* mItem = mb->findItem( mb->idAt( idx - 1 ), &md );
+ if ( md == mData && mItem && mItem->isSeparator() && mItem->widget() && qstrcmp( mItem->widget()->name(), "qt_maxtools" ) )
+ idx--;
+ else
+ break;
}
- return QAction::addTo( w );
+
+ if ( name() == "qt_separator_action" ) // separator
+ myMenuIds.insert( w, mb->insertSeparator( idx ) );
+ else if ( iconSet().isNull() ) // has no icon
+ myMenuIds.insert( w, mb->insertItem( menuText(), this, SIGNAL( activated() ), accel(), -1, idx ) );
+ else // has icon
+ myMenuIds.insert( w, mb->insertItem( iconSet(), menuText(), this, SIGNAL( activated() ), accel(), -1, idx ) );
+
+ mb->setItemEnabled( myMenuIds[w], isEnabled() );
+ mb->setItemVisible( myMenuIds[w], isVisible() );
+ return true;
}
/*!
if ( !addTo( w ) )
return false;
- if ( w->inherits( "QPopupMenu" ) ) {
-
+ if ( w->inherits( "QPopupMenu" ) )
+ {
// --- Add action to the QPopupMenu ---
QPopupMenu* popup = (QPopupMenu*)w;
- if ( index >= 0 && index < (int)popup->count() - 1 ) {
-
+ if ( index >= 0 && index < (int)popup->count() - 1 )
+ {
int id = popup->idAt( popup->count() - 1 );
- if ( id != -1 ) {
-
- QMenuItem* item = popup->findItem( id );
- if ( item && item->isSeparator() ) {
-
- popup->removeItem( id );
- popup->insertSeparator( index );
- }
- else {
-
- QPopupMenu* p = item ? item->popup() : 0;
- int accel = popup->accel( id );
- bool isOn = popup->isItemEnabled( id );
- bool isVisible = popup->isItemVisible( id );
- bool isChecked = popup->isItemChecked( id );
- QString text = popup->text( id );
- QIconSet icon;
- if ( popup->iconSet( id ) )
- icon = *popup->iconSet( id );
- popup->removeItem( id );
- int pos;
- if ( icon.isNull() )
-
- if ( p )
- pos = popup->indexOf( popup->insertItem( text, p, id, index ) );
- else
- pos = popup->indexOf( popup->insertItem( text, id, index ) );
-
- else
-
- if ( p )
- pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
- else
- pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
-
- popup->setId( pos, id );
- popup->setAccel( accel, id );
- popup->setItemEnabled( id, isOn );
- popup->setItemVisible( id, isVisible );
- popup->setItemChecked( id, isChecked );
- if ( !whatsThis().isEmpty() )
- popup->setWhatsThis( id, whatsThis() );
- if ( !p )
- popup->connectItem( id, this, SLOT( internalActivation() ) );
- }
+ if ( id != -1 )
+ {
+ QMenuItem* item = popup->findItem( id );
+ if ( item && item->isSeparator() )
+ {
+ popup->removeItem( id );
+ popup->insertSeparator( index );
+ }
+ else
+ {
+ QPopupMenu* p = item ? item->popup() : 0;
+ int accel = popup->accel( id );
+ bool isOn = popup->isItemEnabled( id );
+ bool isVisible = popup->isItemVisible( id );
+ bool isChecked = popup->isItemChecked( id );
+ QString text = popup->text( id );
+ QIconSet icon;
+ if ( popup->iconSet( id ) )
+ icon = *popup->iconSet( id );
+ popup->removeItem( id );
+ int pos;
+ if ( icon.isNull() )
+ {
+ if ( p )
+ pos = popup->indexOf( popup->insertItem( text, p, id, index ) );
+ else
+ pos = popup->indexOf( popup->insertItem( text, id, index ) );
+ }
+ else
+ {
+ if ( p )
+ pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
+ else
+ pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
+ }
+ popup->setId( pos, id );
+ popup->setAccel( accel, id );
+ popup->setItemEnabled( id, isOn );
+ popup->setItemVisible( id, isVisible );
+ popup->setItemChecked( id, isChecked );
+ if ( !whatsThis().isEmpty() )
+ popup->setWhatsThis( id, whatsThis() );
+ if ( !p )
+ popup->connectItem( id, this, SLOT( internalActivation() ) );
+ }
}
}
}
- else if ( w->inherits( "QMenuBar" ) ) {
-
+ else if ( w->inherits( "QMenuBar" ) )
+ {
// --- Add action to the QMenuBar ---
QMenuBar* mb = (QMenuBar*)w;
- if ( index >= 0 && index < (int)mb->count() - 1 ) {
-
+ if ( index >= 0 && index < (int)mb->count() - 1 )
+ {
int id = mb->idAt( mb->count() - 1 );
- if ( id != -1 ) {
-
- QMenuItem* item = mb->findItem( id );
- if ( item && item->isSeparator() ) {
-
- mb->removeItem( id );
- mb->insertSeparator( index );
- }
- else {
-
- QPopupMenu* p = item ? item->popup() : 0;
- int accel = mb->accel( id );
- bool isOn = mb->isItemEnabled( id );
- bool isVisible = mb->isItemVisible( id );
- QString text = mb->text( id );
- QIconSet icon;
- if ( mb->iconSet( id ) )
- icon = *mb->iconSet( id );
- mb->removeItem( id );
- int pos;
- if ( icon.isNull() )
-
- if ( p )
- pos = mb->indexOf( mb->insertItem( text, p, id, index ) );
- else
- pos = mb->indexOf( mb->insertItem( text, id, index ) );
-
- else
-
- if ( p )
- pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) );
- else
- pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) );
-
- mb->setId( pos, id );
- mb->setAccel( accel, id );
- mb->setItemEnabled( id, isOn );
- mb->setItemVisible( id, isVisible );
- if ( !p )
- mb->connectItem( id, this, SIGNAL( activated() ) );
- }
+ if ( id != -1 )
+ {
+ QMenuItem* item = mb->findItem( id );
+ if ( item && item->isSeparator() )
+ {
+ mb->removeItem( id );
+ mb->insertSeparator( index );
+ }
+ else
+ {
+ QPopupMenu* p = item ? item->popup() : 0;
+ int accel = mb->accel( id );
+ bool isOn = mb->isItemEnabled( id );
+ bool isVisible = mb->isItemVisible( id );
+ QString text = mb->text( id );
+ QIconSet icon;
+ if ( mb->iconSet( id ) )
+ icon = *mb->iconSet( id );
+ mb->removeItem( id );
+ int pos;
+ if ( icon.isNull() )
+ {
+ if ( p )
+ pos = mb->indexOf( mb->insertItem( text, p, id, index ) );
+ else
+ pos = mb->indexOf( mb->insertItem( text, id, index ) );
+ }
+ else
+ {
+ if ( p )
+ pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) );
+ else
+ pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) );
+ }
+ mb->setId( pos, id );
+ mb->setAccel( accel, id );
+ mb->setItemEnabled( id, isOn );
+ mb->setItemVisible( id, isVisible );
+ if ( !p )
+ mb->connectItem( id, this, SIGNAL( activated() ) );
+ }
}
}
}
bool QtxAction::removeFrom( QWidget* w )
{
-
+ bool res = false;
// check if widget is QMenuBar
- if ( w->inherits( "QMenuBar" ) ) {
-
+ if ( w->inherits( "QMenuBar" ) )
+ {
QMenuBar* mb = (QMenuBar*)w;
- if ( myMenuIds.find( w ) == myMenuIds.end() )
- return false; // not yet added
- mb->removeItem( myMenuIds[ w ] );
- myMenuIds.remove( w );
- return true;
-
+ if ( myMenuIds.find( w ) != myMenuIds.end() )
+ {
+ mb->removeItem( myMenuIds[ w ] );
+ myMenuIds.remove( w );
+ res = true;
+ }
}
- return QAction::removeFrom( w );
-
-
+ else
+ res = QAction::removeFrom( w );
+ return res;
}
/*!
pmd->removeItem( id );
// add new item
- if ( w->inherits( "QPopupMenu" ) ) {
-
+ if ( w->inherits( "QPopupMenu" ) )
+ {
// --- QPopupMenu ---
QPopupMenu* popup = (QPopupMenu*)w;
if ( icon.isNull() )
pos = popup->indexOf( subPopup ? popup->insertItem( text, subPopup, id, pos ) :
- popup->insertItem( text, id, pos ) );
+ popup->insertItem( text, id, pos ) );
else
pos = popup->indexOf( subPopup ? popup->insertItem( icon, text, subPopup, id, pos ) :
- popup->insertItem( icon, text, id, pos ) );
+ popup->insertItem( icon, text, id, pos ) );
}
- else {
-
+ else
+ {
// --- QMenuBar ---
QMenuBar* mb = (QMenuBar*)w;
if ( icon.isNull() )
pos = mb->indexOf( subPopup ? mb->insertItem( text, subPopup, id, pos ) :
- mb->insertItem( text, id, pos ) );
+ mb->insertItem( text, id, pos ) );
else
pos = mb->indexOf( subPopup ? mb->insertItem( icon, text, subPopup, id, pos ) :
- mb->insertItem( icon, text, id, pos ) );
+ mb->insertItem( icon, text, id, pos ) );
}
// restore properties
// delete old popup
delete oldPopup;
}
-
-
QPopupMenu* popup() const;
private:
- int myId;
- QPopupMenu* myPopup;
- bool myEmptyEnabled;
- QMap<QWidget*,int> myIds;
+ int myId;
+ QPopupMenu* myPopup;
+ bool myEmptyEnabled;
+ QMap<QWidget*, int> myIds;
};
-
/*!
Constructor for menu action
\param text - description text
\param allowEmpty - if it is true, it makes possible to add this action with empty popup to menu
*/
-QtxActionMenuMgr::MenuAction::MenuAction( const QString& text,
- const QString& menuText,
- QObject* parent,
- const int id,
- const bool allowEmpty )
+QtxActionMenuMgr::MenuAction::MenuAction( const QString& text, const QString& menuText,
+ QObject* parent, const int id, const bool allowEmpty )
: QtxAction( text, menuText, 0, parent ),
- myId( id ),
- myPopup( 0 ),
- myEmptyEnabled( allowEmpty )
+myId( id ),
+myPopup( 0 ),
+myEmptyEnabled( allowEmpty )
{
myPopup = new QPopupMenu();
}
if ( !myEmptyEnabled && !myPopup->count() )
return false; // not allowed empty menu
- if ( w->inherits( "QPopupMenu" ) ) {
-
+ if ( w->inherits( "QPopupMenu" ) )
+ {
QValueList<int> l = prepareIds( w );
int idx;
- if ( QtxAction::addTo( w ) && ( idx = getNewId( w, l, false ) ) != -1 ) {
-
+ if ( QtxAction::addTo( w ) && ( idx = getNewId( w, l, false ) ) != -1 )
+ {
QPopupMenu* pm = (QPopupMenu*)w;
myIds[ w ] = pm->idAt( idx );
if ( myId != -1 )
- pm->setId( idx, myId );
+ pm->setId( idx, myId );
setPopup( pm, myId != -1 ? myId : myIds[ w ], myPopup );
}
}
- else if ( w->inherits( "QMenuBar" ) ) {
-
+ else if ( w->inherits( "QMenuBar" ) )
+ {
QValueList<int> l = prepareIds( w );
int idx;
- if ( QtxAction::addTo( w ) && ( idx = getNewId( w, l, false ) ) != -1 ) {
-
+ if ( QtxAction::addTo( w ) && ( idx = getNewId( w, l, false ) ) != -1 )
+ {
QMenuBar* mb = (QMenuBar*)w;
myIds[ w ] = mb->idAt( idx );
if ( myId != -1 )
- mb->setId( idx, myId );
+ mb->setId( idx, myId );
setPopup( mb, myId != -1 ? myId : myIds[ w ], myPopup );
}
}
if ( myIds.find( w ) == myIds.end() )
return false; // not yet added
- if ( w->inherits( "QPopupMenu" ) ) {
- if ( myId != -1 ) {
-
-
+ if ( w->inherits( "QPopupMenu" ) )
+ {
+ if ( myId != -1 )
+ {
QPopupMenu* pm = (QPopupMenu*)w;
int idx = pm->indexOf( myId );
- if ( idx != -1 ) pm->setId( idx, myIds[ w ] );
-
+ if ( idx != -1 )
+ pm->setId( idx, myIds[ w ] );
}
myIds.remove( w );
return QtxAction::removeFrom( w );;
}
else if ( w->inherits( "QMenuBar" ) )
{
- if ( myId != -1 ) {
-
+ if ( myId != -1 )
+ {
QMenuBar* mb = (QMenuBar*)w;
int idx = mb->indexOf( myId );
- if ( idx != -1 ) mb->setId( idx, myIds[ w ] );
-
+ if ( idx != -1 )
+ mb->setId( idx, myIds[ w ] );
}
myIds.remove( w );
return QtxAction::removeFrom( w );
*/
QtxActionMenuMgr::QtxActionMenuMgr( QMainWindow* p )
: QtxActionMgr( p ),
- myMenu( p ? p->menuBar() : 0 )
+myMenu( p ? p->menuBar() : 0 )
{
myRoot.id = -1;
myRoot.group = -1;
*/
QtxActionMenuMgr::QtxActionMenuMgr( QWidget* mw, QObject* p )
: QtxActionMgr( p ),
- myMenu( mw )
+myMenu( mw )
{
myRoot.id = -1;
myRoot.group = -1;
node->idx = idx;
node->group = group;
- pNode->children.append( node );
+ pNode->children.append( node );
triggerUpdate( pNode->id, false );
node->idx = idx;
node->id = myMenus.insert( gid, ma ).key();
- pNode->children.append( node );
+ pNode->children.append( node );
triggerUpdate( pNode->id, false );
a->removeFrom( mw );
}
/* VSR: commented to allow direct creating of menus by calling insertItem() methods
- if ( mw->inherits( "QMenuBar" ) )
- ((QMenuBar*)mw)->clear();
- else if ( mw->inherits( "QPopupMenu" ) )
- ((QPopupMenu*)mw)->clear();
+ if ( mw->inherits( "QMenuBar" ) )
+ ((QMenuBar*)mw)->clear();
+ else if ( mw->inherits( "QPopupMenu" ) )
+ ((QPopupMenu*)mw)->clear();
*/
QMap<int, NodeList> idMap;
for ( NodeListIterator it2( node->children ); it2.current(); ++it2 )
{
- NodeList& lst = idMap[it2.current()->group];
+ NodeList& lst = idMap[it2.current()->group];
int idx = it2.current()->idx;
if ( idx < 0 || idx >= (int)lst.count() )
lst.append( it2.current() );
const NodeList& lst = idMap[*gIt];
for ( NodeListIterator iter( lst ); iter.current(); ++iter )
{
-
-
+ if ( rec )
+ updateMenu( iter.current(), rec, false );
MenuNode* par = iter.current()->parent;
if ( !isVisible( iter.current()->id, par ? par->id : -1 ) )
continue;
- if ( rec )
- updateMenu( iter.current(), rec, false );
-
QAction* a = itemAction( iter.current()->id );
if ( !a )
a = menuAction( iter.current()->id );
if ( a )
- a->addTo( mw );
+ a->addTo( mw );
}
}
*/
void QtxActionMenuMgr::internalUpdate()
{
- if ( isUpdatesEnabled() )
-
-
- updateMenu();
+ if ( !isUpdatesEnabled() )
+ return;
+ updateMenu();
+ myUpdateIds.clear();
}
/*!
*/
void QtxActionMenuMgr::updateContent()
{
-
-
-
-
-
-
-
+ // Warning: For correct updating it is necessary to update the most enclosed submenu firstly
+ // because not updated empty submenu will be skipped. Now the submenu are iterated in
+ // ascending order their identifiers. For a submenu with automatically generated identifiers
+ // will work correctly as the uppermost submenu have the biggest number (identifiers generated
+ // reduction from -1). Generally when any submenu will have positive identifiers are obviously
+ // appropriated can to work not truly. In this case it is required to improve the given method
+ // and to add preliminary sorting a submenu on depth of an enclosure.
for ( QMap<int, bool>::const_iterator it = myUpdateIds.constBegin(); it != myUpdateIds.constEnd(); ++it )
{
- MenuNode* node = find( it.key(), 0 );
+ MenuNode* node = it.key() == -1 ? &myRoot : find( it.key() );
if ( node )
updateMenu( node, it.data() );
}
\param r - menu reader
\param mgr - menu manager
*/
-QtxActionMenuMgr::MenuCreator::MenuCreator( QtxActionMgr::Reader* r,
- QtxActionMenuMgr* mgr )
+QtxActionMenuMgr::MenuCreator::MenuCreator( QtxActionMgr::Reader* r, QtxActionMenuMgr* mgr )
: QtxActionMgr::Creator( r ),
- myMgr( mgr )
+myMgr( mgr )
{
}