1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ZoneOp.h"
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"
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_Polyline.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
42 #include <TopoDS_Wire.hxx>
44 HYDROGUI_ZoneOp::HYDROGUI_ZoneOp( HYDROGUI_Module* theModule,
45 const bool theIsEdit )
46 : HYDROGUI_Operation( theModule ),
47 myIsEdit( theIsEdit ),
51 setName( theIsEdit ? tr( "EDIT_ZONE" ) : tr( "CREATE_ZONE" ) );
54 HYDROGUI_ZoneOp::~HYDROGUI_ZoneOp()
59 void HYDROGUI_ZoneOp::startOperation()
61 HYDROGUI_Operation::startOperation();
63 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
67 aPanel->blockSignals( true );
71 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Zone" );
73 QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
74 QColor aBorderColor( HYDROData_Zone::DefaultBorderColor() );
75 QString aSelectedPolyline;
76 QStringList aSelectedBathymetries;
80 myEditedObject = Handle(HYDROData_Zone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
81 if( !myEditedObject.IsNull() )
83 anObjectName = myEditedObject->GetName();
85 aFillingColor = myEditedObject->GetFillingColor();
86 aBorderColor = myEditedObject->GetBorderColor();
88 Handle(HYDROData_Polyline) aRefPolyline = myEditedObject->GetPolyline();
89 if ( !aRefPolyline.IsNull() )
90 aSelectedPolyline = aRefPolyline->GetName();
92 HYDROData_SequenceOfObjects aRefBathymetries = myEditedObject->GetBathymetries();
93 HYDROData_SequenceOfObjects::Iterator anIter( aRefBathymetries );
94 for ( ; anIter.More(); anIter.Next() )
96 Handle(HYDROData_Bathymetry) aRefBathymetry =
97 Handle(HYDROData_Bathymetry)::DownCast( anIter.Value() );
98 if ( aRefBathymetry.IsNull() )
101 QString aRefBathymetryName = aRefBathymetry->GetName();
102 if ( aRefBathymetryName.isEmpty() )
105 aSelectedBathymetries.append( aRefBathymetryName );
110 // collect information about existing closed polylines
111 QStringList aPolylines;
113 HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
114 for ( ; anIter.More(); anIter.Next() )
116 Handle(HYDROData_Polyline) aPolylineObj =
117 Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
118 if ( aPolylineObj.IsNull() || !aPolylineObj->isClosed() )
121 QString aPolylineName = aPolylineObj->GetName();
122 if ( aPolylineName.isEmpty() )
125 aPolylines.append( aPolylineName );
128 // collect information about existing bathymetries
129 QStringList aBathymetries;
131 anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
132 for ( ; anIter.More(); anIter.Next() )
134 Handle(HYDROData_Bathymetry) aBathymetryObj =
135 Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
136 if ( aBathymetryObj.IsNull() )
139 QString aBathymetryName = aBathymetryObj->GetName();
140 if ( aBathymetryName.isEmpty() )
143 aBathymetries.append( aBathymetryName );
146 aPanel->setObjectName( anObjectName );
148 aPanel->setFillingColor( aFillingColor );
149 aPanel->setBorderColor( aBorderColor );
151 aPanel->setPolylineNames( aPolylines );
153 aPanel->setBathymetries( aBathymetries );
154 aPanel->setSelectedBathymetries( aSelectedBathymetries );
156 aPanel->blockSignals( false );
158 aPanel->setPolylineName( aSelectedPolyline );
162 void HYDROGUI_ZoneOp::abortOperation()
166 HYDROGUI_Operation::abortOperation();
169 void HYDROGUI_ZoneOp::commitOperation()
173 HYDROGUI_Operation::commitOperation();
176 HYDROGUI_InputPanel* HYDROGUI_ZoneOp::createInputPanel() const
178 HYDROGUI_ZoneDlg* aPanel = new HYDROGUI_ZoneDlg( module(), getName() );
179 connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
180 this, SLOT( onCreatePreview( const QString& ) ) );
184 bool HYDROGUI_ZoneOp::processApply( int& theUpdateFlags,
185 QString& theErrorMsg )
187 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
191 QString anObjectName = aPanel->getObjectName().simplified();
192 if ( anObjectName.isEmpty() )
194 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
198 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
200 // check that there are no other objects with the same name in the document
201 Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
202 if( !anObject.IsNull() )
204 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
209 Handle(HYDROData_Zone) aZoneObj = myIsEdit ? myEditedObject :
210 Handle(HYDROData_Zone)::DownCast( doc()->CreateObject( KIND_ZONE ) );
211 if ( aZoneObj.IsNull() )
214 aZoneObj->SetName( anObjectName );
216 aZoneObj->SetFillingColor( aPanel->getFillingColor() );
217 aZoneObj->SetBorderColor( aPanel->getBorderColor() );
219 Handle(HYDROData_Polyline) aZonePolyline;
221 QString aPolylineName = aPanel->getPolylineName();
222 if ( !aPolylineName.isEmpty() )
224 aZonePolyline = Handle(HYDROData_Polyline)::DownCast(
225 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
228 aZoneObj->SetPolyline( aZonePolyline );
231 aZoneObj->RemoveBathymetries();
233 QStringList aBathymetries = aPanel->getSelectedBathymetries();
234 for ( int i = 0; i < aBathymetries.length(); ++i )
236 const QString& aBathymetryName = aBathymetries.at( i );
237 if ( aBathymetryName.isEmpty() )
240 Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
241 HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
242 if ( aBathymetry.IsNull() )
245 aZoneObj->AddBathymetry( aBathymetry );
251 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
253 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
258 void HYDROGUI_ZoneOp::onCreatePreview( const QString& thePolylineName )
260 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
266 Handle(HYDROData_Polyline) aPolyline = Handle(HYDROData_Polyline)::DownCast(
267 HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINE ) );
268 if ( !aPolyline.IsNull() )
270 aWire = aPolyline->Wire();
273 LightApp_Application* anApp = module()->getApp();
274 if ( !myViewManager )
275 myViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
276 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
278 if ( myViewManager && !myPreviewPrs )
280 if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
282 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
283 if ( !aCtx.IsNull() )
284 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
288 if ( !myViewManager || !myPreviewPrs )
291 myPreviewPrs->setFillingColor( aPanel->getFillingColor(), false, false );
292 myPreviewPrs->setBorderColor( aPanel->getBorderColor(), false, false );
293 myPreviewPrs->setFace( aWire );
296 void HYDROGUI_ZoneOp::closePreview()