1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_GeoreferencementOp.h"
21 #include "HYDROGUI_GeoreferencementDlg.h"
22 #include "HYDROGUI_DataModel.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_Tool2.h"
25 #include "HYDROGUI_UpdateFlags.h"
27 #include <HYDROData_Profile.h>
28 #include <HYDROData_Iterator.h>
29 #include <HYDROData_Entity.h>
31 #include <LightApp_Application.h>
32 #include <LightApp_UpdateFlags.h>
34 #include <SUIT_Desktop.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_ViewWindow.h>
38 #include <OCCViewer_ViewManager.h>
42 HYDROGUI_GeoreferencementOp::HYDROGUI_GeoreferencementOp( HYDROGUI_Module* theModule, const int theInitialMode )
43 : HYDROGUI_Operation( theModule ),
44 myInitialMode( theInitialMode )
46 setName( tr( "PROFILES_GEOREFERENCEMENT" ) );
49 HYDROGUI_GeoreferencementOp::~HYDROGUI_GeoreferencementOp()
53 void HYDROGUI_GeoreferencementOp::startOperation()
55 HYDROGUI_Operation::startOperation();
57 HYDROGUI_GeoreferencementDlg* aPanel =
58 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
64 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
65 module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
68 if ( myInitialMode == All ) {
69 onModeActivated( HYDROGUI_GeoreferencementDlg::AllProfiles );
70 } else if ( myInitialMode == Selected ) {
71 onModeActivated( HYDROGUI_GeoreferencementDlg::SelectedProfiles );
74 LightApp_Application* anApp = module()->getApp();
75 OCCViewer_ViewManager* aViewManager =
76 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), false ) );
78 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
79 aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
80 Qt::UniqueConnection );
83 connect( anApp->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
84 this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ), Qt::UniqueConnection );
87 void HYDROGUI_GeoreferencementOp::abortOperation()
89 LightApp_Application* anApp = module()->getApp();
90 if ( anApp && anApp->desktop() ) {
91 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 );
106 HYDROGUI_Operation::commitOperation();
109 HYDROGUI_InputPanel* HYDROGUI_GeoreferencementOp::createInputPanel() const
111 HYDROGUI_InputPanel* aPanel = new HYDROGUI_GeoreferencementDlg( module(), getName() );
112 connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
113 connect( aPanel, SIGNAL( updateSelection() ), SLOT( onUpdateSelection() ) );
118 bool HYDROGUI_GeoreferencementOp::processApply( int& theUpdateFlags,
119 QString& theErrorMsg,
120 QStringList& theBrowseObjectsEntries )
122 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
124 return store( theErrorMsg );
127 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
132 HYDROGUI_GeoreferencementDlg* aPanel =
133 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
138 aPanel->setMode( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ?
139 HYDROGUI_GeoreferencementDlg::SelectedProfiles :
140 HYDROGUI_GeoreferencementDlg::AllProfiles);
142 // Store the dialog data to the data model
143 bool isToStore = false;
144 // Check if modifications exists
145 if ( aPanel->isModified() ) {
146 // Show confirmation dialog
147 SUIT_MessageBox::StandardButtons aButtons =
148 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
150 isToStore = SUIT_MessageBox::question( module()->getApp()->desktop(),
151 tr( "CONFIRMATION" ),
152 tr( "CONFIRM_STORE" ),
154 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
156 // Store modifications if needed
160 if ( !store( anErrorMsg ) )
164 anErrorMsg.append( "\n" + tr( "INPUT_VALID_DATA" ) );
165 SUIT_MessageBox::critical( module()->getApp()->desktop(),
166 tr( "INSUFFICIENT_INPUT_DATA" ),
172 commitDocOperation();
176 aPanel->setMode( theActualMode );
178 // Get the list of profiles
179 HYDROData_SequenceOfObjects aSeqOfProfiles;
181 if( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ) {
182 HYDROData_Iterator aProfilesIter( doc(), KIND_PROFILE );
183 while ( aProfilesIter.More() ) {
184 aSeqOfProfiles.Append( aProfilesIter.Current() );
185 aProfilesIter.Next();
187 } else if ( theActualMode == HYDROGUI_GeoreferencementDlg::SelectedProfiles ) {
188 aSeqOfProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
191 // Set the profiles to the panel
192 setPanelData( aSeqOfProfiles );
195 void HYDROGUI_GeoreferencementOp::onWindowActivated( SUIT_ViewWindow* theViewWindow )
197 if ( !theViewWindow ) {
201 OCCViewer_ViewManager* aViewManager =
202 dynamic_cast<OCCViewer_ViewManager*>( theViewWindow->getViewManager() );
204 if ( aViewManager ) {
205 HYDROGUI_GeoreferencementDlg* aPanel =
206 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
208 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
209 aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
210 Qt::UniqueConnection);
215 bool HYDROGUI_GeoreferencementOp::store( QString& theErrorMsg )
217 // Clear the error string
220 HYDROGUI_Module* aModule = module();
223 HYDROGUI_GeoreferencementDlg* aPanel =
224 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
229 // Get georeferencement data from the panel
230 HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aGeoDataList;
231 aPanel->getData( aGeoDataList );
232 if ( aGeoDataList.empty() ) {
236 // Check the data validity
237 foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
238 if ( aGeoData.isIncomplete ) {
239 theErrorMsg = tr( "INCOMPLETE_DATA" ).arg( aGeoData.Name );
244 // Store the data in the data model
245 foreach ( const HYDROGUI_GeoreferencementDlg::ProfileGeoData& aGeoData, aGeoDataList ) {
246 Handle(HYDROData_Profile) aProfile =
247 Handle(HYDROData_Profile)::DownCast(
248 HYDROGUI_Tool::FindObjectByName( module(), aGeoData.Name, KIND_PROFILE ) );
249 if ( !aProfile.IsNull() ) {
250 if ( !aGeoData.isEmpty ) {
251 if ( !aProfile->IsValid() ) // Show the profile after it became valid
252 aModule->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( aModule ), aProfile, true );
254 aProfile->SetLeftPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ), false );
255 aProfile->SetRightPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ), false );
257 aProfile->Invalidate();
258 aModule->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( aModule ), aProfile, false );
265 aModule->update( UF_Model | UF_OCCViewer | UF_OCC_Forced );
270 void HYDROGUI_GeoreferencementOp::onUpdateSelection()
272 // Get the selected profiles
273 HYDROData_SequenceOfObjects aSeqOfProfiles =
274 HYDROGUI_Tool::GetSelectedObjects( module() );
276 // Set them to the dialog
277 setPanelData( aSeqOfProfiles );
280 void HYDROGUI_GeoreferencementOp::setPanelData(
281 const HYDROData_SequenceOfObjects& theProfiles )
284 HYDROGUI_GeoreferencementDlg* aPanel =
285 ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
290 // Get georeferencement data from the data model
291 HYDROGUI_GeoreferencementDlg::ProfilesGeoDataList aData;
293 HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
294 for ( ; anIter.More(); anIter.Next() ) {
295 Handle(HYDROData_Profile) aProfile =
296 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
297 if ( aProfile.IsNull() ) {
301 HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData( aProfile->GetName() );
303 gp_XY aFirstPoint, aLastPoint;
304 if ( aProfile->GetLeftPoint( aFirstPoint, false ) &&
305 aProfile->GetRightPoint( aLastPoint, false ) ) {
307 HYDROGUI_GeoreferencementDlg::ProfileGeoData( aGeoData.Name,
308 aFirstPoint.X(), aFirstPoint.Y(),
309 aLastPoint.X(), aLastPoint.Y() );
312 aData.append( aGeoData );
315 // Set the collected data to the dialog
316 aPanel->setData( aData );