#include "QtxResourceMgr.h"
-#include <qpopupmenu.h>
+#include <QMenu>
+#include <QIcon>
/*!
Name: QtxMRUAction [public]
Desc: Constructs an MRU action with given parent and name.
*/
-QtxMRUAction::QtxMRUAction( QObject* parent, const char* name )
-: QtxAction( "Most Recently Used", "Most Recently Used", 0, parent, name ),
+QtxMRUAction::QtxMRUAction( QObject* parent )
+: QtxAction( "Most Recently Used", "Most Recently Used", 0, parent ),
myVisCount( 5 ),
-myPopupMode( SubMenu ),
myInsertMode( MoveFirst )
{
+ setMenu( new QMenu( 0 ) );
+ connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
}
/*!
named specified name.
*/
-QtxMRUAction::QtxMRUAction( const QString& text, const QString& menuText, QObject* parent, const char* name )
-: QtxAction( text, menuText, 0, parent, name ),
+QtxMRUAction::QtxMRUAction( const QString& text, const QString& menuText, QObject* parent )
+: QtxAction( text, menuText, 0, parent ),
myVisCount( 5 ),
-myPopupMode( SubMenu ),
myInsertMode( MoveFirst )
{
+ setMenu( new QMenu( 0 ) );
+ connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
}
/*!
accelerator. It is a child of given parent and named specified name.
*/
-QtxMRUAction::QtxMRUAction( const QString& text, const QIconSet& icon, const QString& menuText, QObject* parent, const char* name )
-: QtxAction( text, icon, menuText, 0, parent, name ),
+QtxMRUAction::QtxMRUAction( const QString& text, const QIcon& icon,
+ const QString& menuText, QObject* parent )
+: QtxAction( text, icon, menuText, 0, parent ),
myVisCount( 5 ),
-myPopupMode( SubMenu ),
myInsertMode( MoveFirst )
{
+ setMenu( new QMenu( 0 ) );
+ connect( menu(), SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
}
/*!
QtxMRUAction::~QtxMRUAction()
{
- for ( ItemsMap::ConstIterator iIt = myItems.begin(); iIt != myItems.end(); ++iIt )
- removeFrom( iIt.key() );
-
- for ( MenusMap::ConstIterator mIt = myMenus.begin(); mIt != myMenus.end(); ++mIt )
- removeFrom( mIt.key() );
+ delete menu();
}
/*!
myInsertMode = mode;
}
-/*!
- Name: popupMode [public]
- Desc: Returns the popup mode.
-*/
-
-int QtxMRUAction::popupMode() const
-{
- return myPopupMode;
-}
-
-/*!
- Name: setPopupMode [public]
- Desc: Set the popup mode. If this mode is 'Items' then method "addTo" creates the
- items in the specified popup menu. If mode is 'SubMenu' then items will be
- create in sub popup menu which will be placed in specified popup.
-*/
-
-void QtxMRUAction::setPopupMode( const int mode )
-{
- myPopupMode = mode;
-}
-
/*!
Name: count [public]
Desc: Returns the number of links.
return;
myVisCount = num;
-
- updateState();
}
/*!
if ( myLinks.contains( link ) && ( insertMode() == AddFirst || insertMode() == AddLast ) )
return;
- myLinks.remove( link );
+ myLinks.removeAll( link );
switch ( insertMode() )
{
myLinks.append( link );
break;
}
-
- updateState();
}
/*!
if ( idx < 0 || idx >= (int)myLinks.count() )
return;
- myLinks.remove( myLinks.at( idx ) );
-
- updateState();
+ myLinks.removeAt( idx );
}
/*!
void QtxMRUAction::remove( const QString& link )
{
- if ( myLinks.remove( link ) )
- updateState();
+ myLinks.removeAll( link );
}
/*!
int QtxMRUAction::find( const QString& link ) const
{
- return myLinks.findIndex( link );
+ return myLinks.indexOf( link );
}
/*!
return myLinks.contains( link );
}
-/*!
- Name: addTo [public]
- Desc: Add the MRU links to the end of specified popup according to the popup mode.
-*/
-
-bool QtxMRUAction::addTo( QWidget* wid )
-{
- if ( !wid || !wid->inherits( "QPopupMenu" ) )
- return false;
-
- QPopupMenu* pm = (QPopupMenu*)wid;
- checkPopup( pm );
-
- int mode = popupMode();
-
- if ( ( mode == Items && myItems.contains( pm ) ) ||
- ( mode == SubMenu && myMenus.contains( pm ) ) )
- return false;
-
- bool exist = myItems.contains( pm ) || myMenus.contains( pm );
-
- if ( mode == SubMenu && !QtxAction::addTo( wid ) )
- return false;
-
- if ( mode == Items )
- {
- myItems.insert( pm, Item() );
- myItems[pm].pId = myItems[pm].nId -1;
- connect( pm, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
- }
- else if ( mode == SubMenu )
- {
- myMenus.insert( pm, new QPopupMenu( pm ) );
- setPopup( pm, pm->idAt( pm->count() - 1 ), myMenus[pm] );
- connect( myMenus[pm], SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
- }
-
- if ( !exist )
- {
- connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
- connect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
- }
-
- return insertLinks( pm, mode );
-}
-
-/*!
- Name: addTo [public]
- Desc: Add the MRU links to the specified popup at given index according to the popup mode.
-*/
-
-bool QtxMRUAction::addTo( QWidget* wid, const int idx )
-{
- if ( !QtxAction::addTo( wid, idx ) )
- return false;
-
- QPopupMenu* pm = (QPopupMenu*)wid;
-
- removeLinks( pm, popupMode() );
- insertLinks( pm, popupMode(), idx );
-
- return true;
-}
-
-/*!
- Name: removeFrom [public]
- Desc: Removes all MRU links from specified popup.
-*/
-
-bool QtxMRUAction::removeFrom( QWidget* wid )
-{
- QtxAction::removeFrom( wid );
-
- QPopupMenu* pm = (QPopupMenu*)wid;
- if ( !wid || !wid->inherits( "QPopupMenu" ) )
- return false;
-
- if ( myItems.contains( pm ) )
- {
- removeLinks( pm, Items );
- myItems.remove( pm );
- disconnect( pm, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
- }
- if ( myMenus.contains( pm ) )
- {
- removeLinks( pm, SubMenu );
- delete myMenus[pm];
- myMenus.remove( pm );
- }
-
- disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
- disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
-
- return true;
-}
-
/*!
Name: loadLinks [public]
Desc: Load the MRU links from specified resource manager section.
myLinks.append( link );
map.insert( link, 0 );
}
-
- updateState();
}
/*!
QStringList lst;
QMap<QString, int> map;
for ( QStringList::const_iterator itr = myLinks.begin(); itr != myLinks.end(); ++itr )
- map.insert( *lst.append( *itr ), 0 );
+ {
+ lst.append( *itr );
+ map.insert( *itr, 0 );
+ }
QString itemPrefix( "item_" );
QStringList items = resMgr->parameters( section );
QString link = resMgr->stringValue( section, *it, QString::null );
if ( !link.isEmpty() && !map.contains( link ) )
- map.insert( *lst.append( link ), 0 );
+ {
+ lst.append( link );
+ map.insert( link, 0 );
+ }
resMgr->remove( section, *it );
}
resMgr->setValue( section, itemPrefix + QString().sprintf( "%03d", counter ), *iter );
}
-/*!
- Name: setEnabled [public slot]
- Desc: Enable or disable all popup items with MRU links.
-*/
-
-void QtxMRUAction::setEnabled( bool on )
-{
- QtxAction::setEnabled( on );
-
- for ( ItemsMap::ConstIterator iter = myItems.begin(); iter != myItems.end(); ++iter )
- for ( QIntList::const_iterator it = iter.data().idList.begin(); it != iter.data().idList.end(); ++it )
- iter.key()->setItemEnabled( *it, on );
-}
-
/*!
Name: onAboutToShow [private slots]
Desc: Enable or disable sub menu item according to number of MRU links
void QtxMRUAction::onAboutToShow()
{
- const QObject* obj = sender();
- if ( obj && obj->inherits( "QPopupMenu" ) )
- {
- QPopupMenu* pm = (QPopupMenu*)obj;
- if ( myMenus.contains( pm ) )
- pm->setItemEnabled( findId( pm, myMenus[pm]), isEnabled() && myMenus[pm] && myMenus[pm]->count() );
- }
+ updateMenu();
}
/*!
Desc: Process popup item activation and emit signal activated with selected MRU link.
*/
-void QtxMRUAction::onActivated( int id )
+void QtxMRUAction::onActivated()
{
- const QObject* obj = sender();
- if ( !obj->inherits( "QPopupMenu" ) )
+ QAction* a = ::qobject_cast<QAction*>( sender() );
+ if ( !a )
return;
- QPopupMenu* pm = (QPopupMenu*)obj;
-
- QString link;
- if ( ( myItems.contains( pm ) && myItems[pm].idList.contains( id ) ) ||
- ( myMenus.contains( (QPopupMenu*)pm->parent() ) && myMenus[(QPopupMenu*)pm->parent()] == pm ) )
- link = pm->text( id );
-
- if ( !link.isEmpty() )
+ QString link = a->text();
+ if ( !link.isEmpty() && myLinks.contains( link ) )
emit activated( link );
}
/*!
- Name: onDestroyed [private slot]
- Desc: Removes deleted popup menu from internal data structures.
-*/
-
-void QtxMRUAction::onDestroyed( QObject* obj )
-{
- if ( !obj )
- return;
-
- myItems.remove( (QPopupMenu*)obj );
- myMenus.remove( (QPopupMenu*)obj );
-}
-
-/*!
- Name: updateState [private]
- Desc: Updates the state of all popup menus which contains MRU link items.
+ Name: updateMenu [private]
+ Desc: Updates the popup menu which contains MRU link items.
*/
-void QtxMRUAction::updateState()
-{
- for ( ItemsMap::ConstIterator iIt = myItems.begin(); iIt != myItems.end(); ++iIt )
- updatePopup( iIt.key(), Items );
-
- for ( MenusMap::ConstIterator mIt = myMenus.begin(); mIt != myMenus.end(); ++mIt )
- updatePopup( mIt.key(), SubMenu );
-}
-
-/*!
- Name: checkPopup [private]
- Desc: Check consistency the popup content and internal datas.
- Synchronize internal data structures with popup content.
-*/
-
-void QtxMRUAction::checkPopup( QPopupMenu* pm )
-{
- if ( myItems.contains( pm ) )
- {
- bool found = true;
- for ( QIntList::const_iterator it = myItems[pm].idList.begin(); it != myItems[pm].idList.end() && found; ++it )
- found = pm->indexOf( *it ) != -1;
- if ( !found )
- {
- removeLinks( pm, Items );
- myItems.remove( pm );
- disconnect( pm, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
- }
- }
- if ( myMenus.contains( pm ) )
- {
- int id = findId( pm, myMenus[pm] );
- if ( id == -1 )
- {
- delete myMenus[pm];
- myMenus.remove( pm );
- }
- }
-
- if ( !myItems.contains( pm ) && !myMenus.contains( pm ) )
- disconnect( pm, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
-}
-
-/*!
- Name: updatePopup [private]
- Desc: Updates the MRU link items state in the specified popup menu.
-*/
-
-void QtxMRUAction::updatePopup( QPopupMenu* pm, const int mode )
+void QtxMRUAction::updateMenu()
{
+ QMenu* pm = menu();
if ( !pm )
return;
- int idx = -1;
- if ( mode == Items && myItems.contains( pm ) )
- {
- if ( !myItems[pm].idList.isEmpty() )
- idx = pm->indexOf( myItems[pm].idList.first() );
- else
- {
- int pIdx = pm->indexOf( myItems[pm].pId );
- int nIdx = pm->indexOf( myItems[pm].nId );
- if ( pIdx != -1 )
- idx = pIdx + 1;
- else if ( nIdx != -1 )
- idx = nIdx - 1;
- }
- }
-
- removeLinks( pm, mode );
- insertLinks( pm, mode, idx );
-}
-
-/*!
- Name: removeLinks [private]
- Desc: Removes MRU link items from specified popup.
-*/
-
-bool QtxMRUAction::removeLinks( QPopupMenu* pm, const int mode )
-{
- if ( !pm )
- return false;
-
- if ( mode == SubMenu && myMenus.contains( pm ) )
- myMenus[pm]->clear();
- else if ( mode == Items && myItems.contains( pm ) )
- {
- for ( QIntList::const_iterator it = myItems[pm].idList.begin(); it != myItems[pm].idList.end(); ++it )
- pm->removeItem( *it );
- myItems[pm].idList.clear();
- }
-
- return true;
-}
-
-/*!
- Name: insertLinks [private]
- Desc: Inserts MRU link items to the specified popup.
-*/
-
-bool QtxMRUAction::insertLinks( QPopupMenu* pm, const int mode, const int idx )
-{
- if ( !pm )
- return false;
+ pm->clear();
int count = visibleCount() < 0 ? myLinks.count() : visibleCount();
- bool isOn = isEnabled();
- if ( mode == SubMenu && myMenus.contains( pm ) )
- {
- for ( QStringList::const_iterator it = myLinks.begin(); it != myLinks.end() && count > 0; ++it, count-- )
- {
- int id = myMenus[pm]->insertItem( *it, -1 );
- myMenus[pm]->setItemEnabled( id, isOn );
- }
- }
- else if ( mode == Items )
- {
- QIntList ids;
- int index = idx;
- for ( QStringList::const_iterator it = myLinks.begin(); it != myLinks.end() && count > 0; ++it, count-- )
- {
- ids.append( pm->insertItem( *it, -1, index ) );
- pm->setItemEnabled( ids.last(), isOn );
- if ( index >= 0 )
- index++;
- }
- myItems[pm].idList = ids;
- if ( !myItems[pm].idList.isEmpty() )
- {
- myItems[pm].pId = pm->idAt( pm->indexOf( myItems[pm].idList.first() ) - 1 );
- myItems[pm].nId = pm->idAt( pm->indexOf( myItems[pm].idList.first() ) + 1 );
- }
- }
- return true;
-}
-
-/*!
- Name: findId [private]
- Desc: Returns identificator of popup item which contains sub popup 'pm' in the popup 'cont'.
-*/
-
-int QtxMRUAction::findId( QPopupMenu* cont, QPopupMenu* pm ) const
-{
- if ( !cont || !pm )
- return -1;
-
- int id = -1;
-
- for ( int i = 0; i < (int)cont->count() && id == -1; i++ )
- {
- QMenuData* md = 0;
- QMenuItem* item = cont->findItem( cont->idAt( i ), &md );
- if ( item && md == cont && item->popup() == pm )
- id = item->id();
- }
- return id;
+ for ( QStringList::const_iterator it = myLinks.begin(); it != myLinks.end() && count > 0; ++it, count-- )
+ pm->addAction( *it, this, SLOT( onActivated() ) );
}
// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
+//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
+// License as published by the Free Software Foundation; either
// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//
+// This library is distributed in the hope that it will be useful
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#include "QtxAction.h"
-#include <qmap.h>
-#include <qstringlist.h>
+#include <QMap>
+#include <QStringList>
-class QPopupMenu;
class QtxResourceMgr;
#ifdef WIN32
Q_PROPERTY( int visibleCount READ visibleCount WRITE setVisibleCount )
public:
- enum { Items, SubMenu };
enum { MoveFirst, MoveLast, AddFirst, AddLast };
public:
- QtxMRUAction( QObject* = 0, const char* = 0 );
- QtxMRUAction( const QString&, const QString&, QObject*, const char* = 0 );
- QtxMRUAction( const QString&, const QIconSet&, const QString&, QObject*, const char* = 0 );
+ QtxMRUAction( QObject* = 0 );
+ QtxMRUAction( const QString&, const QString&, QObject* = 0 );
+ QtxMRUAction( const QString&, const QIcon&, const QString&, QObject* = 0 );
virtual ~QtxMRUAction();
int insertMode() const;
void setInsertMode( const int );
- int popupMode() const;
- void setPopupMode( const int );
-
int count() const;
bool isEmpty() const;
int find( const QString& ) const;
bool contains( const QString& ) const;
- virtual bool addTo( QWidget* );
- virtual bool addTo( QWidget*, const int );
-
- virtual bool removeFrom( QWidget* );
-
virtual void loadLinks( QtxResourceMgr*, const QString&, const bool = true );
virtual void saveLinks( QtxResourceMgr*, const QString&, const bool = true ) const;
-signals:
- void activated( QString );
-
-public slots:
- virtual void setEnabled( bool );
+Q_SIGNALS:
+ void activated( const QString& );
private slots:
+ void onActivated();
void onAboutToShow();
- void onActivated( int );
- void onDestroyed( QObject* );
-
-private:
- void updateState();
- void checkPopup( QPopupMenu* );
- void updatePopup( QPopupMenu*, const int );
- bool removeLinks( QPopupMenu*, const int );
- bool insertLinks( QPopupMenu*, const int, const int = -1 );
-
- int findId( QPopupMenu*, QPopupMenu* ) const;
private:
- typedef struct { int pId, nId; QIntList idList; } Item;
- typedef QMap<QPopupMenu*, Item> ItemsMap;
- typedef QMap<QPopupMenu*, QPopupMenu*> MenusMap;
+ void updateMenu();
private:
QStringList myLinks;
- ItemsMap myItems;
- MenusMap myMenus;
int myVisCount;
- int myPopupMode;
int myInsertMode;
};