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 Handle(HYDROData_PolylineXY) aPolylineObj;
200 aPolylineObj = myEditedObject;
201 aPolylineObj->RemoveSections();
205 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
208 if( aPolylineObj.IsNull() )
211 aPolylineObj->SetName(aPolylineName);
213 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
215 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
216 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
217 bool aSectClosure = myCurve->isClosed( i );
219 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
221 if ( aCurveType == CurveCreator::Spline )
222 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
224 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
226 // Add the points fro section
227 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
228 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
230 HYDROData_PolylineXY::Point aSectPoint;
232 aSectPoint.SetX( aCurveCoords.at( k ) );
234 aSectPoint.SetY( aCurveCoords.at( k ) );
236 aPolylineObj->AddPoint( i, aSectPoint );
240 // Update the wire of polyline
241 aPolylineObj->Update();
243 // the viewer should be release from the widget before the module update it
244 // because it has an opened local context and updated presentation should not be displayed in it
246 aPanel->setOCCViewer( 0 );
248 theUpdateFlags = UF_Model;
250 // the polyline should be rebuild in all viewers, where it is displayed
251 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
252 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
254 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
255 if ( anActiveViewId == 0 )
257 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
262 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
268 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
270 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
275 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
278 //QList<int> aSelSections = aPanel->getSelectedSections();
280 //if( aSelSections.contains(i) ){
281 aDisplayer->highlight( myCurve->constructWire(), aIsHl );
285 void HYDROGUI_PolylineOp::displayPreview()
289 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
291 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
294 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
295 myCurve->setDisplayer( aDisplayer );
296 aDisplayer->display( myCurve->constructWire(), true );
302 void HYDROGUI_PolylineOp::erasePreview()
304 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
305 if( myViewManager && aDisplayer )
307 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
309 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
312 aDisplayer->erase( true );