Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_LandCoverMapOp.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_LandCoverMapOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_OCCDisplayer.h"
23 #include "HYDROGUI_Operations.h"
24 #include "HYDROGUI_LandCoverMapDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27 #include "HYDROGUI_DataObject.h"
28
29 #include <HYDROData_Iterator.h>
30 #include <HYDROData_StricklerTable.h>
31 #include <HYDROData_PolylineXY.h>
32 #include <HYDROData_Object.h>
33
34 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
35 : HYDROGUI_Operation( theModule ),
36   myOperationId( theOperationId ),
37   myPreviewPrs( 0 )
38 {
39   switch( myOperationId )
40   {
41     case CreateLandCoverMapId:
42       setName( tr( "CREATE_LAND_COVER_MAP" ) );
43       break;
44     case AddLandCoverId:
45       setName( tr( "ADD_LAND_COVER" ) );
46       break;
47     case RemoveLandCoverId:
48       setName( tr( "REMOVE_LAND_COVER" ) );
49       break;
50     case SplitLandCoverId:
51       setName( tr( "SPLIT_LAND_COVER" ) );
52       break;
53     case MergeLandCoverId:
54       setName( tr( "MERGE_LAND_COVER" ) );
55       break;
56   }  
57 }
58
59 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
60 {
61   closePreview();
62 }
63
64 void HYDROGUI_LandCoverMapOp::startOperation()
65 {
66   HYDROGUI_Operation::startOperation();
67
68   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
69   if ( !aPanel )
70     return;
71
72   aPanel->blockSignals( true );
73
74   aPanel->reset();
75
76   // Set name of the created/edited land cover map object
77   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
78   if ( myOperationId != CreateLandCoverMapId )
79   {
80     if ( isApplyAndClose() )
81       myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82     if ( !myEditedObject.IsNull() )
83     {
84       anObjectName = myEditedObject->GetName();
85
86       // Construct a list of names of all land cover map objects defined within the data model
87       QStringList aLandCoverMapNames;
88       HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
89       for( ; anIterator.More(); anIterator.Next() )
90       {
91         Handle(HYDROData_LandCoverMap) aLandCoverObj =
92           Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );     
93         if ( !aLandCoverObj.IsNull() )
94           aLandCoverMapNames.append( aLandCoverObj->GetName() );
95       }
96
97       //aLandCoverMapNames.sort();
98       aPanel->setObjectNames( aLandCoverMapNames );
99     }
100   }  
101   aPanel->setObjectName( anObjectName );
102   
103   aPanel->blockSignals( false );
104 }
105
106 void HYDROGUI_LandCoverMapOp::abortOperation()
107 {
108   closePreview();
109
110   HYDROGUI_Operation::abortOperation();
111 }
112
113 void HYDROGUI_LandCoverMapOp::commitOperation()
114 {
115   closePreview();
116
117   HYDROGUI_Operation::commitOperation();
118 }
119
120 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
121 {
122   HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
123   /*
124   connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
125            this,   SLOT( onCreatePreview( const QStringList& ) ) );
126   connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
127   connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
128   */
129   return aPanel;
130 }
131
132 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
133                                             QString& theErrorMsg,
134                                             QStringList& theBrowseObjectsEntries )
135 {
136   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
137   if ( !aPanel )
138     return false;
139
140   // Check name of the created/edited object
141   QString anObjectName = aPanel->getObjectName().simplified();
142   if ( anObjectName.isEmpty() )
143   {
144     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
145     return false;
146   }
147
148   if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
149   {
150     // check that there are no other objects with the same name in the document
151     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
152     if( !anObject.IsNull() )
153     {
154       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
155       return false;
156     }
157   }
158
159   // Get selected polyline/face
160   Handle(HYDROData_PolylineXY) aPolyline;
161   Handle(HYDROData_Object) aFace;
162   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
163   {
164     Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
165     aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
166     aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
167     if ( aPolyline.IsNull() && aFace.IsNull() )
168     {
169       theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
170       return false;
171     }
172   }
173
174   // Get selected Strickler type
175   QString aSelectedStricklerType;
176   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == MergeLandCoverId )
177   {
178     aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
179     if ( aSelectedStricklerType.isEmpty() )
180     {
181       theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
182       return false;
183     }
184   }
185
186   // Create / find the new / edited land cover map object
187   Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
188     Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
189
190   // Set land cover map name
191   aLandCoverMapObj->SetName( anObjectName );
192   
193   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
194   {
195     bool aLandCoverAdded = false;
196     if ( !aPolyline.IsNull() )
197       aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
198     else if ( !aFace.IsNull() )
199       aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
200     if ( !aLandCoverAdded )
201     {
202       theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
203       return false;
204     }
205   }
206     
207   /*TCollection_AsciiString anError;
208   if ( HYDROData_LandCover::buildShape( aZonePolylines, anError ).IsNull() )
209   {
210     if ( !anError.IsEmpty() ) {
211       theErrorMsg = HYDROGUI_Tool::ToQString( anError );
212     } else {
213       theErrorMsg = tr( "LAND_COVER_OBJECT_CANNOT_BE_CREATED" );
214     }
215     return false;
216   }*/  
217
218   /*if ( myOperationId == CreateLandCoverMapId )
219   {    
220     aLandCoverMapObj->SetFillingColor( aLandCoverMapObj->DefaultFillingColor() );
221     aLandCoverMapObj->SetBorderColor( aLandCoverMapObj->DefaultBorderColor() );
222   }*/
223
224   //aLandCoverMapObj->SetPolylines( aZonePolylines );
225   //aLandCoverMapObj->SetStricklerType( aSelectedStricklerType );
226   aLandCoverMapObj->Update();
227
228   closePreview();
229
230   module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
231   if ( myOperationId == CreateLandCoverMapId )
232   {
233     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
234     theBrowseObjectsEntries.append( anEntry );
235   }
236
237   module()->setIsToUpdate( aLandCoverMapObj );
238   module()->getOCCDisplayer()->SetToUpdateColorScale();
239
240   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
241
242   return true;
243 }
244
245 void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineNames )
246 {
247   /*
248   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
249   if ( !aPanel )
250     return;
251
252   QApplication::setOverrideCursor( Qt::WaitCursor );  
253
254   HYDROData_SequenceOfObjects aZonePolylines;
255   QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
256   for( ; anIt!=aLast; anIt++ )
257   {
258     QString aPolylineName = *anIt;
259     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
260       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
261     if ( !aPolyline.IsNull() )
262       aZonePolylines.Append( aPolyline );
263   }
264   
265   TCollection_AsciiString anError;
266   TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );  
267
268   LightApp_Application* anApp = module()->getApp();
269   if ( !getPreviewManager() )
270     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
271                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
272   OCCViewer_ViewManager* aViewManager = getPreviewManager();
273   if ( aViewManager && !myPreviewPrs )
274   {
275     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
276     {
277       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
278       if ( !aCtx.IsNull() )
279         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
280     }
281   }
282
283   if ( aViewManager && myPreviewPrs )
284   {
285     QColor aFillingColor = Qt::magenta;
286     QColor aBorderColor = Qt::transparent;
287     if ( !myEditedObject.IsNull() ) {
288       aFillingColor = myEditedObject->GetFillingColor();
289       aBorderColor = myEditedObject->GetBorderColor();
290     }
291
292     myPreviewPrs->setFillingColor( aFillingColor, false, false );
293     myPreviewPrs->setBorderColor( aBorderColor, false, false );
294
295     if( !aZoneShape.IsNull() )
296       myPreviewPrs->setShape( aZoneShape );
297   }
298
299   QApplication::restoreOverrideCursor();
300   */
301 }
302
303 void HYDROGUI_LandCoverMapOp::closePreview()
304 {
305   if( myPreviewPrs )
306   {
307     delete myPreviewPrs;
308     myPreviewPrs = 0;
309   }
310 }