1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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_PolylineOp.h"
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_DataObject.h"
27 #include "HYDROGUI_PolylineDlg.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
31 #include <HYDROData_Document.h>
33 #include <CurveCreator_Curve.hxx>
34 #include <CurveCreator_Displayer.hxx>
36 #include <LightApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_UpdateFlags.h>
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
44 #include <OCCViewer_AISSelector.h>
46 #include <Precision.hxx>
48 #include <SUIT_MessageBox.h>
49 #include <SUIT_Desktop.h>
51 //static int ZValueIncrement = 0;
53 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
54 : HYDROGUI_Operation( theModule ),
55 myIsEdit( theIsEdit ),
58 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
61 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
67 * Redirect the delete action to input panel
69 void HYDROGUI_PolylineOp::deleteSelected()
71 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
72 aPanel->deleteSelected();
76 * Checks whether there are some to delete
78 bool HYDROGUI_PolylineOp::deleteEnabled()
80 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
81 return aPanel->deleteEnabled();
85 * Set Z layer for the operation preview.
86 \param theLayer a layer position
88 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
90 HYDROGUI_Operation::updatePreviewZLayer( theLayer );
92 int aZLayer = getPreviewZLayer();
95 if( getPreviewManager() )
97 if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
99 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
102 Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
103 aCtx->SetZLayer( anObject, aZLayer );
110 void HYDROGUI_PolylineOp::startOperation()
114 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
115 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
117 // Polyline is imported from GEOM module an is not recognized as
118 // polyline or spline and exist only as shape presentation
119 SUIT_MessageBox::critical( module()->getApp()->desktop(),
120 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
121 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
130 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
132 HYDROGUI_Operation::startOperation();
134 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
137 LightApp_Application* anApp = module()->getApp();
138 OCCViewer_ViewManager* aViewManager =
139 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
140 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
141 setPreviewManager( aViewManager );
143 QString aPolylineName;
144 if( !myEditedObject.IsNull() )
146 NCollection_Sequence<TCollection_AsciiString> aSectNames;
147 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
148 NCollection_Sequence<bool> aSectClosures;
149 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
151 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
153 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
154 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
155 bool aSectClosure = aSectClosures.Value( i );
157 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
158 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
159 aCurveType = CurveCreator::Spline;
161 CurveCreator::Coordinates aCurveCoords;
163 HYDROData_PolylineXY::PointsList aSectPointsList = myEditedObject->GetPoints( i - 1 );
164 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
166 const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
167 aCurveCoords.push_back( aSectPoint.X() );
168 aCurveCoords.push_back( aSectPoint.Y() );
171 myCurve->addSectionInternal( aSectName.toStdString(), aCurveType, aSectClosure, aCurveCoords );
174 aPolylineName = myEditedObject->GetName();
178 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
181 aPanel->setPolylineName( aPolylineName );
182 aPanel->setCurve( myCurve );
187 void HYDROGUI_PolylineOp::abortOperation()
189 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
191 aPanel->setOCCViewer( 0 );
194 HYDROGUI_Operation::abortOperation();
197 void HYDROGUI_PolylineOp::commitOperation()
199 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
201 aPanel->setOCCViewer( 0 );
204 HYDROGUI_Operation::commitOperation();
207 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
209 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
210 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
214 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
215 QString& theErrorMsg,
216 QStringList& theBrowseObjectsEntries )
218 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
222 QString aPolylineName = aPanel->getPolylineName().simplified();
223 if ( aPolylineName.isEmpty() )
225 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
229 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
231 // check that there are no other objects with the same name in the document
232 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
233 if( !anObject.IsNull() )
235 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
240 if ( myCurve->getNbSections() <= 0 )
242 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
246 Handle(HYDROData_PolylineXY) aPolylineObj;
249 aPolylineObj = myEditedObject;
250 aPolylineObj->RemoveSections();
254 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
257 if( aPolylineObj.IsNull() )
260 aPolylineObj->SetName( aPolylineName );
262 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
264 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
265 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
266 bool aSectClosure = myCurve->isClosed( i );
268 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
270 if ( aCurveType == CurveCreator::Spline )
271 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
273 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
275 // Add the points fro section
276 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
278 if ( aCurveCoords.size() <= 2 )
280 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
284 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
286 HYDROData_PolylineXY::Point aSectPoint;
288 aSectPoint.SetX( aCurveCoords.at( k ) );
290 aSectPoint.SetY( aCurveCoords.at( k ) );
292 aPolylineObj->AddPoint( i, aSectPoint );
298 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
301 // Update the wire of polyline
302 aPolylineObj->Update();
303 module()->setIsToUpdate( aPolylineObj );
305 // the viewer should be release from the widget before the module update it
306 // because it has an opened local context and updated presentation should not be displayed in it
308 aPanel->setOCCViewer( 0 );
310 theUpdateFlags = UF_Model;
312 // the polyline should be rebuild in all viewers, where it is displayed
313 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
314 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
315 theUpdateFlags |= UF_VTKViewer;
317 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
318 if ( anActiveViewId == 0 )
320 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
325 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
326 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
327 theBrowseObjectsEntries.append( anEntry );
333 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
335 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
340 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
343 //QList<int> aSelSections = aPanel->getSelectedSections();
345 //if( aSelSections.contains(i) ){
347 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
351 void HYDROGUI_PolylineOp::displayPreview()
353 if( getPreviewManager() )
355 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
357 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
360 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
361 myCurve->setDisplayer( aDisplayer );
362 aDisplayer->display( myCurve->getAISObject( true ), true );
368 void HYDROGUI_PolylineOp::erasePreview()
370 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
371 if( getPreviewManager() && aDisplayer )
373 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
375 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
378 aDisplayer->eraseAll( true );
383 setPreviewManager( NULL );