1 // Copyright (C) 2007-2015 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 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
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
136 QtxAction::QtxAction( const QString& text, const QString& menuText,
137 int accel, QObject* parent, bool toggle, const QString& shortcutAction )
138 : QWidgetAction( parent )
142 setShortcut( accel );
143 setCheckable( toggle );
144 setShortcutActionName(shortcutAction);
146 QApplication::instance()->installEventFilter( this );
152 QtxAction::~QtxAction()
157 \brief Customize action events.
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.
164 \return \c true if further event processing should be stopped
165 \sa customEvent(), addedTo(), removedFrom()
167 bool QtxAction::eventFilter( QObject* o, QEvent* e )
169 if ( o && o->isWidgetType() )
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 ) );
176 return QWidgetAction::eventFilter( o, e );
180 \brief Called when the action is added to the widget.
182 This method can be redefined in the subclasses to customize
183 the action behavior. Base implementation does nothing.
185 \param w widget (should be menu or toolbar)
188 void QtxAction::addedTo( QWidget* /*w*/ )
193 \brief Called when the action is removed from the widget.
195 This method can be redefined in the subclasses to customize
196 the action behavior. Base implementation does nothing.
198 \param w widget (should be menu or toolbar)
201 void QtxAction::removedFrom( QWidget* /*w*/ )
206 \brief Process notification events.
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.
212 \param e noification event
213 \sa eventFilter(), addedTo(), removedFrom()
215 void QtxAction::customEvent( QEvent* e )
217 ActionNotify* ae = (ActionNotify*)e;
222 addedTo( ae->widget() );
224 removedFrom( ae->widget() );
228 \brief Return shortcut action name for the action.
230 \return shortcut action name
231 \sa setShortcutActionName()
233 QString QtxAction::shortcutActionName() const
235 return myShortcutActionName;
239 \brief Set shortcut action name to the action.
241 Shortcut action name is used for shortcuts customization.
243 \param shortcutAction shortcut action name
244 \sa shortcutActionName()
246 void QtxAction::setShortcutActionName( const QString& shortcutAction )
248 myShortcutActionName = shortcutAction;