]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxActionMenuMgr.h
Salome HOME
252ef42abdd370016d6283a7ebbf82bfe038ba35
[modules/gui.git] / src / Qtx / QtxActionMenuMgr.h
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 // File:      QtxActionMenuMgr.h
20 // Author:    Alexander SOLOVYEV, Sergey TELKOV
21
22 #ifndef QTXACTIONMENUMGR_H
23 #define QTXACTIONMENUMGR_H
24
25 #include "Qtx.h"
26 #include "QtxActionMgr.h"
27
28 #include <qptrlist.h>
29 #include <qstringlist.h>
30
31 class QPopupMenu;
32 class QMainWindow;
33
34 #ifdef WIN32
35 #pragma warning( disable:4251 )
36 #endif
37
38 /*!
39   \class QtxActionMenuMgr
40   Allows to use set of action to automatically build main menu.
41   With help of methods insert/append/remove it is possible to 
42   describe whole structure of menu. Method hide allows
43   to temporary remove some items from menu, method show allows to 
44   recreate them.
45   Actions can be grouped with help of group identifictor.
46   Inside popup or menu bar items have order by increasing group id.
47   This manager is able to attune menu: to remove excess separators,
48   to remove empty popup menu etc.
49 */
50 class QTX_EXPORT QtxActionMenuMgr : public QtxActionMgr
51 {
52   Q_OBJECT
53
54   class MenuNode;
55
56   typedef QPtrList<MenuNode>         NodeList;
57   typedef QPtrListIterator<MenuNode> NodeListIterator;
58
59   /*!
60     \class MenuNode
61     Represents a menu item inside main menu structure.
62     For internal purposes only
63   */
64   class MenuNode
65   {
66   public:
67     MenuNode() : parent( 0 ), visible( true ) { children.setAutoDelete( true ); };
68     MenuNode( MenuNode* p ) : parent( p ), visible( true ) { children.setAutoDelete( true ); };
69
70     int       id;
71     int       idx;
72     int       group;
73     MenuNode* parent;
74     bool      visible;
75     NodeList  children;
76   };
77
78   class MenuAction;
79
80 protected:
81   class MenuCreator;
82
83 public:
84   QtxActionMenuMgr( QMainWindow* );
85   QtxActionMenuMgr( QWidget*, QObject* );
86   virtual ~QtxActionMenuMgr();
87
88   virtual bool isVisible( const int, const int ) const;
89   virtual void setVisible( const int, const int, const bool );
90
91   int          insert( const int, const QString&, const int, const int = -1 );
92   int          insert( QAction*, const QString&, const int, const int = -1 );
93
94   int          insert( const int, const QStringList&, const int, const int = -1 );
95   int          insert( QAction*, const QStringList&, const int, const int = -1 );
96
97   virtual int  insert( const int, const int, const int, const int = -1 );
98   int          insert( QAction*, const int, const int, const int = -1 );
99
100   int          insert( const QString&, const QString&, const int, const int = -1, const int = -1, const bool = false );
101   int          insert( const QString&, const QStringList&, const int, const int = -1, const int = -1, const bool = false );
102   virtual int  insert( const QString&, const int, const int, const int = -1, const int = -1, const bool = false );
103
104   int          append( const int, const int, const int );
105   int          append( QAction*, const int, const int );
106   int          append( const QString&, const int, const int, const int = -1, const bool = false );
107
108   int          prepend( const int, const int, const int );
109   int          prepend( QAction*, const int, const int );
110   int          prepend( const QString&, const int, const int, const int = -1, const bool = false );
111
112   void         remove( const int );
113   void         remove( const int, const int, const int = -1 );
114
115   void         show( const int );
116   void         hide( const int );
117
118   bool         isShown( const int ) const;
119   void         setShown( const int, const bool );
120
121   virtual bool load( const QString&, QtxActionMgr::Reader& );
122
123   bool         containsMenu( const QString&, const int ) const;
124   bool         containsMenu( const int, const int ) const;
125
126
127 private slots:
128   void         onDestroyed( QObject* );
129   void         onHighlighted( int );
130
131 signals:
132   void         menuHighlighted( int, int );
133
134 protected:
135   void         setWidget( QWidget* );
136   MenuNode*    find( const int, const int, const bool = true ) const;
137   MenuNode*    find( const int, MenuNode* = 0, const bool = true ) const;
138   bool         find( const int, NodeList&, MenuNode* = 0 ) const;
139   MenuNode*    find( const QString&, const int, const bool = true ) const;
140   MenuNode*    find( const QString&, MenuNode* = 0, const bool = true ) const;
141   bool         find( const QString&, NodeList&, MenuNode* = 0 ) const;
142   int          findId( const int, const int = -1 ) const;
143
144   void         removeMenu( const int, MenuNode* );
145
146   QAction*     itemAction( const int ) const;
147   MenuAction*  menuAction( const int ) const;
148
149   void         updateMenu( MenuNode* = 0, const bool = true, const bool = true );
150   virtual void internalUpdate();  
151
152 private:
153   bool         checkWidget( QWidget* ) const;
154   QWidget*     menuWidget( MenuNode* ) const;
155   void         simplifySeparators( QWidget* );
156   QString      clearTitle( const QString& ) const;
157   int          createMenu( const QStringList&, const int );
158
159 private:
160   typedef QMap<int, MenuAction*> MenuMap;
161
162 private:
163   MenuNode     myRoot;
164   QWidget*     myMenu;
165   MenuMap      myMenus;
166 };
167
168 /*!
169   \class QtxActionMenuMgr::MenuCreator
170   Allows to create automatically main menu by data read from file
171 */
172 class QtxActionMenuMgr::MenuCreator : public QtxActionMgr::Creator
173 {
174 public:
175   MenuCreator( QtxActionMgr::Reader*, QtxActionMenuMgr* );
176   virtual ~MenuCreator();
177
178   virtual int append( const QString&, const bool,
179                       const ItemAttributes&, const int );
180
181 private:
182   QtxActionMenuMgr* myMgr;
183 };
184
185
186 #endif