Salome HOME
526c2c218cc6297754b470b4729cdd1c107afb16
[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
30 HYDROGUI_BathymetrySelectionOp::HYDROGUI_BathymetrySelectionOp( HYDROGUI_Module* theModule )
31 : HYDROGUI_Operation( theModule ), myIsActive( false )
32 {
33 }
34
35 HYDROGUI_BathymetrySelectionOp::~HYDROGUI_BathymetrySelectionOp()
36 {
37 }
38
39 void HYDROGUI_BathymetrySelectionOp::startOperation()
40 {
41   activateSelection( true );
42 }
43
44 void HYDROGUI_BathymetrySelectionOp::abortOperation()
45 {
46   activateSelection( false );
47
48   module()->action( BathymetrySelectionId )->setChecked( false );
49   module()->action( BathymetryTextId )->setChecked( false );
50 }
51
52 bool HYDROGUI_BathymetrySelectionOp::isValid( SUIT_Operation* theOtherOp ) const
53 {
54   HYDROGUI_BathymetryOp* aBathOp = dynamic_cast<HYDROGUI_BathymetryOp*>( theOtherOp );
55   return ( aBathOp != 0 );
56 }
57
58 OCCViewer_Viewer* getViewer( HYDROGUI_Module* theModule )
59 {
60   LightApp_Application* app = theModule->getApp();
61   OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>
62     ( app->getViewManager( OCCViewer_Viewer::Type(), true ) );
63   return mgr->getOCCViewer();
64 }
65
66 Handle(AIS_InteractiveContext) getContext( HYDROGUI_Module* theModule )
67 {
68   return getViewer( theModule )->getAISContext();
69 }
70
71 QList<Handle(HYDROGUI_BathymetryPrs)> getShownBathymetries( HYDROGUI_Module* theModule )
72 {
73   QList<Handle(HYDROGUI_BathymetryPrs)> baths;
74   Handle(AIS_InteractiveContext) ctx = getContext( theModule );
75
76   AIS_ListOfInteractive objs;
77   ctx->DisplayedObjects( objs );
78   AIS_ListIteratorOfListOfInteractive it( objs );
79   for( ; it.More(); it.Next() )
80   {
81     Handle(HYDROGUI_BathymetryPrs) bath = Handle(HYDROGUI_BathymetryPrs)::DownCast( it.Value() );
82     if( !bath.IsNull() )
83       baths.append( bath );
84   }
85   return baths;
86 }
87
88 void HYDROGUI_BathymetrySelectionOp::activateSelection( bool isActive )
89 {
90   if( myIsActive==isActive )
91     return;
92
93   getContext( module() )->ClearSelected();
94
95   Handle(AIS_InteractiveContext) ctx = getContext( module() );
96   QList<Handle(HYDROGUI_BathymetryPrs)> baths = getShownBathymetries( module() );
97   if( isActive )
98   {
99     const int aSelectionMode = 1;
100     ctx->OpenLocalContext( Standard_True );
101     foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
102     {
103       ctx->Activate( bath, aSelectionMode, Standard_True );
104       bath->SetAutoHilight( Standard_False );
105     }
106     ctx->UpdateCurrentViewer();
107   }
108   else
109   {
110     foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
111     {
112       bath->ClearSelected();
113       bath->SetAutoHilight( Standard_True );
114       bath->GetShape()->TextLabels( false );
115       ctx->Deactivate( bath );
116     }
117     ctx->CloseLocalContext( -1, Standard_True );
118   }
119
120   myIsActive = isActive;
121 }