Salome HOME
9fafdb7ac1c0a48a741ce859435264fca581e37c
[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
26 /*!
27         Name: QtxAction [public]
28         Desc: Constructs an action with given parent and name. If toggle is true the
29                   action will be a toggle action, otherwise it will be a command action.
30 */
31
32 QtxAction::QtxAction( QObject* parent, const char* name, bool toggle )
33 : QAction( parent, name, toggle )
34 {
35 }
36
37 /*!
38         Name: QtxAction [public]
39         Desc: This constructor creates an action with the following properties: the
40                   description text, the icon or iconset icon, the menu text and keyboard
41                   accelerator. It is a child of given parent and named specified name.
42                   If toggle is true the action will be a toggle action, otherwise it will
43                   be a command action.
44 */
45
46 QtxAction::QtxAction( const QString& text, const QIconSet& icon,
47                       const QString& menuText, int accel,
48                       QObject* parent, const char* name, bool toggle )
49 : QAction( text, icon, menuText, accel, parent, name, toggle )
50 {
51 }
52
53 /*!
54         Name: QtxAction [public]
55         Desc: This constructor creates an action with the following properties: the
56                   description text, the menu text and keyboard accelerator. It is a child
57                   of given parent and named specified name. If toggle is true the action
58                   will be a toggle action, otherwise it will be a command action.
59 */
60
61 QtxAction::QtxAction( const QString& text, const QString& menuText, int accel,
62                       QObject* parent, const char* name, bool toggle )
63 : QAction( text, menuText, accel, parent, name, toggle )
64 {
65 }
66
67 /*!
68         Name: ~QtxAction [virtual public]
69         Desc: Destructor.
70 */
71
72 QtxAction::~QtxAction()
73 {
74 }
75
76 /*!
77         Name: addTo [virtual public]
78         Desc: Adds this action to widget. Returns true if the action was added
79                   successfully and false otherwise.
80 */
81
82 bool QtxAction::addTo( QWidget* w )
83 {
84   return QAction::addTo( w );
85 }
86
87 /*!
88         Name: addTo [virtual public]
89         Desc: Adds this action to widget. If widget is QPopupMenu given index will
90                   be used for menu item inserting. Returns true if the action was added
91                   successfully and false otherwise.
92 */
93
94 bool QtxAction::addTo( QWidget* w, int index )
95 {
96   if ( !addTo( w ) )
97     return false;
98
99   if ( w->inherits( "QPopupMenu" ) )
100   {
101     QPopupMenu* popup = (QPopupMenu*)w;
102     if ( index < (int)popup->count() - 1 )
103     {
104       int id = popup->idAt( popup->count() - 1 );
105       if ( id != -1 )
106       {
107                           QMenuItem* item = popup->findItem( id );
108                                 if ( item && item->isSeparator() )
109                                 {
110                                         popup->removeItem( id );
111           popup->insertSeparator( index );
112                                 }
113                                 else
114                                 {
115                                         QPopupMenu* p = item ? item->popup() : 0;
116                                         int accel = popup->accel( id );
117                                         bool isOn = popup->isItemEnabled( id );
118                                         QString text = popup->text( id );
119                                         QIconSet icon;
120                                         if ( popup->iconSet( id ) )
121                                                 icon = *popup->iconSet( id );
122                                         popup->removeItem( id );
123                                         int pos;
124                                         if ( icon.isNull() )
125                                                 if ( p )
126                                                         pos = popup->indexOf( popup->insertItem( text, p, id, index ) );
127                                                 else
128                                                         pos = popup->indexOf( popup->insertItem( text, id, index ) );
129                                         else
130                                                 if ( p )
131                                                         pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
132                                                 else
133                                                         pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
134                                         popup->setId( pos, id );
135                                         popup->setAccel( accel, id );
136                                         popup->setItemEnabled( id, isOn );
137                                         if ( !p )
138                                                 popup->connectItem( id, this, SLOT( internalActivation() ) );
139                                 }
140       }
141     }
142   }
143
144   return true;
145 }
146
147 /*!
148         Name: setPopup [virtual public]
149         Desc: Set or unset the sub popup menu for item with specified id in the given popup.
150 */
151
152 void QtxAction::setPopup( QPopupMenu* popup, const int id, QPopupMenu* subPopup ) const
153 {
154   if ( !popup )
155     return;
156
157   QMenuData* md = 0;
158   const QMenuData* pmd = popup;
159   QMenuItem* item = popup->findItem( id, &md );
160   if ( !item || md != pmd )
161     return;
162
163   QPopupMenu* oldPopup = item->popup();
164   if ( oldPopup == subPopup )
165     return;
166
167   int accel = popup->accel( id );
168   bool isOn = popup->isItemEnabled( id );
169   QString text = popup->text( id );
170   QIconSet icon;
171         if ( popup->iconSet( id ) )
172     icon = *popup->iconSet( id );
173   popup->removeItem( id );
174
175   int pos;
176   if ( icon.isNull() )
177     pos = popup->indexOf( subPopup ? popup->insertItem( text, subPopup ) : popup->insertItem( text ) );
178   else
179     pos = popup->indexOf( subPopup ? popup->insertItem( icon, text, subPopup ) : popup->insertItem( icon, text ) );
180
181   popup->setId( pos, id );
182   popup->setAccel( accel, id );
183   popup->setItemEnabled( id, isOn );
184
185   delete oldPopup;
186 }