Back to index

salome-gui  6.5.0
QtxAction.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxAction.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxAction.h"
00027 
00028 #include <QEvent>
00029 #include <QPointer>
00030 #include <QActionEvent>
00031 #include <QApplication>
00032 
00039 class QtxAction::ActionNotify : public QEvent
00040 {
00041 public:
00042   ActionNotify( bool add, QWidget* wid ) : QEvent( QEvent::User ), myAdd( add ), myWidget( wid ) {};
00043   ~ActionNotify() {};
00044 
00045   bool              isAdded() const { return myAdd; }
00046   QWidget*          widget() const { return myWidget; }
00047 
00048 private:
00049   bool              myAdd;
00050   QPointer<QWidget> myWidget;
00051 };
00052 
00078 QtxAction::QtxAction( QObject* parent, bool toggle, const QString& shortcutAction )
00079 : QWidgetAction( parent )
00080 {
00081   setCheckable( toggle );
00082   setShortcutActionName(shortcutAction);
00083 
00084   QApplication::instance()->installEventFilter( this );
00085 }
00086 
00105 QtxAction::QtxAction( const QString& text, const QIcon& icon, const QString& menuText, 
00106                     int accel, QObject* parent, bool toggle, const QString& shortcutAction )
00107 : QWidgetAction( parent )
00108 {
00109   setIcon( icon );
00110   setText( menuText );
00111   setToolTip( text );
00112   setShortcut( accel );
00113   setCheckable( toggle );
00114   setShortcutActionName(shortcutAction);
00115 
00116   QApplication::instance()->installEventFilter( this );
00117 }
00118 
00136 QtxAction::QtxAction( const QString& text, const QString& menuText,
00137                       int accel, QObject* parent, bool toggle, const QString& shortcutAction )
00138 : QWidgetAction( parent )
00139 {
00140   setText( menuText );
00141   setToolTip( text );
00142   setShortcut( accel );
00143   setCheckable( toggle );
00144   setShortcutActionName(shortcutAction);
00145   
00146   QApplication::instance()->installEventFilter( this );
00147 }
00148 
00152 QtxAction::~QtxAction()
00153 {
00154 }
00155 
00167 bool QtxAction::eventFilter( QObject* o, QEvent* e )
00168 {
00169   if ( o && o->isWidgetType() )
00170   {
00171     if ( e->type() == QEvent::ActionAdded && ((QActionEvent*)e)->action() == this )
00172       QApplication::postEvent( this, new ActionNotify( true, (QWidget*)o ) );
00173     if ( e->type() == QEvent::ActionRemoved && ((QActionEvent*)e)->action() == this )
00174       QApplication::postEvent( this, new ActionNotify( false, (QWidget*)o ) );
00175   }
00176   return QWidgetAction::eventFilter( o, e );
00177 }
00178 
00188 void QtxAction::addedTo( QWidget* /*w*/ )
00189 {
00190 }
00191 
00201 void QtxAction::removedFrom( QWidget* /*w*/ )
00202 {
00203 }
00204 
00215 void QtxAction::customEvent( QEvent* e )
00216 {
00217   ActionNotify* ae = (ActionNotify*)e;
00218   if ( !ae->widget() )
00219     return;
00220 
00221   if ( ae->isAdded() )
00222     addedTo( ae->widget() );
00223   else
00224     removedFrom( ae->widget() );
00225 }
00226 
00233 QString QtxAction::shortcutActionName() const
00234 {
00235   return myShortcutActionName;
00236 }
00237 
00246 void QtxAction::setShortcutActionName( const QString& shortcutAction )
00247 {
00248   myShortcutActionName = shortcutAction;
00249 }