Salome HOME
492e871d1512400d072cc0c42061df009d02c98e
[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 <TopoDS.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Wire.hxx>
41
42 #include <QApplication>
43
44 HYDROGUI_LandCoverOp::HYDROGUI_LandCoverOp( HYDROGUI_Module* theModule,
45                                             const bool theIsEdit )
46 : HYDROGUI_Operation( theModule ),
47   myIsEdit( theIsEdit ),
48   myPreviewPrs( 0 )
49 {
50   setName( theIsEdit ? tr( "EDIT_LAND_COVER" ) : tr( "CREATE_LAND_COVER" ) );
51 }
52
53 HYDROGUI_LandCoverOp::~HYDROGUI_LandCoverOp()
54 {
55   closePreview();
56 }
57
58 void HYDROGUI_LandCoverOp::startOperation()
59 {
60   HYDROGUI_Operation::startOperation();
61
62   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
63   if ( !aPanel )
64     return;
65
66   aPanel->blockSignals( true );
67
68   aPanel->reset();
69
70   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_NAME" ) );
71
72   QStringList aSelectedPolylines;
73   QString     aSelectedStricklerType;
74
75   if ( myIsEdit )
76   {
77     if ( isApplyAndClose() )
78       myEditedObject = Handle(HYDROData_LandCover)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79     if ( !myEditedObject.IsNull() )
80     {
81       anObjectName = myEditedObject->GetName();
82
83       HYDROData_SequenceOfObjects aRefPolylines = myEditedObject->GetPolylines();
84       for ( int i = aRefPolylines.Lower(); i <= aRefPolylines.Upper(); i++ ) {
85         Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
86         if ( !aPolyline.IsNull() ) {
87           aSelectedPolylines.append( aPolyline->GetName() );
88         }
89       }
90     }
91
92     aSelectedStricklerType = myEditedObject->GetStricklerType();
93   }
94
95   aPanel->setObjectName( anObjectName );
96   aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
97   // Construct a list of unique names of all Strickler types defined within the data model
98   QStringList aStricklerTypes;
99   HYDROData_Iterator anIterator( doc(), KIND_STRICKLER_TABLE );
100   for( ; anIterator.More(); anIterator.Next() )
101   {
102     Handle(HYDROData_StricklerTable) aStricklerTableObj =
103       Handle(HYDROData_StricklerTable)::DownCast( anIterator.Current() );       
104     if ( !aStricklerTableObj.IsNull() )
105     {
106       // TODO: implement GetTypes method in data model
107       //QStringList aTypes = aStricklerTableObj->GetTypes();
108       // The code below is a sample of data filling list of Strickler types
109       QStringList aTypes;
110       aTypes.append("Zones de champs, prairies, sans cultures");
111       aTypes.append("Canaux naturels");
112       aTypes.append("Zones à faible urbanization (bourg)");
113       QStringList::const_iterator anIt = aTypes.begin(), aLast = aTypes.end();
114       for( ; anIt!=aLast; anIt++ )
115       {
116         QString aType = *anIt;
117         if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
118           aStricklerTypes.append( aType );
119       }
120     }
121   }
122   aStricklerTypes.sort();
123   aPanel->setAdditionalParams( aStricklerTypes );
124
125   aPanel->blockSignals( false );
126
127   aPanel->setSelectedPolylineNames( aSelectedPolylines );
128   aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
129 }
130
131 void HYDROGUI_LandCoverOp::abortOperation()
132 {
133   closePreview();
134
135   HYDROGUI_Operation::abortOperation();
136 }
137
138 void HYDROGUI_LandCoverOp::commitOperation()
139 {
140   closePreview();
141
142   HYDROGUI_Operation::commitOperation();
143 }
144
145 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
146 {
147   HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
148   connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
149            this,   SLOT( onCreatePreview( const QStringList& ) ) );
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->getSelectedPolylineNames();
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   QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
195   
196   // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
197   /*
198   if ( HYDROData_LandCover::generateTopShape( aZonePolylines ).IsNull() )
199   {
200     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
201     return false;
202   }
203   */
204
205   Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
206     Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
207
208   aZoneObj->SetName( anObjectName );
209
210   if ( !myIsEdit )
211   {    
212     aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
213     aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
214   }
215
216   aZoneObj->SetPolylines( aZonePolylines );
217   aZoneObj->SetStricklerType( aSelectedStricklerType );
218   aZoneObj->Update();
219
220   closePreview();
221
222   if( !myIsEdit )
223   {
224     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
225     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
226     theBrowseObjectsEntries.append( anEntry );
227   }
228
229   module()->setIsToUpdate( aZoneObj );
230
231   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
232
233   return true;
234 }
235
236 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
237 {
238   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
239   if ( !aPanel )
240     return;
241
242   QApplication::setOverrideCursor( Qt::WaitCursor );  
243
244   QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
245   QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
246   for( ; anIt!=aLast; anIt++ )
247   {
248     QString aPolylineName = *anIt;
249     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
250       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
251     if ( !aPolyline.IsNull() )
252       aZonePolylines.append( aPolyline );
253   }
254
255   // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method  
256   TopoDS_Shape aZoneShape;/* = HYDROData_LandCover::generateTopShape( aZonePolylines );
257   if( aZoneShape.IsNull() )
258     printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
259   */
260
261   LightApp_Application* anApp = module()->getApp();
262   if ( !getPreviewManager() )
263     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
264                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
265   OCCViewer_ViewManager* aViewManager = getPreviewManager();
266   if ( aViewManager && !myPreviewPrs )
267   {
268     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
269     {
270       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
271       if ( !aCtx.IsNull() )
272         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
273     }
274   }
275
276   if ( aViewManager && myPreviewPrs )
277   {
278     // TODO: Temporary commented until GetFillingColor, DefaultFillingColor,
279     // GetBorderColor and DefaultBorderColor data model methods will be implemented
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
292     TopoDS_Face aFace;
293     if( !aZoneShape.IsNull() )
294       aFace = TopoDS::Face( aZoneShape );
295     myPreviewPrs->setFace( aFace, true, true, "" );
296   }
297
298   QApplication::restoreOverrideCursor();
299 }
300
301 void HYDROGUI_LandCoverOp::closePreview()
302 {
303   if( myPreviewPrs )
304   {
305     delete myPreviewPrs;
306     myPreviewPrs = 0;
307   }
308 }