1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, 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 shortcutAction can be used to assign the shortcut from
71 preferences. This parameter value corresponds to shortcut action identifier
72 in shortcut preferences.
74 \param parent parent object
75 \param toggle if \c true the action will be a toggle action
76 \param shortcutAction shortcut action identifier
78 QtxAction::QtxAction( QObject* parent, bool toggle, const QString& shortcutAction )
79 : QWidgetAction( parent )
81 setCheckable( toggle );
82 setShortcutActionName(shortcutAction);
84 QApplication::instance()->installEventFilter( this );
90 Creates an action owned by \a parent. Parameters \a text,
91 \a icon, \a menuText and \a accel specify the action's attributes.
92 Parameter \a toggle can be used to make the action checkable.
93 Parameter \a shortcutAction can be used to assign the shortcut from
94 preferences. This parameter value corresponds to shortcut action identifier
95 in shortcut preferences.
97 \param text tooltip text
99 \param menuText menu text
100 \param accel shortcut key sequence
101 \param parent parent object
102 \param toggle if \c true the action will be a toggle action
103 \param shortcutAction shortcut action identifier
105 QtxAction::QtxAction( const QString& text, const QIcon& icon, const QString& menuText,
106 int accel, QObject* parent, bool toggle, const QString& shortcutAction )
107 : QWidgetAction( parent )
112 setShortcut( accel );
113 setCheckable( toggle );
114 setShortcutActionName(shortcutAction);
116 QApplication::instance()->installEventFilter( this );
122 Creates an action owned by \a parent. Parameters \a text,
123 \a icon, \a menuText and \a accel specify the action's attributes.
124 Parameter \a toggle can be used to make the action checkable.
125 Parameter \a shortcutAction can be used to assign the shortcut from
126 preferences. This parameter value corresponds to shortcut action identifier
127 in shortcut preferences.
129 \param text tooltip text
131 \param menuText menu text
132 \param accel shortcut key sequence
133 \param parent parent object
134 \param toggle if \c true the action will be a toggle action
135 \param shortcutAction shortcut action identifier
137 QtxAction::QtxAction( const QString& text, const QIcon& icon, const QString& menuText,
138 const QKeySequence& accel, QObject* parent, bool toggle, const QString& shortcutAction )
139 : QWidgetAction( parent )
144 setShortcut( accel );
145 setCheckable( toggle );
146 setShortcutActionName(shortcutAction);
148 QApplication::instance()->installEventFilter( this );
154 Creates an action owned by \a parent. Parameters \a text,
155 \a menuText and \a accel specify the action's attributes.
156 Parameter \a toggle can be used to make the action checkable.
157 Parameter \a shortcutAction can be used to assign the shortcut from
158 preferences. This parameter value corresponds to shortcut action identifier
159 in shortcut preferences.
161 \param text tooltip text
162 \param menuText menu text
163 \param accel shortcut key sequence
164 \param parent parent object
165 \param toggle if \c true the action is a toggle action
166 \param shortcutAction shortcut action identifier
168 QtxAction::QtxAction( const QString& text, const QString& menuText,
169 int accel, QObject* parent, bool toggle, const QString& shortcutAction )
170 : QWidgetAction( parent )
174 setShortcut( accel );
175 setCheckable( toggle );
176 setShortcutActionName(shortcutAction);
178 QApplication::instance()->installEventFilter( this );
184 Creates an action owned by \a parent. Parameters \a text,
185 \a menuText and \a accel specify the action's attributes.
186 Parameter \a toggle can be used to make the action checkable.
187 Parameter \a shortcutAction can be used to assign the shortcut from
188 preferences. This parameter value corresponds to shortcut action identifier
189 in shortcut preferences.
191 \param text tooltip text
192 \param menuText menu text
193 \param accel shortcut key sequence
194 \param parent parent object
195 \param toggle if \c true the action is a toggle action
196 \param shortcutAction shortcut action identifier
198 QtxAction::QtxAction( const QString& text, const QString& menuText,
199 const QKeySequence& accel, QObject* parent, bool toggle, const QString& shortcutAction )
200 : QWidgetAction( parent )
204 setShortcut( accel );
205 setCheckable( toggle );
206 setShortcutActionName(shortcutAction);
208 QApplication::instance()->installEventFilter( this );
214 QtxAction::~QtxAction()
219 \brief Customize action events.
221 Sends a notification event to the action when it is added to
222 the widget or removed from it in order to perform custom processing.
226 \return \c true if further event processing should be stopped
227 \sa customEvent(), addedTo(), removedFrom()
229 bool QtxAction::eventFilter( QObject* o, QEvent* e )
231 if ( o && o->isWidgetType() )
233 if ( e->type() == QEvent::ActionAdded && ((QActionEvent*)e)->action() == this )
234 QApplication::postEvent( this, new ActionNotify( true, (QWidget*)o ) );
235 if ( e->type() == QEvent::ActionRemoved && ((QActionEvent*)e)->action() == this )
236 QApplication::postEvent( this, new ActionNotify( false, (QWidget*)o ) );
238 return QWidgetAction::eventFilter( o, e );
242 \brief Called when the action is added to the widget.
244 This method can be redefined in the subclasses to customize
245 the action behavior. Base implementation does nothing.
247 \param w widget (should be menu or toolbar)
250 void QtxAction::addedTo( QWidget* /*w*/ )
255 \brief Called when the action is removed from the widget.
257 This method can be redefined in the subclasses to customize
258 the action behavior. Base implementation does nothing.
260 \param w widget (should be menu or toolbar)
263 void QtxAction::removedFrom( QWidget* /*w*/ )
268 \brief Process notification events.
270 Calls addedTo() method when the action is added to the widget
271 and removedFrom() when it is removed from the widget
272 in order to perform custom processing.
274 \param e noification event
275 \sa eventFilter(), addedTo(), removedFrom()
277 void QtxAction::customEvent( QEvent* e )
279 ActionNotify* ae = (ActionNotify*)e;
284 addedTo( ae->widget() );
286 removedFrom( ae->widget() );
290 \brief Return shortcut action name for the action.
292 \return shortcut action name
293 \sa setShortcutActionName()
295 QString QtxAction::shortcutActionName() const
297 return myShortcutActionName;
301 \brief Set shortcut action name to the action.
303 Shortcut action name is used for shortcuts customization.
305 \param shortcutAction shortcut action name
306 \sa shortcutActionName()
308 void QtxAction::setShortcutActionName( const QString& shortcutAction )
310 myShortcutActionName = shortcutAction;