Salome HOME
INT PAL 0052660: Plot2D Viewer: Plot2d_Curve can't be selected
[modules/gui.git] / src / SUIT / SUIT_Accel.cxx
index a72a317e1c0c002cdae784615b34a14451583ca7..dd82975a6e685bade1675775db4c8e5755c4a2e9 100644 (file)
-// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-// 
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
-// version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // SUIT_Accel.cxx: implementation of the SUIT_Accel class.
 //
-//////////////////////////////////////////////////////////////////////
-
 #include "SUIT_Accel.h"
-#include "SUIT_Desktop.h"
-#include "SUIT_ViewManager.h"
+
 #include "SUIT_ViewWindow.h"
+#include "SUIT_ViewManager.h"
 #include "SUIT_ViewModel.h"
 
-#include <qaccel.h>
+#include <QCoreApplication>
+#include <QEvent>
+#include <QKeyEvent>
 
-/*!\class SUIT_Accel
- * Class handles keyboard accelerator bindings.
- */
+/*!
+  \class SUIT_Accel
+  \brief Manager of keyboard accelerator bindings.
+*/
 
-/*! Constructor.*/
-SUIT_Accel::SUIT_Accel(SUIT_Desktop* theDesktop)
-  : QObject( theDesktop, "SUIT_Accel" ),
-    myDesktop( theDesktop )
+SUIT_Accel* SUIT_Accel::myself = 0;
+
+/*! Constructor [private].*/
+SUIT_Accel::SUIT_Accel()
+: QObject( QCoreApplication::instance() )
 {
-  myAccel = new QAccel( theDesktop, "SUIT_Accel_interal_qaccel" );
-  connect( myAccel, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
+  QCoreApplication::instance()->installEventFilter( this );
 }
 
-/*! Destructor.*/
-SUIT_Accel::~SUIT_Accel()
+/*! getAccel() : public interface for SUIT_Accel object.  Only one instance is created and returned. */
+SUIT_Accel* SUIT_Accel::getAccel() 
 {
+  if ( !myself )
+    myself = new SUIT_Accel();
+  return myself;
 }
 
-/*! setActionKey  assign a ceratain action for a key accelerator */
+/*! setActionKey() : assign a ceratain action for a key accelerator */
 void SUIT_Accel::setActionKey( const int action, const int key, const QString& type )
-{    
-  // 1. get or generate interal "id" of action
-  int id = myAccel->findKey( key );
-  if ( id == -1 )
-    id = myAccel->insertItem( key );
-
+{
   IdActionMap idActionMap;
   if ( myMap.contains( type ) )
     idActionMap = myMap[type];
 
-  idActionMap[id] = action;
+  idActionMap[key] = action;
   myMap[type] = idActionMap;
+
+  myOptMap[key] = true;
 }
 
-/*! onActivated  slot called when a registered key accelerator was activated */
-void SUIT_Accel::onActivated( int id )
+/*! unsetActionKey() : unregister a certain key accelerator */
+void SUIT_Accel::unsetActionKey( const int key, const QString& type )
 {
-  if ( myDesktop ) {
-    if ( SUIT_ViewWindow* vw = myDesktop->activeWindow() ) {
-      QString type = vw->getViewManager()->getViewModel()->getType();
-      if (  myMap.contains( type ) ) {
-       IdActionMap idActionMap = myMap[type];
-       if ( idActionMap.contains( id ) ) {
-         vw->onAccelAction( idActionMap[id] );
-       }
-      }
+  if ( myMap.contains( type ) )
+  {
+    IdActionMap idActionMap = myMap[type];
+    if ( idActionMap.contains( key ) )
+    {
+      idActionMap.remove( key );
+      myMap[type] = idActionMap;
     }
   }
 }
 
+/*! getParentViewWindow() : returns given object or any of its parents-grandparents-.. if it is a SUIT_ViewWindow */ 
+SUIT_ViewWindow* getParentViewWindow( const QObject* obj )
+{
+  if ( obj )
+  {
+    if ( obj->inherits( "SUIT_ViewWindow" ) )
+      return (SUIT_ViewWindow*)obj;
+    else
+      return getParentViewWindow( obj->parent() );
+  }
+  return 0;
+}
+
+/*! getKey() : returns integer key code (with modifiers) made of key pressed 'inside' given event */
+int getKey( QKeyEvent* keyEvent )
+{
+  int key = keyEvent->key(), state = keyEvent->modifiers();
+  if ( state & Qt::ShiftModifier )   
+    key += Qt::SHIFT;
+  if ( state & Qt::ControlModifier )
+    key += Qt::CTRL;
+  if ( state & Qt::AltModifier )
+    key += Qt::ALT;
+  if ( state & Qt::MetaModifier )
+    key += Qt::META;
+  return key;
+}
+
+/*! 
+  Returns key pressed if 
+  -# event was KeyPress 
+  -# pressed key is a registered accelerator
+*/
+int SUIT_Accel::getAccelKey( QEvent *event )
+{
+  if ( event && event->type() == QEvent::KeyPress ) {
+    int key = ::getKey( (QKeyEvent*)event );
+    if ( myOptMap.contains( key ) )
+      return key;
+  }
+  return 0;
+}
+
+/*! eventFilter() : filtering ALL events of QApplication. */
+bool SUIT_Accel::eventFilter( QObject *obj, QEvent *event )
+{
+  const int key = getAccelKey( event );
+  if ( key ) {
+    SUIT_ViewWindow* vw = ::getParentViewWindow( obj ); 
+    if ( vw ) {
+      if ( vw->getViewManager() && vw->getViewManager()->getViewModel() )
+      {
+        QString type = vw->getViewManager()->getViewModel()->getType();
+        if ( myMap.contains( type ) ) {
+          IdActionMap idActionMap = myMap[type];
+          if ( idActionMap.contains( key ) ) {
+            return vw->onAccelAction( idActionMap[key] );
+          }
+        }
+      }
+    }
+  }
+  return false;
+}