From 3f2f745742afea653055bdc732f39cd0133667ca Mon Sep 17 00:00:00 2001 From: san Date: Tue, 2 Aug 2011 11:34:57 +0000 Subject: [PATCH] Highlight mode for collapsed menu was added. --- src/Qtx/QtxActionMenuMgr.cxx | 56 ++++++++++++++++++++++++------------ src/Qtx/QtxActionMenuMgr.h | 3 ++ src/Qtx/QtxMenu.cxx | 38 +++++++++++++++++++----- src/Qtx/QtxMenu.h | 10 +++++++ 4 files changed, 82 insertions(+), 25 deletions(-) diff --git a/src/Qtx/QtxActionMenuMgr.cxx b/src/Qtx/QtxActionMenuMgr.cxx index 0a3945632..180f72daa 100644 --- a/src/Qtx/QtxActionMenuMgr.cxx +++ b/src/Qtx/QtxActionMenuMgr.cxx @@ -40,10 +40,10 @@ public: MenuNode(); MenuNode( MenuNode*, const int, const int, const int ); ~MenuNode(); - + MenuNode* parent; //!< parent menu node int id; //!< menu nodeID - int idx; //!< menu node index + int idx; //!< menu node index int group; //!< menu group ID bool visible; //!< visibility status int emptyEnabled; //!< enable empty menu flag @@ -109,7 +109,7 @@ QtxActionMenuMgr::MenuNode::~MenuNode() \param p parent main window */ QtxActionMenuMgr::QtxActionMenuMgr( QMainWindow* p ) -: QtxActionMgr( p ), +: QtxActionMgr( p ), myRoot( new MenuNode() ), myMenu( p ? p->menuBar() : 0 ), myCollapse( false ) @@ -125,7 +125,7 @@ QtxActionMenuMgr::QtxActionMenuMgr( QMainWindow* p ) \param p parent object */ QtxActionMenuMgr::QtxActionMenuMgr( QWidget* mw, QObject* p ) -: QtxActionMgr( p ), +: QtxActionMgr( p ), myRoot( new MenuNode() ), myMenu( mw ), myCollapse( false ) @@ -181,7 +181,7 @@ void QtxActionMenuMgr::setVisible( const int actId, const int place, const bool /*! \brief Insert action to the menu. - Insert an action to the named menu. The \a menus parameter represents + Insert an action to the named menu. The \a menus parameter represents the menu name: it can be a sequence of strings, separated by '|' symbol. For example, "File|Edit" means \c File->Edit submenu. If submenu doesn't exist, it will be created. @@ -200,7 +200,7 @@ int QtxActionMenuMgr::insert( const int id, const QString& menus, const int grou /*! \brief Insert action to the menu. - Insert an action to the named menu. The \a menus parameter represents + Insert an action to the named menu. The \a menus parameter represents the menu name: it can be a sequence of strings, separated by '|' symbol. For example, "File|Edit" means \c File->Edit submenu. If submenu doesn't exist, it will be created. @@ -219,7 +219,7 @@ int QtxActionMenuMgr::insert( QAction* a, const QString& menus, const int group, /*! \brief Insert action to the menu. - Insert an action to the named menu. The \a menus parameter represents + Insert an action to the named menu. The \a menus parameter represents the menu names list. For example, string list consisting from two items "File" and "Edit" means \c File->Edit submenu. @@ -243,7 +243,7 @@ int QtxActionMenuMgr::insert( const int id, const QStringList& menus, const int /*! \brief Insert action to the menu. - Insert an action to the named menu. The \a menus parameter represents + Insert an action to the named menu. The \a menus parameter represents the menu names list. For example, string list consisting from two items "File" and "Edit" means \c File->Edit submenu. @@ -349,7 +349,7 @@ int QtxActionMenuMgr::insert( const QString& title, const int pId, const int gro /*! \brief Create and insert menu item action to the menu. - Insert an action to the named menu. The \a menus parameter represents + Insert an action to the named menu. The \a menus parameter represents the menu name: it can be a sequence of strings, separated by '|' symbol. For example, "File|Edit" means \c File->Edit submenu. If submenu doesn't exist, it will be created. @@ -369,7 +369,7 @@ int QtxActionMenuMgr::insert( const QString& title, const QString& menus, const /*! \brief Create and insert menu item action to the menu. - Insert an action to the named menu. The \a menus parameter represents + Insert an action to the named menu. The \a menus parameter represents the menu names list. For example, string list consisting from two items "File" and "Edit" means \c File->Edit submenu. @@ -567,7 +567,7 @@ void QtxActionMenuMgr::change( const int id, const QString& title ) /*! \brief Called when the submenu is about to show. - + Emits the signal menuAboutToShow(QMenu*). */ void QtxActionMenuMgr::onAboutToShow() @@ -579,7 +579,7 @@ void QtxActionMenuMgr::onAboutToShow() /*! \brief Called when the submenu is about to hide. - + Emits the signal menuAboutToHide(QMenu*). */ void QtxActionMenuMgr::onAboutToHide() @@ -593,7 +593,7 @@ void QtxActionMenuMgr::onAboutToHide() \brief Called when the corresponding menu object is destroyed. Clears internal pointer to menu to disable crashes. - + \param obj (menu) object being destroyed */ void QtxActionMenuMgr::onDestroyed( QObject* obj ) @@ -949,7 +949,7 @@ void QtxActionMenuMgr::updateMenu( MenuNode* startNode, const bool rec, const bo foreach( a, formapit.value() ) mw->insertAction( preva, a ); } - + // remove extra separators simplifySeparators( mw ); @@ -960,7 +960,7 @@ void QtxActionMenuMgr::updateMenu( MenuNode* startNode, const bool rec, const bo /*! \brief Internal update. - + Customizes the menu update processing. */ void QtxActionMenuMgr::internalUpdate() @@ -1146,7 +1146,7 @@ bool QtxActionMenuMgr::isEmptyEnabled( const int id ) const MenuNode* node = find( id ); if ( node && menuAction( id ) ) return node->emptyEnabled > 0; - + return false; } @@ -1194,6 +1194,26 @@ void QtxActionMenuMgr::setMenuCollapsible( bool enable ) } } +/*! + \brief Returns the priority for specified menu. + \param id - menu id. +*/ +int QtxActionMenuMgr::menuPriority( const int id ) const +{ + return QtxMenu::actionPriority( menuAction( id ) ); +} + +/*! + \brief Sets the priority for specified menu. Menu with negative value of priority + will be always displayed in menu (permanent menus). + \param id - menu id. + \param p - priority value. +*/ +void QtxActionMenuMgr::setMenuPriority( const int id, const int p ) +{ + return QtxMenu::setActionPriority( menuAction( id ), p ); +} + /*! \brief Perform delayed menu update. \param id menu item ID @@ -1218,11 +1238,11 @@ void QtxActionMenuMgr::updateContent() { // Warning: For correct updating it is necessary to update the most enclosed submenu in first turn // because not updated empty submenu will be skipped. Now the submenus are iterated in - // ascending order according to their identifiers. For a submenus with automatically generated + // ascending order according to their identifiers. For a submenus with automatically generated // identifiers this will work correctly since the uppermost submenus have the biggest number // (identifiers are generated by decrementing 1 starting from -1). In general, if any submenu // have positive identifiers this method might not work correctly. In this case it would be - // necessary to improve this method and to add preliminary sorting a submenus by depth of an + // necessary to improve this method and to add preliminary sorting a submenus by depth of an // enclosure. for ( QMap::const_iterator it = myUpdateIds.constBegin(); it != myUpdateIds.constEnd(); ++it ) { diff --git a/src/Qtx/QtxActionMenuMgr.h b/src/Qtx/QtxActionMenuMgr.h index ff7c47c7d..0e2c250fb 100644 --- a/src/Qtx/QtxActionMenuMgr.h +++ b/src/Qtx/QtxActionMenuMgr.h @@ -102,6 +102,9 @@ public: bool menuCollapsible() const; void setMenuCollapsible( bool ); + int menuPriority( const int ) const; + void setMenuPriority( const int, const int ); + private slots: void onAboutToShow(); void onAboutToHide(); diff --git a/src/Qtx/QtxMenu.cxx b/src/Qtx/QtxMenu.cxx index 5ed9b5676..6275db207 100644 --- a/src/Qtx/QtxMenu.cxx +++ b/src/Qtx/QtxMenu.cxx @@ -697,7 +697,7 @@ QWidget* QtxMenu::Expander::createWidget( QWidget* parent ) pix.setMask( bm ); } - + if ( !pix.mask().isNull() ) setMask( pix.mask() ); */ @@ -750,8 +750,9 @@ QtxMenu::QtxMenu( QWidget* parent ) : QMenu( parent ), myTitleMode( TitleOff ), myTitleAlign( Qt::AlignVCenter | Qt::AlignLeft ), - myLimit( 7 ), - myLimitMode( LimitAuto ), + myLimit( 3 ), + myLimitMode( LimitFrequent ), + myHighlightMode( HighlightFrequent ), myExpandAction( 0 ) { myTitleAction = new TitleMgr( this ); @@ -913,6 +914,25 @@ void QtxMenu::setCollapseLimitMode( const QtxMenu::CollapseLimitMode mode ) myLimitMode = mode; } +/*! + \brief Returns highlight mode. +*/ +QtxMenu::HighlightMode QtxMenu::highlightMode() const +{ + return myHighlightMode; +} + +/*! + \brief Sets highlight items mode. If mode is 'HighlightPermanent' then permanently visible + menu items will be highlighted. If mode is 'HighlightFrequent' then menu will highlight + frequently used menu items only. If mode is 'HighlightNone' then menu doesn't highlight any items. + \param mode - setted highlight mode. +*/ +void QtxMenu::setHighlightMode( HighlightMode mode ) +{ + myHighlightMode = mode; +} + /*! \brief Returns 'true' if the menu in expanded (full) state. */ @@ -941,7 +961,7 @@ int QtxMenu::actionPriority( QAction* a ) if ( _actionPriority.contains( a ) ) p = _actionPriority[a]; - if ( a->menu() ) + if ( p >= 0 && a->menu() ) { QList lst = a->menu()->actions(); for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) @@ -1016,13 +1036,14 @@ void QtxMenu::paintEvent( QPaintEvent* e ) if ( menuCollapsible() ) { QPainter::restoreRedirected( this ); - if ( isTopLevelMenu() ) { + HighlightMode hm = highlightMode(); + if ( isTopLevelMenu() && hm != HighlightNone ) { QRgb bg = palette().color( QPalette::Light ).rgb(); QImage img = pix.toImage(); QList lst = actions(); QSet visible = collapsedActions(); - + for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) { QAction* a = *it; QRect r = actionGeometry( a ); @@ -1030,7 +1051,10 @@ void QtxMenu::paintEvent( QPaintEvent* e ) int x, y, w, h; r.getRect( &x, &y, &w, &h ); - if ( a == myExpandAction || a == myTitleAction || !visible.contains( a ) || a == activeAction() ) + if ( a == myExpandAction || a == myTitleAction || a == activeAction() ) + continue; + + if ( ( hm == HighlightPermanent ) != visible.contains( a ) ) continue; QRgb rc = img.pixel( x, y ); diff --git a/src/Qtx/QtxMenu.h b/src/Qtx/QtxMenu.h index b81a6eb01..1187b565f 100644 --- a/src/Qtx/QtxMenu.h +++ b/src/Qtx/QtxMenu.h @@ -59,6 +59,12 @@ public: LimitTotal } CollapseLimitMode; + typedef enum { + HighlightNone, + HighlightFrequent, + HighlightPermanent + } HighlightMode; + public: QtxMenu( QWidget* = 0 ); virtual ~QtxMenu(); @@ -85,6 +91,9 @@ public: CollapseLimitMode collapseLimitMode() const; void setCollapseLimitMode( const CollapseLimitMode ); + HighlightMode highlightMode() const; + void setHighlightMode( HighlightMode ); + static int actionPriority( QAction* ); static void setActionPriority( QAction*, int ); @@ -129,6 +138,7 @@ private: int myLimit; CollapseLimitMode myLimitMode; + HighlightMode myHighlightMode; QTimer* myShortTimer; QTimer* myExpandTimer; -- 2.39.2