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