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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ImmersibleZoneOp.h"
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"
30 #include <HYDROData_Bathymetry.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_PolylineXY.h>
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
37 #include <LightApp_Application.h>
38 #include <LightApp_UpdateFlags.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_Desktop.h>
44 #include <TopoDS_Face.hxx>
45 #include <TopoDS_Wire.hxx>
47 #include <QApplication>
49 #include "HYDRO_trace.hxx"
51 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
52 const bool theIsEdit )
53 : HYDROGUI_Operation( theModule ),
54 myIsEdit( theIsEdit ),
57 setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
60 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
65 void HYDROGUI_ImmersibleZoneOp::startOperation()
67 HYDROGUI_Operation::startOperation();
69 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
73 aPanel->blockSignals( true );
77 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMMERSIBLE_ZONE_NAME" ) );
79 QString aSelectedPolyline, aSelectedBathymetry;
80 QStringList aSelectedBathymetries;
84 if ( isApplyAndClose() )
85 myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
86 if( !myEditedObject.IsNull() )
88 anObjectName = myEditedObject->GetName();
90 Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
91 if ( !aRefPolyline.IsNull() )
92 aSelectedPolyline = aRefPolyline->GetName();
94 Handle(HYDROData_IAltitudeObject) aRefAltitude = myEditedObject->GetAltitudeObject();
95 if ( !aRefAltitude.IsNull() )
96 aSelectedBathymetry = aRefAltitude->GetName();
100 aPanel->setObjectName( anObjectName );
101 aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
102 aPanel->setAdditionalParams( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_BATHYMETRY ) );
104 aPanel->blockSignals( false );
106 aPanel->setPolylineName( aSelectedPolyline );
107 aPanel->setSelectedAdditionalParamName( aSelectedBathymetry );
110 void HYDROGUI_ImmersibleZoneOp::abortOperation()
114 HYDROGUI_Operation::abortOperation();
117 void HYDROGUI_ImmersibleZoneOp::commitOperation()
121 HYDROGUI_Operation::commitOperation();
124 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
126 HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
127 connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
128 this, SLOT( onCreatePreview( const QString& ) ) );
132 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
133 QString& theErrorMsg,
134 QStringList& theBrowseObjectsEntries )
136 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
140 QString anObjectName = aPanel->getObjectName().simplified();
141 if ( anObjectName.isEmpty() )
143 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
147 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
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() )
153 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
158 Handle(HYDROData_PolylineXY) aZonePolyline;
159 Handle(HYDROData_Bathymetry) aZoneBathymetry;
161 QString aPolylineName = aPanel->getPolylineName();
162 if ( !aPolylineName.isEmpty() )
164 aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
165 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
168 QString aBathymetryName = aPanel->getSelectedAdditionalParamName();
169 if ( !aBathymetryName.isEmpty() )
171 aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
172 HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
176 if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
178 theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
182 Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
183 Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
185 aZoneObj->SetName( anObjectName );
189 aZoneObj->SetFillingColor( aZoneObj->DefaultFillingColor() );
190 aZoneObj->SetBorderColor( aZoneObj->DefaultBorderColor() );
193 bool isPolylineChanged = true;
194 if (aZonePolyline && aZoneObj->GetPolyline() && (aZoneObj->GetPolyline()->GetName() == aZonePolyline->GetName()))
196 isPolylineChanged =false;
197 DEBTRACE("polyline unchanged");
201 if (aZonePolyline) DEBTRACE(aZonePolyline->GetName().toStdString());
202 if (aZoneObj->GetPolyline()) DEBTRACE(aZoneObj->GetPolyline()->GetName().toStdString());
203 aZoneObj->SetPolyline( aZonePolyline );
205 bool isBathymetryChanged = true;
206 if (aZoneBathymetry && aZoneObj->GetAltitudeObject() && IsEqual(aZoneObj->GetAltitudeObject(), aZoneBathymetry))
208 isBathymetryChanged = false;
209 DEBTRACE("bathymetry unchanged");
211 else aZoneObj->SetAltitudeObject( aZoneBathymetry );
218 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
219 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
220 theBrowseObjectsEntries.append( anEntry );
223 module()->setIsToUpdate( aZoneObj );
225 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
227 if (isBathymetryChanged)
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++ )
234 //DEBTRACE(" --- entity: " << zoneList(i)->GetName().toStdString() << " " << zoneList(i)->GetKind());
235 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( zoneList(i) );
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() )
243 Handle(HYDROData_Entity) aMergeObject;
244 Handle(HYDROData_Object) aRefGeomObj = Handle(HYDROData_Object)::DownCast( anIter.Value() );
245 if ( !aRefGeomObj.IsNull() )
247 DEBTRACE(" aRefGeomObj " << aRefGeomObj->GetName().toStdString());
248 if (aRefGeomObj->GetName() == anObjectName)
250 DEBTRACE(" BINGO! modify altitude object in zone");
251 aMergeObject = aRefGeomObj->GetAltitudeObject();
252 aZone->SetMergeObject( aMergeObject );
263 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
265 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
269 QApplication::setOverrideCursor( Qt::WaitCursor );
270 TopoDS_Shape aZoneShape;
272 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
273 HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
274 if ( !aPolyline.IsNull() )
276 aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
277 if( aZoneShape.IsNull() )
278 printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
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 )
288 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
290 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
291 if ( !aCtx.IsNull() )
292 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
296 if ( aViewManager && myPreviewPrs )
298 QColor aFillingColor = Qt::darkBlue;
299 QColor aBorderColor = Qt::transparent;
300 if ( !myEditedObject.IsNull() ) {
301 aFillingColor = myEditedObject->GetFillingColor();
302 aBorderColor = myEditedObject->GetBorderColor();
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 );
313 QApplication::restoreOverrideCursor();
316 void HYDROGUI_ImmersibleZoneOp::closePreview()