1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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_DataObject.h"
27 #include "HYDROGUI_PolylineDlg.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
31 #include <HYDROData_Document.h>
33 #include <CurveCreator_Curve.hxx>
34 #include <CurveCreator_Displayer.hxx>
36 #include <LightApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_UpdateFlags.h>
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
44 #include <OCCViewer_AISSelector.h>
46 #include <Precision.hxx>
48 #include <SUIT_MessageBox.h>
49 #include <SUIT_Desktop.h>
51 //static int ZValueIncrement = 0;
53 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
54 : HYDROGUI_Operation( theModule ),
55 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();
85 * Set Z layer for the operation preview.
86 \param theLayer a layer position
88 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
90 HYDROGUI_Operation::updatePreviewZLayer( theLayer );
92 int aZLayer = getPreviewZLayer();
95 if( getPreviewManager() )
97 if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
99 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
102 Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
103 aCtx->SetZLayer( anObject, aZLayer );
110 void HYDROGUI_PolylineOp::startOperation()
114 if ( isApplyAndClose() )
115 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
116 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
118 // Polyline is imported from GEOM module an is not recognized as
119 // polyline or spline and exist only as shape presentation
120 SUIT_MessageBox::critical( module()->getApp()->desktop(),
121 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
122 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
131 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
133 HYDROGUI_Operation::startOperation();
135 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
138 LightApp_Application* anApp = module()->getApp();
139 OCCViewer_ViewManager* aViewManager =
140 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
141 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
142 setPreviewManager( aViewManager );
144 if ( isApplyAndClose() )
147 QString aPolylineName;
148 if( !myEditedObject.IsNull() )
150 NCollection_Sequence<TCollection_AsciiString> aSectNames;
151 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
152 NCollection_Sequence<bool> aSectClosures;
153 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
155 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
157 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
158 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
159 bool aSectClosure = aSectClosures.Value( i );
161 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
162 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
163 aCurveType = CurveCreator::Spline;
165 CurveCreator::Coordinates aCurveCoords;
167 HYDROData_PolylineXY::PointsList aSectPointsList = myEditedObject->GetPoints( i - 1 );
168 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
170 const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
171 aCurveCoords.push_back( aSectPoint.X() );
172 aCurveCoords.push_back( aSectPoint.Y() );
175 myCurve->addSectionInternal( aSectName.toStdString(), aCurveType, aSectClosure, aCurveCoords );
178 aPolylineName = myEditedObject->GetName();
182 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
185 aPanel->setPolylineName( aPolylineName );
186 aPanel->setCurve( myCurve );
191 void HYDROGUI_PolylineOp::abortOperation()
195 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
197 aPanel->setOCCViewer( 0 );
200 HYDROGUI_Operation::abortOperation();
203 void HYDROGUI_PolylineOp::commitOperation()
205 if ( isApplyAndClose() )
209 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
211 aPanel->setOCCViewer( 0 );
216 HYDROGUI_Operation::commitOperation();
219 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
221 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
222 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
226 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
227 QString& theErrorMsg,
228 QStringList& theBrowseObjectsEntries )
230 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
234 QString aPolylineName = aPanel->getPolylineName().simplified();
235 if ( aPolylineName.isEmpty() )
237 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
241 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
243 // check that there are no other objects with the same name in the document
244 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
245 if( !anObject.IsNull() )
247 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
252 if ( myCurve->getNbSections() <= 0 )
254 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
258 Handle(HYDROData_PolylineXY) aPolylineObj;
261 aPolylineObj = myEditedObject;
262 aPolylineObj->RemoveSections();
266 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
269 if( aPolylineObj.IsNull() )
272 aPolylineObj->SetName( aPolylineName );
274 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
276 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
277 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
278 bool aSectClosure = myCurve->isClosed( i );
280 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
282 if ( aCurveType == CurveCreator::Spline )
283 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
285 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
287 // Add the points fro section
288 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
290 if ( aCurveCoords.size() <= 2 )
292 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
296 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
298 HYDROData_PolylineXY::Point aSectPoint;
300 aSectPoint.SetX( aCurveCoords.at( k ) );
302 aSectPoint.SetY( aCurveCoords.at( k ) );
304 aPolylineObj->AddPoint( i, aSectPoint );
310 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
313 // Update the wire of polyline
314 aPolylineObj->Update();
315 module()->setIsToUpdate( aPolylineObj );
317 // the viewer should be release from the widget before the module update it
318 // because it has an opened local context and updated presentation should not be displayed in it
320 aPanel->setOCCViewer( 0 );
322 theUpdateFlags = UF_Model;
324 // the polyline should be rebuild in all viewers, where it is displayed
325 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
326 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
327 theUpdateFlags |= UF_VTKViewer;
329 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
330 if ( anActiveViewId == 0 )
332 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
337 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
338 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
339 theBrowseObjectsEntries.append( anEntry );
345 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
347 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
352 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
355 //QList<int> aSelSections = aPanel->getSelectedSections();
357 //if( aSelSections.contains(i) ){
359 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
363 void HYDROGUI_PolylineOp::displayPreview()
365 if( getPreviewManager() )
367 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
369 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
372 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
373 myCurve->setDisplayer( aDisplayer );
374 aDisplayer->display( myCurve->getAISObject( true ), true );
380 void HYDROGUI_PolylineOp::erasePreview()
382 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
383 if( getPreviewManager() && aDisplayer )
385 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
387 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
390 aDisplayer->eraseAll( true );
395 setPreviewManager( NULL );