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