Salome HOME
Merge remote-tracking branch 'origin/BR_SINUSX_FORMAT' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PolylineOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_PolylineOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_PolylineDlg.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
26
27 #include <HYDROData_Document.h>
28
29 #include <CurveCreator_Curve.hxx>
30 #include <CurveCreator_Displayer.hxx>
31
32 #include <LightApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34 #include <LightApp_UpdateFlags.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39
40 #include <OCCViewer_AISSelector.h>
41
42 #include <Precision.hxx>
43
44 #include <SUIT_MessageBox.h>
45 #include <SUIT_Desktop.h>
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 {
54   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
55 }
56
57 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
58 {
59   erasePreview();
60 }
61
62 /**
63  * Redirect the delete action to input panel
64  */
65 void HYDROGUI_PolylineOp::deleteSelected()
66 {
67   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
68   aPanel->deleteSelected();
69 }
70
71 /**
72  * Checks whether there are some to delete
73  */
74 bool HYDROGUI_PolylineOp::deleteEnabled()
75 {
76   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
77   return aPanel->deleteEnabled();
78 }
79
80 /**
81  * Set Z layer for the operation preview.
82  \param theLayer a layer position
83  */
84 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
85 {
86   HYDROGUI_Operation::updatePreviewZLayer( theLayer );
87
88   int aZLayer = getPreviewZLayer();
89   if ( aZLayer >= 0 )
90   {
91     if( getPreviewManager() )
92     {
93       if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
94       {
95         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
96         if( !aCtx.IsNull() )
97         {
98           Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
99           aCtx->SetZLayer( anObject, aZLayer );
100         }
101       }
102     }
103   }
104 }
105
106 void HYDROGUI_PolylineOp::startOperation()
107 {
108   if( myIsEdit )
109   {
110     if ( isApplyAndClose() )
111           myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
112     if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
113     {
114       // Polyline is imported from GEOM module an is not recognized as
115       // polyline or spline and exist only as shape presentation
116       SUIT_MessageBox::critical( module()->getApp()->desktop(),
117                                  tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
118                                  tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
119       abort();
120       return;
121     }
122   }
123
124   if( myCurve )
125     delete myCurve;
126
127   myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
128
129   HYDROGUI_Operation::startOperation();
130
131   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
132   aPanel->reset();
133
134   LightApp_Application* anApp = module()->getApp();
135   OCCViewer_ViewManager* aViewManager =
136     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
137   aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
138   setPreviewManager( aViewManager );
139
140   if ( isApplyAndClose() )
141     setCursor();
142
143   QString aPolylineName;
144   if( !myEditedObject.IsNull() )
145   {
146     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
147     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
148     NCollection_Sequence<bool>                              aSectClosures;
149     myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
150
151     for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
152     {
153       QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
154       HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
155       bool aSectClosure = aSectClosures.Value( i );
156
157       CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
158       if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
159         aCurveType = CurveCreator::Spline;
160
161       CurveCreator::Coordinates aCurveCoords;
162
163       HYDROData_PolylineXY::PointsList aSectPointsList = myEditedObject->GetPoints( i - 1 );
164       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
165       {
166         const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
167         aCurveCoords.push_back( aSectPoint.X() );
168         aCurveCoords.push_back( aSectPoint.Y() );
169       }
170
171       myCurve->addSectionInternal( aSectName.toStdString(), aCurveType, aSectClosure, aCurveCoords );
172     }
173
174     aPolylineName = myEditedObject->GetName();
175   }
176   else
177   {
178     aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
179   }
180
181   aPanel->setPolylineName( aPolylineName );
182   aPanel->setCurve( myCurve );
183
184   displayPreview();
185 }
186
187 void HYDROGUI_PolylineOp::abortOperation()
188 {
189   restoreCursor();
190
191   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
192   if ( aPanel )
193     aPanel->setOCCViewer( 0 );
194   erasePreview();
195
196   HYDROGUI_Operation::abortOperation();
197 }
198
199 void HYDROGUI_PolylineOp::commitOperation()
200 {
201   if ( isApplyAndClose() )
202   {
203     restoreCursor();
204
205     HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
206     if ( aPanel )
207       aPanel->setOCCViewer( 0 );
208   }
209
210   erasePreview();
211
212   HYDROGUI_Operation::commitOperation();
213 }
214
215 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
216 {
217   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
218   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
219   return aDlg;
220 }
221
222 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
223                                         QString& theErrorMsg,
224                                         QStringList& theBrowseObjectsEntries )
225 {
226   HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
227   if ( !aPanel )
228     return false;
229
230   QString aPolylineName = aPanel->getPolylineName().simplified();
231   if ( aPolylineName.isEmpty() )
232   {
233     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
234     return false;
235   }
236
237   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
238   {
239     // check that there are no other objects with the same name in the document
240     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
241     if( !anObject.IsNull() )
242     {
243       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
244       return false;
245     }
246   }
247
248   if ( myCurve->getNbSections() <= 0 )
249   {
250     theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
251     return false;
252   }
253
254   Handle(HYDROData_PolylineXY) aPolylineObj;
255   if( myIsEdit )
256   {
257     aPolylineObj = myEditedObject;
258     aPolylineObj->RemoveSections();
259   }
260   else
261   {
262     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
263   }
264
265   if( aPolylineObj.IsNull() )
266     return false;
267
268   aPolylineObj->SetName( aPolylineName );
269
270   for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
271   {
272     TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
273     CurveCreator::SectionType aCurveType =  myCurve->getSectionType( i );
274     bool aSectClosure = myCurve->isClosed( i );
275
276     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
277
278     if ( aCurveType == CurveCreator::Spline )
279       aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
280
281     aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
282
283     // Add the points fro section
284     CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
285
286     if ( aCurveCoords.size() <= 2 )
287     {
288       theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
289       return false;
290     }
291
292     for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
293     {
294       HYDROData_PolylineXY::Point aSectPoint;
295
296       aSectPoint.SetX( aCurveCoords.at( k ) );
297       k++;
298       aSectPoint.SetY( aCurveCoords.at( k ) );
299
300       aPolylineObj->AddPoint( i, aSectPoint );
301     }
302   }
303
304   if ( !myIsEdit )
305   {
306     aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
307   }
308
309   // Update the wire of polyline
310   aPolylineObj->Update();
311   module()->setIsToUpdate( aPolylineObj );
312
313   // the viewer should be release from the widget before the module update it
314   // because it has an opened local context and updated presentation should not be displayed in it
315   if ( aPanel )
316     aPanel->setOCCViewer( 0 );
317
318   theUpdateFlags = UF_Model;
319
320   // the polyline should be rebuild in all viewers, where it is displayed
321   theUpdateFlags |= UF_Viewer | UF_GV_Forced;
322   theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
323   theUpdateFlags |= UF_VTKViewer;
324
325   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
326   if ( anActiveViewId == 0 )
327   {
328     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
329   }
330
331   if( !myIsEdit )
332   {
333     module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
334     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
335     theBrowseObjectsEntries.append( anEntry );
336   }  
337
338   return true;
339 }
340
341 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
342 {
343   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
344   if( !aPanel )
345     return;
346   if( !myCurve )
347     return;
348   CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
349   if( !aDisplayer )
350     return;
351   //QList<int> aSelSections = aPanel->getSelectedSections();
352   bool aIsHl = false;
353   //if( aSelSections.contains(i) ){
354   // TODO
355   //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
356   //}
357 }
358
359 void HYDROGUI_PolylineOp::displayPreview()
360 {
361   if( getPreviewManager() )
362   {
363     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
364     {
365       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
366       if( !aCtx.IsNull() )
367       {
368         CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
369         myCurve->setDisplayer( aDisplayer );
370         aDisplayer->display( myCurve->getAISObject( true ), true );
371       }
372     }
373   }
374 }
375
376 void HYDROGUI_PolylineOp::erasePreview()
377 {
378   CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
379   if( getPreviewManager() && aDisplayer )
380   {
381     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
382     {
383       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
384       if( !aCtx.IsNull() )
385       {
386         aDisplayer->eraseAll( true );
387       }
388     }
389   }
390
391   setPreviewManager( NULL );
392   if ( myCurve )
393   {
394     delete myCurve;
395     myCurve = NULL;
396   }
397 }