]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_PolylineOp.cxx
Salome HOME
refs #529: use 'Apply and Close', 'Apply' and 'Close' buttons in HYDRO dialogs.
[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     if ( isApplyAndClose() )
115           myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
116     if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
117     {
118       // Polyline is imported from GEOM module an is not recognized as
119       // polyline or spline and exist only as shape presentation
120       SUIT_MessageBox::critical( module()->getApp()->desktop(),
121                                  tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
122                                  tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
123       abort();
124       return;
125     }
126   }
127
128   if( myCurve )
129     delete myCurve;
130
131   myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
132
133   HYDROGUI_Operation::startOperation();
134
135   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
136   aPanel->reset();
137
138   LightApp_Application* anApp = module()->getApp();
139   OCCViewer_ViewManager* aViewManager =
140     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
141   aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
142   setPreviewManager( aViewManager );
143
144   if ( isApplyAndClose() )
145     setCursor();
146
147   QString aPolylineName;
148   if( !myEditedObject.IsNull() )
149   {
150     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
151     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
152     NCollection_Sequence<bool>                              aSectClosures;
153     myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
154
155     for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
156     {
157       QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
158       HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
159       bool aSectClosure = aSectClosures.Value( i );
160
161       CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
162       if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
163         aCurveType = CurveCreator::Spline;
164
165       CurveCreator::Coordinates aCurveCoords;
166
167       HYDROData_PolylineXY::PointsList aSectPointsList = myEditedObject->GetPoints( i - 1 );
168       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
169       {
170         const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
171         aCurveCoords.push_back( aSectPoint.X() );
172         aCurveCoords.push_back( aSectPoint.Y() );
173       }
174
175       myCurve->addSectionInternal( aSectName.toStdString(), aCurveType, aSectClosure, aCurveCoords );
176     }
177
178     aPolylineName = myEditedObject->GetName();
179   }
180   else
181   {
182     aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
183   }
184
185   aPanel->setPolylineName( aPolylineName );
186   aPanel->setCurve( myCurve );
187
188   displayPreview();
189 }
190
191 void HYDROGUI_PolylineOp::abortOperation()
192 {
193   restoreCursor();
194
195   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
196   if ( aPanel )
197     aPanel->setOCCViewer( 0 );
198   erasePreview();
199
200   HYDROGUI_Operation::abortOperation();
201 }
202
203 void HYDROGUI_PolylineOp::commitOperation()
204 {
205   if ( isApplyAndClose() )
206   {
207     restoreCursor();
208
209     HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
210     if ( aPanel )
211       aPanel->setOCCViewer( 0 );
212   }
213
214   erasePreview();
215
216   HYDROGUI_Operation::commitOperation();
217 }
218
219 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
220 {
221   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
222   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
223   return aDlg;
224 }
225
226 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
227                                         QString& theErrorMsg,
228                                         QStringList& theBrowseObjectsEntries )
229 {
230   HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
231   if ( !aPanel )
232     return false;
233
234   QString aPolylineName = aPanel->getPolylineName().simplified();
235   if ( aPolylineName.isEmpty() )
236   {
237     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
238     return false;
239   }
240
241   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
242   {
243     // check that there are no other objects with the same name in the document
244     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
245     if( !anObject.IsNull() )
246     {
247       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
248       return false;
249     }
250   }
251
252   if ( myCurve->getNbSections() <= 0 )
253   {
254     theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
255     return false;
256   }
257
258   Handle(HYDROData_PolylineXY) aPolylineObj;
259   if( myIsEdit )
260   {
261     aPolylineObj = myEditedObject;
262     aPolylineObj->RemoveSections();
263   }
264   else
265   {
266     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
267   }
268
269   if( aPolylineObj.IsNull() )
270     return false;
271
272   aPolylineObj->SetName( aPolylineName );
273
274   for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
275   {
276     TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
277     CurveCreator::SectionType aCurveType =  myCurve->getSectionType( i );
278     bool aSectClosure = myCurve->isClosed( i );
279
280     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
281
282     if ( aCurveType == CurveCreator::Spline )
283       aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
284
285     aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
286
287     // Add the points fro section
288     CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
289
290     if ( aCurveCoords.size() <= 2 )
291     {
292       theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
293       return false;
294     }
295
296     for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
297     {
298       HYDROData_PolylineXY::Point aSectPoint;
299
300       aSectPoint.SetX( aCurveCoords.at( k ) );
301       k++;
302       aSectPoint.SetY( aCurveCoords.at( k ) );
303
304       aPolylineObj->AddPoint( i, aSectPoint );
305     }
306   }
307
308   if ( !myIsEdit )
309   {
310     aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
311   }
312
313   // Update the wire of polyline
314   aPolylineObj->Update();
315   module()->setIsToUpdate( aPolylineObj );
316
317   // the viewer should be release from the widget before the module update it
318   // because it has an opened local context and updated presentation should not be displayed in it
319   if ( aPanel )
320     aPanel->setOCCViewer( 0 );
321
322   theUpdateFlags = UF_Model;
323
324   // the polyline should be rebuild in all viewers, where it is displayed
325   theUpdateFlags |= UF_Viewer | UF_GV_Forced;
326   theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
327   theUpdateFlags |= UF_VTKViewer;
328
329   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
330   if ( anActiveViewId == 0 )
331   {
332     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
333   }
334
335   if( !myIsEdit )
336   {
337     module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
338     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
339     theBrowseObjectsEntries.append( anEntry );
340   }  
341
342   return true;
343 }
344
345 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
346 {
347   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
348   if( !aPanel )
349     return;
350   if( !myCurve )
351     return;
352   CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
353   if( !aDisplayer )
354     return;
355   //QList<int> aSelSections = aPanel->getSelectedSections();
356   bool aIsHl = false;
357   //if( aSelSections.contains(i) ){
358   // TODO
359   //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
360   //}
361 }
362
363 void HYDROGUI_PolylineOp::displayPreview()
364 {
365   if( getPreviewManager() )
366   {
367     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
368     {
369       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
370       if( !aCtx.IsNull() )
371       {
372         CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
373         myCurve->setDisplayer( aDisplayer );
374         aDisplayer->display( myCurve->getAISObject( true ), true );
375       }
376     }
377   }
378 }
379
380 void HYDROGUI_PolylineOp::erasePreview()
381 {
382   CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
383   if( getPreviewManager() && aDisplayer )
384   {
385     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
386     {
387       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
388       if( !aCtx.IsNull() )
389       {
390         aDisplayer->eraseAll( true );
391       }
392     }
393   }
394
395   setPreviewManager( NULL );
396   if ( myCurve )
397   {
398     delete myCurve;
399     myCurve = NULL;
400   }
401 }