Salome HOME
merge BR_v14_rc from origin
[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 #include <HYDROData_PolylineOperator.h>
29 #include <HYDROData_TopoCurve.h>
30
31 #include <CurveCreator_Curve.hxx>
32 #include <CurveCreator_Displayer.hxx>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <LightApp_UpdateFlags.h>
37
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
41
42 #include <OCCViewer_AISSelector.h>
43
44 #include <Precision.hxx>
45
46 #include <SUIT_MessageBox.h>
47 #include <SUIT_Desktop.h>
48
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Wire.hxx>
51
52 //static int ZValueIncrement = 0;
53 static const double HYDROGUI_MAXIMAL_DEFLECTION = 1e-2;
54
55 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
56 : HYDROGUI_Operation( theModule ), 
57   myIsEdit( theIsEdit ),
58   myCurve( NULL )
59 {
60   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
61 }
62
63 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
64 {
65   erasePreview();
66 }
67
68 /**
69  * Redirect the delete action to input panel
70  */
71 void HYDROGUI_PolylineOp::deleteSelected()
72 {
73   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
74   aPanel->deleteSelected();
75 }
76
77 /**
78  * Checks whether there are some to delete
79  */
80 bool HYDROGUI_PolylineOp::deleteEnabled()
81 {
82   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
83   return aPanel->deleteEnabled();
84 }
85
86 /**
87  * Set Z layer for the operation preview.
88  \param theLayer a layer position
89  */
90 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
91 {
92   HYDROGUI_Operation::updatePreviewZLayer( theLayer );
93
94   int aZLayer = getPreviewZLayer();
95   if ( aZLayer >= 0 )
96   {
97     if( getPreviewManager() )
98     {
99       if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
100       {
101         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
102         if( !aCtx.IsNull() )
103         {
104           Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
105           aCtx->SetZLayer( anObject, aZLayer );
106         }
107       }
108     }
109   }
110 }
111
112 void HYDROGUI_PolylineOp::startOperation()
113 {
114   if( myIsEdit )
115   {
116     if ( isApplyAndClose() )
117           myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
118     if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
119     {
120       // Polyline is imported from GEOM module an is not recognized as
121       // polyline or spline and exist only as shape presentation
122       SUIT_MessageBox::critical( module()->getApp()->desktop(),
123                                  tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
124                                  tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
125       abort();
126       return;
127     }
128   }
129
130   if( myCurve )
131     delete myCurve;
132
133   myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
134
135   HYDROGUI_Operation::startOperation();
136
137   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
138   aPanel->reset();
139
140   LightApp_Application* anApp = module()->getApp();
141   OCCViewer_ViewManager* aViewManager =
142     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
143   aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
144   setPreviewManager( aViewManager );
145
146   if ( isApplyAndClose() )
147     setCursor();
148
149   QString aPolylineName;
150   if( !myEditedObject.IsNull() )
151   {
152     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
153     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
154     NCollection_Sequence<bool>                              aSectClosures;
155     myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
156
157     if (!aSectNames.IsEmpty())
158     {
159       for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
160       {
161         QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
162         HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
163         bool aSectClosure = aSectClosures.Value( i );
164
165         CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
166         if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
167           aCurveType = CurveCreator::Spline;
168
169         CurveCreator::Coordinates aCurveCoords;
170
171         HYDROData_PolylineXY::PointsList aSectPointsList =
172           myEditedObject->GetPoints( i - 1 );
173         for (int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k)
174         {
175           const HYDROData_PolylineXY::Point& aSectPoint =
176             aSectPointsList.Value( k );
177           aCurveCoords.push_back( aSectPoint.X() );
178           aCurveCoords.push_back( aSectPoint.Y() );
179         }
180
181         myCurve->addSectionInternal( aSectName.toStdString(),
182           aCurveType, aSectClosure, aCurveCoords );
183       }
184     }
185     else
186     {
187       std::deque<CurveCreator::Coordinates> aPs;
188       std::deque<bool> isCloseds;
189       std::vector<TopoDS_Wire> aWires;
190       HYDROData_PolylineOperator::GetWires(myEditedObject, aWires);
191       const int aSCount = aWires.size();
192       bool isError = false;
193       for (int aSI = 0; aSI < aSCount; ++aSI)
194       {
195         HYDROData_TopoCurve aCurve, aCurve2;
196         std::list<gp_XYZ> aPs2;
197         int aMaxPieceCount;
198         if (!aCurve.Initialize(aWires[aSI]) ||
199           (aMaxPieceCount = aCurve.BSplinePiecewiseCurve(
200             HYDROGUI_MAXIMAL_DEFLECTION * 0.1, aCurve2)) == 0)
201         {
202           isError = true;
203           break;
204         }
205
206         double aDefl;
207         aMaxPieceCount *= 100;
208         int aPieceCount = 0;
209         while (aPieceCount < aMaxPieceCount &&
210           (aDefl = HYDROData_PolylineOperator::ReduceDeflection(
211               HYDROGUI_MAXIMAL_DEFLECTION, aCurve2, aPieceCount)) >
212             HYDROGUI_MAXIMAL_DEFLECTION);
213         if (aDefl < 0 || !aCurve2.ValuesInKnots(aPs2))
214         {
215           isError = true;
216           break;
217         }
218
219         aPs.push_back(CurveCreator::Coordinates());
220         CurveCreator::Coordinates& aPs3 = aPs.back();
221         std::list<gp_XYZ>::const_iterator aLastPIt = aPs2.end();
222         std::list<gp_XYZ>::const_iterator aPIt = aPs2.begin();
223         for (; aPIt != aLastPIt; ++aPIt)
224         {
225           const gp_XYZ aP = *aPIt;
226           aPs3.push_back(aP.X());
227           aPs3.push_back(aP.Y());
228         }
229         isCloseds.push_back(aCurve.IsClosed());
230       }
231
232       if (!isError)
233       {
234         const TCollection_AsciiString aNamePrefix = "Section_";
235         for (int aSI = 0; aSI < aSCount; ++aSI)
236         {
237           myCurve->addSectionInternal((aNamePrefix + (aSI + 1)).ToCString(),
238             CurveCreator::Spline, isCloseds[aSI], aPs[aSI]);
239         }
240       }
241     }
242
243     aPolylineName = myEditedObject->GetName();
244   }
245   else
246   {
247     aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
248   }
249
250   aPanel->setPolylineName( aPolylineName );
251   aPanel->setCurve( myCurve );
252
253   displayPreview();
254 }
255
256 void HYDROGUI_PolylineOp::abortOperation()
257 {
258   restoreCursor();
259
260   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
261   if ( aPanel )
262     aPanel->setOCCViewer( 0 );
263   erasePreview();
264
265   HYDROGUI_Operation::abortOperation();
266 }
267
268 void HYDROGUI_PolylineOp::commitOperation()
269 {
270   if ( isApplyAndClose() )
271   {
272     restoreCursor();
273
274     HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
275     if ( aPanel )
276       aPanel->setOCCViewer( 0 );
277   }
278
279   erasePreview();
280
281   HYDROGUI_Operation::commitOperation();
282 }
283
284 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
285 {
286   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
287   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
288   return aDlg;
289 }
290
291 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
292                                         QString& theErrorMsg,
293                                         QStringList& theBrowseObjectsEntries )
294 {
295   HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
296   if ( !aPanel )
297     return false;
298
299   QString aPolylineName = aPanel->getPolylineName().simplified();
300   if ( aPolylineName.isEmpty() )
301   {
302     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
303     return false;
304   }
305
306   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
307   {
308     // check that there are no other objects with the same name in the document
309     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
310     if( !anObject.IsNull() )
311     {
312       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
313       return false;
314     }
315   }
316
317   if ( myCurve->getNbSections() <= 0 )
318   {
319     theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
320     return false;
321   }
322
323   Handle(HYDROData_PolylineXY) aPolylineObj;
324   if( myIsEdit )
325   {
326     aPolylineObj = myEditedObject;
327     aPolylineObj->RemoveSections();
328   }
329   else
330   {
331     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
332   }
333
334   if( aPolylineObj.IsNull() )
335     return false;
336
337   aPolylineObj->SetName( aPolylineName );
338
339   for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
340   {
341     TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
342     CurveCreator::SectionType aCurveType =  myCurve->getSectionType( i );
343     bool aSectClosure = myCurve->isClosed( i );
344
345     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
346
347     if ( aCurveType == CurveCreator::Spline )
348       aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
349
350     aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
351
352     // Add the points fro section
353     CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
354
355     if ( aCurveCoords.size() <= 2 )
356     {
357       theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
358       return false;
359     }
360
361     for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
362     {
363       HYDROData_PolylineXY::Point aSectPoint;
364
365       aSectPoint.SetX( aCurveCoords.at( k ) );
366       k++;
367       aSectPoint.SetY( aCurveCoords.at( k ) );
368
369       aPolylineObj->AddPoint( i, aSectPoint );
370     }
371   }
372
373   if ( !myIsEdit )
374   {
375     aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
376   }
377
378   // Update the wire of polyline
379   aPolylineObj->Update();
380   module()->setIsToUpdate( aPolylineObj );
381
382   // the viewer should be release from the widget before the module update it
383   // because it has an opened local context and updated presentation should not be displayed in it
384   if ( aPanel )
385     aPanel->setOCCViewer( 0 );
386
387   theUpdateFlags = UF_Model;
388
389   // the polyline should be rebuild in all viewers, where it is displayed
390   theUpdateFlags |= UF_Viewer | UF_GV_Forced;
391   theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
392   theUpdateFlags |= UF_VTKViewer;
393
394   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
395   if ( anActiveViewId == 0 )
396   {
397     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
398   }
399
400   if( !myIsEdit )
401   {
402     module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
403     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
404     theBrowseObjectsEntries.append( anEntry );
405   }  
406
407   return true;
408 }
409
410 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
411 {
412   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
413   if( !aPanel )
414     return;
415   if( !myCurve )
416     return;
417   CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
418   if( !aDisplayer )
419     return;
420   //QList<int> aSelSections = aPanel->getSelectedSections();
421   bool aIsHl = false;
422   //if( aSelSections.contains(i) ){
423   // TODO
424   //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
425   //}
426 }
427
428 void HYDROGUI_PolylineOp::displayPreview()
429 {
430   if( getPreviewManager() )
431   {
432     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
433     {
434       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
435       if( !aCtx.IsNull() )
436       {
437         CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
438         myCurve->setDisplayer( aDisplayer );
439         aDisplayer->display( myCurve->getAISObject( true ), true );
440       }
441     }
442   }
443 }
444
445 void HYDROGUI_PolylineOp::erasePreview()
446 {
447   CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
448   if( getPreviewManager() && aDisplayer )
449   {
450     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
451     {
452       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
453       if( !aCtx.IsNull() )
454       {
455         aDisplayer->eraseAll( true );
456       }
457     }
458   }
459
460   setPreviewManager( NULL );
461   if ( myCurve )
462   {
463     delete myCurve;
464     myCurve = NULL;
465   }
466 }