Salome HOME
Icons are shown in the object browser tree.
[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
23 #include "HYDROGUI_PolylineOp.h"
24
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_PolylineDlg.h"
27 #include "HYDROGUI_Tool.h"
28 #include "HYDROGUI_UpdateFlags.h"
29
30 #include <HYDROData_Document.h>
31
32 #include <CurveCreator_Curve.hxx>
33 #include <CurveCreator_Displayer.h>
34
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
38
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
42
43 #include <OCCViewer_AISSelector.h>
44
45 #include <Precision.hxx>
46
47 //static int ZValueIncrement = 0;
48
49 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
50 : HYDROGUI_Operation( theModule ), 
51   myIsEdit( theIsEdit ),
52   myCurve( NULL ), 
53   myViewManager( NULL )
54 {
55   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
56 }
57
58 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
59 {
60   erasePreview();
61 }
62
63 /**
64  * Redirect the delete action to input panel
65  */
66 void HYDROGUI_PolylineOp::deleteSelected()
67 {
68   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
69   aPanel->deleteSelected();
70 }
71
72 /**
73  * Checks whether there are some to delete
74  */
75 bool HYDROGUI_PolylineOp::deleteEnabled()
76 {
77   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
78   return aPanel->deleteEnabled();
79 }
80
81 void HYDROGUI_PolylineOp::startOperation()
82 {
83   if( myCurve )
84     delete myCurve;
85
86   myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
87
88   HYDROGUI_Operation::startOperation();
89
90   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
91   aPanel->reset();
92
93   LightApp_Application* anApp = module()->getApp();
94   myViewManager =
95     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
96   aPanel->setOCCViewer( myViewManager ? myViewManager->getOCCViewer() : 0 );
97
98   if( myIsEdit )
99     myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
100
101   QString aPolylineName;
102   if( !myEditedObject.IsNull() )
103   {
104     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
105     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
106     NCollection_Sequence<bool>                              aSectClosures;
107     myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
108
109     for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
110     {
111       QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
112       HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
113       bool aSectClosure = aSectClosures.Value( i );
114
115       CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
116       if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
117         aCurveType = CurveCreator::Spline;
118
119       CurveCreator::Coordinates aCurveCoords;
120
121       HYDROData_PolylineXY::PointsList aSectPointsList = myEditedObject->GetPoints( i - 1 );
122       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
123       {
124         const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
125         aCurveCoords.push_back( aSectPoint.X() );
126         aCurveCoords.push_back( aSectPoint.Y() );
127       }
128
129       myCurve->addSectionInternal( aSectName.toStdString(), aCurveType, aSectClosure, aCurveCoords );
130     }
131
132     aPolylineName = myEditedObject->GetName();
133   }
134   else
135   {
136     aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
137   }
138
139   aPanel->setPolylineName( aPolylineName );
140   aPanel->setCurve( myCurve );
141
142   displayPreview();
143 }
144
145 void HYDROGUI_PolylineOp::abortOperation()
146 {
147   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
148   if ( aPanel )
149     aPanel->setOCCViewer( 0 );
150   erasePreview();
151
152   HYDROGUI_Operation::abortOperation();
153 }
154
155 void HYDROGUI_PolylineOp::commitOperation()
156 {
157   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
158   if ( aPanel )
159     aPanel->setOCCViewer( 0 );
160   erasePreview();
161
162   HYDROGUI_Operation::commitOperation();
163 }
164
165 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
166 {
167   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
168   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
169   return aDlg;
170 }
171
172 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
173                                         QString& theErrorMsg )
174 {
175   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
176
177   int aStudyId = module()->getStudyId();
178   bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
179   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
180   if( aDocument.IsNull() )
181     return false;
182
183   Handle(HYDROData_PolylineXY) aPolylineObj;
184   if( myIsEdit )
185   {
186     aPolylineObj = myEditedObject;
187     aPolylineObj->RemoveSections();
188   }
189   else{
190     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
191
192     //double aZValue = double( ++ZValueIncrement ) * 1e-2; // empiric value, to be revised
193     //aPolylineObj->SetZValue( aZValue );
194   }
195
196   if( aPolylineObj.IsNull() )
197     return false;
198
199   QString aPolylineName = aPanel->getPolylineName();
200   aPolylineObj->SetName(aPolylineName);
201
202   for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
203   {
204     TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
205     CurveCreator::SectionType aCurveType =  myCurve->getSectionType( i );
206     bool aSectClosure = myCurve->isClosed( i );
207
208     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
209
210     if ( aCurveType == CurveCreator::Spline )
211       aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
212
213     aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
214
215     // Add the points fro section
216     CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
217     for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
218     {
219       HYDROData_PolylineXY::Point aSectPoint;
220
221       aSectPoint.SetX( aCurveCoords.at( k ) );
222       k++;
223       aSectPoint.SetY( aCurveCoords.at( k ) );
224
225       aPolylineObj->AddPoint( i, aSectPoint );
226     }
227   }
228
229   // Update the wire of polyline
230   aPolylineObj->Update();
231
232   // the viewer should be release from the widget before the module update it
233   // because it has an opened local context and updated presentation should not be displayed in it
234   if ( aPanel )
235     aPanel->setOCCViewer( 0 );
236
237   theUpdateFlags = UF_Model;
238
239   // the polyline should be rebuild in all viewers, where it is displayed
240   theUpdateFlags |= UF_Viewer | UF_GV_Forced;
241   theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
242
243   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
244   if ( anActiveViewId == 0 )
245   {
246     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
247   }
248
249   if( !myIsEdit )
250   {
251     module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
252   }  
253
254   return true;
255 }
256
257 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
258 {
259   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
260   if( !aPanel )
261     return;
262   if( !myCurve )
263     return;
264   CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
265   if( !aDisplayer )
266     return;
267   QList<int> aSelSections = aPanel->getSelectedSections();
268   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
269     bool aIsHl = false;
270     if( aSelSections.contains(i) ){
271       aDisplayer->highlight( myCurve->constructSection(i), aIsHl );
272     }
273   }
274 }
275
276 void HYDROGUI_PolylineOp::displayPreview()
277 {
278   if( myViewManager )
279   {
280     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
281     {
282       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
283       if( !aCtx.IsNull() )
284       {
285         CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
286         myCurve->setDisplayer( aDisplayer );
287         aDisplayer->display( myCurve->constructWire(), true );
288       }
289     }
290   }
291 }
292
293 void HYDROGUI_PolylineOp::erasePreview()
294 {
295   CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
296   if( myViewManager && aDisplayer )
297   {
298     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
299     {
300       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
301       if( !aCtx.IsNull() )
302       {
303         aDisplayer->erase( true );
304       }
305     }
306   }
307 }