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 ),
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();
84 void HYDROGUI_PolylineOp::startOperation()
88 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
89 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
91 // Polyline is imported from GEOM module an is not recognized as
92 // polyline or spline and exist only as shape presentation
93 SUIT_MessageBox::critical( module()->getApp()->desktop(),
94 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
95 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
104 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
106 HYDROGUI_Operation::startOperation();
108 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
111 LightApp_Application* anApp = module()->getApp();
113 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
114 aPanel->setOCCViewer( myViewManager ? myViewManager->getOCCViewer() : 0 );
116 QString aPolylineName;
117 if( !myEditedObject.IsNull() )
119 NCollection_Sequence<TCollection_AsciiString> aSectNames;
120 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
121 NCollection_Sequence<bool> aSectClosures;
122 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
124 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
126 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
127 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
128 bool aSectClosure = aSectClosures.Value( i );
130 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
131 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
132 aCurveType = CurveCreator::Spline;
134 CurveCreator::Coordinates aCurveCoords;
136 HYDROData_PolylineXY::PointsList aSectPointsList = myEditedObject->GetPoints( i - 1 );
137 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
139 const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
140 aCurveCoords.push_back( aSectPoint.X() );
141 aCurveCoords.push_back( aSectPoint.Y() );
144 myCurve->addSectionInternal( aSectName.toStdString(), aCurveType, aSectClosure, aCurveCoords );
147 aPolylineName = myEditedObject->GetName();
151 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
154 aPanel->setPolylineName( aPolylineName );
155 aPanel->setCurve( myCurve );
160 void HYDROGUI_PolylineOp::abortOperation()
162 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
164 aPanel->setOCCViewer( 0 );
167 HYDROGUI_Operation::abortOperation();
170 void HYDROGUI_PolylineOp::commitOperation()
172 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
174 aPanel->setOCCViewer( 0 );
177 HYDROGUI_Operation::commitOperation();
180 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
182 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
183 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
187 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
188 QString& theErrorMsg )
190 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
194 QString aPolylineName = aPanel->getPolylineName().simplified();
195 if ( aPolylineName.isEmpty() )
197 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
201 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
203 // check that there are no other objects with the same name in the document
204 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
205 if( !anObject.IsNull() )
207 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
212 if ( myCurve->getNbSections() <= 0 )
214 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
218 Handle(HYDROData_PolylineXY) aPolylineObj;
221 aPolylineObj = myEditedObject;
222 aPolylineObj->RemoveSections();
226 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
229 if( aPolylineObj.IsNull() )
232 aPolylineObj->SetName( aPolylineName );
234 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
236 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
237 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
238 bool aSectClosure = myCurve->isClosed( i );
240 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
242 if ( aCurveType == CurveCreator::Spline )
243 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
245 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
247 // Add the points fro section
248 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
250 if ( aCurveCoords.size() <= 2 )
252 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
256 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
258 HYDROData_PolylineXY::Point aSectPoint;
260 aSectPoint.SetX( aCurveCoords.at( k ) );
262 aSectPoint.SetY( aCurveCoords.at( k ) );
264 aPolylineObj->AddPoint( i, aSectPoint );
270 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
273 // Update the wire of polyline
274 aPolylineObj->Update();
275 module()->setIsToUpdate( aPolylineObj );
277 // the viewer should be release from the widget before the module update it
278 // because it has an opened local context and updated presentation should not be displayed in it
280 aPanel->setOCCViewer( 0 );
282 theUpdateFlags = UF_Model;
284 // the polyline should be rebuild in all viewers, where it is displayed
285 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
286 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
287 theUpdateFlags |= UF_VTKViewer;
289 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
290 if ( anActiveViewId == 0 )
292 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
297 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
303 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
305 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
310 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
313 //QList<int> aSelSections = aPanel->getSelectedSections();
315 //if( aSelSections.contains(i) ){
317 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
321 void HYDROGUI_PolylineOp::displayPreview()
325 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
327 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
330 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
331 myCurve->setDisplayer( aDisplayer );
332 aDisplayer->display( myCurve->getAISObject( true ), true );
338 void HYDROGUI_PolylineOp::erasePreview()
340 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
341 if( myViewManager && aDisplayer )
343 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
345 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
348 aDisplayer->eraseAll( true );
353 myViewManager = NULL;