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_PolylineOp.h"
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_PolylineDlg.h"
27 #include "HYDROGUI_Tool.h"
28 #include "HYDROGUI_UpdateFlags.h"
30 #include <HYDROData_Document.h>
32 #include <CurveCreator_Curve.hxx>
33 #include <CurveCreator_Displayer.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
43 #include <OCCViewer_AISSelector.h>
45 #include <Precision.hxx>
47 #include <SUIT_MessageBox.h>
48 #include <SUIT_Desktop.h>
50 //static int ZValueIncrement = 0;
52 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
53 : HYDROGUI_Operation( theModule ),
54 myIsEdit( theIsEdit ),
57 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
60 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
66 * Redirect the delete action to input panel
68 void HYDROGUI_PolylineOp::deleteSelected()
70 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
71 aPanel->deleteSelected();
75 * Checks whether there are some to delete
77 bool HYDROGUI_PolylineOp::deleteEnabled()
79 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
80 return aPanel->deleteEnabled();
84 * Set Z layer for the operation preview.
85 \param theLayer a layer position
87 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
89 setPreviewZLayer( theLayer );
91 int aZLayer = getPreviewZLayer();
94 if( getPreviewManager() )
96 if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
98 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
101 Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
102 aCtx->SetZLayer( anObject, aZLayer );
109 void HYDROGUI_PolylineOp::startOperation()
113 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
114 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
116 // Polyline is imported from GEOM module an is not recognized as
117 // polyline or spline and exist only as shape presentation
118 SUIT_MessageBox::critical( module()->getApp()->desktop(),
119 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
120 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
129 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
131 HYDROGUI_Operation::startOperation();
133 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
136 LightApp_Application* anApp = module()->getApp();
137 OCCViewer_ViewManager* aViewManager =
138 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
139 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
140 setPreviewManager( aViewManager );
142 QString aPolylineName;
143 if( !myEditedObject.IsNull() )
145 NCollection_Sequence<TCollection_AsciiString> aSectNames;
146 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
147 NCollection_Sequence<bool> aSectClosures;
148 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
150 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
152 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
153 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
154 bool aSectClosure = aSectClosures.Value( i );
156 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
157 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
158 aCurveType = CurveCreator::Spline;
160 CurveCreator::Coordinates aCurveCoords;
162 HYDROData_PolylineXY::PointsList aSectPointsList = myEditedObject->GetPoints( i - 1 );
163 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
165 const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
166 aCurveCoords.push_back( aSectPoint.X() );
167 aCurveCoords.push_back( aSectPoint.Y() );
170 myCurve->addSectionInternal( aSectName.toStdString(), aCurveType, aSectClosure, aCurveCoords );
173 aPolylineName = myEditedObject->GetName();
177 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
180 aPanel->setPolylineName( aPolylineName );
181 aPanel->setCurve( myCurve );
186 void HYDROGUI_PolylineOp::abortOperation()
188 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
190 aPanel->setOCCViewer( 0 );
193 HYDROGUI_Operation::abortOperation();
196 void HYDROGUI_PolylineOp::commitOperation()
198 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
200 aPanel->setOCCViewer( 0 );
203 HYDROGUI_Operation::commitOperation();
206 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
208 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
209 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
213 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
214 QString& theErrorMsg )
216 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
220 QString aPolylineName = aPanel->getPolylineName().simplified();
221 if ( aPolylineName.isEmpty() )
223 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
227 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
229 // check that there are no other objects with the same name in the document
230 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
231 if( !anObject.IsNull() )
233 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
238 if ( myCurve->getNbSections() <= 0 )
240 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
244 Handle(HYDROData_PolylineXY) aPolylineObj;
247 aPolylineObj = myEditedObject;
248 aPolylineObj->RemoveSections();
252 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
255 if( aPolylineObj.IsNull() )
258 aPolylineObj->SetName( aPolylineName );
260 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
262 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
263 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
264 bool aSectClosure = myCurve->isClosed( i );
266 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
268 if ( aCurveType == CurveCreator::Spline )
269 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
271 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
273 // Add the points fro section
274 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
276 if ( aCurveCoords.size() <= 2 )
278 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
282 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
284 HYDROData_PolylineXY::Point aSectPoint;
286 aSectPoint.SetX( aCurveCoords.at( k ) );
288 aSectPoint.SetY( aCurveCoords.at( k ) );
290 aPolylineObj->AddPoint( i, aSectPoint );
296 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
299 // Update the wire of polyline
300 aPolylineObj->Update();
301 module()->setIsToUpdate( aPolylineObj );
303 // the viewer should be release from the widget before the module update it
304 // because it has an opened local context and updated presentation should not be displayed in it
306 aPanel->setOCCViewer( 0 );
308 theUpdateFlags = UF_Model;
310 // the polyline should be rebuild in all viewers, where it is displayed
311 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
312 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
313 theUpdateFlags |= UF_VTKViewer;
315 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
316 if ( anActiveViewId == 0 )
318 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
323 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
329 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
331 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
336 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
339 //QList<int> aSelSections = aPanel->getSelectedSections();
341 //if( aSelSections.contains(i) ){
343 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
347 void HYDROGUI_PolylineOp::displayPreview()
349 if( getPreviewManager() )
351 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
353 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
356 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
357 myCurve->setDisplayer( aDisplayer );
358 aDisplayer->display( myCurve->getAISObject( true ), true );
364 void HYDROGUI_PolylineOp::erasePreview()
366 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
367 if( getPreviewManager() && aDisplayer )
369 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
371 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
374 aDisplayer->eraseAll( true );
379 setPreviewManager( NULL );