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