Salome HOME
refs #1326: unchecking the button on operation finish
[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 }
50
51 bool HYDROGUI_BathymetrySelectionOp::isValid( SUIT_Operation* theOtherOp ) const
52 {
53   HYDROGUI_BathymetryOp* aBathOp = dynamic_cast<HYDROGUI_BathymetryOp*>( theOtherOp );
54   return ( aBathOp != 0 );
55 }
56
57 OCCViewer_Viewer* getViewer( HYDROGUI_Module* theModule )
58 {
59   LightApp_Application* app = theModule->getApp();
60   OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>
61     ( app->getViewManager( OCCViewer_Viewer::Type(), true ) );
62   return mgr->getOCCViewer();
63 }
64
65 Handle(AIS_InteractiveContext) getContext( HYDROGUI_Module* theModule )
66 {
67   return getViewer( theModule )->getAISContext();
68 }
69
70 QList<Handle(HYDROGUI_BathymetryPrs)> getShownBathymetries( HYDROGUI_Module* theModule )
71 {
72   QList<Handle(HYDROGUI_BathymetryPrs)> baths;
73   Handle(AIS_InteractiveContext) ctx = getContext( theModule );
74
75   AIS_ListOfInteractive objs;
76   ctx->DisplayedObjects( objs );
77   AIS_ListIteratorOfListOfInteractive it( objs );
78   for( ; it.More(); it.Next() )
79   {
80     Handle(HYDROGUI_BathymetryPrs) bath = Handle(HYDROGUI_BathymetryPrs)::DownCast( it.Value() );
81     if( !bath.IsNull() )
82       baths.append( bath );
83   }
84   return baths;
85 }
86
87 void HYDROGUI_BathymetrySelectionOp::activateSelection( bool isActive )
88 {
89   if( myIsActive==isActive )
90     return;
91
92   Handle(AIS_InteractiveContext) ctx = getContext( module() );
93   QList<Handle(HYDROGUI_BathymetryPrs)> baths = getShownBathymetries( module() );
94   if( isActive )
95   {
96     const int aSelectionMode = 1;
97     ctx->OpenLocalContext( Standard_True );
98     foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
99     {
100       ctx->Activate( bath, aSelectionMode, Standard_True );
101       bath->SetAutoHilight( Standard_False );
102     }
103     ctx->UpdateCurrentViewer();
104   }
105   else
106   {
107     foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
108     {
109       bath->ClearSelected();
110       bath->SetAutoHilight( Standard_True );
111       bath->GetShape()->TextLabels( false );
112       ctx->Deactivate( bath );
113     }
114     ctx->CloseLocalContext( -1, Standard_True );
115   }
116
117   myIsActive = isActive;
118 }