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