Salome HOME
PR: quadtree
[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              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 {
120   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
121
122   return store( theErrorMsg );
123 }
124
125 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
126 {
127   QString anErrorMsg;
128
129   // Get the panel
130   HYDROGUI_GeoreferencementDlg* aPanel = 
131     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
132   if ( !aPanel ) {
133     return;
134   }
135
136   aPanel->setMode( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ?
137                    HYDROGUI_GeoreferencementDlg::SelectedProfiles :
138                    HYDROGUI_GeoreferencementDlg::AllProfiles);
139
140   // Store the dialog data to the data model
141   bool isToStore = false;
142   // Check if modifications exists
143   if ( aPanel->isModified() ) {
144     // Show confirmation dialog
145     SUIT_MessageBox::StandardButtons aButtons = 
146       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
147
148     isToStore = SUIT_MessageBox::question( module()->getApp()->desktop(),
149                                            tr( "CONFIRMATION" ),
150                                            tr( "CONFIRM_STORE" ),
151                                            aButtons, 
152                                            SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
153   }
154   // Store modifications if needed
155   if ( isToStore )
156   {
157     startDocOperation();
158     if ( !store( anErrorMsg ) )
159     {
160       abortDocOperation();
161
162       anErrorMsg.append( "\n" + tr( "INPUT_VALID_DATA" ) );
163       SUIT_MessageBox::critical( module()->getApp()->desktop(),
164                                  tr( "INSUFFICIENT_INPUT_DATA" ),
165                                  anErrorMsg );
166       return;
167     }
168     else
169     {
170       commitDocOperation();
171     }
172   }
173
174   aPanel->setMode( theActualMode );
175
176   // Get the list of profiles
177   HYDROData_SequenceOfObjects aSeqOfProfiles;
178   
179   if( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ) {
180     HYDROData_Iterator aProfilesIter( doc(), KIND_PROFILE );
181     while ( aProfilesIter.More() ) {
182       aSeqOfProfiles.Append( aProfilesIter.Current() );
183       aProfilesIter.Next();
184     }
185   } else if ( theActualMode == HYDROGUI_GeoreferencementDlg::SelectedProfiles ) {
186     aSeqOfProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
187   }
188
189   // Set the profiles to the panel
190   setPanelData( aSeqOfProfiles );
191 }
192
193 void HYDROGUI_GeoreferencementOp::onWindowActivated( SUIT_ViewWindow* theViewWindow )
194 {
195   if ( !theViewWindow ) {
196     return;
197   }
198
199   OCCViewer_ViewManager* aViewManager =
200     dynamic_cast<OCCViewer_ViewManager*>( theViewWindow->getViewManager() );
201
202   if ( aViewManager ) {
203     HYDROGUI_GeoreferencementDlg* aPanel = 
204       ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
205     if ( aPanel ) {
206       connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
207                aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
208                Qt::UniqueConnection);
209     }
210   }
211 }
212
213 bool HYDROGUI_GeoreferencementOp::store( QString& theErrorMsg )
214 {
215   // Clear the error string
216   theErrorMsg.clear();
217   
218   HYDROGUI_Module* aModule = module();
219
220   // Get the panel
221   HYDROGUI_GeoreferencementDlg* aPanel = 
222     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
223   if ( !aPanel ) {
224     return false;
225   }
226
227   // Get georeferencement data from the panel
228   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aGeoDataList;
229   aPanel->getData( aGeoDataList );
230   if ( aGeoDataList.empty() ) {
231     return true;
232   }
233
234   // Check the data validity
235   foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
236     if ( aGeoData.isIncomplete ) {
237       theErrorMsg = tr( "INCOMPLETE_DATA" ).arg( aGeoData.Name );
238       return false;
239     }
240   }
241
242   // Store the data in the data model
243   foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
244     Handle(HYDROData_Profile) aProfile = 
245       Handle(HYDROData_Profile)::DownCast(
246         HYDROGUI_Tool::FindObjectByName( module(), aGeoData.Name, KIND_PROFILE ) );
247     if ( !aProfile.IsNull() ) {
248       if ( !aGeoData.isEmpty ) {
249         if ( !aProfile->IsValid() ) // Show the profile after it became valid
250           aModule->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( aModule ), aProfile, true );
251
252         aProfile->SetLeftPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ) );
253         aProfile->SetRightPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ) );
254       } else {
255         aProfile->Invalidate();
256         aModule->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( aModule ), aProfile, false );
257       }
258
259       aProfile->Update();
260     }
261   }
262
263   aModule->update( UF_Model | UF_OCCViewer | UF_OCC_Forced );
264
265   return true;
266 }
267
268 void HYDROGUI_GeoreferencementOp::onUpdateSelection()
269 {
270   // Get the selected profiles
271   HYDROData_SequenceOfObjects aSeqOfProfiles = 
272     HYDROGUI_Tool::GetSelectedObjects( module() );
273
274   // Set them to the dialog
275   setPanelData( aSeqOfProfiles );
276 }
277
278 void HYDROGUI_GeoreferencementOp::setPanelData( 
279   const HYDROData_SequenceOfObjects& theProfiles )
280 {
281   // Get the panel
282   HYDROGUI_GeoreferencementDlg* aPanel = 
283     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
284   if ( !aPanel ) {
285     return;
286   }
287
288   // Get georeferencement data from the data model
289   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aData;
290
291   HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
292   for ( ; anIter.More(); anIter.Next() ) {
293     Handle(HYDROData_Profile) aProfile =
294         Handle(HYDROData_Profile)::DownCast( anIter.Value() );
295     if ( aProfile.IsNull() ) {
296       continue;
297     }
298
299     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData( aProfile->GetName() );
300
301     gp_XY aFirstPoint, aLastPoint;
302     if ( aProfile->GetLeftPoint( aFirstPoint ) && aProfile->GetRightPoint( aLastPoint ) ) {
303       aGeoData = 
304         HYDROGUI_GeoreferencementDlg::ProfileGeoData( aGeoData.Name, 
305                                                       aFirstPoint.X(), aFirstPoint.Y(),
306                                                       aLastPoint.X(), aLastPoint.Y() );
307     }
308    
309     aData.append( aGeoData );
310   }
311
312   // Set the collected data to the dialog
313   aPanel->setData( aData );
314 }