Salome HOME
debug abort on stream linear interpolation when bathy displayed and modified
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImmersibleZoneOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ImmersibleZoneOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_ImmersibleZoneDlg.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_Shape.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_Tool2.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_DataObject.h"
29
30 #include <HYDROData_Bathymetry.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_PolylineXY.h>
33
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
36
37 #include <LightApp_Application.h>
38 #include <LightApp_UpdateFlags.h>
39
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_Desktop.h>
42
43 #include <TopoDS.hxx>
44 #include <TopoDS_Face.hxx>
45 #include <TopoDS_Wire.hxx>
46
47 #include <QApplication>
48 //#define _DEVDEBUG_
49 #include "HYDRO_trace.hxx"
50
51 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
52                                                       const bool theIsEdit )
53 : HYDROGUI_Operation( theModule ),
54   myIsEdit( theIsEdit ),
55   myPreviewPrs( 0 )
56 {
57   setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
58 }
59
60 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
61 {
62   closePreview();
63 }
64
65 void HYDROGUI_ImmersibleZoneOp::startOperation()
66 {
67   HYDROGUI_Operation::startOperation();
68
69   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
70   if ( !aPanel )
71     return;
72
73   aPanel->blockSignals( true );
74
75   aPanel->reset();
76
77   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMMERSIBLE_ZONE_NAME" ) );
78
79   QString     aSelectedPolyline, aSelectedBathymetry;
80   QStringList aSelectedBathymetries;
81
82   if ( myIsEdit )
83   {
84     if ( isApplyAndClose() )
85       myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
86     if( !myEditedObject.IsNull() )
87     {
88       anObjectName = myEditedObject->GetName();
89
90       Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
91       if ( !aRefPolyline.IsNull() )
92         aSelectedPolyline = aRefPolyline->GetName();
93
94       Handle(HYDROData_IAltitudeObject) aRefAltitude = myEditedObject->GetAltitudeObject();
95       if ( !aRefAltitude.IsNull() )
96         aSelectedBathymetry = aRefAltitude->GetName();
97     }
98   }
99
100   aPanel->setObjectName( anObjectName );
101   aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
102   aPanel->setAdditionalParams( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_BATHYMETRY ) );
103
104   aPanel->blockSignals( false );
105
106   aPanel->setPolylineName( aSelectedPolyline );
107   aPanel->setSelectedAdditionalParamName( aSelectedBathymetry );
108 }
109
110 void HYDROGUI_ImmersibleZoneOp::abortOperation()
111 {
112   closePreview();
113
114   HYDROGUI_Operation::abortOperation();
115 }
116
117 void HYDROGUI_ImmersibleZoneOp::commitOperation()
118 {
119   closePreview();
120
121   HYDROGUI_Operation::commitOperation();
122 }
123
124 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
125 {
126   HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
127   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
128            this,   SLOT( onCreatePreview( const QString& ) ) );
129   return aPanel;
130 }
131
132 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
133                                               QString& theErrorMsg,
134                                               QStringList& theBrowseObjectsEntries )
135 {
136   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
137   if ( !aPanel )
138     return false;
139
140   QString anObjectName = aPanel->getObjectName().simplified();
141   if ( anObjectName.isEmpty() )
142   {
143     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
144     return false;
145   }
146
147   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
148   {
149     // check that there are no other objects with the same name in the document
150     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
151     if( !anObject.IsNull() )
152     {
153       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
154       return false;
155     }
156   }
157
158   Handle(HYDROData_PolylineXY) aZonePolyline;
159   Handle(HYDROData_Bathymetry) aZoneBathymetry;
160
161   QString aPolylineName = aPanel->getPolylineName();
162   if ( !aPolylineName.isEmpty() )
163   {
164     aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
165       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
166   }
167
168   QString aBathymetryName = aPanel->getSelectedAdditionalParamName();
169   if ( !aBathymetryName.isEmpty() )
170   {
171     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
172       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
173   }
174
175
176   if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
177   {
178     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
179     return false;
180   }
181
182   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
183     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
184
185   aZoneObj->SetName( anObjectName );
186
187   if ( !myIsEdit )
188   {
189     aZoneObj->SetFillingColor( aZoneObj->DefaultFillingColor() );
190     aZoneObj->SetBorderColor( aZoneObj->DefaultBorderColor() );
191   }
192
193   bool isPolylineChanged = true;
194   if (aZonePolyline && aZoneObj->GetPolyline() && (aZoneObj->GetPolyline()->GetName() == aZonePolyline->GetName()))
195   {
196       isPolylineChanged =false;
197       DEBTRACE("polyline unchanged");
198   }
199   else
200   {
201       if (aZonePolyline) DEBTRACE(aZonePolyline->GetName().toStdString());
202       if (aZoneObj->GetPolyline()) DEBTRACE(aZoneObj->GetPolyline()->GetName().toStdString());
203       aZoneObj->SetPolyline( aZonePolyline );
204   }
205   bool isBathymetryChanged = true;
206   if (aZoneBathymetry && aZoneObj->GetAltitudeObject() && IsEqual(aZoneObj->GetAltitudeObject(), aZoneBathymetry))
207   {
208       isBathymetryChanged = false;
209       DEBTRACE("bathymetry unchanged");
210   }
211   else aZoneObj->SetAltitudeObject( aZoneBathymetry );
212   aZoneObj->Update();
213
214   closePreview();
215
216   if( !myIsEdit )
217   {
218     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
219     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
220     theBrowseObjectsEntries.append( anEntry );
221   }
222
223   module()->setIsToUpdate( aZoneObj );
224
225   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
226
227   if (isBathymetryChanged)
228   {
229     DEBTRACE("bathymetry changed on " << anObjectName.toStdString());
230     // Find immersibles objects referenced by all calculation case zones and check if they correspond to the one modified 
231     HYDROData_SequenceOfObjects zoneList = doc()->CollectAllObjects();
232     for (int i=1; i<=zoneList.Size(); i++ )
233     {
234         //DEBTRACE("    --- entity: " << zoneList(i)->GetName().toStdString() << " " << zoneList(i)->GetKind());
235         Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( zoneList(i) );
236         if (aZone.IsNull())
237             continue;
238         DEBTRACE("  zone tested: " << aZone->GetName().toStdString());
239         HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
240         HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
241         for ( ; anIter.More(); anIter.Next() )
242         {
243             Handle(HYDROData_Entity) aMergeObject;
244             Handle(HYDROData_Object) aRefGeomObj = Handle(HYDROData_Object)::DownCast( anIter.Value() );
245             if ( !aRefGeomObj.IsNull() )
246             {
247                 DEBTRACE("    aRefGeomObj " << aRefGeomObj->GetName().toStdString());
248                 if (aRefGeomObj->GetName() == anObjectName)
249                 {
250                      DEBTRACE("      BINGO! modify altitude object in zone");
251                      aMergeObject = aRefGeomObj->GetAltitudeObject();
252                      aZone->SetMergeObject( aMergeObject );
253                      break;
254                 }
255             }            
256         }
257       
258     }
259   }
260   return true;
261 }
262
263 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
264 {
265   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
266   if ( !aPanel )
267     return;
268
269   QApplication::setOverrideCursor( Qt::WaitCursor );
270   TopoDS_Shape aZoneShape;
271
272   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
273     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
274   if ( !aPolyline.IsNull() )
275   {
276     aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
277     if( aZoneShape.IsNull() )
278       printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
279   }
280
281   LightApp_Application* anApp = module()->getApp();
282   if ( !getPreviewManager() )
283     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
284                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
285   OCCViewer_ViewManager* aViewManager = getPreviewManager();
286   if ( aViewManager && !myPreviewPrs )
287   {
288     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
289     {
290       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
291       if ( !aCtx.IsNull() )
292         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
293     }
294   }
295
296   if ( aViewManager && myPreviewPrs )
297   {
298     QColor aFillingColor = Qt::darkBlue;
299     QColor aBorderColor = Qt::transparent;
300     if ( !myEditedObject.IsNull() ) {
301       aFillingColor = myEditedObject->GetFillingColor();
302       aBorderColor = myEditedObject->GetBorderColor();
303     }
304
305     TopoDS_Face aFace;
306     if( !aZoneShape.IsNull() )
307       aFace = TopoDS::Face( aZoneShape );
308     myPreviewPrs->setFace( aFace, true, true, "" );
309     myPreviewPrs->setFillingColor( aFillingColor, false, false );
310     myPreviewPrs->setBorderColor( aBorderColor, false, false );
311   }
312
313   QApplication::restoreOverrideCursor();
314 }
315
316 void HYDROGUI_ImmersibleZoneOp::closePreview()
317 {
318   if( myPreviewPrs )
319   {
320     delete myPreviewPrs;
321     myPreviewPrs = 0;
322   }
323 }