Salome HOME
debug of DTM object
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_GeoreferencementOp.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 //
18
19 #include "HYDROGUI_GeoreferencementOp.h"
20
21 #include "HYDROGUI_GeoreferencementDlg.h"
22 #include "HYDROGUI_DataModel.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_Tool2.h"
25 #include "HYDROGUI_UpdateFlags.h"
26
27 #include <HYDROData_Profile.h>
28 #include <HYDROData_Iterator.h>
29 #include <HYDROData_Entity.h>
30
31 #include <LightApp_Application.h>
32 #include <LightApp_UpdateFlags.h>
33
34 #include <SUIT_Desktop.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_ViewWindow.h>
37
38 #include <OCCViewer_ViewManager.h>
39
40 #include <gp_XY.hxx>
41
42 HYDROGUI_GeoreferencementOp::HYDROGUI_GeoreferencementOp( HYDROGUI_Module* theModule, const int theInitialMode )
43 : HYDROGUI_Operation( theModule ),
44   myInitialMode( theInitialMode )
45 {
46   setName( tr( "PROFILES_GEOREFERENCEMENT" ) );
47 }
48
49 HYDROGUI_GeoreferencementOp::~HYDROGUI_GeoreferencementOp()
50 {
51 }
52
53 void HYDROGUI_GeoreferencementOp::startOperation()
54 {
55   HYDROGUI_Operation::startOperation();
56
57   HYDROGUI_GeoreferencementDlg* aPanel = 
58     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
59   if ( !aPanel ) {
60     return;
61   }
62
63   aPanel->reset();
64   setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
65                        module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
66   setCursor();
67
68   if ( myInitialMode == All ) {
69     onModeActivated( HYDROGUI_GeoreferencementDlg::AllProfiles );
70   } else if ( myInitialMode == Selected ) {
71     onModeActivated( HYDROGUI_GeoreferencementDlg::SelectedProfiles );
72   }
73
74   LightApp_Application* anApp = module()->getApp();
75   OCCViewer_ViewManager* aViewManager =
76     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), false ) );
77   if ( aViewManager ) {
78     connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
79              aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ), 
80              Qt::UniqueConnection );
81   }
82
83   connect( anApp->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
84            this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ), Qt::UniqueConnection );
85 }
86
87 void HYDROGUI_GeoreferencementOp::abortOperation()
88 {
89   LightApp_Application* anApp = module()->getApp();
90   if ( anApp && anApp->desktop() ) {
91     anApp->desktop()->disconnect( this );
92   }
93
94   restoreCursor();
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   restoreCursor();
106   HYDROGUI_Operation::commitOperation();
107 }
108
109 HYDROGUI_InputPanel* HYDROGUI_GeoreferencementOp::createInputPanel() const
110 {
111   HYDROGUI_InputPanel* aPanel = new HYDROGUI_GeoreferencementDlg( module(), getName() );
112   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
113   connect( aPanel, SIGNAL( updateSelection() ), SLOT( onUpdateSelection() ) );
114
115   return aPanel;
116 }
117
118 bool HYDROGUI_GeoreferencementOp::processApply( int& theUpdateFlags,
119                                                 QString& theErrorMsg,
120                                                 QStringList& theBrowseObjectsEntries )
121 {
122   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
123
124   return store( theErrorMsg );
125 }
126
127 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
128 {
129   QString anErrorMsg;
130
131   // Get the panel
132   HYDROGUI_GeoreferencementDlg* aPanel = 
133     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
134   if ( !aPanel ) {
135     return;
136   }
137
138   aPanel->setMode( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ?
139                    HYDROGUI_GeoreferencementDlg::SelectedProfiles :
140                    HYDROGUI_GeoreferencementDlg::AllProfiles);
141
142   // Store the dialog data to the data model
143   bool isToStore = false;
144   // Check if modifications exists
145   if ( aPanel->isModified() ) {
146     // Show confirmation dialog
147     SUIT_MessageBox::StandardButtons aButtons = 
148       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
149
150     isToStore = SUIT_MessageBox::question( module()->getApp()->desktop(),
151                                            tr( "CONFIRMATION" ),
152                                            tr( "CONFIRM_STORE" ),
153                                            aButtons, 
154                                            SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
155   }
156   // Store modifications if needed
157   if ( isToStore )
158   {
159     startDocOperation();
160     if ( !store( anErrorMsg ) )
161     {
162       abortDocOperation();
163
164       anErrorMsg.append( "\n" + tr( "INPUT_VALID_DATA" ) );
165       SUIT_MessageBox::critical( module()->getApp()->desktop(),
166                                  tr( "INSUFFICIENT_INPUT_DATA" ),
167                                  anErrorMsg );
168       return;
169     }
170     else
171     {
172       commitDocOperation();
173     }
174   }
175
176   aPanel->setMode( theActualMode );
177
178   // Get the list of profiles
179   HYDROData_SequenceOfObjects aSeqOfProfiles;
180   
181   if( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ) {
182     HYDROData_Iterator aProfilesIter( doc(), KIND_PROFILE );
183     while ( aProfilesIter.More() ) {
184       aSeqOfProfiles.Append( aProfilesIter.Current() );
185       aProfilesIter.Next();
186     }
187   } else if ( theActualMode == HYDROGUI_GeoreferencementDlg::SelectedProfiles ) {
188     aSeqOfProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
189   }
190
191   // Set the profiles to the panel
192   setPanelData( aSeqOfProfiles );
193 }
194
195 void HYDROGUI_GeoreferencementOp::onWindowActivated( SUIT_ViewWindow* theViewWindow )
196 {
197   if ( !theViewWindow ) {
198     return;
199   }
200
201   OCCViewer_ViewManager* aViewManager =
202     dynamic_cast<OCCViewer_ViewManager*>( theViewWindow->getViewManager() );
203
204   if ( aViewManager ) {
205     HYDROGUI_GeoreferencementDlg* aPanel = 
206       ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
207     if ( aPanel ) {
208       connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
209                aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
210                Qt::UniqueConnection);
211     }
212   }
213 }
214
215 bool HYDROGUI_GeoreferencementOp::store( QString& theErrorMsg )
216 {
217   // Clear the error string
218   theErrorMsg.clear();
219   
220   HYDROGUI_Module* aModule = module();
221
222   // Get the panel
223   HYDROGUI_GeoreferencementDlg* aPanel = 
224     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
225   if ( !aPanel ) {
226     return false;
227   }
228
229   // Get georeferencement data from the panel
230   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aGeoDataList;
231   aPanel->getData( aGeoDataList );
232   if ( aGeoDataList.empty() ) {
233     return true;
234   }
235
236   // Check the data validity
237   foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
238     if ( aGeoData.isIncomplete ) {
239       theErrorMsg = tr( "INCOMPLETE_DATA" ).arg( aGeoData.Name );
240       return false;
241     }
242   }
243
244   // Store the data in the data model
245   foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
246     Handle(HYDROData_Profile) aProfile = 
247       Handle(HYDROData_Profile)::DownCast(
248         HYDROGUI_Tool::FindObjectByName( module(), aGeoData.Name, KIND_PROFILE ) );
249     if ( !aProfile.IsNull() ) {
250       if ( !aGeoData.isEmpty ) {
251         if ( !aProfile->IsValid() ) // Show the profile after it became valid
252           aModule->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( aModule ), aProfile, true );
253
254         aProfile->SetLeftPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ), false );
255         aProfile->SetRightPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ), false );
256       } else {
257         aProfile->Invalidate();
258         aModule->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( aModule ), aProfile, false );
259       }
260
261       aProfile->Update();
262     }
263   }
264
265   aModule->update( UF_Model | UF_OCCViewer | UF_OCC_Forced );
266
267   return true;
268 }
269
270 void HYDROGUI_GeoreferencementOp::onUpdateSelection()
271 {
272   // Get the selected profiles
273   HYDROData_SequenceOfObjects aSeqOfProfiles = 
274     HYDROGUI_Tool::GetSelectedObjects( module() );
275
276   // Set them to the dialog
277   setPanelData( aSeqOfProfiles );
278 }
279
280 void HYDROGUI_GeoreferencementOp::setPanelData( 
281   const HYDROData_SequenceOfObjects& theProfiles )
282 {
283   // Get the panel
284   HYDROGUI_GeoreferencementDlg* aPanel = 
285     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
286   if ( !aPanel ) {
287     return;
288   }
289
290   // Get georeferencement data from the data model
291   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aData;
292
293   HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
294   for ( ; anIter.More(); anIter.Next() ) {
295     Handle(HYDROData_Profile) aProfile =
296         Handle(HYDROData_Profile)::DownCast( anIter.Value() );
297     if ( aProfile.IsNull() ) {
298       continue;
299     }
300
301     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData( aProfile->GetName() );
302
303     gp_XY aFirstPoint, aLastPoint;
304     if ( aProfile->GetLeftPoint( aFirstPoint, false ) &&
305          aProfile->GetRightPoint( aLastPoint, false ) ) {
306       aGeoData = 
307         HYDROGUI_GeoreferencementDlg::ProfileGeoData( aGeoData.Name, 
308                                                       aFirstPoint.X(), aFirstPoint.Y(),
309                                                       aLastPoint.X(), aLastPoint.Y() );
310     }
311    
312     aData.append( aGeoData );
313   }
314
315   // Set the collected data to the dialog
316   aPanel->setData( aData );
317 }