Salome HOME
Create/edit polyline operation was updated
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PolylineOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_PolylineOp.h"
24 #include "HYDROGUI_PolylineDlg.h"
25 #include "HYDROGUI_Tool.h"
26
27 #include <HYDROData_Document.h>
28 #include <HYDROData_Polyline.h>
29 #include <CurveCreator_Curve.hxx>
30 #include <CurveCreator_CurveEditor.hxx>
31
32 #include <LightApp_Application.h>
33 #include <LightApp_UpdateFlags.h>
34
35
36 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
37 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL)
38 {
39   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
40 }
41
42 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
43 {
44 }
45
46 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
47 {
48   return new HYDROGUI_PolylineDlg( module(), getName() );
49 }
50
51 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
52                                         QString& theErrorMsg )
53 {
54   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
55
56   int aStudyId = module()->getStudyId();
57   bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
58   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
59   if( aDocument.IsNull() )
60     return false;
61
62   Handle(HYDROData_Polyline) aPolylineObj;
63   if( myIsEdit ){
64     aPolylineObj = myEditedObject;
65   }
66   else{
67     aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
68   }
69
70   if( aPolylineObj.IsNull() )
71     return false;
72
73   QString aPolylineName = aPanel->getPolylineName();
74   aPolylineObj->SetName(aPolylineName);
75   int aDimInt = 3;
76   if( myCurve->getDimension() == CurveCreator::Dim2d )
77     aDimInt = 2;
78   aPolylineObj->setDimension(aDimInt);
79   QList<PolylineSection> aPolylineData;
80   for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
81     PolylineSection aSect;
82     aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
83     aSect.myIsClosed = myCurve->isClosed(i);
84     aSect.myType = PolylineSection::SECTION_POLYLINE;
85     if( myCurve->getType(i) == CurveCreator::BSpline ){
86       aSect.myType = PolylineSection::SECTION_SPLINE;
87     }
88     CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
89     for( int j = 0 ; j < aCoords.size() ; j++ ){
90       aSect.myCoords << aCoords.at(j);
91     }
92     aPolylineData << aSect;
93   }
94   aPolylineObj->setPolylineData(aPolylineData);
95
96   theUpdateFlags = UF_Model;
97   aPolylineObj->SetVisibility( true );
98   return true;
99 }
100
101 void HYDROGUI_PolylineOp::startOperation()
102 {
103   CurveCreator_Curve* anOldCurve = myCurve;
104
105   HYDROGUI_Operation::startOperation();
106
107   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
108   aPanel->reset();
109
110   myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
111   if( !myEditedObject.IsNull() )
112   {
113     int anIntDim = myEditedObject->getDimension();
114     CurveCreator::Dimension aDim = CurveCreator::Dim3d;
115     if( anIntDim == 2 )
116       aDim = CurveCreator::Dim2d;
117     myCurve = new CurveCreator_Curve(aDim);
118     QList<PolylineSection> aPolylineData = myEditedObject->getPolylineData();
119
120     CurveCreator_CurveEditor* anEdit = new CurveCreator_CurveEditor(myCurve);
121     for( int i = 0 ; i < aPolylineData.size() ; i++ ){
122       std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
123       bool isClosed = aPolylineData[i].myIsClosed;
124       CurveCreator::Type aType = CurveCreator::Polyline;
125       if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
126         aType = CurveCreator::BSpline;
127       }
128       CurveCreator::Coordinates aCoords;
129       for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
130         aCoords.push_back(aPolylineData[i].myCoords[j]);
131       }
132       anEdit->addSection( aName, aType, isClosed, aCoords );
133     }
134     delete anEdit;
135     aPanel->setPolylineName( myEditedObject->GetName() );
136
137   }
138   else{
139     myCurve = new CurveCreator_Curve(CurveCreator::Dim3d);
140     aPanel->setCurve(myCurve);
141     QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), tr("POLYLINE_PREFIX") );
142     aPanel->setPolylineName(aNewName);
143   }
144   aPanel->setCurve(myCurve);
145   if( anOldCurve )
146     delete anOldCurve;
147 }