Salome HOME
Feature #86: The hierarchy in the Object Browser (T 19).
[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)
50 {
51   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
52 }
53
54 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
55 {
56   erasePreview();
57 }
58
59 /**
60  * Redirect the delete action to input panel
61  */
62 void HYDROGUI_PolylineOp::deleteSelected()
63 {
64   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
65   aPanel->deleteSelected();
66 }
67
68 /**
69  * Checks whether there are some to delete
70  */
71 bool HYDROGUI_PolylineOp::deleteEnabled()
72 {
73   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
74   return aPanel->deleteEnabled();
75 }
76
77 void HYDROGUI_PolylineOp::startOperation()
78 {
79   if( myCurve )
80   {
81     delete myCurve;
82     myCurve = 0;
83   }
84
85   HYDROGUI_Operation::startOperation();
86
87   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
88   aPanel->reset();
89
90   LightApp_Application* anApp = module()->getApp();
91   myViewManager =
92     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
93   aPanel->setOCCViewer( myViewManager ? myViewManager->getOCCViewer() : 0 );
94
95   if( myIsEdit )
96     myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
97   if( !myEditedObject.IsNull() )
98   {
99     int anIntDim = myEditedObject->GetDimension();
100     CurveCreator::Dimension aDim = CurveCreator::Dim3d;
101     if( anIntDim == 2 )
102       aDim = CurveCreator::Dim2d;
103     myCurve = new CurveCreator_Curve(aDim);
104     QList<PolylineSection> aPolylineData = myEditedObject->GetPolylineData();
105
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;
112       }
113       CurveCreator::Coordinates aCoords;
114       for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
115         aCoords.push_back(aPolylineData[i].myCoords[j]);
116       }
117       myCurve->addSectionInternal( aName, aType, isClosed, aCoords );
118     }
119     aPanel->setPolylineName( myEditedObject->GetName() );
120
121   }
122   else{
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);
127   }
128   aPanel->setCurve(myCurve);
129
130   displayPreview();
131 }
132
133 void HYDROGUI_PolylineOp::abortOperation()
134 {
135   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
136   if ( aPanel )
137     aPanel->setOCCViewer( 0 );
138   erasePreview();
139
140   HYDROGUI_Operation::abortOperation();
141 }
142
143 void HYDROGUI_PolylineOp::commitOperation()
144 {
145   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
146   if ( aPanel )
147     aPanel->setOCCViewer( 0 );
148   erasePreview();
149
150   HYDROGUI_Operation::commitOperation();
151 }
152
153 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
154 {
155   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
156   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
157   return aDlg;
158 }
159
160 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
161                                         QString& theErrorMsg )
162 {
163   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
164
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() )
169     return false;
170
171   Handle(HYDROData_Polyline) aPolylineObj;
172   if( myIsEdit ){
173     aPolylineObj = myEditedObject;
174   }
175   else{
176     aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
177
178     //double aZValue = double( ++ZValueIncrement ) * 1e-2; // empiric value, to be revised
179     //aPolylineObj->SetZValue( aZValue );
180   }
181
182   if( aPolylineObj.IsNull() )
183     return false;
184
185   QString aPolylineName = aPanel->getPolylineName();
186   aPolylineObj->SetName(aPolylineName);
187   int aDimInt = 3;
188   if( myCurve->getDimension() == CurveCreator::Dim2d )
189     aDimInt = 2;
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;
199     }
200     CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
201     for( int j = 0 ; j < aCoords.size() ; j++ ){
202       aSect.myCoords << aCoords.at(j);
203     }
204     aPolylineData << aSect;
205   }
206   aPolylineObj->SetPolylineData(aPolylineData);
207
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
210   if ( aPanel )
211     aPanel->setOCCViewer( 0 );
212
213   if( !myIsEdit )
214     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aPolylineObj, true );
215
216   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
217
218   return true;
219 }
220
221 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
222 {
223   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
224   if( !aPanel )
225     return;
226   if( !myCurve )
227     return;
228   CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
229   if( !aDisplayer )
230     return;
231   QList<int> aSelSections = aPanel->getSelectedSections();
232   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
233     bool aIsHl = false;
234     if( aSelSections.contains(i) ){
235       aDisplayer->highlight( myCurve->constructSection(i), aIsHl );
236     }
237   }
238 }
239
240 void HYDROGUI_PolylineOp::displayPreview()
241 {
242   if( myViewManager )
243   {
244     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
245     {
246       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
247       if( !aCtx.IsNull() )
248       {
249         CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
250         myCurve->setDisplayer( aDisplayer );
251         aDisplayer->display( myCurve->constructWire() );
252       }
253     }
254   }
255 }
256
257 void HYDROGUI_PolylineOp::erasePreview()
258 {
259   CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
260   if( myViewManager && aDisplayer )
261   {
262     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
263     {
264       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
265       if( !aCtx.IsNull() )
266       {
267         aDisplayer->erase();
268       }
269     }
270   }
271 }