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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_LandCoverOp.h"
21 #include "HYDROGUI_LandCoverDlg.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_OCCDisplayer.h"
24 #include "HYDROGUI_Shape.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27 #include "HYDROGUI_DataObject.h"
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_StricklerTable.h>
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
37 #include <LightApp_Application.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Desktop.h>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Wire.hxx>
46 #include <QApplication>
48 HYDROGUI_LandCoverOp::HYDROGUI_LandCoverOp( HYDROGUI_Module* theModule,
49 const bool theIsEdit )
50 : HYDROGUI_Operation( theModule ),
51 myIsEdit( theIsEdit ),
54 setName( theIsEdit ? tr( "EDIT_LAND_COVER" ) : tr( "CREATE_LAND_COVER" ) );
57 HYDROGUI_LandCoverOp::~HYDROGUI_LandCoverOp()
62 void HYDROGUI_LandCoverOp::startOperation()
64 HYDROGUI_Operation::startOperation();
66 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
70 aPanel->blockSignals( true );
74 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_NAME" ) );
76 HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
77 QString aSelectedStricklerType;
81 if ( isApplyAndClose() )
82 myEditedObject = Handle(HYDROData_LandCover)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
83 if ( !myEditedObject.IsNull() )
85 anObjectName = myEditedObject->GetName();
87 HYDROData_SequenceOfObjects aRefPolylines = myEditedObject->GetPolylines();
88 for ( int i = aRefPolylines.Lower(); i <= aRefPolylines.Upper(); i++ ) {
89 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
90 if ( !aPolyline.IsNull() ) {
91 aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyline, true ) );
96 aSelectedStricklerType = myEditedObject->GetStricklerType();
99 aPanel->setObjectName( anObjectName );
100 // Construct a list of unique names of all Strickler types defined within the data model
101 QStringList aStricklerTypes;
102 HYDROData_Iterator anIterator( doc(), KIND_STRICKLER_TABLE );
103 for( ; anIterator.More(); anIterator.Next() )
105 Handle(HYDROData_StricklerTable) aStricklerTableObj =
106 Handle(HYDROData_StricklerTable)::DownCast( anIterator.Current() );
107 if ( !aStricklerTableObj.IsNull() )
109 // Get Strickler table data from the data model
110 QStringList aTypes = aStricklerTableObj->GetTypes();
111 for ( QStringList::iterator it = aTypes.begin(); it != aTypes.end(); ++it )
114 if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ) )
115 aStricklerTypes.append( aType );
120 aStricklerTypes.sort();
121 aPanel->setAdditionalParams( aStricklerTypes );
123 aPanel->blockSignals( false );
125 aPanel->includePolylines( aSelectedPolylines );
126 aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
129 void HYDROGUI_LandCoverOp::abortOperation()
133 HYDROGUI_Operation::abortOperation();
136 void HYDROGUI_LandCoverOp::commitOperation()
140 HYDROGUI_Operation::commitOperation();
143 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
145 HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
146 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
147 this, SLOT( onCreatePreview( const QStringList& ) ) );
148 connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
149 connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
153 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
154 QString& theErrorMsg,
155 QStringList& theBrowseObjectsEntries )
157 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
161 QString anObjectName = aPanel->getObjectName().simplified();
162 if ( anObjectName.isEmpty() )
164 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
168 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
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() )
174 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
179 HYDROData_SequenceOfObjects aZonePolylines;
180 QString aStricklerType;
182 QStringList aSelectedPolylineNames = aPanel->getPolylineNames();
183 QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
184 for( ; anIt!=aLast; anIt++ )
186 QString aPolylineName = *anIt;
187 if ( !aPolylineName.isEmpty() )
189 aZonePolylines.Append( Handle(HYDROData_PolylineXY)::DownCast(
190 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
194 if ( aZonePolylines.IsEmpty() )
196 theErrorMsg = tr( "POLYLINES_NOT_DEFINED" );
200 QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
202 TCollection_AsciiString anError;
203 if ( HYDROData_LandCover::buildShape( aZonePolylines, anError ).IsNull() )
205 if ( !anError.IsEmpty() ) {
206 theErrorMsg = HYDROGUI_Tool::ToQString( anError );
208 theErrorMsg = tr( "LAND_COVER_OBJECT_CANNOT_BE_CREATED" );
213 Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
214 Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
216 aZoneObj->SetName( anObjectName );
220 aZoneObj->SetFillingColor( aZoneObj->DefaultFillingColor() );
221 aZoneObj->SetBorderColor( aZoneObj->DefaultBorderColor() );
224 aZoneObj->SetPolylines( aZonePolylines );
225 aZoneObj->SetStricklerType( aSelectedStricklerType );
232 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
233 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
234 theBrowseObjectsEntries.append( anEntry );
237 module()->setIsToUpdate( aZoneObj );
238 module()->getOCCDisplayer()->SetToUpdateColorScale();
240 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
245 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
247 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
251 QApplication::setOverrideCursor( Qt::WaitCursor );
253 HYDROData_SequenceOfObjects aZonePolylines;
254 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
255 for( ; anIt!=aLast; anIt++ )
257 QString aPolylineName = *anIt;
258 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
259 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
260 if ( !aPolyline.IsNull() )
261 aZonePolylines.Append( aPolyline );
264 TCollection_AsciiString anError;
265 TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );
267 LightApp_Application* anApp = module()->getApp();
268 if ( !getPreviewManager() )
269 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
270 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
271 OCCViewer_ViewManager* aViewManager = getPreviewManager();
272 if ( aViewManager && !myPreviewPrs )
274 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
276 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
277 if ( !aCtx.IsNull() )
278 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
282 if ( aViewManager && myPreviewPrs )
284 QColor aFillingColor = Qt::magenta;
285 QColor aBorderColor = Qt::transparent;
286 if ( !myEditedObject.IsNull() ) {
287 aFillingColor = myEditedObject->GetFillingColor();
288 aBorderColor = myEditedObject->GetBorderColor();
291 myPreviewPrs->setFillingColor( aFillingColor, false, false );
292 myPreviewPrs->setBorderColor( aBorderColor, false, false );
294 if( !aZoneShape.IsNull() )
295 myPreviewPrs->setShape( aZoneShape );
298 QApplication::restoreOverrideCursor();
301 void HYDROGUI_LandCoverOp::onAddPolylines()
303 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
307 // Add polylines selected in the module browser
308 Handle(HYDROData_PolylineXY) aPolyXY;
309 HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
310 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
312 if ( aSeq.IsEmpty() || !confirmPolylinesChange() )
315 for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
317 aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
318 if (!aPolyXY.IsNull())
319 aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyXY, true ) );
322 if ( aPanel->includePolylines( aSelectedPolylines ) )
325 onCreatePreview( aPanel->getPolylineNames() );
329 void HYDROGUI_LandCoverOp::onRemovePolylines()
331 // Remove selected polylines from the calculation case
332 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
336 QStringList aSelectedList = aPanel->getSelectedPolylineNames();
337 if ( aSelectedList.isEmpty() || !confirmPolylinesChange() )
340 HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
341 for (int i = 0; i < aSelectedList.length(); i++)
343 Handle(HYDROData_PolylineXY) anObject = Handle(HYDROData_PolylineXY)::DownCast(
344 HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
345 if ( anObject.IsNull() )
348 aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( anObject, true ) );
351 module()->update( UF_OCCViewer );
353 if ( aPanel->excludePolylines( aSelectedPolylines ) )
356 onCreatePreview( aPanel->getPolylineNames() );
360 void HYDROGUI_LandCoverOp::closePreview()
369 bool HYDROGUI_LandCoverOp::confirmPolylinesChange() const
371 if ( myEditedObject.IsNull() )
374 // Check if the land cover object is already modified or not
375 bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
378 // If not modified check if the land cover has already defined polylines
379 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetPolylines();
380 if ( aSeq.Length() > 0 )
382 // If there are already defined polylines then ask a user to confirm land cover recalculation
383 isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
384 tr( "POLYLINES_CHANGED" ),
385 tr( "CONFIRM_LAND_COVER_RECALCULATION" ),
386 QMessageBox::Yes | QMessageBox::No,
387 QMessageBox::No ) == QMessageBox::Yes );
391 isConfirmed = true; // No polylines - nothing to recalculate