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