Salome HOME
2ec102f4eca6d1fed78a5da5a79eb589144f0025
[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   QStringList aPolylineFaceNames;
122   onCreatePreview( aPolylineFaceNames );
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   connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
153            this,   SLOT( onCreatePreview( const QStringList& ) ) );
154   return aPanel;
155 }
156
157 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
158                                             QString& theErrorMsg,
159                                             QStringList& theBrowseObjectsEntries )
160 {
161   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
162   if ( !aPanel )
163     return false;
164
165   // Check name of the created/edited object
166   QString anObjectName = aPanel->getObjectName().simplified();
167   if ( anObjectName.isEmpty() )
168   {
169     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
170     return false;
171   }
172
173   if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
174   {
175     // check that there are no other objects with the same name in the document
176     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
177     if( !anObject.IsNull() )
178     {
179       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
180       return false;
181     }
182   }
183
184   Handle(HYDROData_PolylineXY) aPolyline;
185   Handle(HYDROData_Object) aFace;
186
187   TopTools_ListOfShape aFacesSelectedInViewer;
188
189   // Get polyline/face selected in combo-box
190   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
191   {
192     Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
193     aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
194     aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
195     if ( aPolyline.IsNull() && aFace.IsNull() )
196     {
197       theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
198       return false;
199     }
200   }
201   // Get face(s) selected in the 3d viewer
202   else if ( myOperationId == RemoveLandCoverId || 
203             myOperationId == MergeLandCoverId || 
204             myOperationId == ChangeLandCoverTypeId )
205   {
206     if ( myPreviewPrs )
207     {
208       // Fill in aFacesSelectedInViewer list
209       Handle(AIS_InteractiveContext) aCtx;
210       getSelectedShapes( aFacesSelectedInViewer, aCtx );
211     }
212   }
213
214   // Get selected Strickler type
215   QString aSelectedStricklerType;
216   if ( myOperationId == CreateLandCoverMapId || 
217        myOperationId == AddLandCoverId || 
218        myOperationId == MergeLandCoverId ||
219        myOperationId == ChangeLandCoverTypeId )
220   {
221     aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
222     if ( aSelectedStricklerType.isEmpty() )
223     {
224       theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
225       return false;
226     }
227   }
228
229   // Create / find the new / edited land cover map object
230   Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
231     Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
232
233   // Set land cover map name
234   aLandCoverMapObj->SetName( anObjectName );
235   
236   // Add land cover to new / edited land cover map
237   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
238   {
239     bool aLandCoverAdded = false;
240     if ( !aPolyline.IsNull() )
241       aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
242     else if ( !aFace.IsNull() )
243       aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
244     if ( !aLandCoverAdded )
245     {
246       theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
247       return false;
248     }
249   }
250
251   // Remove land cover from edited land cover map
252   if ( myOperationId == RemoveLandCoverId )
253   {
254     bool aLandCoverRemoved = false;
255     if ( !aFacesSelectedInViewer.IsEmpty() )
256     {
257       aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
258       if ( !aLandCoverRemoved )
259       {
260         theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
261         return false;
262       }
263     }
264   }
265
266   // Split land cover(s) inside edited land cover map
267   if ( myOperationId == SplitLandCoverId )
268   {
269     bool aLandCoverSplitted = false;
270     if ( !aPolyline.IsNull() )
271       aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
272     else if ( !aFace.IsNull() )
273     {
274       // Get the complete boundary of the object face as the splitting polyline
275       QList<TopoDS_Shape> aBoundShapes;
276       QStringList aBoundNames;
277       aFace->GetBoundaries( aBoundShapes, aBoundNames );
278
279       for( int i=0, n=aBoundShapes.size(); i<n; i++ )
280       {
281         TopoDS_Shape aShape = aBoundShapes[i];
282         if( aShape.IsNull() )
283           continue;
284
285         bool aSplitResult = aLandCoverMapObj->Split( aShape );
286         aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
287       }
288     }
289     if ( !aLandCoverSplitted )
290     {
291       theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
292       return false;
293     }
294   }
295
296   // Merge land covers inside edited land cover map
297   if ( myOperationId == MergeLandCoverId )
298   {
299     bool aLandCoverMerged = false;
300     if ( !aFacesSelectedInViewer.IsEmpty() )
301     {
302       aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
303       if ( !aLandCoverMerged )
304       {
305         theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
306         return false;
307       }
308     }
309   }
310
311   // Change Strickler type for land cover(s) inside edited land cover map
312   if ( myOperationId == ChangeLandCoverTypeId )
313   {
314     bool aLandCoverChangeType = false;
315     if ( !aFacesSelectedInViewer.IsEmpty() )
316     {
317       aLandCoverChangeType = aLandCoverMapObj->ChangeType( aFacesSelectedInViewer, aSelectedStricklerType );
318       if ( !aLandCoverChangeType )
319       {
320         theErrorMsg = tr( "LAND_COVER_TYPE_NOT_CHANGED" );
321         return false;
322       }
323     }
324   }
325     
326   // Update land cover map object and close preview
327   aLandCoverMapObj->Update();
328
329   closePreview();
330
331   // Publish the newly created land cover map in the Object Browser
332   module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
333   if ( myOperationId == CreateLandCoverMapId )
334   {
335     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
336     theBrowseObjectsEntries.append( anEntry );
337   }
338
339   // Update presentation of land cover object in the 3d viewer
340   module()->setIsToUpdate( aLandCoverMapObj );
341   module()->getOCCDisplayer()->SetToUpdateColorScale();
342
343   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
344
345   return true;
346 }
347
348 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
349 {
350   // If the edited land cover map was changed in the combo-box, update myEditedObject
351   if ( myOperationId != CreateLandCoverMapId )
352   {
353     myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
354     if ( !myEditedObject.IsNull() )
355     {
356       // Show preview of the newly selected land cover map
357       closePreview();
358       QStringList aPolylineFaceNames;
359       onCreatePreview( aPolylineFaceNames );
360     }
361   }
362 }
363
364 void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineFaceNames )
365 {
366   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
367   if ( !aPanel )
368     return;
369
370   QApplication::setOverrideCursor( Qt::WaitCursor );  
371
372   LightApp_Application* anApp = module()->getApp();
373   if ( !getPreviewManager() )
374     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
375                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
376   OCCViewer_ViewManager* aViewManager = getPreviewManager();
377   if ( aViewManager && !myPreviewPrs )
378   {
379     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
380     {
381       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
382       if ( !aCtx.IsNull() )
383       {
384         connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
385         myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
386       }
387     }
388   }
389
390   if ( aViewManager && myPreviewPrs )
391   {
392     TopoDS_Shape aLandCoverMapShape = myEditedObject->GetShape();
393     if( !aLandCoverMapShape.IsNull() )
394     {
395       if ( myOperationId == RemoveLandCoverId ||
396            myOperationId == MergeLandCoverId ||
397            myOperationId == ChangeLandCoverTypeId )
398         myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) ); 
399       myPreviewPrs->setShape( aLandCoverMapShape );      
400     }
401   }
402   
403   module()->update( UF_OCCViewer | UF_FitAll );
404
405   QApplication::restoreOverrideCursor();  
406 }
407
408 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
409 {
410   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
411   if ( !aPanel )
412     return;
413
414   Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
415   if ( !aCtx.IsNull() )
416   {
417     int aNbSelected = aCtx->NbSelected();
418
419     if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
420       // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
421       aPanel->setApplyEnabled( aNbSelected > 0 );
422     else if ( myOperationId == MergeLandCoverId )
423       // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
424       aPanel->setApplyEnabled( aNbSelected > 1 );
425
426     if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
427     {
428       if ( aNbSelected == 1 && !myEditedObject.IsNull() )
429       {
430         TopTools_ListOfShape aFacesSelectedInViewer;
431         getSelectedShapes( aFacesSelectedInViewer, aCtx );
432         if ( aFacesSelectedInViewer.Extent() == 1 )
433         {
434           QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
435           if ( !aType.isEmpty() )
436             aPanel->setSelectedStricklerTypeName( aType );
437         }
438       }
439     }
440   }
441 }
442
443 void HYDROGUI_LandCoverMapOp::closePreview()
444 {
445   if( myPreviewPrs )
446   {
447     delete myPreviewPrs;
448     myPreviewPrs = 0;
449   }
450
451   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
452   if ( !aPanel )
453     return;
454
455   if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
456     aPanel->setApplyEnabled( false );
457 }
458
459 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
460 {
461   OCCViewer_ViewManager* aViewManager = getPreviewManager();
462   Handle(AIS_InteractiveContext) aCtx = NULL;
463   if ( aViewManager ) {
464     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
465       aCtx = aViewer->getAISContext();
466     }
467   }
468   return aCtx;
469 }
470
471 void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes,
472                                                  Handle(AIS_InteractiveContext)& theCtx )
473 {
474   if ( theCtx.IsNull() )
475     theCtx = getInteractiveContext();
476
477   if ( !theCtx.IsNull() && theCtx->NbSelected() > 0 )
478   {
479     for ( theCtx->InitSelected(); theCtx->MoreSelected(); theCtx->NextSelected() )
480     {
481       TopoDS_Shape aSelectedShape = theCtx->SelectedShape();
482       if ( aSelectedShape.IsNull() )
483         continue;
484
485       theSelectedShapes.Append( aSelectedShape );
486     }
487   }
488 }