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