]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxAction.cxx
Salome HOME
[bos #42871] Clipping plane remains applied after being deleted
[modules/gui.git] / src / Qtx / QtxAction.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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 ID is used to de(serialize) shortcut settings.
71
72   \param parent parent object
73   \param toggle if \c true the action will be a toggle action
74   \param ID shortcut action identifier
75 */
76 QtxAction::QtxAction( QObject* parent, bool toggle, const QString& ID )
77 : QWidgetAction( parent )
78 {
79   setCheckable( toggle );
80   setID(ID);
81   setText(ID);
82
83   QApplication::instance()->installEventFilter( this );
84 }
85
86 /*!
87   \brief Constructor.
88
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.
93
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
98   \param text menu text
99 */
100 QtxAction::QtxAction( QObject* parent, bool toggle, const QString& ID,
101                       const QString& toolTip, const QString& text, const QIcon& icon )
102 : QWidgetAction( parent )
103 {
104   setCheckable( toggle );
105   setID(ID);
106   setToolTip( toolTip );
107   setText( text );
108   setIcon( icon );
109
110   QApplication::instance()->installEventFilter( this );
111 }
112
113 /*!
114   \brief Constructor.
115
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.
120
121   \param text tooltip text
122   \param icon iconset
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
128 */
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 )
132 {
133   setIcon( icon );
134   setText( menuText );
135   setToolTip( text );
136   setCheckable( toggle );
137   setID(ID);
138   setShortcut(accel);
139
140   QApplication::instance()->installEventFilter( this );
141 }
142
143 /*!
144   \brief Constructor.
145
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.
150
151   \param text tooltip text
152   \param icon iconset
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
158 */
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 )
162 {
163   setIcon( icon );
164   setText( menuText );
165   setToolTip( text );
166   setCheckable( toggle );
167   setID(ID);
168   setShortcut(accel);
169
170   QApplication::instance()->installEventFilter( this );
171 }
172
173 /*!
174   \brief Constructor.
175
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.
180
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
187 */
188 QtxAction::QtxAction( const QString& text, const QString& menuText,
189                       int accel, QObject* parent, bool toggle, const QString& ID )
190 : QWidgetAction( parent )
191 {
192   setText( menuText );
193   setToolTip( text );
194   setCheckable( toggle );
195   setID(ID);
196   setShortcut(accel);
197
198   QApplication::instance()->installEventFilter( this );
199 }
200
201 /*!
202   \brief Constructor.
203
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.
208
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
215 */
216 QtxAction::QtxAction( const QString& text, const QString& menuText,
217                       const QKeySequence& accel, QObject* parent, bool toggle, const QString& ID )
218 : QWidgetAction( parent )
219 {
220   setText( menuText );
221   setToolTip( text );
222   setCheckable( toggle );
223   setID(ID);
224   setShortcut(accel);
225
226   QApplication::instance()->installEventFilter( this );
227 }
228
229 /*!
230   \brief Destructor.
231 */
232 QtxAction::~QtxAction()
233 {
234 }
235
236 /*!
237   \brief Customize action events.
238
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.
241
242   \param o object
243   \param e event
244   \return \c true if further event processing should be stopped
245   \sa customEvent(), addedTo(), removedFrom()
246 */
247 bool QtxAction::eventFilter( QObject* o, QEvent* e )
248 {
249   if ( o && o->isWidgetType() )
250   {
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 ) );
255   }
256   return QWidgetAction::eventFilter( o, e );
257 }
258
259 QString QtxAction::shortcutActionName() const
260 {
261   return myID;
262 }
263
264 void QtxAction::setShortcutActionName( const QString& shortcutAction )
265 {
266   myID = shortcutAction;
267 }
268
269 const QString& QtxAction::ID() const {
270   return myID;
271 }
272
273 void QtxAction::setID( const QString& theID) {
274   myID = theID;
275 }
276
277 /*!
278   \brief Called when the action is added to the widget.
279
280   This method can be redefined in the subclasses to customize
281   the action behavior. Base implementation does nothing.
282
283   \param w widget (should be menu or toolbar)
284   \sa removedFrom()
285 */
286 void QtxAction::addedTo( QWidget* /*w*/ )
287 {
288 }
289
290 /*!
291   \brief Called when the action is removed from the widget.
292
293   This method can be redefined in the subclasses to customize
294   the action behavior. Base implementation does nothing.
295
296   \param w widget (should be menu or toolbar)
297   \sa addedTo()
298 */
299 void QtxAction::removedFrom( QWidget* /*w*/ )
300 {
301 }
302
303 /*!
304   \brief Process notification events.
305
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.
309
310   \param e noification event
311   \sa eventFilter(), addedTo(), removedFrom()
312 */
313 void QtxAction::customEvent( QEvent* e )
314 {
315   ActionNotify* ae = (ActionNotify*)e;
316   if ( !ae->widget() )
317     return;
318
319   if ( ae->isAdded() )
320     addedTo( ae->widget() );
321   else
322     removedFrom( ae->widget() );
323 }