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