Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_LandCoverOp.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_LandCoverOp.h"
20
21 #include "HYDROGUI_LandCoverDlg.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_OCCDisplayer.h"
24 #include "HYDROGUI_Shape.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27 #include "HYDROGUI_DataObject.h"
28
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_StricklerTable.h>
33
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
36
37 #include <LightApp_Application.h>
38
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Desktop.h>
41
42 #include <TopoDS.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Wire.hxx>
45
46 #include <QApplication>
47
48 HYDROGUI_LandCoverOp::HYDROGUI_LandCoverOp( HYDROGUI_Module* theModule,
49                                             const bool theIsEdit )
50 : HYDROGUI_Operation( theModule ),
51   myIsEdit( theIsEdit ),
52   myPreviewPrs( 0 )
53 {
54   setName( theIsEdit ? tr( "EDIT_LAND_COVER" ) : tr( "CREATE_LAND_COVER" ) );
55 }
56
57 HYDROGUI_LandCoverOp::~HYDROGUI_LandCoverOp()
58 {
59   closePreview();
60 }
61
62 void HYDROGUI_LandCoverOp::startOperation()
63 {
64   HYDROGUI_Operation::startOperation();
65
66   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
67   if ( !aPanel )
68     return;
69
70   aPanel->blockSignals( true );
71
72   aPanel->reset();
73
74   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_NAME" ) );
75
76   HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
77   QString     aSelectedStricklerType;
78
79   if ( myIsEdit )
80   {
81     if ( isApplyAndClose() )
82       myEditedObject = Handle(HYDROData_LandCover)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
83     if ( !myEditedObject.IsNull() )
84     {
85       anObjectName = myEditedObject->GetName();
86
87       HYDROData_SequenceOfObjects aRefPolylines = myEditedObject->GetPolylines();
88       for ( int i = aRefPolylines.Lower(); i <= aRefPolylines.Upper(); i++ ) {
89         Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
90         if ( !aPolyline.IsNull() ) {
91           aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyline, true ) );
92         }
93       }
94     }
95
96     aSelectedStricklerType = myEditedObject->GetStricklerType();
97   }
98
99   aPanel->setObjectName( anObjectName );
100   // Construct a list of unique names of all Strickler types defined within the data model
101   QStringList aStricklerTypes;
102   HYDROData_Iterator anIterator( doc(), KIND_STRICKLER_TABLE );
103   for( ; anIterator.More(); anIterator.Next() )
104   {
105     Handle(HYDROData_StricklerTable) aStricklerTableObj =
106       Handle(HYDROData_StricklerTable)::DownCast( anIterator.Current() );       
107     if ( !aStricklerTableObj.IsNull() )
108     {
109       // Get Strickler table data from the data model
110       QStringList aTypes = aStricklerTableObj->GetTypes();
111       for ( QStringList::iterator it = aTypes.begin(); it != aTypes.end(); ++it )
112       {
113         QString aType = *it;
114         if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ) )
115           aStricklerTypes.append( aType );
116       }
117     }
118   }
119
120   aStricklerTypes.sort();
121   aPanel->setAdditionalParams( aStricklerTypes );
122
123   aPanel->blockSignals( false );
124
125   aPanel->includePolylines( aSelectedPolylines );
126   aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
127 }
128
129 void HYDROGUI_LandCoverOp::abortOperation()
130 {
131   closePreview();
132
133   HYDROGUI_Operation::abortOperation();
134 }
135
136 void HYDROGUI_LandCoverOp::commitOperation()
137 {
138   closePreview();
139
140   HYDROGUI_Operation::commitOperation();
141 }
142
143 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
144 {
145   HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
146   connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
147            this,   SLOT( onCreatePreview( const QStringList& ) ) );
148   connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
149   connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
150   return aPanel;
151 }
152
153 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
154                                          QString& theErrorMsg,
155                                          QStringList& theBrowseObjectsEntries )
156 {
157   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
158   if ( !aPanel )
159     return false;
160
161   QString anObjectName = aPanel->getObjectName().simplified();
162   if ( anObjectName.isEmpty() )
163   {
164     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
165     return false;
166   }
167
168   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
169   {
170     // check that there are no other objects with the same name in the document
171     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
172     if( !anObject.IsNull() )
173     {
174       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
175       return false;
176     }
177   }
178
179   HYDROData_SequenceOfObjects aZonePolylines;
180   QString aStricklerType;
181
182   QStringList aSelectedPolylineNames = aPanel->getPolylineNames();
183   QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
184   for( ; anIt!=aLast; anIt++ )
185   {
186     QString aPolylineName = *anIt;
187     if ( !aPolylineName.isEmpty() )
188     {
189       aZonePolylines.Append( Handle(HYDROData_PolylineXY)::DownCast(
190         HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
191     }
192   }
193
194   if ( aZonePolylines.IsEmpty() )
195   {
196     theErrorMsg = tr( "POLYLINES_NOT_DEFINED" );
197     return false;
198   }
199
200   QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
201   
202   TCollection_AsciiString anError;
203   if ( HYDROData_LandCover::buildShape( aZonePolylines, anError ).IsNull() )
204   {
205     if ( !anError.IsEmpty() ) {
206       theErrorMsg = HYDROGUI_Tool::ToQString( anError );
207     } else {
208       theErrorMsg = tr( "LAND_COVER_OBJECT_CANNOT_BE_CREATED" );
209     }
210     return false;
211   }
212   
213   Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
214     Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
215
216   aZoneObj->SetName( anObjectName );
217
218   if ( !myIsEdit )
219   {    
220     aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
221     aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
222   }
223
224   aZoneObj->SetPolylines( aZonePolylines );
225   aZoneObj->SetStricklerType( aSelectedStricklerType );
226   aZoneObj->Update();
227
228   closePreview();
229
230   if( !myIsEdit )
231   {
232     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
233     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
234     theBrowseObjectsEntries.append( anEntry );
235   }
236
237   module()->setIsToUpdate( aZoneObj );
238   module()->getOCCDisplayer()->SetToUpdateColorScale();
239
240   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
241
242   return true;
243 }
244
245 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
246 {
247   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
248   if ( !aPanel )
249     return;
250
251   QApplication::setOverrideCursor( Qt::WaitCursor );  
252
253   HYDROData_SequenceOfObjects aZonePolylines;
254   QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
255   for( ; anIt!=aLast; anIt++ )
256   {
257     QString aPolylineName = *anIt;
258     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
259       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
260     if ( !aPolyline.IsNull() )
261       aZonePolylines.Append( aPolyline );
262   }
263   
264   TCollection_AsciiString anError;
265   TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );  
266
267   LightApp_Application* anApp = module()->getApp();
268   if ( !getPreviewManager() )
269     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
270                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
271   OCCViewer_ViewManager* aViewManager = getPreviewManager();
272   if ( aViewManager && !myPreviewPrs )
273   {
274     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
275     {
276       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
277       if ( !aCtx.IsNull() )
278         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
279     }
280   }
281
282   if ( aViewManager && myPreviewPrs )
283   {
284     QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
285     QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
286     if ( !myEditedObject.IsNull() ) {
287       aFillingColor = myEditedObject->GetFillingColor();
288       aBorderColor = myEditedObject->GetBorderColor();
289     }
290
291     myPreviewPrs->setFillingColor( aFillingColor, false, false );
292     myPreviewPrs->setBorderColor( aBorderColor, false, false );
293
294     if( !aZoneShape.IsNull() )
295       myPreviewPrs->setShape( aZoneShape );
296   }
297
298   QApplication::restoreOverrideCursor();
299 }
300
301 void HYDROGUI_LandCoverOp::onAddPolylines()
302 {
303   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
304   if ( !aPanel )
305     return;
306
307   // Add polylines selected in the module browser
308   Handle(HYDROData_PolylineXY) aPolyXY;
309   HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
310   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
311
312   if ( aSeq.IsEmpty() || !confirmPolylinesChange() )
313     return;
314
315   for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
316   {
317     aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
318     if (!aPolyXY.IsNull())
319       aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyXY, true ) );
320   }
321   
322   if ( aPanel->includePolylines( aSelectedPolylines ) )
323   {
324     closePreview();
325     onCreatePreview( aPanel->getPolylineNames() );
326   }  
327 }
328
329 void HYDROGUI_LandCoverOp::onRemovePolylines()
330 {
331   // Remove selected polylines from the calculation case
332   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
333   if ( !aPanel )
334     return;
335
336   QStringList aSelectedList = aPanel->getSelectedPolylineNames();
337   if ( aSelectedList.isEmpty() || !confirmPolylinesChange() )
338     return;
339   
340   HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
341   for (int i = 0; i < aSelectedList.length(); i++)
342   {
343     Handle(HYDROData_PolylineXY) anObject = Handle(HYDROData_PolylineXY)::DownCast( 
344       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
345     if ( anObject.IsNull() )
346       continue;
347
348     aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( anObject, true ) );
349   }
350
351   module()->update( UF_OCCViewer );
352   
353   if ( aPanel->excludePolylines( aSelectedPolylines ) )
354   {
355     closePreview();
356     onCreatePreview( aPanel->getPolylineNames() );
357   }
358 }
359
360 void HYDROGUI_LandCoverOp::closePreview()
361 {
362   if( myPreviewPrs )
363   {
364     delete myPreviewPrs;
365     myPreviewPrs = 0;
366   }
367 }
368
369 bool HYDROGUI_LandCoverOp::confirmPolylinesChange() const
370 {
371   if ( myEditedObject.IsNull() )
372     return true;
373
374   // Check if the land cover object is already modified or not
375   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
376   if ( !isConfirmed )
377   {
378     // If not modified check if the land cover has already defined polylines
379     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetPolylines();
380     if ( aSeq.Length() > 0 )
381     {
382       // If there are already defined polylines then ask a user to confirm land cover recalculation
383       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
384                       tr( "POLYLINES_CHANGED" ),
385                       tr( "CONFIRM_LAND_COVER_RECALCULATION" ),
386                       QMessageBox::Yes | QMessageBox::No,
387                       QMessageBox::No ) == QMessageBox::Yes );
388     }
389     else
390     {
391       isConfirmed = true; // No polylines - nothing to recalculate
392     }
393   }
394   return isConfirmed;
395 }