1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
19 // File: QtxAction.cxx
20 // Author: Sergey TELKOV
22 #include "QtxAction.h"
24 #include <qpopupmenu.h>
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.
33 QtxAction::QtxAction( QObject* parent, const char* name, bool toggle )
34 : QAction( parent, name, toggle )
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
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 )
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.
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 )
69 Name: ~QtxAction [virtual public]
73 QtxAction::~QtxAction()
78 Name: addTo [virtual public]
79 Desc: Adds this action to widget. Returns true if the action was added
80 successfully and false otherwise.
83 bool QtxAction::addTo( QWidget* w )
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:
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() );
100 myMenuIds[ w ] = mb->insertItem( iconSet(), menuText(), this, SIGNAL( activated() ), accel() );
101 mb->setItemEnabled( myMenuIds[ w ], isEnabled() );
102 mb->setItemVisible( myMenuIds[ w ], isVisible() );
105 return QAction::addTo( w );
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.
115 bool QtxAction::addTo( QWidget* w, int index )
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 );
126 QMenuItem* item = popup->findItem( id );
127 if ( item && item->isSeparator() ) {
128 popup->removeItem( id );
129 popup->insertSeparator( index );
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 );
139 if ( popup->iconSet( id ) )
140 icon = *popup->iconSet( id );
141 popup->removeItem( id );
145 pos = popup->indexOf( popup->insertItem( text, p, id, index ) );
147 pos = popup->indexOf( popup->insertItem( text, id, index ) );
150 pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
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() );
161 popup->connectItem( id, this, SLOT( internalActivation() ) );
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 );
172 QMenuItem* item = mb->findItem( id );
173 if ( item && item->isSeparator() ) {
174 mb->removeItem( id );
175 mb->insertSeparator( index );
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 );
184 if ( mb->iconSet( id ) )
185 icon = *mb->iconSet( id );
186 mb->removeItem( id );
190 pos = mb->indexOf( mb->insertItem( text, p, id, index ) );
192 pos = mb->indexOf( mb->insertItem( text, id, index ) );
195 pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) );
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 );
203 mb->connectItem( id, this, SIGNAL( activated() ) );
212 Name: removeFrom [virtual public]
213 Desc: Removes this action from widget. Returns true if the action was removed
214 successfully and false otherwise.
217 bool QtxAction::removeFrom( QWidget* w )
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 );
228 return QAction::removeFrom( w );
232 Name: setPopup [virtual public]
233 Desc: Set or unset the sub popup menu for item with specified id in the given popup.
236 void QtxAction::setPopup( QWidget* w, const int id, QPopupMenu* subPopup ) const
241 if ( !w->inherits( "QPopupMenu" ) && !w->inherits( "QMenuBar" ) )
242 return; // unsupported widget type
245 QMenuData* pmd = dynamic_cast<QMenuData*>( w );
247 return; // bad widget
249 QMenuItem* item = pmd->findItem( id, &md );
250 if ( !item || md != pmd )
251 return; // item is not found
253 QPopupMenu* oldPopup = item->popup();
254 if ( oldPopup == subPopup )
255 return; // popup is not changed
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 );
264 if ( pmd->iconSet( id ) )
265 icon = *pmd->iconSet( id );
267 // remove previous item
268 pmd->removeItem( id );
271 if ( w->inherits( "QPopupMenu" ) ) {
272 // --- QPopupMenu ---
273 QPopupMenu* popup = (QPopupMenu*)w;
275 pos = popup->indexOf( subPopup ? popup->insertItem( text, subPopup, id, pos ) :
276 popup->insertItem( text, id, pos ) );
278 pos = popup->indexOf( subPopup ? popup->insertItem( icon, text, subPopup, id, pos ) :
279 popup->insertItem( icon, text, id, pos ) );
283 QMenuBar* mb = (QMenuBar*)w;
285 pos = mb->indexOf( subPopup ? mb->insertItem( text, subPopup, id, pos ) :
286 mb->insertItem( text, id, pos ) );
288 pos = mb->indexOf( subPopup ? mb->insertItem( icon, text, subPopup, id, pos ) :
289 mb->insertItem( icon, text, id, pos ) );
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 );