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