Salome HOME
refs #594: create preview for 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_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       TColStd_SequenceOfExtendedString aTypes = aStricklerTableObj->GetTypes();
110       for ( int i = 1; i <= aTypes.Length(); i++ )
111       {
112         QString aType = HYDROGUI_Tool::ToQString( aTypes.Value( i ) );
113         if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
114           aStricklerTypes.append( aType );
115       }
116     }
117   }
118   aStricklerTypes.sort();
119   aPanel->setAdditionalParams( aStricklerTypes );
120
121   aPanel->blockSignals( false );
122
123   aPanel->includePolylines( aSelectedPolylines );
124   aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
125 }
126
127 void HYDROGUI_LandCoverOp::abortOperation()
128 {
129   closePreview();
130
131   HYDROGUI_Operation::abortOperation();
132 }
133
134 void HYDROGUI_LandCoverOp::commitOperation()
135 {
136   closePreview();
137
138   HYDROGUI_Operation::commitOperation();
139 }
140
141 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
142 {
143   HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
144   connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
145            this,   SLOT( onCreatePreview( const QStringList& ) ) );
146   connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
147   connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
148   return aPanel;
149 }
150
151 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
152                                          QString& theErrorMsg,
153                                          QStringList& theBrowseObjectsEntries )
154 {
155   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
156   if ( !aPanel )
157     return false;
158
159   QString anObjectName = aPanel->getObjectName().simplified();
160   if ( anObjectName.isEmpty() )
161   {
162     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
163     return false;
164   }
165
166   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
167   {
168     // check that there are no other objects with the same name in the document
169     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
170     if( !anObject.IsNull() )
171     {
172       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
173       return false;
174     }
175   }
176
177   HYDROData_SequenceOfObjects aZonePolylines;
178   QString aStricklerType;
179
180   QStringList aSelectedPolylineNames = aPanel->getPolylineNames();
181   QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
182   for( ; anIt!=aLast; anIt++ )
183   {
184     QString aPolylineName = *anIt;
185     if ( !aPolylineName.isEmpty() )
186     {
187       aZonePolylines.Append( Handle(HYDROData_PolylineXY)::DownCast(
188         HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
189     }
190   }
191
192   if ( aZonePolylines.IsEmpty() )
193   {
194     theErrorMsg = tr( "POLYLINES_NOT_DEFINED" );
195     return false;
196   }
197
198   QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
199   
200   TCollection_AsciiString anError;
201   if ( HYDROData_LandCover::buildShape( aZonePolylines, anError ).IsNull() )
202   {
203     if ( !anError.IsEmpty() ) {
204       theErrorMsg = HYDROGUI_Tool::ToQString( anError );
205     } else {
206       theErrorMsg = tr( "LAND_COVER_OBJECT_CANNOT_BE_CREATED" );
207     }
208     return false;
209   }
210   
211   Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
212     Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
213
214   aZoneObj->SetName( anObjectName );
215
216   if ( !myIsEdit )
217   {    
218     aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
219     aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
220   }
221
222   aZoneObj->SetPolylines( aZonePolylines );
223   aZoneObj->SetStricklerType( aSelectedStricklerType );
224   aZoneObj->Update();
225
226   closePreview();
227
228   if( !myIsEdit )
229   {
230     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
231     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
232     theBrowseObjectsEntries.append( anEntry );
233   }
234
235   module()->setIsToUpdate( aZoneObj );
236
237   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
238
239   return true;
240 }
241
242 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
243 {
244   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
245   if ( !aPanel )
246     return;
247
248   QApplication::setOverrideCursor( Qt::WaitCursor );  
249
250   HYDROData_SequenceOfObjects aZonePolylines;
251   QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
252   for( ; anIt!=aLast; anIt++ )
253   {
254     QString aPolylineName = *anIt;
255     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
256       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
257     if ( !aPolyline.IsNull() )
258       aZonePolylines.Append( aPolyline );
259   }
260   
261   TCollection_AsciiString anError;
262   TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );  
263
264   LightApp_Application* anApp = module()->getApp();
265   if ( !getPreviewManager() )
266     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
267                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
268   OCCViewer_ViewManager* aViewManager = getPreviewManager();
269   if ( aViewManager && !myPreviewPrs )
270   {
271     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
272     {
273       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
274       if ( !aCtx.IsNull() )
275         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
276     }
277   }
278
279   if ( aViewManager && myPreviewPrs )
280   {
281     QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
282     QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
283     if ( !myEditedObject.IsNull() ) {
284       aFillingColor = myEditedObject->GetFillingColor();
285       aBorderColor = myEditedObject->GetBorderColor();
286     }
287
288     myPreviewPrs->setFillingColor( aFillingColor, false, false );
289     myPreviewPrs->setBorderColor( aBorderColor, false, false );
290
291     if( !aZoneShape.IsNull() )
292       myPreviewPrs->setShape( aZoneShape );
293   }
294
295   QApplication::restoreOverrideCursor();
296 }
297
298 void HYDROGUI_LandCoverOp::onAddPolylines()
299 {
300   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
301   if ( !aPanel )
302     return;
303
304   // Add polylines selected in the module browser
305   Handle(HYDROData_PolylineXY) aPolyXY;
306   HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
307   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
308
309   if ( aSeq.IsEmpty() || !confirmPolylinesChange() )
310     return;
311
312   for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
313   {
314     aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
315     if (!aPolyXY.IsNull())
316       aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyXY, true ) );
317   }
318   
319   if ( aPanel->includePolylines( aSelectedPolylines ) )
320   {
321     closePreview();
322     onCreatePreview( aPanel->getPolylineNames() );
323   }  
324 }
325
326 void HYDROGUI_LandCoverOp::onRemovePolylines()
327 {
328   // Remove selected polylines from the calculation case
329   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
330   if ( !aPanel )
331     return;
332
333   QStringList aSelectedList = aPanel->getSelectedPolylineNames();
334   if ( aSelectedList.isEmpty() || !confirmPolylinesChange() )
335     return;
336   
337   HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
338   for (int i = 0; i < aSelectedList.length(); i++)
339   {
340     Handle(HYDROData_PolylineXY) anObject = Handle(HYDROData_PolylineXY)::DownCast( 
341       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
342     if ( anObject.IsNull() )
343       continue;
344
345     aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( anObject, true ) );
346   }
347
348   module()->update( UF_OCCViewer );
349   
350   if ( aPanel->excludePolylines( aSelectedPolylines ) )
351   {
352     closePreview();
353     onCreatePreview( aPanel->getPolylineNames() );
354   }
355 }
356
357 void HYDROGUI_LandCoverOp::closePreview()
358 {
359   if( myPreviewPrs )
360   {
361     delete myPreviewPrs;
362     myPreviewPrs = 0;
363   }
364 }
365
366 bool HYDROGUI_LandCoverOp::confirmPolylinesChange() const
367 {
368   if ( myEditedObject.IsNull() )
369     return true;
370
371   // Check if the land cover object is already modified or not
372   bool isConfirmed = myEditedObject->IsMustBeUpdated();
373   if ( !isConfirmed )
374   {
375     // If not modified check if the land cover has already defined polylines
376     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetPolylines();
377     if ( aSeq.Length() > 0 )
378     {
379       // If there are already defined polylines then ask a user to confirm land cover recalculation
380       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
381                       tr( "POLYLINES_CHANGED" ),
382                       tr( "CONFIRM_LAND_COVER_RECALCULATION" ),
383                       QMessageBox::Yes | QMessageBox::No,
384                       QMessageBox::No ) == QMessageBox::Yes );
385     }
386     else
387     {
388       isConfirmed = true; // No polylines - nothing to recalculate
389     }
390   }
391   return isConfirmed;
392 }