1 // Copyright (C) 2007-2016 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 // GEOM GEOMGUI : GUI for Geometry component
24 // File : MeasureGUI_AnnotationInteractor.cxx
25 // Author : Anton POLETAEV, Open CASCADE S.A.S.
27 #include "MeasureGUI_AnnotationInteractor.h"
30 #include <GeometryGUI.h>
31 #include <OCCViewer_ViewManager.h>
32 #include <OCCViewer_ViewPort3d.h>
33 #include <OCCViewer_ViewWindow.h>
34 #include <SUIT_ViewManager.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_Desktop.h>
37 #include <SalomeApp_Application.h>
40 #include <QMouseEvent>
43 #include <V3d_View.hxx>
45 //=================================================================================
46 // function : Constructor
48 //=================================================================================
49 MeasureGUI_AnnotationInteractor::MeasureGUI_AnnotationInteractor( GeometryGUI* theGUI,
51 : QObject( theParent ),
56 myActiveViewPort( NULL )
60 //=================================================================================
61 // function : Deactivate
63 //=================================================================================
64 MeasureGUI_AnnotationInteractor::~MeasureGUI_AnnotationInteractor()
66 if ( myActiveViewPort )
68 myActiveViewPort->releaseMouse();
69 myActiveViewPort = NULL;
75 //=================================================================================
77 // purpose : Enables event processing and interaction handlers.
78 //=================================================================================
79 void MeasureGUI_AnnotationInteractor::Enable()
88 // install event filtering on viewer windows
89 SalomeApp_Application* anApp = myGeomGUI->getApp();
95 myVM = (OCCViewer_ViewManager*) anApp->getViewManager( OCCViewer_Viewer::Type(), false );
96 myViewer = (OCCViewer_Viewer*) myVM->getViewModel();
97 if ( !myVM || !myViewer )
102 connect( myVM, SIGNAL( viewCreated( SUIT_ViewWindow* ) ), SLOT( OnViewCreated( SUIT_ViewWindow* ) ) );
103 connect( myVM, SIGNAL( deleteView ( SUIT_ViewWindow* ) ), SLOT( OnViewRemoved( SUIT_ViewWindow* ) ) );
105 QVector<SUIT_ViewWindow*> aViews = myVM->getViews();
106 QVector<SUIT_ViewWindow*>::iterator aViewIt = aViews.begin();
107 for ( ; aViewIt != aViews.end(); ++aViewIt )
109 ConnectView( *aViewIt );
113 //=================================================================================
114 // function : Disable
115 // purpose : Disables event processing and interaction handlers.
116 //=================================================================================
117 void MeasureGUI_AnnotationInteractor::Disable()
126 // remove event filtering from viewer windows
127 QVector<SUIT_ViewWindow*> aViews = myVM->getViews();
128 QVector<SUIT_ViewWindow*>::iterator aViewIt = aViews.begin();
129 for ( ; aViewIt != aViews.end(); ++aViewIt )
131 DisconnectView( *aViewIt );
134 if ( myActiveViewPort )
136 myActiveViewPort->releaseMouse();
137 myActiveViewPort = NULL;
140 if ( !myActiveIO.IsNull() )
142 emit SignalInteractionFinished( myActiveIO );
143 myActiveIO.Nullify();
147 //=================================================================================
148 // function : ConnectView
149 // purpose : Connect interactor's event handler to the view window given.
150 //=================================================================================
151 void MeasureGUI_AnnotationInteractor::ConnectView( SUIT_ViewWindow* theView )
153 ( (OCCViewer_ViewWindow*) theView )->getViewPort()->installEventFilter( this );
156 //=================================================================================
157 // function : DisconnectView
158 // purpose : Disconnect interactor's event handler from the view window given.
159 //=================================================================================
160 void MeasureGUI_AnnotationInteractor::DisconnectView( SUIT_ViewWindow* theView )
162 ( (OCCViewer_ViewWindow*) theView )->getViewPort()->removeEventFilter( this );
165 //=================================================================================
166 // function : OnViewCreated
167 // purpose : Handler for signal coming from GUI layer.
168 //=================================================================================
169 void MeasureGUI_AnnotationInteractor::OnViewCreated( SUIT_ViewWindow* theView )
171 ConnectView( theView );
174 //=================================================================================
175 // function : OnViewRemoved
176 // purpose : Handler for signal coming from GUI layer.
177 //=================================================================================
178 void MeasureGUI_AnnotationInteractor::OnViewRemoved( SUIT_ViewWindow* theView )
180 DisconnectView( theView );
183 //=================================================================================
184 // function : eventFilter
185 // purpose : Hooks and process events from OCCT viewer prior to their coming into the base viewer class.
186 //=================================================================================
187 bool MeasureGUI_AnnotationInteractor::eventFilter( QObject* theObject, QEvent* theEvent )
189 OCCViewer_ViewPort3d* aViewPort = (OCCViewer_ViewPort3d*)theObject;
191 const Handle(V3d_View) aView3d = aViewPort->getView();
193 switch( theEvent->type() )
195 // ------------------------------------------------------------------------
196 // Start dragging ("grab") event
197 // ------------------------------------------------------------------------
198 case QEvent::MouseButtonPress :
200 QMouseEvent* aMouseEv = dynamic_cast<QMouseEvent*>( theEvent );
202 if ( myEditEntry.isEmpty() )
207 if ( !( aMouseEv->buttons() & Qt::LeftButton ) )
212 const Handle(AIS_InteractiveContext) anAISContext = myViewer->getAISContext();
214 anAISContext->MoveTo( aMouseEv->x(), aMouseEv->y(), aView3d, Standard_True );
216 if ( !anAISContext->HasDetected() )
221 const Handle(SelectMgr_EntityOwner) aDetected = anAISContext->DetectedOwner();
223 if( aDetected.IsNull() )
228 const Handle(GEOM_Annotation) aAnnotation =
229 Handle(GEOM_Annotation)::DownCast( aDetected->Selectable() );
231 if ( aAnnotation.IsNull() )
236 const Handle(SALOME_InteractiveObject) anIO =
237 Handle(SALOME_InteractiveObject)::DownCast( aAnnotation->GetOwner() );
239 if ( anIO.IsNull() || anIO->getEntry() != myEditEntry )
244 myStartPoint = aMouseEv->pos();
247 for ( anAISContext->InitSelected(); anAISContext->MoreSelected(); anAISContext->NextSelected() )
249 mySelection.Append( anAISContext->SelectedOwner() );
252 anAISContext->ClearSelected( Standard_False );
253 anAISContext->Unhilight( myActiveIO, Standard_True );
255 myActiveViewPort = aViewPort;
256 myActiveViewPort->grabMouse();
257 myActiveIO = aAnnotation;
258 myActiveIO->BeginDrag();
260 emit SignalInteractionStarted( myActiveIO );
265 // ------------------------------------------------------------------------
266 // Perform dragging operation
267 // ------------------------------------------------------------------------
268 case QEvent::MouseMove :
270 QMouseEvent* aMouseEv = (QMouseEvent*) theEvent;
272 if ( !myActiveIO.IsNull() )
274 const Handle(AIS_InteractiveContext) anAISContext = myViewer->getAISContext();
276 if ( anAISContext->IsHilighted( Handle(AIS_InteractiveObject)::DownCast( myActiveIO ) ) )
278 anAISContext->Unhilight( myActiveIO, Standard_False );
281 const QPoint aDelta = aMouseEv->pos() - myStartPoint;
282 myActiveIO->Drag( aDelta.x(), (-aDelta.y()), aView3d );
283 anAISContext->Update( myActiveIO, Standard_False );
284 anAISContext->UpdateCurrentViewer();
291 // ------------------------------------------------------------------------
292 // Stop dragging operation
293 // ------------------------------------------------------------------------
294 case QEvent::FocusOut :
295 case QEvent::MouseButtonRelease :
297 QMouseEvent* aMouseEv = (QMouseEvent*) theEvent;
299 if ( myActiveViewPort )
301 myActiveViewPort->releaseMouse();
302 myActiveViewPort = NULL;
304 if ( !myActiveIO.IsNull() )
306 myActiveIO->EndDrag();
308 const Handle(AIS_InteractiveContext) anAISContext = myViewer->getAISContext();
310 anAISContext->ClearSelected( Standard_False );
311 SelectMgr_SequenceOfOwner::Iterator anIt( mySelection );
312 for( ; anIt.More(); anIt.Next() )
314 anAISContext->AddOrRemoveSelected( anIt.Value(), Standard_False );
317 anAISContext->Update( myActiveIO, Standard_False );
318 anAISContext->UpdateCurrentViewer();
319 anAISContext->MoveTo( aMouseEv->pos().x(), aMouseEv->pos().y(), aView3d, Standard_True );
321 emit SignalInteractionFinished( myActiveIO );
324 myActiveIO.Nullify();
325 return (theEvent->type() == QEvent::MouseButtonRelease);
331 default: return false;