Salome HOME
The initial implementation of Feature #84: Profiles georeferencement (12.4).
[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 <gp_XY.hxx>
39
40 HYDROGUI_GeoreferencementOp::HYDROGUI_GeoreferencementOp( HYDROGUI_Module* theModule, const int theInitialMode )
41 : HYDROGUI_Operation( theModule ),
42   myInitialMode( theInitialMode )
43 {
44   setName( tr( "PROFILES_GEOREFERENCEMENT" ) );
45 }
46
47 HYDROGUI_GeoreferencementOp::~HYDROGUI_GeoreferencementOp()
48 {
49 }
50
51 void HYDROGUI_GeoreferencementOp::startOperation()
52 {
53   HYDROGUI_Operation::startOperation();
54
55   HYDROGUI_GeoreferencementDlg* aPanel = 
56     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
57   if ( !aPanel ) {
58     return;
59   }
60
61   aPanel->reset();
62
63   if ( myInitialMode == All ) {
64     int anAllMode = HYDROGUI_GeoreferencementDlg::AllProfiles;
65     aPanel->setMode( anAllMode );
66     onModeActivated( anAllMode );
67   } else if ( myInitialMode == Selected ) {
68     int aSelectionMode = HYDROGUI_GeoreferencementDlg::SelectedProfiles;
69     aPanel->setMode( aSelectionMode );
70     onModeActivated( aSelectionMode );
71   }
72 }
73
74 void HYDROGUI_GeoreferencementOp::abortOperation()
75 {
76   HYDROGUI_Operation::abortOperation();
77 }
78
79 void HYDROGUI_GeoreferencementOp::commitOperation()
80 {
81   HYDROGUI_Operation::commitOperation();
82 }
83
84 HYDROGUI_InputPanel* HYDROGUI_GeoreferencementOp::createInputPanel() const
85 {
86   HYDROGUI_InputPanel* aPanel = new HYDROGUI_GeoreferencementDlg( module(), getName() );
87   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
88
89   return aPanel;
90 }
91
92 bool HYDROGUI_GeoreferencementOp::processApply( int& theUpdateFlags,
93                                                 QString& theErrorMsg )
94 {
95   HYDROGUI_GeoreferencementDlg* aPanel = 
96     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
97   if ( !aPanel ) {
98     return false;
99   }
100
101   // Get georeferencement data from the panel
102   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataMap aGeoDataMap;
103   aPanel->getData( aGeoDataMap );
104
105   // Set the data
106   foreach ( const QString& aProfileName, aGeoDataMap.keys() ) {
107     Handle(HYDROData_Profile) aProfile = 
108       Handle(HYDROData_Profile)::DownCast(
109         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
110     if ( !aProfile.IsNull() ) {
111       HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData = 
112         aGeoDataMap.value( aProfileName );
113       if ( aGeoData.isValid ) {
114         aProfile->SetFirstPoint( gp_XY( aGeoData.Xg, aGeoData.Yg ) );
115         aProfile->SetLastPoint( gp_XY( aGeoData.Xd, aGeoData.Yd ) );
116       } else {
117         aProfile->Invalidate();
118       }
119     }
120   }
121   
122   theUpdateFlags = UF_Model;
123   return true;
124 }
125
126 void HYDROGUI_GeoreferencementOp::onModeActivated( const int theActualMode )
127 {
128   HYDROGUI_GeoreferencementDlg* aPanel = 
129     ::qobject_cast<HYDROGUI_GeoreferencementDlg*>( inputPanel() );
130   if ( !aPanel ) {
131     return;
132   }
133
134   HYDROGUI_GeoreferencementDlg::ProfilesGeoDataMap aDataMap;
135   HYDROData_SequenceOfObjects aSeqOfProfiles;
136   
137   if( theActualMode == HYDROGUI_GeoreferencementDlg::AllProfiles ) {
138     HYDROData_Iterator aProfilesIter( doc(), KIND_PROFILE );
139     while ( aProfilesIter.More() ) {
140       aSeqOfProfiles.Append( aProfilesIter.Current() );
141       aProfilesIter.Next();
142     }
143   } else if ( theActualMode == HYDROGUI_GeoreferencementDlg::SelectedProfiles ) {
144     aSeqOfProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
145   }
146
147   HYDROData_SequenceOfObjects::Iterator anIter( aSeqOfProfiles );
148   for ( ; anIter.More(); anIter.Next() ) {
149     Handle(HYDROData_Profile) aProfile =
150         Handle(HYDROData_Profile)::DownCast( anIter.Value() );
151     if ( aProfile.IsNull() ) {
152       continue;
153     }
154
155     HYDROGUI_GeoreferencementDlg::ProfileGeoData aGeoData;
156
157     gp_XY aFirstPoint, aLastPoint;
158     if ( aProfile->GetFirstPoint( aFirstPoint ) && aProfile->GetLastPoint( aLastPoint ) ) {
159       aGeoData = 
160         HYDROGUI_GeoreferencementDlg::ProfileGeoData( aFirstPoint.X(), aFirstPoint.Y(),
161                                                       aLastPoint.X(), aLastPoint.Y() );
162     }
163    
164     aDataMap.insert( aProfile->GetName(), aGeoData );
165   }
166
167   aPanel->setData( aDataMap );
168 }
169
170
171