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