Salome HOME
Compilation errors.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ZoneOp.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_ZoneOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ZoneDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Shape.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_Polyline.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 #include <TopoDS_Wire.hxx>
43
44 HYDROGUI_ZoneOp::HYDROGUI_ZoneOp( HYDROGUI_Module* theModule,
45                                   const bool theIsEdit )
46 : HYDROGUI_Operation( theModule ),
47   myIsEdit( theIsEdit ),
48   myViewManager( 0 ),
49   myPreviewPrs( 0 )
50 {
51   setName( theIsEdit ? tr( "EDIT_ZONE" ) : tr( "CREATE_ZONE" ) );
52 }
53
54 HYDROGUI_ZoneOp::~HYDROGUI_ZoneOp()
55 {
56   closePreview();
57 }
58
59 void HYDROGUI_ZoneOp::startOperation()
60 {
61   HYDROGUI_Operation::startOperation();
62
63   HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
64   if ( !aPanel )
65     return;
66
67   aPanel->blockSignals( true );
68
69   aPanel->reset();
70
71   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Zone" );
72
73   QColor      aFillingColor( HYDROData_Zone::DefaultFillingColor() );
74   QColor      aBorderColor( HYDROData_Zone::DefaultBorderColor() );
75   QString     aSelectedPolyline;
76   QStringList aSelectedBathymetries;
77
78   if ( myIsEdit )
79   {
80     myEditedObject = Handle(HYDROData_Zone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
81     if( !myEditedObject.IsNull() )
82     {
83       anObjectName = myEditedObject->GetName();
84
85       aFillingColor = myEditedObject->GetFillingColor();
86       aBorderColor = myEditedObject->GetBorderColor();
87
88       Handle(HYDROData_Polyline) aRefPolyline = myEditedObject->GetPolyline();
89       if ( !aRefPolyline.IsNull() )
90         aSelectedPolyline = aRefPolyline->GetName();
91
92       HYDROData_SequenceOfObjects aRefBathymetries = myEditedObject->GetBathymetries();
93       HYDROData_SequenceOfObjects::Iterator anIter( aRefBathymetries );
94       for ( ; anIter.More(); anIter.Next() )
95       {
96         Handle(HYDROData_Bathymetry) aRefBathymetry = 
97           Handle(HYDROData_Bathymetry)::DownCast( anIter.Value() );
98         if ( aRefBathymetry.IsNull() )
99           continue;
100
101         QString aRefBathymetryName = aRefBathymetry->GetName();
102         if ( aRefBathymetryName.isEmpty() )
103           continue;
104
105         aSelectedBathymetries.append( aRefBathymetryName );
106       }
107     }
108   }
109
110   // collect information about existing closed polylines
111   QStringList aPolylines;
112
113   HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
114   for ( ; anIter.More(); anIter.Next() )
115   {
116     Handle(HYDROData_Polyline) aPolylineObj = 
117       Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
118     if ( aPolylineObj.IsNull() || !aPolylineObj->isClosed() )
119       continue;
120
121     QString aPolylineName = aPolylineObj->GetName();
122     if ( aPolylineName.isEmpty() )
123       continue;
124
125     aPolylines.append( aPolylineName );
126   }
127
128   // collect information about existing bathymetries
129   QStringList aBathymetries;
130
131   anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
132   for ( ; anIter.More(); anIter.Next() )
133   {
134     Handle(HYDROData_Bathymetry) aBathymetryObj = 
135       Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
136     if ( aBathymetryObj.IsNull() )
137       continue;
138
139     QString aBathymetryName = aBathymetryObj->GetName();
140     if ( aBathymetryName.isEmpty() )
141       continue;
142
143     aBathymetries.append( aBathymetryName );
144   }
145   
146   aPanel->setObjectName( anObjectName );
147   
148   aPanel->setFillingColor( aFillingColor );
149   aPanel->setBorderColor( aBorderColor );
150
151   aPanel->setPolylineNames( aPolylines );
152
153   aPanel->setBathymetries( aBathymetries );
154   aPanel->setSelectedBathymetries( aSelectedBathymetries );
155
156   aPanel->blockSignals( false );
157
158   aPanel->setPolylineName( aSelectedPolyline );
159
160 }
161
162 void HYDROGUI_ZoneOp::abortOperation()
163 {
164   closePreview();
165
166   HYDROGUI_Operation::abortOperation();
167 }
168
169 void HYDROGUI_ZoneOp::commitOperation()
170 {
171   closePreview();
172
173   HYDROGUI_Operation::commitOperation();
174 }
175
176 HYDROGUI_InputPanel* HYDROGUI_ZoneOp::createInputPanel() const
177 {
178   HYDROGUI_ZoneDlg* aPanel = new HYDROGUI_ZoneDlg( module(), getName() );
179   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
180            this,   SLOT( onCreatePreview( const QString& ) ) );
181   return aPanel;
182 }
183
184 bool HYDROGUI_ZoneOp::processApply( int& theUpdateFlags,
185                                     QString& theErrorMsg )
186 {
187   HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
188   if ( !aPanel )
189     return false;
190
191   QString anObjectName = aPanel->getObjectName().simplified();
192   if ( anObjectName.isEmpty() )
193   {
194     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
195     return false;
196   }
197
198   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
199   {
200     // check that there are no other objects with the same name in the document
201     Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
202     if( !anObject.IsNull() )
203     {
204       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
205       return false;
206     }
207   }
208
209   Handle(HYDROData_Zone) aZoneObj = myIsEdit ? myEditedObject :
210     Handle(HYDROData_Zone)::DownCast( doc()->CreateObject( KIND_ZONE ) );
211   if ( aZoneObj.IsNull() )
212     return false;
213
214   aZoneObj->SetName( anObjectName );
215
216   aZoneObj->SetFillingColor( aPanel->getFillingColor() );
217   aZoneObj->SetBorderColor( aPanel->getBorderColor() );
218
219   Handle(HYDROData_Polyline) aZonePolyline;
220
221   QString aPolylineName = aPanel->getPolylineName();
222   if ( !aPolylineName.isEmpty() )
223   {
224     aZonePolyline = Handle(HYDROData_Polyline)::DownCast(
225       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
226   }
227
228   aZoneObj->SetPolyline( aZonePolyline );
229
230
231   aZoneObj->RemoveBathymetries();
232
233   QStringList aBathymetries = aPanel->getSelectedBathymetries();
234   for ( int i = 0; i < aBathymetries.length(); ++i )
235   {
236     const QString& aBathymetryName = aBathymetries.at( i );
237     if ( aBathymetryName.isEmpty() )
238       continue;
239
240     Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
241         HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
242     if ( aBathymetry.IsNull() )
243       continue;
244
245     aZoneObj->AddBathymetry( aBathymetry );
246   }
247
248   closePreview();
249
250   if( !myIsEdit )
251     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
252
253   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
254
255   return true;
256 }
257
258 void HYDROGUI_ZoneOp::onCreatePreview( const QString& thePolylineName )
259 {
260   HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
261   if ( !aPanel )
262     return;
263
264   TopoDS_Wire aWire;
265
266   Handle(HYDROData_Polyline) aPolyline = Handle(HYDROData_Polyline)::DownCast(
267     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINE ) );
268   if ( !aPolyline.IsNull() )
269   {
270     aWire = aPolyline->Wire();
271   }
272
273   LightApp_Application* anApp = module()->getApp();
274   if ( !myViewManager )
275     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
276       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
277
278   if ( myViewManager && !myPreviewPrs )
279   {
280     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
281     {
282       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
283       if ( !aCtx.IsNull() )
284         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
285     }
286   }
287
288   if ( !myViewManager || !myPreviewPrs )
289     return;
290
291   myPreviewPrs->setFillingColor( aPanel->getFillingColor(), false, false );
292   myPreviewPrs->setBorderColor( aPanel->getBorderColor(), false, false );
293   myPreviewPrs->setFace( aWire );
294 }
295
296 void HYDROGUI_ZoneOp::closePreview()
297 {
298   if( myPreviewPrs )
299   {
300     delete myPreviewPrs;
301     myPreviewPrs = 0;
302   }
303 }