1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_GeoreferencementOp.h"
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"
31 #include <HYDROData_Profile.h>
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_Entity.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_UpdateFlags.h>
38 #include <SUIT_Desktop.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_ViewWindow.h>
42 #include <OCCViewer_ViewManager.h>
46 HYDROGUI_GeoreferencementOp::HYDROGUI_GeoreferencementOp( HYDROGUI_Module* theModule, const int theInitialMode )
47 : HYDROGUI_Operation( theModule ),
48 myInitialMode( theInitialMode )
50 setName( tr( "PROFILES_GEOREFERENCEMENT" ) );
53 HYDROGUI_GeoreferencementOp::~HYDROGUI_GeoreferencementOp()
57 void HYDROGUI_GeoreferencementOp::startOperation()
59 HYDROGUI_Operation::startOperation();
61 HYDROGUI_GeoreferencementDlg* aPanel =
62 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
69 if ( myInitialMode == All ) {
70 onModeActivated( HYDROGUI_GeoreferencementDlg::AllProfiles );
71 } else if ( myInitialMode == Selected ) {
72 onModeActivated( HYDROGUI_GeoreferencementDlg::SelectedProfiles );
75 LightApp_Application* anApp = module()->getApp();
76 OCCViewer_ViewManager* aViewManager =
77 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), false ) );
79 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
80 aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
81 Qt::UniqueConnection );
84 connect( anApp->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
85 this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ), Qt::UniqueConnection );
88 void HYDROGUI_GeoreferencementOp::abortOperation()
90 LightApp_Application* anApp = module()->getApp();
91 if ( anApp && anApp->desktop() ) {
92 anApp->desktop()->disconnect( this );
95 HYDROGUI_Operation::abortOperation();
98 void HYDROGUI_GeoreferencementOp::commitOperation()
100 LightApp_Application* anApp = module()->getApp();
101 if ( anApp && anApp->desktop() ) {
102 anApp->desktop()->disconnect( this );
105 HYDROGUI_Operation::commitOperation();
108 HYDROGUI_InputPanel* HYDROGUI_GeoreferencementOp::createInputPanel() const
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() ) );
117 bool HYDROGUI_GeoreferencementOp::processApply( int& theUpdateFlags,
118 QString& theErrorMsg )
120 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
122 return store( theErrorMsg );
125 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
130 HYDROGUI_GeoreferencementDlg* aPanel =
131 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
136 aPanel->setMode( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ?
137 HYDROGUI_GeoreferencementDlg::SelectedProfiles :
138 HYDROGUI_GeoreferencementDlg::AllProfiles);
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;
148 isToStore = SUIT_MessageBox::question( module()->getApp()->desktop(),
149 tr( "CONFIRMATION" ),
150 tr( "CONFIRM_STORE" ),
152 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
154 // Store modifications if needed
155 if ( isToStore && !store( anErrorMsg ) ) {
156 anErrorMsg.append( "\n" + tr( "INPUT_VALID_DATA" ) );
157 SUIT_MessageBox::critical( module()->getApp()->desktop(),
158 tr( "INSUFFICIENT_INPUT_DATA" ),
163 aPanel->setMode( theActualMode );
165 // Get the list of profiles
166 HYDROData_SequenceOfObjects aSeqOfProfiles;
168 if( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ) {
169 HYDROData_Iterator aProfilesIter( doc(), KIND_PROFILE );
170 while ( aProfilesIter.More() ) {
171 aSeqOfProfiles.Append( aProfilesIter.Current() );
172 aProfilesIter.Next();
174 } else if ( theActualMode == HYDROGUI_GeoreferencementDlg::SelectedProfiles ) {
175 aSeqOfProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
178 // Set the profiles to the panel
179 setPanelData( aSeqOfProfiles );
182 void HYDROGUI_GeoreferencementOp::onWindowActivated( SUIT_ViewWindow* theViewWindow )
184 if ( !theViewWindow ) {
188 OCCViewer_ViewManager* aViewManager =
189 dynamic_cast<OCCViewer_ViewManager*>( theViewWindow->getViewManager() );
191 if ( aViewManager ) {
192 HYDROGUI_GeoreferencementDlg* aPanel =
193 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
195 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
196 aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
197 Qt::UniqueConnection);
202 bool HYDROGUI_GeoreferencementOp::store( QString& theErrorMsg )
204 // Clear the error string
208 HYDROGUI_GeoreferencementDlg* aPanel =
209 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
214 // Get georeferencement data from the panel
215 HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aGeoDataList;
216 aPanel->getData( aGeoDataList );
217 if ( aGeoDataList.empty() ) {
221 // Check the data validity
222 foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
223 if ( aGeoData.isIncomplete ) {
224 theErrorMsg = tr( "INCOMPLETE_DATA" ).arg( aGeoData.Name );
229 // Store the data in the data model
230 foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
231 Handle(HYDROData_Profile) aProfile =
232 Handle(HYDROData_Profile)::DownCast(
233 HYDROGUI_Tool::FindObjectByName( module(), aGeoData.Name, KIND_PROFILE ) );
234 if ( !aProfile.IsNull() ) {
235 if ( !aGeoData.isEmpty ) {
236 aProfile->SetFirstPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ) );
237 aProfile->SetLastPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ) );
239 aProfile->Invalidate();
246 module()->update( UF_Model | UF_OCCViewer | UF_OCC_Forced );
247 commitDocOperation(); // TODO: analyze the consequences
252 void HYDROGUI_GeoreferencementOp::onUpdateSelection()
254 // Get the selected profiles
255 HYDROData_SequenceOfObjects aSeqOfProfiles =
256 HYDROGUI_Tool::GetSelectedObjects( module() );
258 // Set them to the dialog
259 setPanelData( aSeqOfProfiles );
262 void HYDROGUI_GeoreferencementOp::setPanelData(
263 const HYDROData_SequenceOfObjects& theProfiles )
266 HYDROGUI_GeoreferencementDlg* aPanel =
267 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
272 // Get georeferencement data from the data model
273 HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aData;
275 HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
276 for ( ; anIter.More(); anIter.Next() ) {
277 Handle(HYDROData_Profile) aProfile =
278 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
279 if ( aProfile.IsNull() ) {
283 HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData( aProfile->GetName() );
285 gp_XY aFirstPoint, aLastPoint;
286 if ( aProfile->GetFirstPoint( aFirstPoint ) && aProfile->GetLastPoint( aLastPoint ) ) {
288 HYDROGUI_GeoreferencementDlg::ProfileGeoData( aGeoData.Name,
289 aFirstPoint.X(), aFirstPoint.Y(),
290 aLastPoint.X(), aLastPoint.Y() );
293 aData.append( aGeoData );
296 // Set the collected data to the dialog
297 aPanel->setData( aData );