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
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_PolylineOp.h"
24 #include "HYDROGUI_PolylineDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROData_Document.h"
29 #include "HYDROData_Polyline.h"
30 #include "CurveCreator_Curve.hxx"
31 #include "CurveCreator_Displayer.h"
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
41 #include <OCCViewer_AISSelector.h>
43 #include <Precision.hxx>
45 //static int ZValueIncrement = 0;
47 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
48 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL),
51 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
54 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
60 * Redirect the delete action to input panel
62 void HYDROGUI_PolylineOp::deleteSelected()
64 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
65 aPanel->deleteSelected();
69 * Checks whether there are some to delete
71 bool HYDROGUI_PolylineOp::deleteEnabled()
73 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
74 return aPanel->deleteEnabled();
77 void HYDROGUI_PolylineOp::startOperation()
85 HYDROGUI_Operation::startOperation();
87 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
90 LightApp_Application* anApp = module()->getApp();
92 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
93 aPanel->setOCCViewer( myViewManager ? myViewManager->getOCCViewer() : 0 );
96 myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
97 if( !myEditedObject.IsNull() )
99 int anIntDim = myEditedObject->GetDimension();
100 CurveCreator::Dimension aDim = CurveCreator::Dim3d;
102 aDim = CurveCreator::Dim2d;
103 myCurve = new CurveCreator_Curve(aDim);
104 QList<PolylineSection> aPolylineData = myEditedObject->GetPolylineData();
106 for( int i = 0 ; i < aPolylineData.size() ; i++ ){
107 std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
108 bool isClosed = aPolylineData[i].myIsClosed;
109 CurveCreator::SectionType aType = CurveCreator::Polyline;
110 if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
111 aType = CurveCreator::Spline;
113 CurveCreator::Coordinates aCoords;
114 for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
115 aCoords.push_back(aPolylineData[i].myCoords[j]);
117 myCurve->addSection( aName, aType, isClosed, aCoords );
119 aPanel->setPolylineName( myEditedObject->GetName() );
123 myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);
124 //aPanel->setCurve(myCurve);
125 QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
126 aPanel->setPolylineName(aNewName);
128 aPanel->setCurve(myCurve);
133 void HYDROGUI_PolylineOp::abortOperation()
135 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
137 aPanel->setOCCViewer( 0 );
140 HYDROGUI_Operation::abortOperation();
143 void HYDROGUI_PolylineOp::commitOperation()
145 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
147 aPanel->setOCCViewer( 0 );
150 HYDROGUI_Operation::commitOperation();
153 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
155 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
156 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
160 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
161 QString& theErrorMsg )
163 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
165 int aStudyId = module()->getStudyId();
166 bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
167 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
168 if( aDocument.IsNull() )
171 Handle(HYDROData_Polyline) aPolylineObj;
173 aPolylineObj = myEditedObject;
176 aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
178 //double aZValue = double( ++ZValueIncrement ) * 1e-2; // empiric value, to be revised
179 //aPolylineObj->SetZValue( aZValue );
182 if( aPolylineObj.IsNull() )
185 QString aPolylineName = aPanel->getPolylineName();
186 aPolylineObj->SetName(aPolylineName);
188 if( myCurve->getDimension() == CurveCreator::Dim2d )
190 aPolylineObj->SetDimension(aDimInt);
191 QList<PolylineSection> aPolylineData;
192 for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
193 PolylineSection aSect;
194 aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
195 aSect.myIsClosed = myCurve->isClosed(i);
196 aSect.myType = PolylineSection::SECTION_POLYLINE;
197 if( myCurve->getSectionType(i) == CurveCreator::Spline ){
198 aSect.myType = PolylineSection::SECTION_SPLINE;
200 CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
201 for( int j = 0 ; j < aCoords.size() ; j++ ){
202 aSect.myCoords << aCoords.at(j);
204 aPolylineData << aSect;
206 aPolylineObj->SetPolylineData(aPolylineData);
208 // the viewer should be release from the widget before the module update it
209 // because it has an opened local context and updated presentation should not be displayed in it
211 aPanel->setOCCViewer( 0 );
214 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aPolylineObj, true );
216 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
221 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
223 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
228 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
231 QList<int> aSelSections = aPanel->getSelectedSections();
232 for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
234 if( aSelSections.contains(i) ){
235 aDisplayer->highlight( myCurve->constructSection(i), aIsHl );
240 void HYDROGUI_PolylineOp::displayPreview()
244 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
246 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
249 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
250 myCurve->setDisplayer( aDisplayer );
251 aDisplayer->display( myCurve->constructWire() );
257 void HYDROGUI_PolylineOp::erasePreview()
259 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
260 if( myViewManager && aDisplayer )
262 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
264 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();