Salome HOME
updated copyright message
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewportInputFilter.cxx
1 // Copyright (C) 2007-2023  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 #include "OCCViewer_ViewportInputFilter.h"
24 #include "OCCViewer_ViewManager.h"
25 #include "OCCViewer_ViewModel.h"
26 #include "OCCViewer_ViewWindow.h"
27 #include "OCCViewer_ViewPort3d.h"
28
29 #include <QMouseEvent>
30 #include <QKeyEvent>
31
32 /*!
33   \brief Constructor.
34   \param theVM [in] the view manager to embed the filter into.
35   \param theParent [in] the parent object.
36 */
37 OCCViewer_ViewportInputFilter::OCCViewer_ViewportInputFilter( OCCViewer_ViewManager* theVM,
38                                                               QObject* theParent )
39 : QObject( theParent ),
40   myVM( theVM )
41 {
42 }
43
44 /*!
45   \brief Destructor.
46 */
47 OCCViewer_ViewportInputFilter::~OCCViewer_ViewportInputFilter()
48 {
49   setEnabled( false );
50 }
51
52 /*!
53   \brief Enables or disables event processing within the viewer.
54 */
55 void OCCViewer_ViewportInputFilter::setEnabled(const bool theIsEnabled)
56 {
57   if ( theIsEnabled == myIsEnabled )
58   {
59     return;
60   }
61
62   if ( theIsEnabled )
63   {
64     // install event filtering on viewer windows
65     myViewer = (OCCViewer_Viewer*) myVM->getViewModel();
66     if ( !myVM || !myViewer )
67     {
68       return;
69     }
70
71     connect( myVM, SIGNAL( viewCreated( SUIT_ViewWindow* ) ), SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
72     connect( myVM, SIGNAL( deleteView ( SUIT_ViewWindow* ) ), SLOT( onViewRemoved( SUIT_ViewWindow* ) ) );
73
74     QVector<SUIT_ViewWindow*>           aViews  = myVM->getViews();
75     QVector<SUIT_ViewWindow*>::iterator aViewIt = aViews.begin();
76     for ( ; aViewIt != aViews.end(); ++aViewIt )
77     {
78       connectView( *aViewIt );
79     }
80   }
81   else
82   {
83     // remove event filtering from viewer windows
84     QVector<SUIT_ViewWindow*>           aViews  = myVM->getViews();
85     QVector<SUIT_ViewWindow*>::iterator aViewIt = aViews.begin();
86     for ( ; aViewIt != aViews.end(); ++aViewIt )
87     {
88       disconnectView( *aViewIt );
89     }
90   }
91
92   myIsEnabled = theIsEnabled;
93 }
94
95 /*!
96   \brief Base-level implementation of event filtering.
97          Routes user input events to associated implementation methods.
98   \param theObject [in] the filtered object.
99   \param theEvent [in] the incoming event.
100 */
101 bool OCCViewer_ViewportInputFilter::eventFilter( QObject* theObject, QEvent* theEvent )
102 {
103   OCCViewer_ViewPort3d* aViewPort = (OCCViewer_ViewPort3d*) theObject;
104
105   if ( !aViewPort )
106   {
107     return false;
108   }
109
110   switch ( theEvent->type() )
111   {
112     case QEvent::MouseMove           : return mouseMove( (QMouseEvent*)theEvent, aViewPort );
113     case QEvent::MouseButtonPress    : return mousePress( (QMouseEvent*)theEvent, aViewPort );
114     case QEvent::MouseButtonRelease  : return mouseRelease( (QMouseEvent*)theEvent, aViewPort );
115     case QEvent::MouseButtonDblClick : return mouseDoubleClick( (QMouseEvent*)theEvent, aViewPort );
116     case QEvent::KeyPress            : return keyPress( (QKeyEvent*)theEvent, aViewPort );
117     case QEvent::KeyRelease          : return keyRelease( (QKeyEvent*)theEvent, aViewPort );
118     default :
119       return false;
120   }
121 }
122
123 /*!
124   \brief Connects view to event processing.
125   \param theView [in] the view to connect.
126 */
127 void OCCViewer_ViewportInputFilter::connectView( SUIT_ViewWindow* theView )
128 {
129   ( (OCCViewer_ViewWindow*) theView )->getViewPort()->installEventFilter( this );
130 }
131
132 /*!
133   \brief Disconnects view to event processing.
134   \param theView [in] the view to disconnect.
135 */
136 void OCCViewer_ViewportInputFilter::disconnectView( SUIT_ViewWindow* theView )
137 {
138   ( (OCCViewer_ViewWindow*) theView )->getViewPort()->removeEventFilter( this );
139 }
140
141 /*!
142   \brief Connects newly created within viewer to event processing.
143   \param theView [in] the view to connect.
144 */
145 void OCCViewer_ViewportInputFilter::onViewCreated( SUIT_ViewWindow* theView )
146 {
147   connectView( theView );
148 }
149
150 /*!
151   \brief Disconnects view being removed from viewer.
152   \param theView [in] the view to disconnect.
153 */
154 void OCCViewer_ViewportInputFilter::onViewRemoved( SUIT_ViewWindow* theView )
155 {
156   disconnectView( theView );
157 }