Salome HOME
refs #500: regression in bathymetry show in OCCT
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_GeoreferencementOp.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_GeoreferencementOp.h"
24
25 #include "HYDROGUI_GeoreferencementDlg.h"
26 #include "HYDROGUI_DataModel.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <HYDROData_Profile.h>
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_Entity.h>
34
35 #include <LightApp_Application.h>
36 #include <LightApp_UpdateFlags.h>
37
38 #include <SUIT_Desktop.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_ViewWindow.h>
41
42 #include <OCCViewer_ViewManager.h>
43
44 #include <gp_XY.hxx>
45
46 HYDROGUI_GeoreferencementOp::HYDROGUI_GeoreferencementOp( HYDROGUI_Module* theModule, const int theInitialMode )
47 : HYDROGUI_Operation( theModule ),
48   myInitialMode( theInitialMode )
49 {
50   setName( tr( "PROFILES_GEOREFERENCEMENT" ) );
51 }
52
53 HYDROGUI_GeoreferencementOp::~HYDROGUI_GeoreferencementOp()
54 {
55 }
56
57 void HYDROGUI_GeoreferencementOp::startOperation()
58 {
59   HYDROGUI_Operation::startOperation();
60
61   HYDROGUI_GeoreferencementDlg* aPanel = 
62     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
63   if ( !aPanel ) {
64     return;
65   }
66
67   aPanel->reset();
68
69   if ( myInitialMode == All ) {
70     onModeActivated( HYDROGUI_GeoreferencementDlg::AllProfiles );
71   } else if ( myInitialMode == Selected ) {
72     onModeActivated( HYDROGUI_GeoreferencementDlg::SelectedProfiles );
73   }
74
75   LightApp_Application* anApp = module()->getApp();
76   OCCViewer_ViewManager* aViewManager =
77     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), false ) );
78   if ( aViewManager ) {
79     connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
80              aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ), 
81              Qt::UniqueConnection );
82   }
83
84   connect( anApp->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
85            this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ), Qt::UniqueConnection );
86 }
87
88 void HYDROGUI_GeoreferencementOp::abortOperation()
89 {
90   LightApp_Application* anApp = module()->getApp();
91   if ( anApp && anApp->desktop() ) {
92     anApp->desktop()->disconnect( this );
93   }
94
95   HYDROGUI_Operation::abortOperation();
96 }
97
98 void HYDROGUI_GeoreferencementOp::commitOperation()
99 {
100   LightApp_Application* anApp = module()->getApp();
101   if ( anApp && anApp->desktop() ) {
102     anApp->desktop()->disconnect( this );
103   }
104
105   HYDROGUI_Operation::commitOperation();
106 }
107
108 HYDROGUI_InputPanel* HYDROGUI_GeoreferencementOp::createInputPanel() const
109 {
110   HYDROGUI_InputPanel* aPanel = new HYDROGUI_GeoreferencementDlg( module(), getName() );
111   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
112   connect( aPanel, SIGNAL( updateSelection() ), SLOT( onUpdateSelection() ) );
113
114   return aPanel;
115 }
116
117 bool HYDROGUI_GeoreferencementOp::processApply( int& theUpdateFlags,
118                                                 QString& theErrorMsg,
119                                                 QStringList& theBrowseObjectsEntries )
120 {
121   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
122
123   return store( theErrorMsg );
124 }
125
126 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
127 {
128   QString anErrorMsg;
129
130   // Get the panel
131   HYDROGUI_GeoreferencementDlg* aPanel = 
132     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
133   if ( !aPanel ) {
134     return;
135   }
136
137   aPanel->setMode( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ?
138                    HYDROGUI_GeoreferencementDlg::SelectedProfiles :
139                    HYDROGUI_GeoreferencementDlg::AllProfiles);
140
141   // Store the dialog data to the data model
142   bool isToStore = false;
143   // Check if modifications exists
144   if ( aPanel->isModified() ) {
145     // Show confirmation dialog
146     SUIT_MessageBox::StandardButtons aButtons = 
147       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
148
149     isToStore = SUIT_MessageBox::question( module()->getApp()->desktop(),
150                                            tr( "CONFIRMATION" ),
151                                            tr( "CONFIRM_STORE" ),
152                                            aButtons, 
153                                            SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
154   }
155   // Store modifications if needed
156   if ( isToStore )
157   {
158     startDocOperation();
159     if ( !store( anErrorMsg ) )
160     {
161       abortDocOperation();
162
163       anErrorMsg.append( "\n" + tr( "INPUT_VALID_DATA" ) );
164       SUIT_MessageBox::critical( module()->getApp()->desktop(),
165                                  tr( "INSUFFICIENT_INPUT_DATA" ),
166                                  anErrorMsg );
167       return;
168     }
169     else
170     {
171       commitDocOperation();
172     }
173   }
174
175   aPanel->setMode( theActualMode );
176
177   // Get the list of profiles
178   HYDROData_SequenceOfObjects aSeqOfProfiles;
179   
180   if( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ) {
181     HYDROData_Iterator aProfilesIter( doc(), KIND_PROFILE );
182     while ( aProfilesIter.More() ) {
183       aSeqOfProfiles.Append( aProfilesIter.Current() );
184       aProfilesIter.Next();
185     }
186   } else if ( theActualMode == HYDROGUI_GeoreferencementDlg::SelectedProfiles ) {
187     aSeqOfProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
188   }
189
190   // Set the profiles to the panel
191   setPanelData( aSeqOfProfiles );
192 }
193
194 void HYDROGUI_GeoreferencementOp::onWindowActivated( SUIT_ViewWindow* theViewWindow )
195 {
196   if ( !theViewWindow ) {
197     return;
198   }
199
200   OCCViewer_ViewManager* aViewManager =
201     dynamic_cast<OCCViewer_ViewManager*>( theViewWindow->getViewManager() );
202
203   if ( aViewManager ) {
204     HYDROGUI_GeoreferencementDlg* aPanel = 
205       ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
206     if ( aPanel ) {
207       connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
208                aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
209                Qt::UniqueConnection);
210     }
211   }
212 }
213
214 bool HYDROGUI_GeoreferencementOp::store( QString& theErrorMsg )
215 {
216   // Clear the error string
217   theErrorMsg.clear();
218   
219   HYDROGUI_Module* aModule = module();
220
221   // Get the panel
222   HYDROGUI_GeoreferencementDlg* aPanel = 
223     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
224   if ( !aPanel ) {
225     return false;
226   }
227
228   // Get georeferencement data from the panel
229   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aGeoDataList;
230   aPanel->getData( aGeoDataList );
231   if ( aGeoDataList.empty() ) {
232     return true;
233   }
234
235   // Check the data validity
236   foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
237     if ( aGeoData.isIncomplete ) {
238       theErrorMsg = tr( "INCOMPLETE_DATA" ).arg( aGeoData.Name );
239       return false;
240     }
241   }
242
243   // Store the data in the data model
244   foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
245     Handle(HYDROData_Profile) aProfile = 
246       Handle(HYDROData_Profile)::DownCast(
247         HYDROGUI_Tool::FindObjectByName( module(), aGeoData.Name, KIND_PROFILE ) );
248     if ( !aProfile.IsNull() ) {
249       if ( !aGeoData.isEmpty ) {
250         if ( !aProfile->IsValid() ) // Show the profile after it became valid
251           aModule->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( aModule ), aProfile, true );
252
253         aProfile->SetLeftPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ), false );
254         aProfile->SetRightPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ), false );
255       } else {
256         aProfile->Invalidate();
257         aModule->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( aModule ), aProfile, false );
258       }
259
260       aProfile->Update();
261     }
262   }
263
264   aModule->update( UF_Model | UF_OCCViewer | UF_OCC_Forced );
265
266   return true;
267 }
268
269 void HYDROGUI_GeoreferencementOp::onUpdateSelection()
270 {
271   // Get the selected profiles
272   HYDROData_SequenceOfObjects aSeqOfProfiles = 
273     HYDROGUI_Tool::GetSelectedObjects( module() );
274
275   // Set them to the dialog
276   setPanelData( aSeqOfProfiles );
277 }
278
279 void HYDROGUI_GeoreferencementOp::setPanelData( 
280   const HYDROData_SequenceOfObjects& theProfiles )
281 {
282   // Get the panel
283   HYDROGUI_GeoreferencementDlg* aPanel = 
284     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
285   if ( !aPanel ) {
286     return;
287   }
288
289   // Get georeferencement data from the data model
290   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aData;
291
292   HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
293   for ( ; anIter.More(); anIter.Next() ) {
294     Handle(HYDROData_Profile) aProfile =
295         Handle(HYDROData_Profile)::DownCast( anIter.Value() );
296     if ( aProfile.IsNull() ) {
297       continue;
298     }
299
300     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData( aProfile->GetName() );
301
302     gp_XY aFirstPoint, aLastPoint;
303     if ( aProfile->GetLeftPoint( aFirstPoint, false ) &&
304          aProfile->GetRightPoint( aLastPoint, false ) ) {
305       aGeoData = 
306         HYDROGUI_GeoreferencementDlg::ProfileGeoData( aGeoData.Name, 
307                                                       aFirstPoint.X(), aFirstPoint.Y(),
308                                                       aLastPoint.X(), aLastPoint.Y() );
309     }
310    
311     aData.append( aGeoData );
312   }
313
314   // Set the collected data to the dialog
315   aPanel->setData( aData );
316 }