Salome HOME
HYDRO bug 17: add HYDRO version in About dialog.
[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 #include "CurveCreator.hxx"
27 #include "CurveCreator_Curve.hxx"
28 #include "CurveCreator_CurveEditor.hxx"
29 #include "HYDROGUI_AISCurve.h"
30
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Polyline.h>
33 #include <CurveCreator_Curve.hxx>
34 #include <CurveCreator_CurveEditor.hxx>
35
36 #include <LightApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_UpdateFlags.h>
39
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42
43 #include <OCCViewer_AISSelector.h>
44
45 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
46 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL), 
47   myActiveViewManager(NULL), myPreviewViewManager(NULL), myAISCurve(NULL)
48 {
49   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
50 }
51
52 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
53 {
54 }
55
56 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
57 {
58   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
59   connect( aDlg ,SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
60   return aDlg;
61 }
62
63 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
64                                         QString& theErrorMsg )
65 {
66   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
67
68   int aStudyId = module()->getStudyId();
69   bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
70   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
71   if( aDocument.IsNull() )
72     return false;
73
74   Handle(HYDROData_Polyline) aPolylineObj;
75   if( myIsEdit ){
76     aPolylineObj = myEditedObject;
77   }
78   else{
79     aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
80   }
81
82   if( aPolylineObj.IsNull() )
83     return false;
84
85   QString aPolylineName = aPanel->getPolylineName();
86   aPolylineObj->SetName(aPolylineName);
87   int aDimInt = 3;
88   if( myCurve->getDimension() == CurveCreator::Dim2d )
89     aDimInt = 2;
90   aPolylineObj->setDimension(aDimInt);
91   QList<PolylineSection> aPolylineData;
92   for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
93     PolylineSection aSect;
94     aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
95     aSect.myIsClosed = myCurve->isClosed(i);
96     aSect.myType = PolylineSection::SECTION_POLYLINE;
97     if( myCurve->getType(i) == CurveCreator::BSpline ){
98       aSect.myType = PolylineSection::SECTION_SPLINE;
99     }
100     CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
101     for( int j = 0 ; j < aCoords.size() ; j++ ){
102       aSect.myCoords << aCoords.at(j);
103     }
104     aPolylineData << aSect;
105   }
106   aPolylineObj->setPolylineData(aPolylineData);
107
108   theUpdateFlags = UF_Model;
109   module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), aPolylineObj, true );
110   return true;
111 }
112
113 void HYDROGUI_PolylineOp::onCreatePreview()
114 {
115   LightApp_Application* anApp = module()->getApp();
116
117   myActiveViewManager = anApp->activeViewManager();
118
119   myPreviewViewManager =
120     dynamic_cast<OCCViewer_ViewManager*>( anApp->createViewManager( OCCViewer_Viewer::Type() ) );
121   if( myPreviewViewManager )
122   {
123     anApp->selectionMgr()->setEnabled(false);
124     myPreviewViewManager->setTitle( tr( "CREATE_CURVE" ) );
125     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
126     aViewer->enableSelection(true);
127     aViewer->enableMultiselection(true);
128     Handle_AIS_InteractiveContext aCtx = aViewer->getAISContext();
129
130     myAISCurve = new HYDROGUI_AISCurve(myCurve, aCtx);
131
132     myAISCurve->Display();
133   }
134 }
135
136 void HYDROGUI_PolylineOp::startOperation()
137 {
138   CurveCreator_Curve* anOldCurve = myCurve;
139
140   HYDROGUI_Operation::startOperation();
141
142   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
143   aPanel->reset();
144
145   myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
146   if( !myEditedObject.IsNull() )
147   {
148     int anIntDim = myEditedObject->getDimension();
149     CurveCreator::Dimension aDim = CurveCreator::Dim3d;
150     if( anIntDim == 2 )
151       aDim = CurveCreator::Dim2d;
152     myCurve = new CurveCreator_Curve(aDim);
153     QList<PolylineSection> aPolylineData = myEditedObject->getPolylineData();
154
155     CurveCreator_CurveEditor* anEdit = new CurveCreator_CurveEditor(myCurve);
156     for( int i = 0 ; i < aPolylineData.size() ; i++ ){
157       std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
158       bool isClosed = aPolylineData[i].myIsClosed;
159       CurveCreator::Type aType = CurveCreator::Polyline;
160       if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
161         aType = CurveCreator::BSpline;
162       }
163       CurveCreator::Coordinates aCoords;
164       for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
165         aCoords.push_back(aPolylineData[i].myCoords[j]);
166       }
167       anEdit->addSection( aName, aType, isClosed, aCoords );
168     }
169     delete anEdit;
170     aPanel->setPolylineName( myEditedObject->GetName() );
171
172   }
173   else{
174     myCurve = new CurveCreator_Curve(CurveCreator::Dim3d);
175     aPanel->setCurve(myCurve);
176     QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), tr("POLYLINE_PREFIX") );
177     aPanel->setPolylineName(aNewName);
178   }
179   aPanel->setCurve(myCurve);
180   if( myAISCurve )
181     myAISCurve->setCurve(myCurve);
182   if( anOldCurve )
183     delete anOldCurve;
184   onCreatePreview();
185 }
186
187 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
188 {
189   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
190   if( !aPanel )
191     return;
192   if( !myCurve )
193     return;
194   if( !myAISCurve )
195     return;
196   QList<int> aSelSections = aPanel->getSelectedSections();
197   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
198     bool aIsHl = false;
199     if( aSelSections.contains(i) ){
200       myAISCurve->highlightSection(i, aIsHl);
201     }
202   }
203 }