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