]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_LandCoverMapOp.cxx
Salome HOME
refs #668: get the complete boundary of the object face as the splitting polyline.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_LandCoverMapOp.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_LandCoverMapOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_OCCDisplayer.h"
23 #include "HYDROGUI_Operations.h"
24 #include "HYDROGUI_LandCoverMapDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27 #include "HYDROGUI_DataObject.h"
28
29 #include <HYDROData_Iterator.h>
30 #include <HYDROData_StricklerTable.h>
31 #include <HYDROData_PolylineXY.h>
32 #include <HYDROData_Object.h>
33
34 #include <TopoDS_Face.hxx>
35 #include <TopTools_ListOfShape.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37
38 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
39 : HYDROGUI_Operation( theModule ),
40   myOperationId( theOperationId ),
41   myPreviewPrs( 0 )
42 {
43   switch( myOperationId )
44   {
45     case CreateLandCoverMapId:
46       setName( tr( "CREATE_LAND_COVER_MAP" ) );
47       break;
48     case AddLandCoverId:
49       setName( tr( "ADD_LAND_COVER" ) );
50       break;
51     case RemoveLandCoverId:
52       setName( tr( "REMOVE_LAND_COVER" ) );
53       break;
54     case SplitLandCoverId:
55       setName( tr( "SPLIT_LAND_COVER" ) );
56       break;
57     case MergeLandCoverId:
58       setName( tr( "MERGE_LAND_COVER" ) );
59       break;
60     case ChangeLandCoverTypeId:
61       setName( tr( "CHANGE_LAND_COVER_TYPE" ) );
62       break;
63   }  
64 }
65
66 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
67 {
68   closePreview();
69 }
70
71 void HYDROGUI_LandCoverMapOp::startOperation()
72 {
73   HYDROGUI_Operation::startOperation();
74
75   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
76   if ( !aPanel )
77     return;
78
79   aPanel->blockSignals( true );
80
81   aPanel->reset();
82
83   // Set name of the created/edited land cover map object
84   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
85   if ( myOperationId != CreateLandCoverMapId )
86   {
87     if ( isApplyAndClose() )
88       myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
89     if ( !myEditedObject.IsNull() )
90     {
91       anObjectName = myEditedObject->GetName();
92
93       // Construct a list of names of all land cover map objects defined within the data model
94       QStringList aLandCoverMapNames;
95       HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
96       for( ; anIterator.More(); anIterator.Next() )
97       {
98         Handle(HYDROData_LandCoverMap) aLandCoverObj =
99           Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );     
100         if ( !aLandCoverObj.IsNull() )
101           aLandCoverMapNames.append( aLandCoverObj->GetName() );
102       }
103
104       //aLandCoverMapNames.sort();
105       aPanel->setObjectNames( aLandCoverMapNames );
106     }
107   }  
108   aPanel->setObjectName( anObjectName );
109   
110   aPanel->blockSignals( false );
111 }
112
113 void HYDROGUI_LandCoverMapOp::abortOperation()
114 {
115   closePreview();
116
117   HYDROGUI_Operation::abortOperation();
118 }
119
120 void HYDROGUI_LandCoverMapOp::commitOperation()
121 {
122   closePreview();
123
124   HYDROGUI_Operation::commitOperation();
125 }
126
127 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
128 {
129   HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
130   connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
131            this, SLOT( onLandCoverMapChanged( const QString& ) ) );
132   connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
133            this,   SLOT( onCreatePreview( const QStringList& ) ) );
134   /*
135   connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
136   connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
137   */
138   return aPanel;
139 }
140
141 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
142                                             QString& theErrorMsg,
143                                             QStringList& theBrowseObjectsEntries )
144 {
145   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
146   if ( !aPanel )
147     return false;
148
149   // Check name of the created/edited object
150   QString anObjectName = aPanel->getObjectName().simplified();
151   if ( anObjectName.isEmpty() )
152   {
153     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
154     return false;
155   }
156
157   if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
158   {
159     // check that there are no other objects with the same name in the document
160     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
161     if( !anObject.IsNull() )
162     {
163       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
164       return false;
165     }
166   }
167
168   Handle(HYDROData_PolylineXY) aPolyline;
169   Handle(HYDROData_Object) aFace;
170
171   TopTools_ListOfShape aFacesSelectedInViewer;
172
173   // Get polyline/face selected in combo-box
174   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
175   {
176     Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
177     aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
178     aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
179     if ( aPolyline.IsNull() && aFace.IsNull() )
180     {
181       theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
182       return false;
183     }
184   }
185   // Get face(s) selected in the 3d viewer
186   else if ( myOperationId == RemoveLandCoverId || 
187             myOperationId == MergeLandCoverId || 
188             myOperationId == ChangeLandCoverTypeId )
189   {
190     // TODO:
191     //Fill in aFacesSelectedInViewer list
192   }
193
194   // Get selected Strickler type
195   QString aSelectedStricklerType;
196   if ( myOperationId == CreateLandCoverMapId || 
197        myOperationId == AddLandCoverId || 
198        myOperationId == MergeLandCoverId )
199   {
200     aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
201     if ( aSelectedStricklerType.isEmpty() )
202     {
203       theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
204       return false;
205     }
206   }
207
208   // Create / find the new / edited land cover map object
209   Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
210     Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
211
212   // Set land cover map name
213   aLandCoverMapObj->SetName( anObjectName );
214   
215   // Add land cover to new / edited land cover map
216   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
217   {
218     bool aLandCoverAdded = false;
219     if ( !aPolyline.IsNull() )
220       aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
221     else if ( !aFace.IsNull() )
222       aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
223     if ( !aLandCoverAdded )
224     {
225       theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
226       return false;
227     }
228   }
229
230   // Remove land cover from edited land cover map
231   if ( myOperationId == RemoveLandCoverId )
232   {
233     bool aLandCoverRemoved = false;
234     if ( !aFacesSelectedInViewer.IsEmpty() )
235     {
236       aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
237       if ( !aLandCoverRemoved )
238       {
239         theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
240         return false;
241       }
242     }
243   }
244
245   // Split land cover(s) inside edited land cover map
246   if ( myOperationId == SplitLandCoverId )
247   {
248     bool aLandCoverSplitted = false;
249     if ( !aPolyline.IsNull() )
250       aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
251     else if ( !aFace.IsNull() )
252     {
253       // Get the complete boundary of the object face as the splitting polyline
254       QList<TopoDS_Shape> aBoundShapes;
255       QStringList aBoundNames;
256       aFace->GetBoundaries( aBoundShapes, aBoundNames );
257
258       for( int i=0, n=aBoundShapes.size(); i<n; i++ )
259       {
260         TopoDS_Shape aShape = aBoundShapes[i];
261         if( aShape.IsNull() )
262           continue;
263
264         bool aSplitResult = aLandCoverMapObj->Split( aShape );
265         aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
266       }
267     }
268     if ( !aLandCoverSplitted )
269     {
270       theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
271       return false;
272     }
273   }
274
275   // Merge land covers inside edited land cover map
276   if ( myOperationId == MergeLandCoverId )
277   {
278     bool aLandCoverMerged = false;
279     if ( !aFacesSelectedInViewer.IsEmpty() )
280     {
281       aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
282       if ( !aLandCoverMerged )
283       {
284         theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
285         return false;
286       }
287     }
288   }
289     
290   /*if ( myOperationId == CreateLandCoverMapId )
291   {    
292     aLandCoverMapObj->SetFillingColor( aLandCoverMapObj->DefaultFillingColor() );
293     aLandCoverMapObj->SetBorderColor( aLandCoverMapObj->DefaultBorderColor() );
294   }*/
295
296   // Update land cover map object and close preview
297   aLandCoverMapObj->Update();
298
299   closePreview();
300
301   // Publish the newly created land cover map in the Object Browser
302   module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
303   if ( myOperationId == CreateLandCoverMapId )
304   {
305     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
306     theBrowseObjectsEntries.append( anEntry );
307   }
308
309   // Update presentation of land cover object in the 3d viewer
310   module()->setIsToUpdate( aLandCoverMapObj );
311   module()->getOCCDisplayer()->SetToUpdateColorScale();
312
313   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
314
315   return true;
316 }
317
318 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
319 {
320   // If the edited land cover map was changed in the combo-box, update myEditedObject
321   if ( myOperationId != CreateLandCoverMapId )
322   {
323     myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
324     if ( !myEditedObject.IsNull() )
325     {
326       // Show preview of the newly selected land cover map
327       QStringList aPolylineFaceNames;
328       onCreatePreview( aPolylineFaceNames );
329     }
330   }
331 }
332
333 void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineFaceNames )
334 {
335   /*
336   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
337   if ( !aPanel )
338     return;
339
340   QApplication::setOverrideCursor( Qt::WaitCursor );  
341
342   HYDROData_SequenceOfObjects aZonePolylines;
343   QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
344   for( ; anIt!=aLast; anIt++ )
345   {
346     QString aPolylineName = *anIt;
347     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
348       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
349     if ( !aPolyline.IsNull() )
350       aZonePolylines.Append( aPolyline );
351   }
352   
353   TCollection_AsciiString anError;
354   TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );  
355
356   LightApp_Application* anApp = module()->getApp();
357   if ( !getPreviewManager() )
358     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
359                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
360   OCCViewer_ViewManager* aViewManager = getPreviewManager();
361   if ( aViewManager && !myPreviewPrs )
362   {
363     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
364     {
365       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
366       if ( !aCtx.IsNull() )
367         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
368     }
369   }
370
371   if ( aViewManager && myPreviewPrs )
372   {
373     QColor aFillingColor = Qt::magenta;
374     QColor aBorderColor = Qt::transparent;
375     if ( !myEditedObject.IsNull() ) {
376       aFillingColor = myEditedObject->GetFillingColor();
377       aBorderColor = myEditedObject->GetBorderColor();
378     }
379
380     myPreviewPrs->setFillingColor( aFillingColor, false, false );
381     myPreviewPrs->setBorderColor( aBorderColor, false, false );
382
383     if( !aZoneShape.IsNull() )
384       myPreviewPrs->setShape( aZoneShape );
385   }
386
387   QApplication::restoreOverrideCursor();
388   */
389 }
390
391 void HYDROGUI_LandCoverMapOp::closePreview()
392 {
393   if( myPreviewPrs )
394   {
395     delete myPreviewPrs;
396     myPreviewPrs = 0;
397   }
398 }