Salome HOME
Merge branch 'BR_LAND_COVER' of ssh://git.salome-platform.org/modules/hydro into...
[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       // Get Strickler table data from the data model
107       TColStd_SequenceOfExtendedString aTypes = aStricklerTableObj->GetTypes();
108       for ( int i = 1; i <= aTypes.Length(); i++ )
109       {
110         QString aType = HYDROGUI_Tool::ToQString( aTypes.Value( i ) );
111         if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
112           aStricklerTypes.append( aType );
113       }
114     }
115   }
116   aStricklerTypes.sort();
117   aPanel->setAdditionalParams( aStricklerTypes );
118
119   aPanel->blockSignals( false );
120
121   aPanel->setSelectedPolylineNames( aSelectedPolylines );
122   aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
123 }
124
125 void HYDROGUI_LandCoverOp::abortOperation()
126 {
127   closePreview();
128
129   HYDROGUI_Operation::abortOperation();
130 }
131
132 void HYDROGUI_LandCoverOp::commitOperation()
133 {
134   closePreview();
135
136   HYDROGUI_Operation::commitOperation();
137 }
138
139 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
140 {
141   HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
142   connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
143            this,   SLOT( onCreatePreview( const QStringList& ) ) );
144   return aPanel;
145 }
146
147 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
148                                          QString& theErrorMsg,
149                                          QStringList& theBrowseObjectsEntries )
150 {
151   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
152   if ( !aPanel )
153     return false;
154
155   QString anObjectName = aPanel->getObjectName().simplified();
156   if ( anObjectName.isEmpty() )
157   {
158     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
159     return false;
160   }
161
162   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
163   {
164     // check that there are no other objects with the same name in the document
165     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
166     if( !anObject.IsNull() )
167     {
168       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
169       return false;
170     }
171   }
172
173   HYDROData_SequenceOfObjects aZonePolylines;
174   QString aStricklerType;
175
176   QStringList aSelectedPolylineNames = aPanel->getSelectedPolylineNames();
177   QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
178   for( ; anIt!=aLast; anIt++ )
179   {
180     QString aPolylineName = *anIt;
181     if ( !aPolylineName.isEmpty() )
182     {
183       aZonePolylines.Append( Handle(HYDROData_PolylineXY)::DownCast(
184         HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
185     }
186   }
187
188   QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
189   
190   // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
191   /*
192   if ( HYDROData_LandCover::generateTopShape( aZonePolylines ).IsNull() )
193   {
194     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
195     return false;
196   }
197   */
198
199   Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
200     Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
201
202   aZoneObj->SetName( anObjectName );
203
204   if ( !myIsEdit )
205   {    
206     aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
207     aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
208   }
209
210   aZoneObj->SetPolylines( aZonePolylines );
211   aZoneObj->SetStricklerType( aSelectedStricklerType );
212   aZoneObj->Update();
213
214   closePreview();
215
216   if( !myIsEdit )
217   {
218     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
219     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
220     theBrowseObjectsEntries.append( anEntry );
221   }
222
223   module()->setIsToUpdate( aZoneObj );
224
225   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
226
227   return true;
228 }
229
230 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
231 {
232   HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
233   if ( !aPanel )
234     return;
235
236   QApplication::setOverrideCursor( Qt::WaitCursor );  
237
238   QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
239   QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
240   for( ; anIt!=aLast; anIt++ )
241   {
242     QString aPolylineName = *anIt;
243     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
244       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
245     if ( !aPolyline.IsNull() )
246       aZonePolylines.append( aPolyline );
247   }
248
249   // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method  
250   TopoDS_Shape aZoneShape;/* = HYDROData_LandCover::generateTopShape( aZonePolylines );
251   if( aZoneShape.IsNull() )
252     printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
253   */
254
255   LightApp_Application* anApp = module()->getApp();
256   if ( !getPreviewManager() )
257     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
258                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
259   OCCViewer_ViewManager* aViewManager = getPreviewManager();
260   if ( aViewManager && !myPreviewPrs )
261   {
262     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
263     {
264       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
265       if ( !aCtx.IsNull() )
266         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
267     }
268   }
269
270   if ( aViewManager && myPreviewPrs )
271   {
272     QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
273     QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
274     if ( !myEditedObject.IsNull() ) {
275       aFillingColor = myEditedObject->GetFillingColor();
276       aBorderColor = myEditedObject->GetBorderColor();
277     }
278
279     myPreviewPrs->setFillingColor( aFillingColor, false, false );
280     myPreviewPrs->setBorderColor( aBorderColor, false, false );
281
282     TopoDS_Face aFace;
283     if( !aZoneShape.IsNull() )
284       aFace = TopoDS::Face( aZoneShape );
285     myPreviewPrs->setFace( aFace, true, true, "" );
286   }
287
288   QApplication::restoreOverrideCursor();
289 }
290
291 void HYDROGUI_LandCoverOp::closePreview()
292 {
293   if( myPreviewPrs )
294   {
295     delete myPreviewPrs;
296     myPreviewPrs = 0;
297   }
298 }