Salome HOME
Merging with V7_main branch.
[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 "HYDROGUI_UpdateFlags.h"
27
28 #include "HYDROData_Document.h"
29 #include "HYDROData_Polyline.h"
30 #include "CurveCreator_Curve.hxx"
31 #include "CurveCreator_Displayer.h"
32
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
40
41 #include <OCCViewer_AISSelector.h>
42
43 #include <Precision.hxx>
44
45 //static int ZValueIncrement = 0;
46
47 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
48 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL), 
49   myViewManager(NULL), myCurveDisplayer(NULL)
50 {
51   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
52 }
53
54 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
55 {
56   erasePreview();
57 }
58
59 void HYDROGUI_PolylineOp::startOperation()
60 {
61   if( myCurve )
62   {
63     delete myCurve;
64     myCurve = 0;
65   }
66
67   HYDROGUI_Operation::startOperation();
68
69   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
70   aPanel->reset();
71
72   LightApp_Application* anApp = module()->getApp();
73   myViewManager =
74     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
75   aPanel->setOCCViewer( myViewManager ? myViewManager->getOCCViewer() : 0 );
76
77   if( myIsEdit )
78     myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79   if( !myEditedObject.IsNull() )
80   {
81     int anIntDim = myEditedObject->GetDimension();
82     CurveCreator::Dimension aDim = CurveCreator::Dim3d;
83     if( anIntDim == 2 )
84       aDim = CurveCreator::Dim2d;
85     myCurve = new CurveCreator_Curve(aDim);
86     QList<PolylineSection> aPolylineData = myEditedObject->GetPolylineData();
87
88     for( int i = 0 ; i < aPolylineData.size() ; i++ ){
89       std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
90       bool isClosed = aPolylineData[i].myIsClosed;
91       CurveCreator::SectionType aType = CurveCreator::Polyline;
92       if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
93         aType = CurveCreator::Spline;
94       }
95       CurveCreator::Coordinates aCoords;
96       for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
97         aCoords.push_back(aPolylineData[i].myCoords[j]);
98       }
99       myCurve->addSection( aName, aType, isClosed, aCoords );
100     }
101     aPanel->setPolylineName( myEditedObject->GetName() );
102
103   }
104   else{
105     myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);
106     //aPanel->setCurve(myCurve);
107     QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
108     aPanel->setPolylineName(aNewName);
109   }
110   aPanel->setCurve(myCurve);
111
112   displayPreview();
113 }
114
115 void HYDROGUI_PolylineOp::abortOperation()
116 {
117   erasePreview();
118
119   HYDROGUI_Operation::abortOperation();
120 }
121
122 void HYDROGUI_PolylineOp::commitOperation()
123 {
124   erasePreview();
125
126   HYDROGUI_Operation::commitOperation();
127 }
128
129 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
130 {
131   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
132   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
133   return aDlg;
134 }
135
136 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
137                                         QString& theErrorMsg )
138 {
139   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
140
141   int aStudyId = module()->getStudyId();
142   bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
143   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
144   if( aDocument.IsNull() )
145     return false;
146
147   Handle(HYDROData_Polyline) aPolylineObj;
148   if( myIsEdit ){
149     aPolylineObj = myEditedObject;
150   }
151   else{
152     aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
153
154     //double aZValue = double( ++ZValueIncrement ) * 1e-2; // empiric value, to be revised
155     //aPolylineObj->SetZValue( aZValue );
156   }
157
158   if( aPolylineObj.IsNull() )
159     return false;
160
161   QString aPolylineName = aPanel->getPolylineName();
162   aPolylineObj->SetName(aPolylineName);
163   int aDimInt = 3;
164   if( myCurve->getDimension() == CurveCreator::Dim2d )
165     aDimInt = 2;
166   aPolylineObj->SetDimension(aDimInt);
167   QList<PolylineSection> aPolylineData;
168   for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
169     PolylineSection aSect;
170     aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
171     aSect.myIsClosed = myCurve->isClosed(i);
172     aSect.myType = PolylineSection::SECTION_POLYLINE;
173     if( myCurve->getSectionType(i) == CurveCreator::Spline ){
174       aSect.myType = PolylineSection::SECTION_SPLINE;
175     }
176     CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
177     for( int j = 0 ; j < aCoords.size() ; j++ ){
178       aSect.myCoords << aCoords.at(j);
179     }
180     aPolylineData << aSect;
181   }
182   aPolylineObj->SetPolylineData(aPolylineData);
183
184   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
185   module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), aPolylineObj, true );
186   return true;
187 }
188
189 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
190 {
191   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
192   if( !aPanel )
193     return;
194   if( !myCurve )
195     return;
196   if( !myCurveDisplayer )
197     return;
198   QList<int> aSelSections = aPanel->getSelectedSections();
199   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
200     bool aIsHl = false;
201     if( aSelSections.contains(i) ){
202       myCurveDisplayer->highlight( myCurve->constructSection(i), aIsHl );
203     }
204   }
205 }
206
207 void HYDROGUI_PolylineOp::displayPreview()
208 {
209   if( myViewManager )
210   {
211     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
212     {
213       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
214       if( !aCtx.IsNull() )
215       {
216         myCurveDisplayer = new CurveCreator_Displayer( aCtx );
217         myCurve->setDisplayer( myCurveDisplayer );
218         myCurveDisplayer->display( myCurve->constructWire() );
219       }
220     }
221   }
222 }
223
224 void HYDROGUI_PolylineOp::erasePreview()
225 {
226   if( myViewManager )
227   {
228     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
229     {
230       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
231       if( !aCtx.IsNull() && myCurveDisplayer )
232       {
233         myCurveDisplayer->erase();
234         delete myCurveDisplayer;
235         myCurveDisplayer = 0;
236       }
237     }
238   }
239 }