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 "CurveCreator.hxx"
27 #include "CurveCreator_Curve.hxx"
28 #include "CurveCreator_CurveEditor.hxx"
29 #include "HYDROGUI_AISCurve.h"
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Polyline.h>
33 #include <CurveCreator_Curve.hxx>
34 #include <CurveCreator_CurveEditor.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 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
47 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL),
48 myActiveViewManager(NULL), myPreviewViewManager(NULL), myAISCurve(NULL)
50 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
53 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
58 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
60 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
61 connect( aDlg ,SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
65 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
66 QString& theErrorMsg )
68 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
70 int aStudyId = module()->getStudyId();
71 bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
72 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
73 if( aDocument.IsNull() )
76 Handle(HYDROData_Polyline) aPolylineObj;
78 aPolylineObj = myEditedObject;
81 aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
84 if( aPolylineObj.IsNull() )
87 QString aPolylineName = aPanel->getPolylineName();
88 aPolylineObj->SetName(aPolylineName);
90 if( myCurve->getDimension() == CurveCreator::Dim2d )
92 aPolylineObj->setDimension(aDimInt);
93 QList<PolylineSection> aPolylineData;
94 for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
95 PolylineSection aSect;
96 aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
97 aSect.myIsClosed = myCurve->isClosed(i);
98 aSect.myType = PolylineSection::SECTION_POLYLINE;
99 if( myCurve->getType(i) == CurveCreator::BSpline ){
100 aSect.myType = PolylineSection::SECTION_SPLINE;
102 CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
103 for( int j = 0 ; j < aCoords.size() ; j++ ){
104 aSect.myCoords << aCoords.at(j);
106 aPolylineData << aSect;
108 aPolylineObj->setPolylineData(aPolylineData);
110 theUpdateFlags = UF_Model;
111 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), aPolylineObj, true );
115 void HYDROGUI_PolylineOp::onCreatePreview()
117 LightApp_Application* anApp = module()->getApp();
119 myActiveViewManager = anApp->activeViewManager();
121 myPreviewViewManager =
122 dynamic_cast<OCCViewer_ViewManager*>( anApp->createViewManager( OCCViewer_Viewer::Type() ) );
123 if( myPreviewViewManager )
125 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
126 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
128 //anApp->selectionMgr()->setEnabled(false); // what the hell?!
129 myPreviewViewManager->setTitle( tr( "CREATE_POLYLINE" ) );
130 OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
131 aViewer->enableSelection(true);
132 aViewer->enableMultiselection(true);
133 Handle_AIS_InteractiveContext aCtx = aViewer->getAISContext();
135 OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView();
138 myAISCurve = new HYDROGUI_AISCurve(myCurve, aCtx);
140 myAISCurve->Display();
144 void HYDROGUI_PolylineOp::startOperation()
146 CurveCreator_Curve* anOldCurve = myCurve;
148 HYDROGUI_Operation::startOperation();
150 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
153 myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
154 if( !myEditedObject.IsNull() )
156 int anIntDim = myEditedObject->getDimension();
157 CurveCreator::Dimension aDim = CurveCreator::Dim3d;
159 aDim = CurveCreator::Dim2d;
160 myCurve = new CurveCreator_Curve(aDim);
161 QList<PolylineSection> aPolylineData = myEditedObject->getPolylineData();
163 CurveCreator_CurveEditor* anEdit = new CurveCreator_CurveEditor(myCurve);
164 for( int i = 0 ; i < aPolylineData.size() ; i++ ){
165 std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
166 bool isClosed = aPolylineData[i].myIsClosed;
167 CurveCreator::Type aType = CurveCreator::Polyline;
168 if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
169 aType = CurveCreator::BSpline;
171 CurveCreator::Coordinates aCoords;
172 for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
173 aCoords.push_back(aPolylineData[i].myCoords[j]);
175 anEdit->addSection( aName, aType, isClosed, aCoords );
178 aPanel->setPolylineName( myEditedObject->GetName() );
182 myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);
183 aPanel->setCurve(myCurve);
184 QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), "Polyline" );
185 aPanel->setPolylineName(aNewName);
187 aPanel->setCurve(myCurve);
189 myAISCurve->setCurve(myCurve);
195 void HYDROGUI_PolylineOp::abortOperation()
199 HYDROGUI_Operation::abortOperation();
202 void HYDROGUI_PolylineOp::commitOperation()
206 HYDROGUI_Operation::commitOperation();
209 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
211 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
218 QList<int> aSelSections = aPanel->getSelectedSections();
219 for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
221 if( aSelSections.contains(i) ){
222 myAISCurve->highlightSection(i, aIsHl);
227 void HYDROGUI_PolylineOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
232 void HYDROGUI_PolylineOp::closePreview()
234 if( myPreviewViewManager )
236 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
237 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
239 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
240 myPreviewViewManager = 0;
243 if( myActiveViewManager )
245 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
246 myActiveViewManager = 0;