Salome HOME
Polylines 3D partition is added to the objects tree.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_GeoreferencementOp.cxx
1 // Copyright (C) 2007-2013  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.
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     //@MZN aPanel->setMode( anAllMode );
71     onModeActivated( HYDROGUI_GeoreferencementDlg::AllProfiles );
72   } else if ( myInitialMode == Selected ) {
73     //@MZN aPanel->setMode( aSelectionMode );
74     onModeActivated( HYDROGUI_GeoreferencementDlg::SelectedProfiles );
75   }
76
77   LightApp_Application* anApp = module()->getApp();
78   OCCViewer_ViewManager* aViewManager =
79     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), false ) );
80   if ( aViewManager ) {
81     connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
82              aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
83   }
84
85   connect( anApp->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
86            this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
87 }
88
89 void HYDROGUI_GeoreferencementOp::abortOperation()
90 {
91   HYDROGUI_Operation::abortOperation();
92 }
93
94 void HYDROGUI_GeoreferencementOp::commitOperation()
95 {
96   HYDROGUI_Operation::commitOperation();
97 }
98
99 HYDROGUI_InputPanel* HYDROGUI_GeoreferencementOp::createInputPanel() const
100 {
101   HYDROGUI_InputPanel* aPanel = new HYDROGUI_GeoreferencementDlg( module(), getName() );
102   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
103   connect( aPanel, SIGNAL( updateSelection() ), SLOT( onUpdateSelection() ) );
104
105   return aPanel;
106 }
107
108 bool HYDROGUI_GeoreferencementOp::processApply( int& theUpdateFlags,
109                                                 QString& theErrorMsg )
110 {
111   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
112
113   return store( theErrorMsg );
114 }
115
116 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
117 {
118   QString anErrorMsg;
119
120   // Get the panel
121   HYDROGUI_GeoreferencementDlg* aPanel = 
122     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
123   if ( !aPanel ) {
124     return;
125   }
126
127   aPanel->setMode( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ?
128                    HYDROGUI_GeoreferencementDlg::SelectedProfiles :
129                    HYDROGUI_GeoreferencementDlg::AllProfiles);
130
131   // Store the dialog data to the data model
132   bool isToStore = false;
133   // Check if modifications exists
134   if ( aPanel->isModified() ) {
135     // Show confirmation dialog
136     SUIT_MessageBox::StandardButtons aButtons = 
137       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
138
139     isToStore = SUIT_MessageBox::question( module()->getApp()->desktop(),
140                                            tr( "CONFIRMATION" ),
141                                            tr( "CONFIRM_STORE" ),
142                                            aButtons, 
143                                            SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
144   }
145   // Store modifications if needed
146   if ( isToStore && !store( anErrorMsg ) ) {
147     anErrorMsg.append( "\n" + tr( "INPUT_VALID_DATA" ) );
148     SUIT_MessageBox::critical( module()->getApp()->desktop(),
149                                tr( "INSUFFICIENT_INPUT_DATA" ),
150                                anErrorMsg );
151     return;
152   }
153
154   aPanel->setMode( theActualMode );
155
156   // Get the list of profiles
157   HYDROData_SequenceOfObjects aSeqOfProfiles;
158   
159   if( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ) {
160     HYDROData_Iterator aProfilesIter( doc(), KIND_PROFILE );
161     while ( aProfilesIter.More() ) {
162       aSeqOfProfiles.Append( aProfilesIter.Current() );
163       aProfilesIter.Next();
164     }
165   } else if ( theActualMode == HYDROGUI_GeoreferencementDlg::SelectedProfiles ) {
166     aSeqOfProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
167   }
168
169   // Set the profiles to the panel
170   setPanelData( aSeqOfProfiles );
171 }
172
173 void HYDROGUI_GeoreferencementOp::onWindowActivated( SUIT_ViewWindow* theViewWindow )
174 {
175   if ( !theViewWindow ) {
176     return;
177   }
178
179   OCCViewer_ViewManager* aViewManager =
180     dynamic_cast<OCCViewer_ViewManager*>( theViewWindow->getViewManager() );
181
182   if ( aViewManager ) {
183     HYDROGUI_GeoreferencementDlg* aPanel = 
184       ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
185     if ( aPanel ) {
186       connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
187                aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
188     }
189   }
190 }
191
192 bool HYDROGUI_GeoreferencementOp::store( QString& theErrorMsg )
193 {
194   // Clear the error string
195   theErrorMsg.clear();
196
197   // Get the panel
198   HYDROGUI_GeoreferencementDlg* aPanel = 
199     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
200   if ( !aPanel ) {
201     return false;
202   }
203
204   // Get georeferencement data from the panel
205   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataMap aGeoDataMap;
206   aPanel->getData( aGeoDataMap );
207
208   if ( aGeoDataMap.empty() ) {
209     return true;
210   }
211
212   // Check the data validity
213   foreach ( const QString& aProfileName, aGeoDataMap.keys() ) {
214     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData = 
215       aGeoDataMap.value( aProfileName );
216     if ( aGeoData.isIncomplete ) {
217       theErrorMsg = tr( "INCOMPLETE_DATA" ).arg( aProfileName );
218       return false;
219     }
220   }
221
222   // Store the data in the data model
223   foreach ( const QString& aProfileName, aGeoDataMap.keys() ) {
224     Handle(HYDROData_Profile) aProfile = 
225       Handle(HYDROData_Profile)::DownCast(
226         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
227     if ( !aProfile.IsNull() ) {
228       HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData = 
229         aGeoDataMap.value( aProfileName );
230       if ( !aGeoData.isEmpty ) {
231         aProfile->SetFirstPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ) );
232         aProfile->SetLastPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ) );
233       } else {
234         aProfile->Invalidate();
235       }
236
237       aProfile->Update();
238     }
239   }
240
241   module()->update( UF_Model | UF_OCCViewer | UF_OCC_Forced );
242   commitDocOperation(); // TODO: analyze the consequences
243
244   return true;
245 }
246
247 void HYDROGUI_GeoreferencementOp::onUpdateSelection()
248 {
249   // Get the selected profiles
250   HYDROData_SequenceOfObjects aSeqOfProfiles = 
251     HYDROGUI_Tool::GetSelectedObjects( module() );
252
253   // Set them to the dialog
254   setPanelData( aSeqOfProfiles );
255 }
256
257 void HYDROGUI_GeoreferencementOp::setPanelData( 
258   const HYDROData_SequenceOfObjects& theProfiles )
259 {
260   // Get the panel
261   HYDROGUI_GeoreferencementDlg* aPanel = 
262     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
263   if ( !aPanel ) {
264     return;
265   }
266
267   // Get georeferencement data from the data model
268   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataMap aDataMap;
269
270   HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
271   for ( ; anIter.More(); anIter.Next() ) {
272     Handle(HYDROData_Profile) aProfile =
273         Handle(HYDROData_Profile)::DownCast( anIter.Value() );
274     if ( aProfile.IsNull() ) {
275       continue;
276     }
277
278     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData;
279
280     gp_XY aFirstPoint, aLastPoint;
281     if ( aProfile->GetFirstPoint( aFirstPoint ) && aProfile->GetLastPoint( aLastPoint ) ) {
282       aGeoData = 
283         HYDROGUI_GeoreferencementDlg::ProfileGeoData( aFirstPoint.X(), aFirstPoint.Y(),
284                                                       aLastPoint.X(), aLastPoint.Y() );
285     }
286    
287     aDataMap.insert( aProfile->GetName(), aGeoData );
288   }
289
290   // Set the collected data to the dialog
291   aPanel->setData( aDataMap );
292 }