1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_PolylineOp.h"
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_PolylineDlg.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
27 #include <HYDROData_Document.h>
29 #include <CurveCreator_Curve.hxx>
30 #include <CurveCreator_Displayer.hxx>
32 #include <LightApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34 #include <LightApp_UpdateFlags.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
40 #include <OCCViewer_AISSelector.h>
42 #include <Precision.hxx>
44 #include <SUIT_MessageBox.h>
45 #include <SUIT_Desktop.h>
47 //static int ZValueIncrement = 0;
49 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
50 : HYDROGUI_Operation( theModule ),
51 myIsEdit( theIsEdit ),
54 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
57 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
63 * Redirect the delete action to input panel
65 void HYDROGUI_PolylineOp::deleteSelected()
67 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
68 aPanel->deleteSelected();
72 * Checks whether there are some to delete
74 bool HYDROGUI_PolylineOp::deleteEnabled()
76 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
77 return aPanel->deleteEnabled();
81 * Set Z layer for the operation preview.
82 \param theLayer a layer position
84 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
86 HYDROGUI_Operation::updatePreviewZLayer( theLayer );
88 int aZLayer = getPreviewZLayer();
91 if( getPreviewManager() )
93 if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
95 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
98 Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
99 aCtx->SetZLayer( anObject, aZLayer );
106 void HYDROGUI_PolylineOp::startOperation()
110 if ( isApplyAndClose() )
111 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
112 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
114 // Polyline is imported from GEOM module an is not recognized as
115 // polyline or spline and exist only as shape presentation
116 SUIT_MessageBox::critical( module()->getApp()->desktop(),
117 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
118 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
127 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
129 HYDROGUI_Operation::startOperation();
131 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
134 LightApp_Application* anApp = module()->getApp();
135 OCCViewer_ViewManager* aViewManager =
136 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
137 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
138 setPreviewManager( aViewManager );
140 if ( isApplyAndClose() )
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()
191 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
193 aPanel->setOCCViewer( 0 );
196 HYDROGUI_Operation::abortOperation();
199 void HYDROGUI_PolylineOp::commitOperation()
201 if ( isApplyAndClose() )
205 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
207 aPanel->setOCCViewer( 0 );
212 HYDROGUI_Operation::commitOperation();
215 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
217 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
218 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
222 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
223 QString& theErrorMsg,
224 QStringList& theBrowseObjectsEntries )
226 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
230 QString aPolylineName = aPanel->getPolylineName().simplified();
231 if ( aPolylineName.isEmpty() )
233 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
237 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
239 // check that there are no other objects with the same name in the document
240 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
241 if( !anObject.IsNull() )
243 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
248 if ( myCurve->getNbSections() <= 0 )
250 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
254 Handle(HYDROData_PolylineXY) aPolylineObj;
257 aPolylineObj = myEditedObject;
258 aPolylineObj->RemoveSections();
262 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
265 if( aPolylineObj.IsNull() )
268 aPolylineObj->SetName( aPolylineName );
270 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
272 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
273 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
274 bool aSectClosure = myCurve->isClosed( i );
276 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
278 if ( aCurveType == CurveCreator::Spline )
279 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
281 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
283 // Add the points fro section
284 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
286 if ( aCurveCoords.size() <= 2 )
288 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
292 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
294 HYDROData_PolylineXY::Point aSectPoint;
296 aSectPoint.SetX( aCurveCoords.at( k ) );
298 aSectPoint.SetY( aCurveCoords.at( k ) );
300 aPolylineObj->AddPoint( i, aSectPoint );
306 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
309 // Update the wire of polyline
310 aPolylineObj->Update();
311 module()->setIsToUpdate( aPolylineObj );
313 // the viewer should be release from the widget before the module update it
314 // because it has an opened local context and updated presentation should not be displayed in it
316 aPanel->setOCCViewer( 0 );
318 theUpdateFlags = UF_Model;
320 // the polyline should be rebuild in all viewers, where it is displayed
321 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
322 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
323 theUpdateFlags |= UF_VTKViewer;
325 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
326 if ( anActiveViewId == 0 )
328 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
333 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
334 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
335 theBrowseObjectsEntries.append( anEntry );
341 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
343 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
348 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
351 //QList<int> aSelSections = aPanel->getSelectedSections();
353 //if( aSelSections.contains(i) ){
355 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
359 void HYDROGUI_PolylineOp::displayPreview()
361 if( getPreviewManager() )
363 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
365 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
368 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
369 myCurve->setDisplayer( aDisplayer );
370 aDisplayer->display( myCurve->getAISObject( true ), true );
376 void HYDROGUI_PolylineOp::erasePreview()
378 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
379 if( getPreviewManager() && aDisplayer )
381 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
383 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
386 aDisplayer->eraseAll( true );
391 setPreviewManager( NULL );