Salome HOME
refs #569: improve scalar map presentation of Land Covers.
[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       TColStd_SequenceOfExtendedString aTypes = aStricklerTableObj->GetTypes();
111       for ( int i = 1; i <= aTypes.Length(); i++ )
112       {
113         QString aType = HYDROGUI_Tool::ToQString( aTypes.Value( i ) );
114         if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
115           aStricklerTypes.append( aType );
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   if ( aZonePolylines.IsEmpty() )
194   {
195     theErrorMsg = tr( "POLYLINES_NOT_DEFINED" );
196     return false;
197   }
198
199   QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
200   
201   TCollection_AsciiString anError;
202   if ( HYDROData_LandCover::buildShape( aZonePolylines, anError ).IsNull() )
203   {
204     if ( !anError.IsEmpty() ) {
205       theErrorMsg = HYDROGUI_Tool::ToQString( anError );
206     } else {
207       theErrorMsg = tr( "LAND_COVER_OBJECT_CANNOT_BE_CREATED" );
208     }
209     return false;
210   }
211   
212   Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
213     Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
214
215   aZoneObj->SetName( anObjectName );
216
217   if ( !myIsEdit )
218   {    
219     aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
220     aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
221   }
222
223   aZoneObj->SetPolylines( aZonePolylines );
224   aZoneObj->SetStricklerType( aSelectedStricklerType );
225   aZoneObj->Update();
226
227   closePreview();
228
229   if( !myIsEdit )
230   {
231     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
232     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
233     theBrowseObjectsEntries.append( anEntry );
234   }
235
236   module()->setIsToUpdate( aZoneObj );
237   module()->getOCCDisplayer()->SetToUpdateColorScale();
238
239   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
240
241   return true;
242 }
243
244 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
245 {
246   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
247   if ( !aPanel )
248     return;
249
250   QApplication::setOverrideCursor( Qt::WaitCursor );  
251
252   HYDROData_SequenceOfObjects aZonePolylines;
253   QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
254   for( ; anIt!=aLast; anIt++ )
255   {
256     QString aPolylineName = *anIt;
257     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
258       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
259     if ( !aPolyline.IsNull() )
260       aZonePolylines.Append( aPolyline );
261   }
262   
263   TCollection_AsciiString anError;
264   TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );  
265
266   LightApp_Application* anApp = module()->getApp();
267   if ( !getPreviewManager() )
268     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
269                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
270   OCCViewer_ViewManager* aViewManager = getPreviewManager();
271   if ( aViewManager && !myPreviewPrs )
272   {
273     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
274     {
275       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
276       if ( !aCtx.IsNull() )
277         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
278     }
279   }
280
281   if ( aViewManager && myPreviewPrs )
282   {
283     QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
284     QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
285     if ( !myEditedObject.IsNull() ) {
286       aFillingColor = myEditedObject->GetFillingColor();
287       aBorderColor = myEditedObject->GetBorderColor();
288     }
289
290     myPreviewPrs->setFillingColor( aFillingColor, false, false );
291     myPreviewPrs->setBorderColor( aBorderColor, false, false );
292
293     if( !aZoneShape.IsNull() )
294       myPreviewPrs->setShape( aZoneShape );
295   }
296
297   QApplication::restoreOverrideCursor();
298 }
299
300 void HYDROGUI_LandCoverOp::onAddPolylines()
301 {
302   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
303   if ( !aPanel )
304     return;
305
306   // Add polylines selected in the module browser
307   Handle(HYDROData_PolylineXY) aPolyXY;
308   HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
309   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
310
311   if ( aSeq.IsEmpty() || !confirmPolylinesChange() )
312     return;
313
314   for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
315   {
316     aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
317     if (!aPolyXY.IsNull())
318       aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyXY, true ) );
319   }
320   
321   if ( aPanel->includePolylines( aSelectedPolylines ) )
322   {
323     closePreview();
324     onCreatePreview( aPanel->getPolylineNames() );
325   }  
326 }
327
328 void HYDROGUI_LandCoverOp::onRemovePolylines()
329 {
330   // Remove selected polylines from the calculation case
331   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
332   if ( !aPanel )
333     return;
334
335   QStringList aSelectedList = aPanel->getSelectedPolylineNames();
336   if ( aSelectedList.isEmpty() || !confirmPolylinesChange() )
337     return;
338   
339   HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
340   for (int i = 0; i < aSelectedList.length(); i++)
341   {
342     Handle(HYDROData_PolylineXY) anObject = Handle(HYDROData_PolylineXY)::DownCast( 
343       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
344     if ( anObject.IsNull() )
345       continue;
346
347     aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( anObject, true ) );
348   }
349
350   module()->update( UF_OCCViewer );
351   
352   if ( aPanel->excludePolylines( aSelectedPolylines ) )
353   {
354     closePreview();
355     onCreatePreview( aPanel->getPolylineNames() );
356   }
357 }
358
359 void HYDROGUI_LandCoverOp::closePreview()
360 {
361   if( myPreviewPrs )
362   {
363     delete myPreviewPrs;
364     myPreviewPrs = 0;
365   }
366 }
367
368 bool HYDROGUI_LandCoverOp::confirmPolylinesChange() const
369 {
370   if ( myEditedObject.IsNull() )
371     return true;
372
373   // Check if the land cover object is already modified or not
374   bool isConfirmed = myEditedObject->IsMustBeUpdated();
375   if ( !isConfirmed )
376   {
377     // If not modified check if the land cover has already defined polylines
378     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetPolylines();
379     if ( aSeq.Length() > 0 )
380     {
381       // If there are already defined polylines then ask a user to confirm land cover recalculation
382       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
383                       tr( "POLYLINES_CHANGED" ),
384                       tr( "CONFIRM_LAND_COVER_RECALCULATION" ),
385                       QMessageBox::Yes | QMessageBox::No,
386                       QMessageBox::No ) == QMessageBox::Yes );
387     }
388     else
389     {
390       isConfirmed = true; // No polylines - nothing to recalculate
391     }
392   }
393   return isConfirmed;
394 }