1 // Copyright (C) 2007-2008 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.
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
22 // File: QtxAction.cxx
23 // Author: Sergey TELKOV
25 #include "QtxAction.h"
29 #include <QActionEvent>
30 #include <QApplication>
33 \class QtxAction::ActionNotify
34 \brief Notify event used to signalize about event adding/removing.
38 class QtxAction::ActionNotify : public QEvent
41 ActionNotify( bool add, QWidget* wid ) : QEvent( QEvent::User ), myAdd( add ), myWidget( wid ) {};
44 bool isAdded() const { return myAdd; }
45 QWidget* widget() const { return myWidget; }
49 QPointer<QWidget> myWidget;
54 \brief Generic action class.
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
67 Creates an action owned by \a parent.
68 Parameter \a toggle can be used to make the action checkable.
70 \param parent parent object
71 \param toggle if \c true the action will be a toggle action
73 QtxAction::QtxAction( QObject* parent, bool toggle )
74 : QWidgetAction( parent )
76 setCheckable( toggle );
78 QApplication::instance()->installEventFilter( this );
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.
88 \param text tooltip text
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
95 QtxAction::QtxAction( const QString& text, const QIcon& icon,
96 const QString& menuText, int accel, QObject* parent, bool toggle )
97 : QWidgetAction( parent )
102 setShortcut( accel );
103 setCheckable( toggle );
105 QApplication::instance()->installEventFilter( this );
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.
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
121 QtxAction::QtxAction( const QString& text, const QString& menuText,
122 int accel, QObject* parent, bool toggle )
123 : QWidgetAction( parent )
127 setShortcut( accel );
128 setCheckable( toggle );
130 QApplication::instance()->installEventFilter( this );
136 QtxAction::~QtxAction()
141 \brief Customize action events.
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.
148 \return \c true if further event processing should be stopped
149 \sa customEvent(), addedTo(), removedFrom()
151 bool QtxAction::eventFilter( QObject* o, QEvent* e )
153 if ( o && o->isWidgetType() )
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 ) );
160 return QWidgetAction::eventFilter( o, e );
164 \brief Called when the action is added to the widget.
166 This method can be redefined in the subclasses to customize
167 the action behavior. Base implementation does nothing.
169 \param w widget (should be menu or toolbar)
172 void QtxAction::addedTo( QWidget* /*w*/ )
177 \brief Called when the action is removed from the widget.
179 This method can be redefined in the subclasses to customize
180 the action behavior. Base implementation does nothing.
182 \param w widget (should be menu or toolbar)
185 void QtxAction::removedFrom( QWidget* /*w*/ )
190 \brief Process notification events.
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.
196 \param e noification event
197 \sa eventFilter(), addedTo(), removedFrom()
199 void QtxAction::customEvent( QEvent* e )
201 ActionNotify* ae = (ActionNotify*)e;
206 addedTo( ae->widget() );
208 removedFrom( ae->widget() );