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