Salome HOME
Copyright update 2022
[modules/gui.git] / src / Qtx / QtxAction.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      QtxAction.cxx
24 // Author:    Sergey TELKOV
25 //
26 #include "QtxAction.h"
27
28 #include <QEvent>
29 #include <QPointer>
30 #include <QActionEvent>
31 #include <QApplication>
32
33 /*!
34   \class QtxAction::ActionNotify
35   \brief Notify event used to signalize about event adding/removing.
36   \internal
37 */
38
39 class QtxAction::ActionNotify : public QEvent
40 {
41 public:
42   ActionNotify( bool add, QWidget* wid ) : QEvent( QEvent::User ), myAdd( add ), myWidget( wid ) {};
43   ~ActionNotify() {};
44
45   bool              isAdded() const { return myAdd; }
46   QWidget*          widget() const { return myWidget; }
47
48 private:
49   bool              myAdd;
50   QPointer<QWidget> myWidget;
51 };
52
53 /*!
54   \class QtxAction
55   \brief Generic action class.
56
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
62   the action behavior.  
63 */
64
65 /*!
66   \brief Constructor.
67
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.
73
74   \param parent parent object
75   \param toggle if \c true the action will be a toggle action
76   \param shortcutAction shortcut action identifier
77 */
78 QtxAction::QtxAction( QObject* parent, bool toggle, const QString& shortcutAction )
79 : QWidgetAction( parent )
80 {
81   setCheckable( toggle );
82   setShortcutActionName(shortcutAction);
83
84   QApplication::instance()->installEventFilter( this );
85 }
86
87 /*!
88   \brief Constructor.
89
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.
96
97   \param text tooltip text
98   \param icon iconset
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
104 */
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 )
108 {
109   setIcon( icon );
110   setText( menuText );
111   setToolTip( text );
112   setShortcut( accel );
113   setCheckable( toggle );
114   setShortcutActionName(shortcutAction);
115
116   QApplication::instance()->installEventFilter( this );
117 }
118
119 /*!
120   \brief Constructor.
121
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.
128
129   \param text tooltip text
130   \param icon iconset
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
136 */
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 )
140 {
141   setIcon( icon );
142   setText( menuText );
143   setToolTip( text );
144   setShortcut( accel );
145   setCheckable( toggle );
146   setShortcutActionName(shortcutAction);
147
148   QApplication::instance()->installEventFilter( this );
149 }
150
151 /*!
152   \brief Constructor.
153
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.
160
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
167 */
168 QtxAction::QtxAction( const QString& text, const QString& menuText,
169                       int accel, QObject* parent, bool toggle, const QString& shortcutAction )
170 : QWidgetAction( parent )
171 {
172   setText( menuText );
173   setToolTip( text );
174   setShortcut( accel );
175   setCheckable( toggle );
176   setShortcutActionName(shortcutAction);
177   
178   QApplication::instance()->installEventFilter( this );
179 }
180
181 /*!
182   \brief Constructor.
183
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.
190
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
197 */
198 QtxAction::QtxAction( const QString& text, const QString& menuText,
199                       const QKeySequence& accel, QObject* parent, bool toggle, const QString& shortcutAction )
200 : QWidgetAction( parent )
201 {
202   setText( menuText );
203   setToolTip( text );
204   setShortcut( accel );
205   setCheckable( toggle );
206   setShortcutActionName(shortcutAction);
207   
208   QApplication::instance()->installEventFilter( this );
209 }
210
211 /*!
212   \brief Destructor.
213 */
214 QtxAction::~QtxAction()
215 {
216 }
217
218 /*!
219   \brief Customize action events.
220   
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.
223
224   \param o object
225   \param e event
226   \return \c true if further event processing should be stopped
227   \sa customEvent(), addedTo(), removedFrom()
228 */
229 bool QtxAction::eventFilter( QObject* o, QEvent* e )
230 {
231   if ( o && o->isWidgetType() )
232   {
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 ) );
237   }
238   return QWidgetAction::eventFilter( o, e );
239 }
240
241 /*!
242   \brief Called when the action is added to the widget.
243
244   This method can be redefined in the subclasses to customize 
245   the action behavior. Base implementation does nothing. 
246
247   \param w widget (should be menu or toolbar)
248   \sa removedFrom()
249 */
250 void QtxAction::addedTo( QWidget* /*w*/ )
251 {
252 }
253
254 /*!
255   \brief Called when the action is removed from the widget.
256
257   This method can be redefined in the subclasses to customize
258   the action behavior. Base implementation does nothing.
259
260   \param w widget (should be menu or toolbar)
261   \sa addedTo()
262 */
263 void QtxAction::removedFrom( QWidget* /*w*/ )
264 {
265 }
266
267 /*!
268   \brief Process notification events.
269   
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.
273
274   \param e noification event
275   \sa eventFilter(), addedTo(), removedFrom()
276 */
277 void QtxAction::customEvent( QEvent* e )
278 {
279   ActionNotify* ae = (ActionNotify*)e;
280   if ( !ae->widget() )
281     return;
282
283   if ( ae->isAdded() )
284     addedTo( ae->widget() );
285   else
286     removedFrom( ae->widget() );
287 }
288
289 /*!
290   \brief Return shortcut action name for the action.
291   
292   \return shortcut action name
293   \sa setShortcutActionName()
294 */
295 QString QtxAction::shortcutActionName() const
296 {
297   return myShortcutActionName;
298 }
299
300 /*!
301   \brief Set shortcut action name to the action.
302
303   Shortcut action name is used for shortcuts customization.
304
305   \param shortcutAction shortcut action name
306   \sa shortcutActionName()
307 */
308 void QtxAction::setShortcutActionName( const QString& shortcutAction )
309 {
310   myShortcutActionName = shortcutAction;
311 }