Salome HOME
ea84464306e067fdbb20c1d566b5c47227bf3531
[modules/gui.git] / src / Qtx / QtxAction.cxx
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:      QtxAction.cxx
20 // Author:    Sergey TELKOV
21
22 #include "QtxAction.h"
23
24 #include <qpopupmenu.h>
25 #include <qmenubar.h>
26
27 /*!
28         Name: QtxAction [public]
29         Desc: Constructs an action with given parent and name. If toggle is true the
30                   action will be a toggle action, otherwise it will be a command action.
31 */
32
33 QtxAction::QtxAction( QObject* parent, const char* name, bool toggle )
34   : QAction( parent, name, toggle )
35 {
36 }
37
38 /*!
39         Name: QtxAction [public]
40         Desc: This constructor creates an action with the following properties: the
41                   description text, the icon or iconset icon, the menu text and keyboard
42                   accelerator. It is a child of given parent and named specified name.
43                   If toggle is true the action will be a toggle action, otherwise it will
44                   be a command action.
45 */
46
47 QtxAction::QtxAction( const QString& text, const QIconSet& icon,
48                       const QString& menuText, int accel,
49                       QObject* parent, const char* name, bool toggle )
50   : QAction( text, icon, menuText, accel, parent, name, toggle )
51 {
52 }
53
54 /*!
55         Name: QtxAction [public]
56         Desc: This constructor creates an action with the following properties: the
57                   description text, the menu text and keyboard accelerator. It is a child
58                   of given parent and named specified name. If toggle is true the action
59                   will be a toggle action, otherwise it will be a command action.
60 */
61
62 QtxAction::QtxAction( const QString& text, const QString& menuText, int accel,
63                       QObject* parent, const char* name, bool toggle )
64   : QAction( text, menuText, accel, parent, name, toggle )
65 {
66 }
67
68 /*!
69         Name: ~QtxAction [virtual public]
70         Desc: Destructor.
71 */
72
73 QtxAction::~QtxAction()
74 {
75 }
76
77 /*!
78         Name: addTo [virtual public]
79         Desc: Adds this action to widget. Returns true if the action was added
80                   successfully and false otherwise.
81 */
82
83 bool QtxAction::addTo( QWidget* w )
84 {
85   if ( w->inherits( "QMenuBar" ) ) {
86     // --- Add action to the QMenuBar ---
87     // n.b. currently for the actions inserted to the menu bar 
88     // the following properties are not supported:
89     // * tooltips
90     // * what's this info
91     // * toggle mode
92     QMenuBar* mb = (QMenuBar*)w;
93     if ( myMenuIds.find( w ) != myMenuIds.end() )
94       return false;                        // already added
95     if ( name() == "qt_separator_action" ) // separator
96       myMenuIds[ w ] = mb->insertSeparator();
97     else if ( iconSet().isNull() )         // has no icon
98       myMenuIds[ w ] = mb->insertItem( menuText(), this, SIGNAL( activated() ), accel() );
99     else                                   // has icon
100       myMenuIds[ w ] = mb->insertItem( iconSet(), menuText(), this, SIGNAL( activated() ), accel() );
101     mb->setItemEnabled( myMenuIds[ w ], isEnabled() );
102     mb->setItemVisible( myMenuIds[ w ], isVisible() );
103     return true;
104   }
105   return QAction::addTo( w );
106 }
107
108 /*!
109         Name: addTo [virtual public]
110         Desc: Adds this action to widget. If widget is QPopupMenu given index will
111                   be used for menu item inserting. Returns true if the action was added
112                   successfully and false otherwise.
113 */
114
115 bool QtxAction::addTo( QWidget* w, int index )
116 {
117   if ( !addTo( w ) )
118     return false;
119
120   if ( w->inherits( "QPopupMenu" ) ) {
121     // --- Add action to the QPopupMenu ---
122     QPopupMenu* popup = (QPopupMenu*)w;
123     if ( index >= 0 && index < (int)popup->count() - 1 ) {
124       int id = popup->idAt( popup->count() - 1 );
125       if ( id != -1 ) {
126         QMenuItem* item = popup->findItem( id );
127         if ( item && item->isSeparator() ) {
128           popup->removeItem( id );
129           popup->insertSeparator( index );
130         }
131         else {
132           QPopupMenu* p = item ? item->popup() : 0;
133           int accel = popup->accel( id );
134           bool isOn = popup->isItemEnabled( id );
135           bool isVisible = popup->isItemVisible( id );
136           bool isChecked = popup->isItemChecked( id );
137           QString text = popup->text( id );
138           QIconSet icon;
139           if ( popup->iconSet( id ) )
140             icon = *popup->iconSet( id );
141           popup->removeItem( id );
142           int pos;
143           if ( icon.isNull() )
144             if ( p )
145               pos = popup->indexOf( popup->insertItem( text, p, id, index ) );
146             else
147               pos = popup->indexOf( popup->insertItem( text, id, index ) );
148           else
149             if ( p )
150               pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
151             else
152               pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
153           popup->setId( pos, id );
154           popup->setAccel( accel, id );
155           popup->setItemEnabled( id, isOn );
156           popup->setItemVisible( id, isVisible );
157           popup->setItemChecked( id, isChecked );
158           if ( !whatsThis().isEmpty() )
159             popup->setWhatsThis( id, whatsThis() );
160           if ( !p )
161             popup->connectItem( id, this, SLOT( internalActivation() ) );
162         }
163       }
164     }
165   }
166   else if ( w->inherits( "QMenuBar" ) ) {
167     // --- Add action to the QMenuBar ---
168     QMenuBar* mb = (QMenuBar*)w;
169     if ( index >= 0 && index < (int)mb->count() - 1 ) {
170       int id = mb->idAt( mb->count() - 1 );
171       if ( id != -1 ) {
172         QMenuItem* item = mb->findItem( id );
173         if ( item && item->isSeparator() ) {
174           mb->removeItem( id );
175           mb->insertSeparator( index );
176         }
177         else {
178           QPopupMenu* p = item ? item->popup() : 0;
179           int accel = mb->accel( id );
180           bool isOn = mb->isItemEnabled( id );
181           bool isVisible = mb->isItemVisible( id );
182           QString text = mb->text( id );
183           QIconSet icon;
184           if ( mb->iconSet( id ) )
185             icon = *mb->iconSet( id );
186           mb->removeItem( id );
187           int pos;
188           if ( icon.isNull() )
189             if ( p )
190               pos = mb->indexOf( mb->insertItem( text, p, id, index ) );
191             else
192               pos = mb->indexOf( mb->insertItem( text, id, index ) );
193           else
194             if ( p )
195               pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) );
196             else
197               pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) );
198           mb->setId( pos, id );
199           mb->setAccel( accel, id );
200           mb->setItemEnabled( id, isOn );
201           mb->setItemVisible( id, isVisible );
202           if ( !p )
203             mb->connectItem( id, this, SIGNAL( activated() ) );
204         }
205       }
206     }
207   }
208   return true;
209 }
210
211 /*!
212         Name: removeFrom [virtual public]
213         Desc: Removes this action from widget. Returns true if the action was removed
214                   successfully and false otherwise.
215 */
216
217 bool QtxAction::removeFrom( QWidget* w )
218 {
219   // check if widget is QMenuBar
220   if ( w->inherits( "QMenuBar" ) ) {
221     QMenuBar* mb = (QMenuBar*)w;
222     if ( myMenuIds.find( w ) == myMenuIds.end() )
223       return false;  // not yet added
224     mb->removeItem( myMenuIds[ w ] );
225     myMenuIds.remove( w );
226     return true;
227   }
228   return QAction::removeFrom( w );
229 }
230
231 /*!
232         Name: setPopup [virtual public]
233         Desc: Set or unset the sub popup menu for item with specified id in the given popup.
234 */
235
236 void QtxAction::setPopup( QWidget* w, const int id, QPopupMenu* subPopup ) const
237 {
238   if ( !w )
239     return;
240
241   if ( !w->inherits( "QPopupMenu" ) && !w->inherits( "QMenuBar" ) )
242     return;  // unsupported widget type
243
244   QMenuData* md = 0;
245   QMenuData* pmd = dynamic_cast<QMenuData*>( w );
246   if ( !pmd )
247     return;  // bad widget
248   
249   QMenuItem* item = pmd->findItem( id, &md );
250   if ( !item || md != pmd )
251     return;  // item is not found
252
253   QPopupMenu* oldPopup = item->popup();
254   if ( oldPopup == subPopup )
255     return;  // popup is not changed
256
257   // get properties
258   int accel = pmd->accel( id );
259   bool isOn = pmd->isItemEnabled( id );
260   bool isVisible = pmd->isItemVisible( id );
261   int pos = pmd->indexOf( id );
262   QString text = pmd->text( id );
263   QIconSet icon;
264   if ( pmd->iconSet( id ) )
265     icon = *pmd->iconSet( id );
266
267   // remove previous item
268   pmd->removeItem( id );
269
270   // add new item
271   if ( w->inherits( "QPopupMenu" ) ) {
272     // --- QPopupMenu ---
273     QPopupMenu* popup = (QPopupMenu*)w;
274     if ( icon.isNull() )
275       pos = popup->indexOf( subPopup ? popup->insertItem( text, subPopup, id, pos ) :
276                                        popup->insertItem( text, id, pos ) );
277     else
278       pos = popup->indexOf( subPopup ? popup->insertItem( icon, text, subPopup, id, pos ) : 
279                                        popup->insertItem( icon, text, id, pos ) );
280   }
281   else {
282     // --- QMenuBar ---
283     QMenuBar* mb = (QMenuBar*)w;
284     if ( icon.isNull() )
285       pos = mb->indexOf( subPopup ? mb->insertItem( text, subPopup, id, pos ) : 
286                                     mb->insertItem( text, id, pos ) );
287     else
288       pos = mb->indexOf( subPopup ? mb->insertItem( icon, text, subPopup, id, pos ) : 
289                                     mb->insertItem( icon, text, id, pos ) );
290   }
291
292   // restore properties
293   pmd->setId( pos, id ); // for sure (if id < 0)
294   pmd->setAccel( accel, id );
295   pmd->setItemEnabled( id, isOn );
296   pmd->setItemVisible( id, isVisible );
297
298   // delete old popup
299   delete oldPopup;
300 }
301