]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxAction.cxx
Salome HOME
49102514222393f3ab651da2144ce6458db2e29b
[modules/gui.git] / src / Qtx / QtxAction.cxx
1 //  Copyright (C) 2007-2008  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.
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 // File:      QtxAction.cxx
23 // Author:    Sergey TELKOV
24 //
25 #include "QtxAction.h"
26
27 #include <QEvent>
28 #include <QPointer>
29 #include <QActionEvent>
30 #include <QApplication>
31
32 /*!
33   \class QtxAction::ActionNotify
34   \brief Notify event used to signalize about event adding/removing.
35   \internal
36 */
37
38 class QtxAction::ActionNotify : public QEvent
39 {
40 public:
41   ActionNotify( bool add, QWidget* wid ) : QEvent( QEvent::User ), myAdd( add ), myWidget( wid ) {};
42   ~ActionNotify() {};
43
44   bool              isAdded() const { return myAdd; }
45   QWidget*          widget() const { return myWidget; }
46
47 private:
48   bool              myAdd;
49   QPointer<QWidget> myWidget;
50 };
51
52 /*!
53   \class QtxAction
54   \brief Generic action class.
55
56   The class QtxAction inherits QWidgetAction class and can be used 
57   as base class when implementing any custom menu/toolbar actions.
58   It is necessary to subclass from QtxAction and redefine virtual 
59   callback methods addedTo(), removedFrom() (which are called automatically
60   when the action is added to the widget and removed from it) to customize
61   the action behavior.  
62 */
63
64 /*!
65   \brief Constructor.
66
67   Creates an action owned by \a parent. 
68   Parameter \a toggle can be used to make the action checkable.
69
70   \param parent parent object
71   \param toggle if \c true the action will be a toggle action
72 */
73 QtxAction::QtxAction( QObject* parent, bool toggle )
74 : QWidgetAction( parent )
75 {
76   setCheckable( toggle );
77
78   QApplication::instance()->installEventFilter( this );
79 }
80
81 /*!
82   \brief Constructor.
83
84   Creates an action owned by \a parent. Parameters \a text,
85   \a icon, \a menuText and \a accel specify the action's attributes.
86   Parameter \a toggle can be used to make the action checkable.
87
88   \param text tooltip text
89   \param icon iconset
90   \param menuText menu text
91   \param accel shortcut key sequence
92   \param parent parent object
93   \param toggle if \c true the action will be a toggle action
94 */
95 QtxAction::QtxAction( const QString& text, const QIcon& icon,
96                       const QString& menuText, int accel, QObject* parent, bool toggle )
97 : QWidgetAction( parent )
98 {
99   setIcon( icon );
100   setText( menuText );
101   setToolTip( text );
102   setShortcut( accel );
103   setCheckable( toggle );
104
105   QApplication::instance()->installEventFilter( this );
106 }
107
108 /*!
109   \brief Constructor.
110
111   Creates an action owned by \a parent. Parameters \a text,
112   \a menuText and \a accel specify the action's attributes.
113   Parameter \a toggle can be used to make the action checkable.
114
115   \param text tooltip text
116   \param menuText menu text
117   \param accel shortcut key sequence
118   \param parent parent object
119   \param toggle if \c true the action is a toggle action
120 */
121 QtxAction::QtxAction( const QString& text, const QString& menuText,
122                       int accel, QObject* parent, bool toggle )
123 : QWidgetAction( parent )
124 {
125   setText( menuText );
126   setToolTip( text );
127   setShortcut( accel );
128   setCheckable( toggle );
129
130   QApplication::instance()->installEventFilter( this );
131 }
132
133 /*!
134   \brief Destructor.
135 */
136 QtxAction::~QtxAction()
137 {
138 }
139
140 /*!
141   \brief Customize action events.
142   
143   Sends a notification event to the action when it is added to 
144   the widget or removed from it in order to perform custom processing.
145
146   \param o object
147   \param e event
148   \return \c true if further event processing should be stopped
149   \sa customEvent(), addedTo(), removedFrom()
150 */
151 bool QtxAction::eventFilter( QObject* o, QEvent* e )
152 {
153   if ( o && o->isWidgetType() )
154   {
155     if ( e->type() == QEvent::ActionAdded && ((QActionEvent*)e)->action() == this )
156       QApplication::postEvent( this, new ActionNotify( true, (QWidget*)o ) );
157     if ( e->type() == QEvent::ActionRemoved && ((QActionEvent*)e)->action() == this )
158       QApplication::postEvent( this, new ActionNotify( false, (QWidget*)o ) );
159   }
160   return QWidgetAction::eventFilter( o, e );
161 }
162
163 /*!
164   \brief Called when the action is added to the widget.
165
166   This method can be redefined in the subclasses to customize 
167   the action behavior. Base implementation does nothing. 
168
169   \param w widget (should be menu or toolbar)
170   \sa removedFrom()
171 */
172 void QtxAction::addedTo( QWidget* /*w*/ )
173 {
174 }
175
176 /*!
177   \brief Called when the action is removed from the widget.
178
179   This method can be redefined in the subclasses to customize
180   the action behavior. Base implementation does nothing.
181
182   \param w widget (should be menu or toolbar)
183   \sa addedTo()
184 */
185 void QtxAction::removedFrom( QWidget* /*w*/ )
186 {
187 }
188
189 /*!
190   \brief Process notification events.
191   
192   Calls addedTo() method when the action is added to the widget
193   and removedFrom() when it is removed from the widget
194   in order to perform custom processing.
195
196   \param e noification event
197   \sa eventFilter(), addedTo(), removedFrom()
198 */
199 void QtxAction::customEvent( QEvent* e )
200 {
201   ActionNotify* ae = (ActionNotify*)e;
202   if ( !ae->widget() )
203     return;
204
205   if ( ae->isAdded() )
206     addedTo( ae->widget() );
207   else
208     removedFrom( ae->widget() );
209 }