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