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