Salome HOME
d8b4fdc36811221377d778809512b54583fd8076
[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       // TODO: Temporary commented until getPolylinesCount, GetPolyline and GetStricklerType data model methods will be implemented
84       /*
85       int nNoPolylines = myEditedObject->getPolylinesCount();
86       for (int i=0; i<nNoPolylines; i++)
87       {
88         Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline(i);
89         if ( !aRefPolyline.IsNull() )
90           aSelectedPolylines.append( aRefPolyline->GetName() );
91       }
92
93       aSelectedStricklerType = myEditedObject->GetStricklerType();
94       */
95
96       // The code below is a sample of data filling Land cover object
97       aSelectedPolylines.append( "Lake_1" );
98       aSelectedPolylines.append( "Polyline_1" );
99       aSelectedStricklerType = "Canaux naturels";
100     }
101   }
102
103   aPanel->setObjectName( anObjectName );
104   aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
105   // Construct a list of unique names of all Strickler types defined within the data model
106   QStringList aStricklerTypes;
107   HYDROData_Iterator anIterator( doc(), KIND_STRICKLER_TABLE );
108   for( ; anIterator.More(); anIterator.Next() )
109   {
110     Handle(HYDROData_StricklerTable) aStricklerTableObj =
111       Handle(HYDROData_StricklerTable)::DownCast( anIterator.Current() );       
112     if ( !aStricklerTableObj.IsNull() )
113     {
114       // TODO: implement GetTypes method in data model
115       //QStringList aTypes = aStricklerTableObj->GetTypes();
116       // The code below is a sample of data filling list of Strickler types
117       QStringList aTypes;
118       aTypes.append("Zones de champs, prairies, sans cultures");
119       aTypes.append("Canaux naturels");
120       aTypes.append("Zones à faible urbanization (bourg)");
121       QStringList::const_iterator anIt = aTypes.begin(), aLast = aTypes.end();
122       for( ; anIt!=aLast; anIt++ )
123       {
124         QString aType = *anIt;
125         if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
126           aStricklerTypes.append( aType );
127       }
128     }
129   }
130   aStricklerTypes.sort();
131   aPanel->setAdditionalParams( aStricklerTypes );
132
133   aPanel->blockSignals( false );
134
135   aPanel->setSelectedPolylineNames( aSelectedPolylines );
136   aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
137 }
138
139 void HYDROGUI_LandCoverOp::abortOperation()
140 {
141   closePreview();
142
143   HYDROGUI_Operation::abortOperation();
144 }
145
146 void HYDROGUI_LandCoverOp::commitOperation()
147 {
148   closePreview();
149
150   HYDROGUI_Operation::commitOperation();
151 }
152
153 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
154 {
155   HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
156   connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
157            this,   SLOT( onCreatePreview( const QStringList& ) ) );
158   return aPanel;
159 }
160
161 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
162                                          QString& theErrorMsg,
163                                          QStringList& theBrowseObjectsEntries )
164 {
165   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
166   if ( !aPanel )
167     return false;
168
169   QString anObjectName = aPanel->getObjectName().simplified();
170   if ( anObjectName.isEmpty() )
171   {
172     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
173     return false;
174   }
175
176   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
177   {
178     // check that there are no other objects with the same name in the document
179     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
180     if( !anObject.IsNull() )
181     {
182       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
183       return false;
184     }
185   }
186
187   QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
188   QString aStricklerType;
189
190   QStringList aSelectedPolylineNames = aPanel->getSelectedPolylineNames();
191   QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
192   for( ; anIt!=aLast; anIt++ )
193   {
194     QString aPolylineName = *anIt;
195     if ( !aPolylineName.isEmpty() )
196     {
197       aZonePolylines.append( Handle(HYDROData_PolylineXY)::DownCast(
198         HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
199     }
200   }
201
202   QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
203   
204   // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
205   /*
206   if ( HYDROData_LandCover::generateTopShape( aZonePolylines ).IsNull() )
207   {
208     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
209     return false;
210   }
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   // TODO: Temporary commented until SetFillingColor, DefaultFillingColor,
219   // SetBorderColor and DefaultBorderColor data model methods will be implemented
220   /*
221   if ( !myIsEdit )
222   {    
223     aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
224     aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
225   }
226   */
227
228   // TODO: Temporary commented until SetPolylines and SetStricklerType data model methods will be implemented
229   //aZoneObj->SetPolylines( aZonePolylines );
230   //aZoneObj->SetStricklerType( aSelectedStricklerType );
231   aZoneObj->Update();
232
233   closePreview();
234
235   if( !myIsEdit )
236   {
237     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
238     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
239     theBrowseObjectsEntries.append( anEntry );
240   }
241
242   module()->setIsToUpdate( aZoneObj );
243
244   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
245
246   return true;
247 }
248
249 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
250 {
251   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
252   if ( !aPanel )
253     return;
254
255   QApplication::setOverrideCursor( Qt::WaitCursor );  
256
257   QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
258   QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
259   for( ; anIt!=aLast; anIt++ )
260   {
261     QString aPolylineName = *anIt;
262     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
263       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
264     if ( !aPolyline.IsNull() )
265       aZonePolylines.append( aPolyline );
266   }
267
268   // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method  
269   TopoDS_Shape aZoneShape;/* = HYDROData_LandCover::generateTopShape( aZonePolylines );
270   if( aZoneShape.IsNull() )
271     printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
272   */
273
274   LightApp_Application* anApp = module()->getApp();
275   if ( !getPreviewManager() )
276     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
277                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
278   OCCViewer_ViewManager* aViewManager = getPreviewManager();
279   if ( aViewManager && !myPreviewPrs )
280   {
281     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
282     {
283       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
284       if ( !aCtx.IsNull() )
285         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
286     }
287   }
288
289   if ( aViewManager && myPreviewPrs )
290   {
291     // TODO: Temporary commented until GetFillingColor, DefaultFillingColor,
292     // GetBorderColor and DefaultBorderColor data model methods will be implemented
293     /*
294     QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
295     QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
296     if ( !myEditedObject.IsNull() ) {
297       aFillingColor = myEditedObject->GetFillingColor();
298       aBorderColor = myEditedObject->GetBorderColor();
299     }
300
301     myPreviewPrs->setFillingColor( aFillingColor, false, false );
302     myPreviewPrs->setBorderColor( aBorderColor, false, false );
303     */
304
305     TopoDS_Face aFace;
306     if( !aZoneShape.IsNull() )
307       aFace = TopoDS::Face( aZoneShape );
308     myPreviewPrs->setFace( aFace, true, true, "" );
309   }
310
311   QApplication::restoreOverrideCursor();
312 }
313
314 void HYDROGUI_LandCoverOp::closePreview()
315 {
316   if( myPreviewPrs )
317   {
318     delete myPreviewPrs;
319     myPreviewPrs = 0;
320   }
321 }