Salome HOME
New files added.
[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_PrsZone.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 <GraphicsView_ViewManager.h>
37 #include <GraphicsView_ViewPort.h>
38 #include <GraphicsView_Viewer.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42
43 HYDROGUI_ZoneOp::HYDROGUI_ZoneOp( HYDROGUI_Module* theModule,
44                                   const bool theIsEdit )
45 : HYDROGUI_Operation( theModule ),
46   myIsEdit( theIsEdit ),
47   myActiveViewManager( 0 ),
48   myPreviewViewManager( 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->reset();
68
69   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Zone" );
70
71   QColor      aFillingColor( HYDROData_Zone::DefaultFillingColor() );
72   QColor      aBorderColor( HYDROData_Zone::DefaultBorderColor() );
73   QString     aSelectedPolyline;
74   QStringList aSelectedBathymetries;
75
76   if ( myIsEdit )
77   {
78     myEditedObject = Handle(HYDROData_Zone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79     if( !myEditedObject.IsNull() )
80     {
81       anObjectName = myEditedObject->GetName();
82
83       aFillingColor = myEditedObject->GetFillingColor();
84       aBorderColor = myEditedObject->GetBorderColor();
85
86       Handle(HYDROData_Polyline) aRefPolyline = myEditedObject->GetPolyline();
87       if ( !aRefPolyline.IsNull() )
88         aSelectedPolyline = aRefPolyline->GetName();
89
90       HYDROData_SequenceOfObjects aRefBathymetries = myEditedObject->GetBathymetries();
91       HYDROData_SequenceOfObjects::Iterator anIter( aRefBathymetries );
92       for ( ; anIter.More(); anIter.Next() )
93       {
94         Handle(HYDROData_Bathymetry) aRefBathymetry = 
95           Handle(HYDROData_Bathymetry)::DownCast( anIter.Value() );
96         if ( aRefBathymetry.IsNull() )
97           continue;
98
99         QString aRefBathymetryName = aRefBathymetry->GetName();
100         if ( aRefBathymetryName.isEmpty() )
101           continue;
102
103         aSelectedBathymetries.append( aRefBathymetryName );
104       }
105     }
106   }
107
108   // collect information about existing closed polylines
109   QStringList aPolylines;
110
111   HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
112   for ( ; anIter.More(); anIter.Next() )
113   {
114     Handle(HYDROData_Polyline) aPolylineObj = 
115       Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
116     if ( aPolylineObj.IsNull() || !aPolylineObj->isClosed() )
117       continue;
118
119     QString aPolylineName = aPolylineObj->GetName();
120     if ( aPolylineName.isEmpty() )
121       continue;
122
123     aPolylines.append( aPolylineName );
124   }
125
126   // collect information about existing bathymetries
127   QStringList aBathymetries;
128
129   anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
130   for ( ; anIter.More(); anIter.Next() )
131   {
132     Handle(HYDROData_Bathymetry) aBathymetryObj = 
133       Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
134     if ( aBathymetryObj.IsNull() )
135       continue;
136
137     QString aBathymetryName = aBathymetryObj->GetName();
138     if ( aBathymetryName.isEmpty() )
139       continue;
140
141     aBathymetries.append( aBathymetryName );
142   }
143   
144   aPanel->setObjectName( anObjectName );
145   
146   aPanel->setFillingColor( aFillingColor );
147   aPanel->setBorderColor( aBorderColor );
148
149   aPanel->setPolylineNames( aPolylines );
150   aPanel->setPolylineName( aSelectedPolyline );
151
152   aPanel->setBathymetries( aBathymetries );
153   aPanel->setSelectedBathymetries( aSelectedBathymetries );
154
155 }
156
157 void HYDROGUI_ZoneOp::abortOperation()
158 {
159   closePreview();
160
161   HYDROGUI_Operation::abortOperation();
162 }
163
164 void HYDROGUI_ZoneOp::commitOperation()
165 {
166   closePreview();
167
168   HYDROGUI_Operation::commitOperation();
169 }
170
171 HYDROGUI_InputPanel* HYDROGUI_ZoneOp::createInputPanel() const
172 {
173   HYDROGUI_ZoneDlg* aPanel = new HYDROGUI_ZoneDlg( module(), getName() );
174   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
175            this,   SLOT( onCreatePreview( const QString& ) ) );
176   return aPanel;
177 }
178
179 bool HYDROGUI_ZoneOp::processApply( int& theUpdateFlags,
180                                     QString& theErrorMsg )
181 {
182   HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
183   if ( !aPanel )
184     return false;
185
186   QString anObjectName = aPanel->getObjectName().simplified();
187   if ( anObjectName.isEmpty() )
188   {
189     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
190     return false;
191   }
192
193   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
194   {
195     // check that there are no other objects with the same name in the document
196     Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
197     if( !anObject.IsNull() )
198     {
199       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
200       return false;
201     }
202   }
203
204   Handle(HYDROData_Zone) aZoneObj = myIsEdit ? myEditedObject :
205     Handle(HYDROData_Zone)::DownCast( doc()->CreateObject( KIND_ZONE ) );
206   if ( aZoneObj.IsNull() )
207     return false;
208
209   aZoneObj->SetName( anObjectName );
210
211   aZoneObj->SetFillingColor( aPanel->getFillingColor() );
212   aZoneObj->SetBorderColor( aPanel->getBorderColor() );
213
214   Handle(HYDROData_Polyline) aZonePolyline;
215
216   QString aPolylineName = aPanel->getPolylineName();
217   if ( !aPolylineName.isEmpty() )
218   {
219     aZonePolyline = Handle(HYDROData_Polyline)::DownCast(
220       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
221   }
222
223   aZoneObj->SetPolyline( aZonePolyline );
224
225
226   aZoneObj->RemoveBathymetries();
227
228   QStringList aBathymetries = aPanel->getSelectedBathymetries();
229   for ( int i = 0; i < aBathymetries.length(); ++i )
230   {
231     const QString& aBathymetryName = aBathymetries.at( i );
232     if ( aBathymetryName.isEmpty() )
233       continue;
234
235     Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
236         HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
237     if ( aBathymetry.IsNull() )
238       continue;
239
240     aZoneObj->AddBathymetry( aBathymetry );
241   }
242
243
244   theUpdateFlags = UF_Model;
245
246   return true;
247 }
248
249 void HYDROGUI_ZoneOp::onCreatePreview( const QString& thePolylineName )
250 {
251   HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
252   if ( !aPanel )
253     return;
254
255   QPainterPath aPath;
256
257   Handle(HYDROData_Polyline) aPolyline = Handle(HYDROData_Polyline)::DownCast(
258     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINE ) );
259   if ( !aPolyline.IsNull() )
260   {
261     aPath = aPolyline->painterPath();
262   }
263
264   LightApp_Application* anApp = module()->getApp();
265
266   if ( !myActiveViewManager )
267   {
268     myActiveViewManager = anApp->activeViewManager();
269   }
270
271   if ( !myPreviewPrs )
272   {
273     myPreviewPrs = new HYDROGUI_PrsZone( myIsEdit ? myEditedObject : 0 );
274   }
275
276   myPreviewPrs->setPath( aPath );
277   myPreviewPrs->setFillingColor( aPanel->getFillingColor() );
278   myPreviewPrs->setBorderColor( aPanel->getBorderColor() );
279   myPreviewPrs->compute();
280
281   if ( !myPreviewViewManager )
282   {
283     myPreviewViewManager = ::qobject_cast<GraphicsView_ViewManager*>( 
284       anApp->createViewManager( GraphicsView_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       if ( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
293       {
294         if ( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
295         {
296           aViewPort->addItem( myPreviewPrs );
297         }
298       }
299     }
300   }
301
302   if ( myPreviewViewManager )
303   {
304     if ( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
305     {
306       if ( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
307       {
308         aViewPort->update();
309         aViewPort->fitAll();
310       }
311     }
312   }
313 }
314
315 void HYDROGUI_ZoneOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
316 {
317   closePreview();
318 }
319
320 void HYDROGUI_ZoneOp::closePreview()
321 {
322   // It's very strange, but without calling this method (it's quite safe) a crash is stably reproduced.
323   // Scenario: create any non-Graphics view, activate import op, click apply.
324   // Result: a few SIGSEGVs coming from processEvents(), then crash.
325   if( myPreviewViewManager )
326     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
327       if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
328         aViewPort->onBoundingRectChanged();
329
330   if( myPreviewPrs )
331   {
332     delete myPreviewPrs;
333     myPreviewPrs = 0;
334   }
335
336   if( myPreviewViewManager )
337   {
338     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
339                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
340
341     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
342     myPreviewViewManager = 0;
343   }
344
345   if( myActiveViewManager )
346   {
347     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
348     myActiveViewManager = 0;
349   }
350 }