Salome HOME
Merge branch 'BR_2017_PORTING' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_BathymetrySelectionOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <HYDROGUI_BathymetrySelectionOp.h>
21 #include <HYDROGUI_Module.h>
22 #include <HYDROGUI_BathymetryPrs.h>
23 #include <HYDROGUI_BathymetryOp.h>
24 #include <HYDROGUI_ShapeBathymetry.h>
25 #include <HYDROGUI_Operations.h>
26 #include <OCCViewer_ViewManager.h>
27 #include <LightApp_Application.h>
28 #include <QAction>
29 #include <QApplication>
30
31 HYDROGUI_BathymetrySelectionOp::HYDROGUI_BathymetrySelectionOp( HYDROGUI_Module* theModule )
32 : HYDROGUI_Operation( theModule ), myIsActive( false )
33 {
34 }
35
36 HYDROGUI_BathymetrySelectionOp::~HYDROGUI_BathymetrySelectionOp()
37 {
38 }
39
40 void HYDROGUI_BathymetrySelectionOp::startOperation()
41 {
42   activateSelection( true );
43 }
44
45 void HYDROGUI_BathymetrySelectionOp::abortOperation()
46 {
47   activateSelection( false );
48
49   module()->action( BathymetrySelectionId )->setChecked( false );
50   module()->action( BathymetryTextId )->setChecked( false );
51 }
52
53 bool HYDROGUI_BathymetrySelectionOp::isValid( SUIT_Operation* theOtherOp ) const
54 {
55   HYDROGUI_BathymetryOp* aBathOp = dynamic_cast<HYDROGUI_BathymetryOp*>( theOtherOp );
56   return ( aBathOp != 0 );
57 }
58
59 OCCViewer_Viewer* getViewer( HYDROGUI_Module* theModule )
60 {
61   LightApp_Application* app = theModule->getApp();
62   OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>
63     ( app->getViewManager( OCCViewer_Viewer::Type(), true ) );
64   return mgr->getOCCViewer();
65 }
66
67 Handle(AIS_InteractiveContext) getContext( HYDROGUI_Module* theModule )
68 {
69   return getViewer( theModule )->getAISContext();
70 }
71
72 QList<Handle(HYDROGUI_BathymetryPrs)> getShownBathymetries( HYDROGUI_Module* theModule )
73 {
74   QList<Handle(HYDROGUI_BathymetryPrs)> baths;
75   Handle(AIS_InteractiveContext) ctx = getContext( theModule );
76
77   AIS_ListOfInteractive objs;
78   ctx->DisplayedObjects( objs );
79   AIS_ListIteratorOfListOfInteractive it( objs );
80   for( ; it.More(); it.Next() )
81   {
82     Handle(HYDROGUI_BathymetryPrs) bath = Handle(HYDROGUI_BathymetryPrs)::DownCast( it.Value() );
83     if( !bath.IsNull() )
84       baths.append( bath );
85   }
86   return baths;
87 }
88
89 void HYDROGUI_BathymetrySelectionOp::activateSelection( bool isActive )
90 {
91   if( myIsActive==isActive )
92     return;
93
94   getContext( module() )->ClearSelected(true);
95   qApp->setOverrideCursor( Qt::WaitCursor );
96   getContext( module() )->ClearSelected(true);
97   Handle(AIS_InteractiveContext) ctx = getContext( module() );
98   QList<Handle(HYDROGUI_BathymetryPrs)> baths = getShownBathymetries( module() );
99   if( isActive )
100   {
101     const int aSelectionMode = 1;
102     ctx->OpenLocalContext( Standard_True );
103     foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
104     {
105       ctx->Activate( bath, aSelectionMode, Standard_True );
106       bath->SetAutoHilight( Standard_False );
107     }
108     ctx->UpdateCurrentViewer();
109   }
110   else
111   {
112     foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
113     {
114       bath->ClearSelected();
115       bath->SetAutoHilight( Standard_True );
116       bath->GetShape()->TextLabels( false );
117       ctx->Deactivate( bath );
118     }
119     ctx->CloseLocalContext( -1, Standard_True );
120   }
121
122   myIsActive = isActive;
123
124   qApp->restoreOverrideCursor();
125 }