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_Shape.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
26 #include "HYDROGUI_DataObject.h"
28 #include <HYDROData_PolylineXY.h>
29 #include <HYDROData_Document.h>
30 #include <HYDROData_Iterator.h>
31 #include <HYDROData_StricklerTable.h>
33 #include <OCCViewer_ViewManager.h>
34 #include <OCCViewer_ViewModel.h>
36 #include <LightApp_Application.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Wire.hxx>
45 #include <QApplication>
47 HYDROGUI_LandCoverOp::HYDROGUI_LandCoverOp( HYDROGUI_Module* theModule,
48 const bool theIsEdit )
49 : HYDROGUI_Operation( theModule ),
50 myIsEdit( theIsEdit ),
53 setName( theIsEdit ? tr( "EDIT_LAND_COVER" ) : tr( "CREATE_LAND_COVER" ) );
56 HYDROGUI_LandCoverOp::~HYDROGUI_LandCoverOp()
61 void HYDROGUI_LandCoverOp::startOperation()
63 HYDROGUI_Operation::startOperation();
65 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
69 aPanel->blockSignals( true );
73 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_NAME" ) );
75 HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
76 QString aSelectedStricklerType;
80 if ( isApplyAndClose() )
81 myEditedObject = Handle(HYDROData_LandCover)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82 if ( !myEditedObject.IsNull() )
84 anObjectName = myEditedObject->GetName();
86 HYDROData_SequenceOfObjects aRefPolylines = myEditedObject->GetPolylines();
87 for ( int i = aRefPolylines.Lower(); i <= aRefPolylines.Upper(); i++ ) {
88 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
89 if ( !aPolyline.IsNull() ) {
90 aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyline, true ) );
95 aSelectedStricklerType = myEditedObject->GetStricklerType();
98 aPanel->setObjectName( anObjectName );
99 // Construct a list of unique names of all Strickler types defined within the data model
100 QStringList aStricklerTypes;
101 HYDROData_Iterator anIterator( doc(), KIND_STRICKLER_TABLE );
102 for( ; anIterator.More(); anIterator.Next() )
104 Handle(HYDROData_StricklerTable) aStricklerTableObj =
105 Handle(HYDROData_StricklerTable)::DownCast( anIterator.Current() );
106 if ( !aStricklerTableObj.IsNull() )
108 // Get Strickler table data from the data model
109 TColStd_SequenceOfExtendedString aTypes = aStricklerTableObj->GetTypes();
110 for ( int i = 1; i <= aTypes.Length(); i++ )
112 QString aType = HYDROGUI_Tool::ToQString( aTypes.Value( i ) );
113 if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
114 aStricklerTypes.append( aType );
118 aStricklerTypes.sort();
119 aPanel->setAdditionalParams( aStricklerTypes );
121 aPanel->blockSignals( false );
123 aPanel->includePolylines( aSelectedPolylines );
124 aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
127 void HYDROGUI_LandCoverOp::abortOperation()
131 HYDROGUI_Operation::abortOperation();
134 void HYDROGUI_LandCoverOp::commitOperation()
138 HYDROGUI_Operation::commitOperation();
141 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
143 HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
144 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
145 this, SLOT( onCreatePreview( const QStringList& ) ) );
146 connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
147 connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
151 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
152 QString& theErrorMsg,
153 QStringList& theBrowseObjectsEntries )
155 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
159 QString anObjectName = aPanel->getObjectName().simplified();
160 if ( anObjectName.isEmpty() )
162 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
166 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
168 // check that there are no other objects with the same name in the document
169 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
170 if( !anObject.IsNull() )
172 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
177 HYDROData_SequenceOfObjects aZonePolylines;
178 QString aStricklerType;
180 QStringList aSelectedPolylineNames = aPanel->getPolylineNames();
181 QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
182 for( ; anIt!=aLast; anIt++ )
184 QString aPolylineName = *anIt;
185 if ( !aPolylineName.isEmpty() )
187 aZonePolylines.Append( Handle(HYDROData_PolylineXY)::DownCast(
188 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
192 if ( aZonePolylines.IsEmpty() )
194 theErrorMsg = tr( "POLYLINES_NOT_DEFINED" );
198 QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
200 TCollection_AsciiString anError;
201 if ( HYDROData_LandCover::buildShape( aZonePolylines, anError ).IsNull() )
203 if ( !anError.IsEmpty() ) {
204 theErrorMsg = HYDROGUI_Tool::ToQString( anError );
206 theErrorMsg = tr( "LAND_COVER_OBJECT_CANNOT_BE_CREATED" );
211 Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
212 Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
214 aZoneObj->SetName( anObjectName );
218 aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
219 aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
222 aZoneObj->SetPolylines( aZonePolylines );
223 aZoneObj->SetStricklerType( aSelectedStricklerType );
230 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
231 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
232 theBrowseObjectsEntries.append( anEntry );
235 module()->setIsToUpdate( aZoneObj );
237 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
242 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
244 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
248 QApplication::setOverrideCursor( Qt::WaitCursor );
250 QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
251 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
252 for( ; anIt!=aLast; anIt++ )
254 QString aPolylineName = *anIt;
255 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
256 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
257 if ( !aPolyline.IsNull() )
258 aZonePolylines.append( aPolyline );
261 // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
262 TopoDS_Shape aZoneShape;/* = HYDROData_LandCover::generateTopShape( aZonePolylines );
263 if( aZoneShape.IsNull() )
264 printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
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 = HYDROData_LandCover::DefaultFillingColor();
285 QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
286 if ( !myEditedObject.IsNull() ) {
287 aFillingColor = myEditedObject->GetFillingColor();
288 aBorderColor = myEditedObject->GetBorderColor();
291 myPreviewPrs->setFillingColor( aFillingColor, false, false );
292 myPreviewPrs->setBorderColor( aBorderColor, false, false );
295 if( !aZoneShape.IsNull() )
296 aFace = TopoDS::Face( aZoneShape );
297 myPreviewPrs->setFace( aFace, true, true, "" );
300 QApplication::restoreOverrideCursor();
303 void HYDROGUI_LandCoverOp::onAddPolylines()
305 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
309 // Add polylines selected in the module browser
310 Handle(HYDROData_PolylineXY) aPolyXY;
311 HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
312 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
314 if ( aSeq.IsEmpty() || !confirmPolylinesChange() )
317 for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
319 aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
320 if (!aPolyXY.IsNull())
321 aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyXY, true ) );
324 aPanel->includePolylines( aSelectedPolylines );
325 // TODO: create preview of included polylines
328 void HYDROGUI_LandCoverOp::onRemovePolylines()
330 // Remove selected polylines from the calculation case
331 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
335 QStringList aSelectedList = aPanel->getSelectedPolylineNames();
336 if ( aSelectedList.isEmpty() || !confirmPolylinesChange() )
339 HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
340 for (int i = 0; i < aSelectedList.length(); i++)
342 Handle(HYDROData_PolylineXY) anObject = Handle(HYDROData_PolylineXY)::DownCast(
343 HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
344 if ( anObject.IsNull() )
347 aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( anObject, true ) );
350 module()->update( UF_OCCViewer );
351 aPanel->excludePolylines( aSelectedPolylines );
354 void HYDROGUI_LandCoverOp::closePreview()
363 bool HYDROGUI_LandCoverOp::confirmPolylinesChange() const
365 if ( myEditedObject.IsNull() )
368 // Check if the land cover object is already modified or not
369 bool isConfirmed = myEditedObject->IsMustBeUpdated();
372 // If not modified check if the land cover has already defined polylines
373 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetPolylines();
374 if ( aSeq.Length() > 0 )
376 // If there are already defined polylines then ask a user to confirm land cover recalculation
377 isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
378 tr( "POLYLINES_CHANGED" ),
379 tr( "CONFIRM_LAND_COVER_RECALCULATION" ),
380 QMessageBox::Yes | QMessageBox::No,
381 QMessageBox::No ) == QMessageBox::Yes );
385 isConfirmed = true; // No polylines - nothing to recalculate