Salome HOME
a04f5a1ca1267a996e1a13c46627899fc7415722
[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 #include "HYDROGUI_ShapeLandCoverMap.h"
29
30 #include <HYDROData_Iterator.h>
31 #include <HYDROData_StricklerTable.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Object.h>
34
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewModel.h>
37
38 #include <LightApp_Application.h>
39
40 #include <TopoDS.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <AIS_Shape.hxx>
45
46 #include <QApplication>
47
48 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
49 : HYDROGUI_Operation( theModule ),
50   myOperationId( theOperationId ),
51   myPreviewPrs( 0 )
52 {
53   switch( myOperationId )
54   {
55     case CreateLandCoverMapId:
56       setName( tr( "CREATE_LAND_COVER_MAP" ) );
57       break;
58     case AddLandCoverId:
59       setName( tr( "ADD_LAND_COVER" ) );
60       break;
61     case RemoveLandCoverId:
62       setName( tr( "REMOVE_LAND_COVER" ) );
63       break;
64     case SplitLandCoverId:
65       setName( tr( "SPLIT_LAND_COVER" ) );
66       break;
67     case MergeLandCoverId:
68       setName( tr( "MERGE_LAND_COVER" ) );
69       break;
70     case ChangeLandCoverTypeId:
71       setName( tr( "CHANGE_LAND_COVER_TYPE" ) );
72       break;
73   }  
74 }
75
76 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
77 {
78   closePreview();
79 }
80
81 void HYDROGUI_LandCoverMapOp::startOperation()
82 {
83   HYDROGUI_Operation::startOperation();
84
85   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
86   if ( !aPanel )
87     return;
88
89   aPanel->blockSignals( true );
90
91   aPanel->reset();
92
93   // Set name of the created/edited land cover map object
94   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
95   if ( myOperationId != CreateLandCoverMapId )
96   {
97     if ( isApplyAndClose() )
98       myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
99     if ( !myEditedObject.IsNull() )
100     {
101       anObjectName = myEditedObject->GetName();
102
103       // Construct a list of names of all land cover map objects defined within the data model
104       QStringList aLandCoverMapNames;
105       HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
106       for( ; anIterator.More(); anIterator.Next() )
107       {
108         Handle(HYDROData_LandCoverMap) aLandCoverObj =
109           Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );     
110         if ( !aLandCoverObj.IsNull() )
111           aLandCoverMapNames.append( aLandCoverObj->GetName() );
112       }
113
114       //aLandCoverMapNames.sort();
115       aPanel->setObjectNames( aLandCoverMapNames );
116     }
117   }  
118   aPanel->setObjectName( anObjectName );
119
120   closePreview();
121   if ( myOperationId != CreateLandCoverMapId )
122     onCreatePreview();
123
124   aPanel->blockSignals( false );
125
126   module()->update( UF_OCCViewer | UF_FitAll );
127 }
128
129 void HYDROGUI_LandCoverMapOp::abortOperation()
130 {
131   closePreview();
132
133   HYDROGUI_Operation::abortOperation();
134
135   module()->update( UF_OCCViewer | UF_FitAll );
136 }
137
138 void HYDROGUI_LandCoverMapOp::commitOperation()
139 {
140   closePreview();
141
142   HYDROGUI_Operation::commitOperation();
143
144   module()->update( UF_OCCViewer | UF_FitAll );
145 }
146
147 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
148 {
149   HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
150   connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
151            this, SLOT( onLandCoverMapChanged( const QString& ) ) );
152   return aPanel;
153 }
154
155 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
156                                             QString& theErrorMsg,
157                                             QStringList& theBrowseObjectsEntries )
158 {
159   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
160   if ( !aPanel )
161     return false;
162
163   // Check name of the created/edited object
164   QString anObjectName = aPanel->getObjectName().simplified();
165   if ( anObjectName.isEmpty() )
166   {
167     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
168     return false;
169   }
170
171   if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
172   {
173     // check that there are no other objects with the same name in the document
174     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
175     if( !anObject.IsNull() )
176     {
177       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
178       return false;
179     }
180   }
181
182   Handle(HYDROData_PolylineXY) aPolyline;
183   Handle(HYDROData_Object) aFace;
184
185   TopTools_ListOfShape aFacesSelectedInViewer;
186
187   // Get polyline/face selected in combo-box
188   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
189   {
190     Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
191     aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
192     aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
193     if ( aPolyline.IsNull() && aFace.IsNull() )
194     {
195       theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
196       return false;
197     }
198   }
199   // Get face(s) selected in the 3d viewer
200   else if ( myOperationId == RemoveLandCoverId || 
201             myOperationId == MergeLandCoverId || 
202             myOperationId == ChangeLandCoverTypeId )
203   {
204     if ( myPreviewPrs )
205     {
206       // Fill in aFacesSelectedInViewer list
207       Handle(AIS_InteractiveContext) aCtx;
208       getSelectedShapes( aFacesSelectedInViewer, aCtx );
209     }
210   }
211
212   // Get selected Strickler type
213   QString aSelectedStricklerType;
214   if ( myOperationId == CreateLandCoverMapId || 
215        myOperationId == AddLandCoverId || 
216        myOperationId == MergeLandCoverId ||
217        myOperationId == ChangeLandCoverTypeId )
218   {
219     aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
220     if ( aSelectedStricklerType.isEmpty() )
221     {
222       theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
223       return false;
224     }
225   }
226
227   // Create / find the new / edited land cover map object
228   Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
229     Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
230
231   // Set land cover map name
232   aLandCoverMapObj->SetName( anObjectName );
233   
234   // Add land cover to new / edited land cover map
235   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
236   {
237     bool aLandCoverAdded = false;
238     if ( !aPolyline.IsNull() )
239       aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
240     else if ( !aFace.IsNull() )
241       aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
242     if ( !aLandCoverAdded )
243     {
244       theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
245       return false;
246     }
247   }
248
249   // Remove land cover from edited land cover map
250   if ( myOperationId == RemoveLandCoverId )
251   {
252     bool aLandCoverRemoved = false;
253     if ( !aFacesSelectedInViewer.IsEmpty() )
254     {
255       aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
256       if ( !aLandCoverRemoved )
257       {
258         theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
259         return false;
260       }
261     }
262   }
263
264   // Split land cover(s) inside edited land cover map
265   if ( myOperationId == SplitLandCoverId )
266   {
267     bool aLandCoverSplitted = false;
268     if ( !aPolyline.IsNull() )
269       aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
270     else if ( !aFace.IsNull() )
271     {
272       // Get the complete boundary of the object face as the splitting polyline
273       QList<TopoDS_Shape> aBoundShapes;
274       QStringList aBoundNames;
275       aFace->GetBoundaries( aBoundShapes, aBoundNames );
276
277       for( int i=0, n=aBoundShapes.size(); i<n; i++ )
278       {
279         TopoDS_Shape aShape = aBoundShapes[i];
280         if( aShape.IsNull() )
281           continue;
282
283         bool aSplitResult = aLandCoverMapObj->Split( aShape );
284         aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
285       }
286     }
287     if ( !aLandCoverSplitted )
288     {
289       theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
290       return false;
291     }
292   }
293
294   // Merge land covers inside edited land cover map
295   if ( myOperationId == MergeLandCoverId )
296   {
297     bool aLandCoverMerged = false;
298     if ( !aFacesSelectedInViewer.IsEmpty() )
299     {
300       aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
301       if ( !aLandCoverMerged )
302       {
303         theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
304         return false;
305       }
306     }
307   }
308
309   // Change Strickler type for land cover(s) inside edited land cover map
310   if ( myOperationId == ChangeLandCoverTypeId )
311   {
312     bool aLandCoverChangeType = false;
313     if ( !aFacesSelectedInViewer.IsEmpty() )
314     {
315       aLandCoverChangeType = aLandCoverMapObj->ChangeType( aFacesSelectedInViewer, aSelectedStricklerType );
316       if ( !aLandCoverChangeType )
317       {
318         theErrorMsg = tr( "LAND_COVER_TYPE_NOT_CHANGED" );
319         return false;
320       }
321     }
322   }
323     
324   // Update land cover map object and close preview
325   aLandCoverMapObj->Update();
326
327   closePreview();
328
329   // Publish the newly created land cover map in the Object Browser
330   module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
331   if ( myOperationId == CreateLandCoverMapId )
332   {
333     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
334     theBrowseObjectsEntries.append( anEntry );
335   }
336
337   // Update presentation of land cover object in the 3d viewer
338   module()->setIsToUpdate( aLandCoverMapObj );
339   module()->getOCCDisplayer()->SetToUpdateColorScale();
340
341   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
342
343   return true;
344 }
345
346 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
347 {
348   // If the edited land cover map was changed in the combo-box, update myEditedObject
349   if ( myOperationId != CreateLandCoverMapId )
350   {
351     myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
352     if ( !myEditedObject.IsNull() )
353     {
354       // Show preview of the newly selected land cover map
355       closePreview();
356       onCreatePreview();
357     }
358   }
359 }
360
361 void HYDROGUI_LandCoverMapOp::onCreatePreview()
362 {
363   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
364   if ( !aPanel )
365     return;
366
367   QApplication::setOverrideCursor( Qt::WaitCursor );  
368
369   LightApp_Application* anApp = module()->getApp();
370   if ( !getPreviewManager() )
371     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
372                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
373   OCCViewer_ViewManager* aViewManager = getPreviewManager();
374   if ( aViewManager && !myPreviewPrs )
375   {
376     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
377     {
378       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
379       if ( !aCtx.IsNull() )
380       {
381         connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
382         myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
383       }
384     }
385   }
386
387   if ( aViewManager && myPreviewPrs )
388   {
389     TopoDS_Shape aLandCoverMapShape = myEditedObject->GetShape();
390     if( !aLandCoverMapShape.IsNull() )
391     {
392       if ( myOperationId == RemoveLandCoverId ||
393            myOperationId == MergeLandCoverId ||
394            myOperationId == ChangeLandCoverTypeId )
395         myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) ); 
396       myPreviewPrs->setShape( aLandCoverMapShape );      
397     }
398   }
399   
400   module()->update( UF_OCCViewer | UF_FitAll );
401
402   QApplication::restoreOverrideCursor();  
403 }
404
405 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
406 {
407   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
408   if ( !aPanel )
409     return;
410
411   Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
412   if ( !aCtx.IsNull() )
413   {
414     int aNbSelected = aCtx->NbSelected();
415
416     if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
417       // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
418       aPanel->setApplyEnabled( aNbSelected > 0 );
419     else if ( myOperationId == MergeLandCoverId )
420       // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
421       aPanel->setApplyEnabled( aNbSelected > 1 );
422
423     if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
424     {
425       if ( aNbSelected == 1 && !myEditedObject.IsNull() )
426       {
427         TopTools_ListOfShape aFacesSelectedInViewer;
428         getSelectedShapes( aFacesSelectedInViewer, aCtx );
429         if ( aFacesSelectedInViewer.Extent() == 1 )
430         {
431           QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
432           if ( !aType.isEmpty() )
433             aPanel->setSelectedStricklerTypeName( aType );
434         }
435       }
436     }
437   }
438 }
439
440 void HYDROGUI_LandCoverMapOp::closePreview()
441 {
442   if( myPreviewPrs )
443   {
444     delete myPreviewPrs;
445     myPreviewPrs = 0;
446   }
447
448   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
449   if ( !aPanel )
450     return;
451
452   if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
453     aPanel->setApplyEnabled( false );
454 }
455
456 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
457 {
458   OCCViewer_ViewManager* aViewManager = getPreviewManager();
459   Handle(AIS_InteractiveContext) aCtx = NULL;
460   if ( aViewManager ) {
461     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
462       aCtx = aViewer->getAISContext();
463     }
464   }
465   return aCtx;
466 }
467
468 void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes,
469                                                  Handle(AIS_InteractiveContext)& theCtx )
470 {
471   if ( theCtx.IsNull() )
472     theCtx = getInteractiveContext();
473
474   if ( !theCtx.IsNull() && theCtx->NbSelected() > 0 )
475   {
476     for ( theCtx->InitSelected(); theCtx->MoreSelected(); theCtx->NextSelected() )
477     {
478       TopoDS_Shape aSelectedShape = theCtx->SelectedShape();
479       if ( aSelectedShape.IsNull() )
480         continue;
481
482       theSelectedShapes.Append( aSelectedShape );
483     }
484   }
485 }