Salome HOME
15595b6050a611b08f682823a8b8b6bf5499cb39
[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     int anAllMode = HYDROGUI_GeoreferencementDlg::AllProfiles;
71     aPanel->setMode( anAllMode );
72     onModeActivated( anAllMode );
73   } else if ( myInitialMode == Selected ) {
74     int aSelectionMode = HYDROGUI_GeoreferencementDlg::SelectedProfiles;
75     aPanel->setMode( aSelectionMode );
76     onModeActivated( aSelectionMode );
77   }
78
79   LightApp_Application* anApp = module()->getApp();
80   OCCViewer_ViewManager* aViewManager =
81     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), false ) );
82   if ( aViewManager ) {
83     connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
84              aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
85   }
86
87   connect( anApp->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
88            this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
89 }
90
91 void HYDROGUI_GeoreferencementOp::abortOperation()
92 {
93   HYDROGUI_Operation::abortOperation();
94 }
95
96 void HYDROGUI_GeoreferencementOp::commitOperation()
97 {
98   HYDROGUI_Operation::commitOperation();
99 }
100
101 HYDROGUI_InputPanel* HYDROGUI_GeoreferencementOp::createInputPanel() const
102 {
103   HYDROGUI_InputPanel* aPanel = new HYDROGUI_GeoreferencementDlg( module(), getName() );
104   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
105
106   return aPanel;
107 }
108
109 bool HYDROGUI_GeoreferencementOp::processApply( int& theUpdateFlags,
110                                                 QString& theErrorMsg )
111 {
112   theUpdateFlags = UF_Model;
113
114   return store( theErrorMsg );
115 }
116
117 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
118 {
119   QString anErrorMsg;
120
121   // Get the panel
122   HYDROGUI_GeoreferencementDlg* aPanel = 
123     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
124   if ( !aPanel ) {
125     return;
126   }
127
128   // Store the dialog data to the data model
129   if ( !store( anErrorMsg ) ) {
130     aPanel->setMode( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ?
131                      HYDROGUI_GeoreferencementDlg::SelectedProfiles :
132                      HYDROGUI_GeoreferencementDlg::AllProfiles);
133
134     anErrorMsg.append( "\n" + tr( "INPUT_VALID_DATA" ) );
135     SUIT_MessageBox::critical( module()->getApp()->desktop(),
136                                tr( "INSUFFICIENT_INPUT_DATA" ),
137                                anErrorMsg );
138     return;
139   }
140
141   // Get georeferencement data from the data model
142   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataMap aDataMap;
143   HYDROData_SequenceOfObjects aSeqOfProfiles;
144   
145   if( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ) {
146     HYDROData_Iterator aProfilesIter( doc(), KIND_PROFILE );
147     while ( aProfilesIter.More() ) {
148       aSeqOfProfiles.Append( aProfilesIter.Current() );
149       aProfilesIter.Next();
150     }
151   } else if ( theActualMode == HYDROGUI_GeoreferencementDlg::SelectedProfiles ) {
152     aSeqOfProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
153   }
154
155   HYDROData_SequenceOfObjects::Iterator anIter( aSeqOfProfiles );
156   for ( ; anIter.More(); anIter.Next() ) {
157     Handle(HYDROData_Profile) aProfile =
158         Handle(HYDROData_Profile)::DownCast( anIter.Value() );
159     if ( aProfile.IsNull() ) {
160       continue;
161     }
162
163     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData;
164
165     gp_XY aFirstPoint, aLastPoint;
166     if ( aProfile->GetFirstPoint( aFirstPoint ) && aProfile->GetLastPoint( aLastPoint ) ) {
167       aGeoData = 
168         HYDROGUI_GeoreferencementDlg::ProfileGeoData( aFirstPoint.X(), aFirstPoint.Y(),
169                                                       aLastPoint.X(), aLastPoint.Y() );
170     }
171    
172     aDataMap.insert( aProfile->GetName(), aGeoData );
173   }
174
175   // Set the collected data to the dialog
176   aPanel->setData( aDataMap );
177 }
178
179 void HYDROGUI_GeoreferencementOp::onWindowActivated( SUIT_ViewWindow* theViewWindow )
180 {
181   if ( !theViewWindow ) {
182     return;
183   }
184
185   OCCViewer_ViewManager* aViewManager =
186     dynamic_cast<OCCViewer_ViewManager*>( theViewWindow->getViewManager() );
187
188   if ( aViewManager ) {
189     HYDROGUI_GeoreferencementDlg* aPanel = 
190       ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
191     if ( aPanel ) {
192       connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
193                aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
194     }
195   }
196 }
197
198 bool HYDROGUI_GeoreferencementOp::store( QString& theErrorMsg )
199 {
200   // Clear the error string
201   theErrorMsg.clear();
202
203   // Get the panel
204   HYDROGUI_GeoreferencementDlg* aPanel = 
205     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
206   if ( !aPanel ) {
207     return false;
208   }
209
210   // Get georeferencement data from the panel
211   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataMap aGeoDataMap;
212   aPanel->getData( aGeoDataMap );
213
214   if ( aGeoDataMap.empty() ) {
215     return true;
216   }
217
218   // Check the data validity
219   foreach ( const QString& aProfileName, aGeoDataMap.keys() ) {
220     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData = 
221       aGeoDataMap.value( aProfileName );
222     if ( aGeoData.isIncomplete ) {
223       theErrorMsg = tr( "INCOMPLETE_DATA" ).arg( aProfileName );
224       return false;
225     }
226   }
227
228   // Store the data in the data model
229   foreach ( const QString& aProfileName, aGeoDataMap.keys() ) {
230     Handle(HYDROData_Profile) aProfile = 
231       Handle(HYDROData_Profile)::DownCast(
232         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
233     if ( !aProfile.IsNull() ) {
234       HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData = 
235         aGeoDataMap.value( aProfileName );
236       if ( !aGeoData.isEmpty ) {
237         aProfile->SetFirstPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ) );
238         aProfile->SetLastPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ) );
239       } else {
240         aProfile->Invalidate();
241       }
242     }
243   }
244
245   return true;
246 }