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 //static int ZValueIncrement = 0;
49 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
50 : HYDROGUI_Operation( theModule ),
51 myIsEdit( theIsEdit ),
55 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
58 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
64 * Redirect the delete action to input panel
66 void HYDROGUI_PolylineOp::deleteSelected()
68 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
69 aPanel->deleteSelected();
73 * Checks whether there are some to delete
75 bool HYDROGUI_PolylineOp::deleteEnabled()
77 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
78 return aPanel->deleteEnabled();
81 void HYDROGUI_PolylineOp::startOperation()
86 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
88 HYDROGUI_Operation::startOperation();
90 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
93 LightApp_Application* anApp = module()->getApp();
95 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
96 aPanel->setOCCViewer( myViewManager ? myViewManager->getOCCViewer() : 0 );
99 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
101 QString aPolylineName;
102 if( !myEditedObject.IsNull() )
104 NCollection_Sequence<TCollection_AsciiString> aSectNames;
105 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
106 NCollection_Sequence<bool> aSectClosures;
107 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
109 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
111 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
112 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
113 bool aSectClosure = aSectClosures.Value( i );
115 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
116 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
117 aCurveType = CurveCreator::Spline;
119 CurveCreator::Coordinates aCurveCoords;
121 HYDROData_PolylineXY::PointsList aSectPointsList = myEditedObject->GetPoints( i - 1 );
122 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
124 const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
125 aCurveCoords.push_back( aSectPoint.X() );
126 aCurveCoords.push_back( aSectPoint.Y() );
129 myCurve->addSectionInternal( aSectName.toStdString(), aCurveType, aSectClosure, aCurveCoords );
132 aPolylineName = myEditedObject->GetName();
136 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
139 aPanel->setPolylineName( aPolylineName );
140 aPanel->setCurve( myCurve );
145 void HYDROGUI_PolylineOp::abortOperation()
147 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
149 aPanel->setOCCViewer( 0 );
152 HYDROGUI_Operation::abortOperation();
155 void HYDROGUI_PolylineOp::commitOperation()
157 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
159 aPanel->setOCCViewer( 0 );
162 HYDROGUI_Operation::commitOperation();
165 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
167 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
168 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
172 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
173 QString& theErrorMsg )
175 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
179 QString aPolylineName = aPanel->getPolylineName().simplified();
180 if ( aPolylineName.isEmpty() )
182 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
186 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
188 // check that there are no other objects with the same name in the document
189 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
190 if( !anObject.IsNull() )
192 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
197 if ( myCurve->getNbSections() <= 0 )
199 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
203 Handle(HYDROData_PolylineXY) aPolylineObj;
206 aPolylineObj = myEditedObject;
207 aPolylineObj->RemoveSections();
211 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
214 if( aPolylineObj.IsNull() )
217 aPolylineObj->SetName( aPolylineName );
219 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
221 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
222 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
223 bool aSectClosure = myCurve->isClosed( i );
225 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
227 if ( aCurveType == CurveCreator::Spline )
228 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
230 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
232 // Add the points fro section
233 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
235 if ( aCurveCoords.size() <= 2 )
237 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
241 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
243 HYDROData_PolylineXY::Point aSectPoint;
245 aSectPoint.SetX( aCurveCoords.at( k ) );
247 aSectPoint.SetY( aCurveCoords.at( k ) );
249 aPolylineObj->AddPoint( i, aSectPoint );
253 // Update the wire of polyline
254 aPolylineObj->Update();
256 // the viewer should be release from the widget before the module update it
257 // because it has an opened local context and updated presentation should not be displayed in it
259 aPanel->setOCCViewer( 0 );
261 theUpdateFlags = UF_Model;
263 // the polyline should be rebuild in all viewers, where it is displayed
264 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
265 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
267 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
268 if ( anActiveViewId == 0 )
270 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
275 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
281 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
283 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
288 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
291 //QList<int> aSelSections = aPanel->getSelectedSections();
293 //if( aSelSections.contains(i) ){
295 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
299 void HYDROGUI_PolylineOp::displayPreview()
303 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
305 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
308 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
309 myCurve->setDisplayer( aDisplayer );
310 aDisplayer->display( myCurve->getAISObject( true ), true );
316 void HYDROGUI_PolylineOp::erasePreview()
318 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
319 if( myViewManager && aDisplayer )
321 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
323 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
326 aDisplayer->eraseAll( true );