Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_2017' into BR_2017_PORTING
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_BathymetryOp.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_BathymetryOp.h>
21 #include <HYDROGUI_Operations.h>
22 #include <HYDROGUI_BathymetryPrs.h>
23 #include <HYDROGUI_ShapeBathymetry.h>
24 #include <HYDROGUI_Module.h>
25 #include <HYDROGUI_OCCDisplayer.h>
26 #include <LightApp_Application.h>
27 #include <OCCViewer_ViewManager.h>
28 #include <OCCViewer_ViewWindow.h>
29 #include <QtxDoubleSpinBox.h>
30 #include <SUIT_Desktop.h>
31 #include <QLayout>
32 #include <QPushButton>
33 #include <QApplication>
34
35 HYDROGUI_BathymetryLimitsDlg::HYDROGUI_BathymetryLimitsDlg( QWidget* theParent )
36   : QDialog( theParent )
37 {
38   QGridLayout* layout = new QGridLayout();
39   setLayout( layout );
40
41   layout->addWidget( new QLabel( tr( "Min value" ) ), 0, 0 );
42   layout->addWidget( new QLabel( tr( "Max value" ) ), 1, 0 );
43
44   myMin = new QtxDoubleSpinBox( this );
45   myMin->setRange( -1000, 1000 );
46   myMax = new QtxDoubleSpinBox( this );
47   myMax->setRange( -1000, 1000 );
48
49   layout->addWidget( myMin, 0, 1 );
50   layout->addWidget( myMax, 1, 1 );
51
52   QFrame* aBtnFrame = new QFrame( this );
53   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnFrame );
54   layout->addWidget( aBtnFrame, 2, 0, 1, 2 );
55
56   QPushButton* ok = new QPushButton( tr( "OK" ), this );
57   QPushButton* cancel = new QPushButton( tr( "Cancel" ), this );
58   aBtnLayout->addWidget( ok );
59   aBtnLayout->addWidget( cancel );
60
61   connect( ok, SIGNAL( clicked() ), this, SLOT( accept() ) );
62   connect( cancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
63 }
64
65 HYDROGUI_BathymetryLimitsDlg::~HYDROGUI_BathymetryLimitsDlg()
66 {
67 }
68
69 double HYDROGUI_BathymetryLimitsDlg::GetMin() const
70 {
71   return myMin->value();
72 }
73
74 double HYDROGUI_BathymetryLimitsDlg::GetMax() const
75 {
76   return myMax->value();
77 }
78
79 void HYDROGUI_BathymetryLimitsDlg::SetMinMax( double theMin, double theMax )
80 {
81   myMin->setValue( theMin );
82   myMax->setValue( theMax );
83 }
84
85
86
87
88
89 HYDROGUI_BathymetryOp::HYDROGUI_BathymetryOp( HYDROGUI_Module* theModule, int theMode )
90 : HYDROGUI_Operation( theModule ), myMode( theMode ), myIsActivate( false )
91 {
92 }
93
94 HYDROGUI_BathymetryOp::~HYDROGUI_BathymetryOp()
95 {
96 }
97
98 OCCViewer_Viewer* getViewer( HYDROGUI_Module* theModule );
99 Handle(AIS_InteractiveContext) getContext( HYDROGUI_Module* theModule );
100 QList<Handle(HYDROGUI_BathymetryPrs)> getShownBathymetries( HYDROGUI_Module* theModule );
101
102 void HYDROGUI_BathymetryOp::startOperation()
103 {
104   activate( true );
105 }
106
107 void HYDROGUI_BathymetryOp::commitOperation()
108 {
109   if( myMode!=BathymetryTextId )
110     activate( false );
111 }
112
113 void HYDROGUI_BathymetryOp::abortOperation()
114 {
115   activate( false );
116 }
117
118 OCCViewer_ViewWindow* HYDROGUI_BathymetryOp::activeViewWindow() const
119 {
120   LightApp_Application* app = module()->getApp();
121   OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>
122     ( app->getViewManager( OCCViewer_Viewer::Type(), true ) );
123   return dynamic_cast<OCCViewer_ViewWindow*>( mgr->getActiveView() );
124 }
125
126 void HYDROGUI_BathymetryOp::activate( bool isActivate )
127 {
128   if( myIsActivate==isActivate )
129     return;
130
131   myIsActivate = isActivate;
132   QList<Handle(HYDROGUI_BathymetryPrs)> baths = getShownBathymetries( module() );
133   bool isUpdateCS = false;
134
135   if( myMode!=BathymetryRescaleUserId )
136     qApp->setOverrideCursor( Qt::WaitCursor );
137
138   switch( myMode )
139   {
140   case BathymetryTextId:
141     {
142       foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
143         bath->GetShape()->TextLabels( isActivate );
144       commit();
145       break;
146     }
147
148   case BathymetryRescaleSelectionId:
149     {
150       if( isActivate )
151       {
152         foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
153           bath->GetShape()->RescaleBySelection();
154         isUpdateCS = true;
155       }
156       commit();
157       break;
158     }
159
160   case BathymetryRescaleVisibleId:
161     {
162       if( isActivate )
163       {
164         foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
165           bath->GetShape()->RescaleByVisible( activeViewWindow() );
166         isUpdateCS = true;
167       }
168       commit();
169       break;
170     }
171
172   case BathymetryRescaleUserId:
173     {
174       if( isActivate )
175       {
176         double min=0, max=0, lmin=0, lmax=0;
177         bool first = true;
178         foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
179         {
180           bath->GetShape()->GetRange( lmin, lmax );
181           if( first || lmin < min )
182             min = lmin;
183           if( first || lmax > max )
184             max = lmax;
185           first = false;
186         }
187
188         HYDROGUI_BathymetryLimitsDlg dlg( module()->getApp()->desktop() );
189         dlg.SetMinMax( min, max );
190         if( dlg.exec()==QDialog::Accepted )
191         {
192           qApp->setOverrideCursor( Qt::WaitCursor );
193
194           min = dlg.GetMin();
195           max = dlg.GetMax();
196           foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
197             bath->GetShape()->Rescale( min, max );
198
199           isUpdateCS = true;
200           commit();
201         }
202         else
203           abort();
204         break;
205       }
206     }
207
208   case BathymetryRescaleDefaultId:
209     {
210       if( isActivate )
211       {
212         foreach( Handle(HYDROGUI_BathymetryPrs) bath, baths )
213           bath->GetShape()->RescaleDefault();
214         isUpdateCS = true;
215       }
216       commit();
217       break;
218     }
219   }
220
221   if( isUpdateCS )
222     module()->getOCCDisplayer()->UpdateColorScale( getViewer( module() ) );
223
224   qApp->restoreOverrideCursor();
225 }