Salome HOME
bug #134 - stepped bathymetry presentation
[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_ViewWindow.h>
40
41 #include <OCCViewer_ViewManager.h>
42
43 #include <gp_XY.hxx>
44
45 HYDROGUI_GeoreferencementOp::HYDROGUI_GeoreferencementOp( HYDROGUI_Module* theModule, const int theInitialMode )
46 : HYDROGUI_Operation( theModule ),
47   myInitialMode( theInitialMode )
48 {
49   setName( tr( "PROFILES_GEOREFERENCEMENT" ) );
50 }
51
52 HYDROGUI_GeoreferencementOp::~HYDROGUI_GeoreferencementOp()
53 {
54 }
55
56 void HYDROGUI_GeoreferencementOp::startOperation()
57 {
58   HYDROGUI_Operation::startOperation();
59
60   HYDROGUI_GeoreferencementDlg* aPanel = 
61     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
62   if ( !aPanel ) {
63     return;
64   }
65
66   aPanel->reset();
67
68   if ( myInitialMode == All ) {
69     int anAllMode = HYDROGUI_GeoreferencementDlg::AllProfiles;
70     aPanel->setMode( anAllMode );
71     onModeActivated( anAllMode );
72   } else if ( myInitialMode == Selected ) {
73     int aSelectionMode = HYDROGUI_GeoreferencementDlg::SelectedProfiles;
74     aPanel->setMode( aSelectionMode );
75     onModeActivated( aSelectionMode );
76   }
77
78   LightApp_Application* anApp = module()->getApp();
79   OCCViewer_ViewManager* aViewManager =
80     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), false ) );
81   if ( aViewManager ) {
82     connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
83              aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
84   }
85
86   connect( anApp->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
87            this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
88 }
89
90 void HYDROGUI_GeoreferencementOp::abortOperation()
91 {
92   HYDROGUI_Operation::abortOperation();
93 }
94
95 void HYDROGUI_GeoreferencementOp::commitOperation()
96 {
97   HYDROGUI_Operation::commitOperation();
98 }
99
100 HYDROGUI_InputPanel* HYDROGUI_GeoreferencementOp::createInputPanel() const
101 {
102   HYDROGUI_InputPanel* aPanel = new HYDROGUI_GeoreferencementDlg( module(), getName() );
103   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
104
105   return aPanel;
106 }
107
108 bool HYDROGUI_GeoreferencementOp::processApply( int& theUpdateFlags,
109                                                 QString& theErrorMsg )
110 {
111   HYDROGUI_GeoreferencementDlg* aPanel = 
112     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
113   if ( !aPanel ) {
114     return false;
115   }
116
117   // Get georeferencement data from the panel
118   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataMap aGeoDataMap;
119   aPanel->getData( aGeoDataMap );
120
121   if ( aGeoDataMap.empty() ) {
122     return false;
123   }
124
125   // Set the data
126   foreach ( const QString& aProfileName, aGeoDataMap.keys() ) {
127     Handle(HYDROData_Profile) aProfile = 
128       Handle(HYDROData_Profile)::DownCast(
129         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
130     if ( !aProfile.IsNull() ) {
131       HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData = 
132         aGeoDataMap.value( aProfileName );
133       if ( aGeoData.isValid ) {
134         aProfile->SetFirstPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ) );
135         aProfile->SetLastPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ) );
136       } else {
137         aProfile->Invalidate();
138       }
139     }
140   }
141   
142   theUpdateFlags = UF_Model;
143   return true;
144 }
145
146 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
147 {
148   HYDROGUI_GeoreferencementDlg* aPanel = 
149     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
150   if ( !aPanel ) {
151     return;
152   }
153
154   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataMap aDataMap;
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   HYDROData_SequenceOfObjects::Iterator anIter( aSeqOfProfiles );
168   for ( ; anIter.More(); anIter.Next() ) {
169     Handle(HYDROData_Profile) aProfile =
170         Handle(HYDROData_Profile)::DownCast( anIter.Value() );
171     if ( aProfile.IsNull() ) {
172       continue;
173     }
174
175     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData;
176
177     gp_XY aFirstPoint, aLastPoint;
178     if ( aProfile->GetFirstPoint( aFirstPoint ) && aProfile->GetLastPoint( aLastPoint ) ) {
179       aGeoData = 
180         HYDROGUI_GeoreferencementDlg::ProfileGeoData( aFirstPoint.X(), aFirstPoint.Y(),
181                                                       aLastPoint.X(), aLastPoint.Y() );
182     }
183    
184     aDataMap.insert( aProfile->GetName(), aGeoData );
185   }
186
187   aPanel->setData( aDataMap );
188 }
189
190 void HYDROGUI_GeoreferencementOp::onWindowActivated( SUIT_ViewWindow* theViewWindow )
191 {
192   if ( !theViewWindow ) {
193     return;
194   }
195
196   OCCViewer_ViewManager* aViewManager =
197     dynamic_cast<OCCViewer_ViewManager*>( theViewWindow->getViewManager() );
198
199   if ( aViewManager ) {
200     HYDROGUI_GeoreferencementDlg* aPanel = 
201       ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
202     if ( aPanel ) {
203       connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
204                aPanel, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
205     }
206   }
207 }
208