1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: QtxAction.cxx
24 // Author: Sergey TELKOV
26 #include "QtxAction.h"
30 #include <QActionEvent>
31 #include <QApplication>
34 \class QtxAction::ActionNotify
35 \brief Notify event used to signalize about event adding/removing.
39 class QtxAction::ActionNotify : public QEvent
42 ActionNotify( bool add, QWidget* wid ) : QEvent( QEvent::User ), myAdd( add ), myWidget( wid ) {};
45 bool isAdded() const { return myAdd; }
46 QWidget* widget() const { return myWidget; }
50 QPointer<QWidget> myWidget;
55 \brief Generic action class.
57 The class QtxAction inherits QWidgetAction class and can be used
58 as base class when implementing any custom menu/toolbar actions.
59 It is necessary to subclass from QtxAction and redefine virtual
60 callback methods addedTo(), removedFrom() (which are called automatically
61 when the action is added to the widget and removed from it) to customize
68 Creates an action owned by \a parent.
69 Parameter \a toggle can be used to make the action checkable.
70 Parameter \a ID is used to de(serialize) shortcut settings.
72 \param parent parent object
73 \param toggle if \c true the action will be a toggle action
74 \param ID shortcut action identifier
76 QtxAction::QtxAction( QObject* parent, bool toggle, const QString& ID )
77 : QWidgetAction( parent )
79 setCheckable( toggle );
83 QApplication::instance()->installEventFilter( this );
89 Creates an action owned by \a parent. Parameters \a text,
90 \a menuText and \a ID specify the action's attributes.
91 Parameter \a toggle can be used to make the action checkable.
92 Parameter \a ID is used to de(serialize) shortcut settings.
94 \param parent parent object
95 \param toggle if \c true the action is a toggle action
96 \param ID shortcut action identifier
97 \param toolTip tooltip text
100 QtxAction::QtxAction( QObject* parent, bool toggle, const QString& ID,
101 const QString& toolTip, const QString& text, const QIcon& icon )
102 : QWidgetAction( parent )
104 setCheckable( toggle );
106 setToolTip( toolTip );
110 QApplication::instance()->installEventFilter( this );
116 Creates an action owned by \a parent. Parameters \a text,
117 \a icon, \a menuText and \a ID specify the action's attributes.
118 Parameter \a toggle can be used to make the action checkable.
119 Parameter \a ID is used to de(serialize) shortcut settings.
121 \param text tooltip text
123 \param menuText menu text
124 \param accel will be disabled by SUIT_ShortcutMgr!
125 \param parent parent object
126 \param toggle if \c true the action will be a toggle action
127 \param ID shortcut action identifier
129 QtxAction::QtxAction( const QString& text, const QIcon& icon, const QString& menuText,
130 int accel, QObject* parent, bool toggle, const QString& ID )
131 : QWidgetAction( parent )
136 setCheckable( toggle );
140 QApplication::instance()->installEventFilter( this );
146 Creates an action owned by \a parent. Parameters \a text,
147 \a icon, \a menuText and \a ID specify the action's attributes.
148 Parameter \a toggle can be used to make the action checkable.
149 Parameter \a ID is used to de(serialize) shortcut settings.
151 \param text tooltip text
153 \param menuText menu text
154 \param accel will be disabled by SUIT_ShortcutMgr!
155 \param parent parent object
156 \param toggle if \c true the action will be a toggle action
157 \param ID shortcut action identifier
159 QtxAction::QtxAction( const QString& text, const QIcon& icon, const QString& menuText,
160 const QKeySequence& accel, QObject* parent, bool toggle, const QString& ID )
161 : QWidgetAction( parent )
166 setCheckable( toggle );
170 QApplication::instance()->installEventFilter( this );
176 Creates an action owned by \a parent. Parameters \a text,
177 \a menuText and \a ID specify the action's attributes.
178 Parameter \a toggle can be used to make the action checkable.
179 Parameter \a ID is used to de(serialize) shortcut settings.
181 \param text tooltip text
182 \param menuText menu text
183 \param accel will be disabled by SUIT_ShortcutMgr!
184 \param parent parent object
185 \param toggle if \c true the action is a toggle action
186 \param ID shortcut action identifier
188 QtxAction::QtxAction( const QString& text, const QString& menuText,
189 int accel, QObject* parent, bool toggle, const QString& ID )
190 : QWidgetAction( parent )
194 setCheckable( toggle );
198 QApplication::instance()->installEventFilter( this );
204 Creates an action owned by \a parent. Parameters \a text,
205 \a menuText and \a ID specify the action's attributes.
206 Parameter \a toggle can be used to make the action checkable.
207 Parameter \a ID is used to de(serialize) shortcut settings.
209 \param text tooltip text
210 \param menuText menu text
211 \param accel will be disabled by SUIT_ShortcutMgr!
212 \param parent parent object
213 \param toggle if \c true the action is a toggle action
214 \param ID shortcut action identifier
216 QtxAction::QtxAction( const QString& text, const QString& menuText,
217 const QKeySequence& accel, QObject* parent, bool toggle, const QString& ID )
218 : QWidgetAction( parent )
222 setCheckable( toggle );
226 QApplication::instance()->installEventFilter( this );
232 QtxAction::~QtxAction()
237 \brief Customize action events.
239 Sends a notification event to the action when it is added to
240 the widget or removed from it in order to perform custom processing.
244 \return \c true if further event processing should be stopped
245 \sa customEvent(), addedTo(), removedFrom()
247 bool QtxAction::eventFilter( QObject* o, QEvent* e )
249 if ( o && o->isWidgetType() )
251 if ( e->type() == QEvent::ActionAdded && ((QActionEvent*)e)->action() == this )
252 QApplication::postEvent( this, new ActionNotify( true, (QWidget*)o ) );
253 if ( e->type() == QEvent::ActionRemoved && ((QActionEvent*)e)->action() == this )
254 QApplication::postEvent( this, new ActionNotify( false, (QWidget*)o ) );
256 return QWidgetAction::eventFilter( o, e );
259 QString QtxAction::shortcutActionName() const
264 void QtxAction::setShortcutActionName( const QString& shortcutAction )
266 myID = shortcutAction;
269 const QString& QtxAction::ID() const {
273 void QtxAction::setID( const QString& theID) {
278 \brief Called when the action is added to the widget.
280 This method can be redefined in the subclasses to customize
281 the action behavior. Base implementation does nothing.
283 \param w widget (should be menu or toolbar)
286 void QtxAction::addedTo( QWidget* /*w*/ )
291 \brief Called when the action is removed from the widget.
293 This method can be redefined in the subclasses to customize
294 the action behavior. Base implementation does nothing.
296 \param w widget (should be menu or toolbar)
299 void QtxAction::removedFrom( QWidget* /*w*/ )
304 \brief Process notification events.
306 Calls addedTo() method when the action is added to the widget
307 and removedFrom() when it is removed from the widget
308 in order to perform custom processing.
310 \param e noification event
311 \sa eventFilter(), addedTo(), removedFrom()
313 void QtxAction::customEvent( QEvent* e )
315 ActionNotify* ae = (ActionNotify*)e;
320 addedTo( ae->widget() );
322 removedFrom( ae->widget() );