Salome HOME
Copyright update: 2016
[modules/gui.git] / src / Qtx / QtxAction.cxx
1 // Copyright (C) 2007-2016  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 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 menuText menu text
131   \param accel shortcut key sequence
132   \param parent parent object
133   \param toggle if \c true the action is a toggle action
134   \param shortcutAction shortcut action identifier
135 */
136 QtxAction::QtxAction( const QString& text, const QString& menuText,
137                       int accel, QObject* parent, bool toggle, const QString& shortcutAction )
138 : QWidgetAction( parent )
139 {
140   setText( menuText );
141   setToolTip( text );
142   setShortcut( accel );
143   setCheckable( toggle );
144   setShortcutActionName(shortcutAction);
145   
146   QApplication::instance()->installEventFilter( this );
147 }
148
149 /*!
150   \brief Destructor.
151 */
152 QtxAction::~QtxAction()
153 {
154 }
155
156 /*!
157   \brief Customize action events.
158   
159   Sends a notification event to the action when it is added to 
160   the widget or removed from it in order to perform custom processing.
161
162   \param o object
163   \param e event
164   \return \c true if further event processing should be stopped
165   \sa customEvent(), addedTo(), removedFrom()
166 */
167 bool QtxAction::eventFilter( QObject* o, QEvent* e )
168 {
169   if ( o && o->isWidgetType() )
170   {
171     if ( e->type() == QEvent::ActionAdded && ((QActionEvent*)e)->action() == this )
172       QApplication::postEvent( this, new ActionNotify( true, (QWidget*)o ) );
173     if ( e->type() == QEvent::ActionRemoved && ((QActionEvent*)e)->action() == this )
174       QApplication::postEvent( this, new ActionNotify( false, (QWidget*)o ) );
175   }
176   return QWidgetAction::eventFilter( o, e );
177 }
178
179 /*!
180   \brief Called when the action is added to the widget.
181
182   This method can be redefined in the subclasses to customize 
183   the action behavior. Base implementation does nothing. 
184
185   \param w widget (should be menu or toolbar)
186   \sa removedFrom()
187 */
188 void QtxAction::addedTo( QWidget* /*w*/ )
189 {
190 }
191
192 /*!
193   \brief Called when the action is removed from the widget.
194
195   This method can be redefined in the subclasses to customize
196   the action behavior. Base implementation does nothing.
197
198   \param w widget (should be menu or toolbar)
199   \sa addedTo()
200 */
201 void QtxAction::removedFrom( QWidget* /*w*/ )
202 {
203 }
204
205 /*!
206   \brief Process notification events.
207   
208   Calls addedTo() method when the action is added to the widget
209   and removedFrom() when it is removed from the widget
210   in order to perform custom processing.
211
212   \param e noification event
213   \sa eventFilter(), addedTo(), removedFrom()
214 */
215 void QtxAction::customEvent( QEvent* e )
216 {
217   ActionNotify* ae = (ActionNotify*)e;
218   if ( !ae->widget() )
219     return;
220
221   if ( ae->isAdded() )
222     addedTo( ae->widget() );
223   else
224     removedFrom( ae->widget() );
225 }
226
227 /*!
228   \brief Return shortcut action name for the action.
229   
230   \return shortcut action name
231   \sa setShortcutActionName()
232 */
233 QString QtxAction::shortcutActionName() const
234 {
235   return myShortcutActionName;
236 }
237
238 /*!
239   \brief Set shortcut action name to the action.
240
241   Shortcut action name is used for shortcuts customization.
242
243   \param shortcutAction shortcut action name
244   \sa shortcutActionName()
245 */
246 void QtxAction::setShortcutActionName( const QString& shortcutAction )
247 {
248   myShortcutActionName = shortcutAction;
249 }