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