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