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 TColStd_SequenceOfExtendedString aTypes = aStricklerTableObj->GetTypes();
111 for ( int i = 1; i <= aTypes.Length(); i++ )
113 QString aType = HYDROGUI_Tool::ToQString( aTypes.Value( i ) );
114 if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
115 aStricklerTypes.append( aType );
119 aStricklerTypes.sort();
120 aPanel->setAdditionalParams( aStricklerTypes );
122 aPanel->blockSignals( false );
124 aPanel->includePolylines( aSelectedPolylines );
125 aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
128 void HYDROGUI_LandCoverOp::abortOperation()
132 HYDROGUI_Operation::abortOperation();
135 void HYDROGUI_LandCoverOp::commitOperation()
139 HYDROGUI_Operation::commitOperation();
142 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
144 HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
145 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
146 this, SLOT( onCreatePreview( const QStringList& ) ) );
147 connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
148 connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
152 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
153 QString& theErrorMsg,
154 QStringList& theBrowseObjectsEntries )
156 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
160 QString anObjectName = aPanel->getObjectName().simplified();
161 if ( anObjectName.isEmpty() )
163 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
167 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
169 // check that there are no other objects with the same name in the document
170 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
171 if( !anObject.IsNull() )
173 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
178 HYDROData_SequenceOfObjects aZonePolylines;
179 QString aStricklerType;
181 QStringList aSelectedPolylineNames = aPanel->getPolylineNames();
182 QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
183 for( ; anIt!=aLast; anIt++ )
185 QString aPolylineName = *anIt;
186 if ( !aPolylineName.isEmpty() )
188 aZonePolylines.Append( Handle(HYDROData_PolylineXY)::DownCast(
189 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
193 if ( aZonePolylines.IsEmpty() )
195 theErrorMsg = tr( "POLYLINES_NOT_DEFINED" );
199 QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
201 TCollection_AsciiString anError;
202 if ( HYDROData_LandCover::buildShape( aZonePolylines, anError ).IsNull() )
204 if ( !anError.IsEmpty() ) {
205 theErrorMsg = HYDROGUI_Tool::ToQString( anError );
207 theErrorMsg = tr( "LAND_COVER_OBJECT_CANNOT_BE_CREATED" );
212 Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
213 Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
215 aZoneObj->SetName( anObjectName );
219 aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
220 aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
223 aZoneObj->SetPolylines( aZonePolylines );
224 aZoneObj->SetStricklerType( aSelectedStricklerType );
231 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
232 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
233 theBrowseObjectsEntries.append( anEntry );
236 module()->setIsToUpdate( aZoneObj );
237 module()->getOCCDisplayer()->SetToUpdateColorScale();
239 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
244 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
246 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
250 QApplication::setOverrideCursor( Qt::WaitCursor );
252 HYDROData_SequenceOfObjects aZonePolylines;
253 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
254 for( ; anIt!=aLast; anIt++ )
256 QString aPolylineName = *anIt;
257 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
258 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
259 if ( !aPolyline.IsNull() )
260 aZonePolylines.Append( aPolyline );
263 TCollection_AsciiString anError;
264 TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );
266 LightApp_Application* anApp = module()->getApp();
267 if ( !getPreviewManager() )
268 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
269 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
270 OCCViewer_ViewManager* aViewManager = getPreviewManager();
271 if ( aViewManager && !myPreviewPrs )
273 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
275 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
276 if ( !aCtx.IsNull() )
277 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
281 if ( aViewManager && myPreviewPrs )
283 QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
284 QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
285 if ( !myEditedObject.IsNull() ) {
286 aFillingColor = myEditedObject->GetFillingColor();
287 aBorderColor = myEditedObject->GetBorderColor();
290 myPreviewPrs->setFillingColor( aFillingColor, false, false );
291 myPreviewPrs->setBorderColor( aBorderColor, false, false );
293 if( !aZoneShape.IsNull() )
294 myPreviewPrs->setShape( aZoneShape );
297 QApplication::restoreOverrideCursor();
300 void HYDROGUI_LandCoverOp::onAddPolylines()
302 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
306 // Add polylines selected in the module browser
307 Handle(HYDROData_PolylineXY) aPolyXY;
308 HYDROGUI_ListModel::Object2VisibleList aSelectedPolylines;
309 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
311 if ( aSeq.IsEmpty() || !confirmPolylinesChange() )
314 for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
316 aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
317 if (!aPolyXY.IsNull())
318 aSelectedPolylines.append( HYDROGUI_ListModel::Object2Visible( aPolyXY, true ) );
321 if ( aPanel->includePolylines( aSelectedPolylines ) )
324 onCreatePreview( aPanel->getPolylineNames() );
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 );
352 if ( aPanel->excludePolylines( aSelectedPolylines ) )
355 onCreatePreview( aPanel->getPolylineNames() );
359 void HYDROGUI_LandCoverOp::closePreview()
368 bool HYDROGUI_LandCoverOp::confirmPolylinesChange() const
370 if ( myEditedObject.IsNull() )
373 // Check if the land cover object is already modified or not
374 bool isConfirmed = myEditedObject->IsMustBeUpdated();
377 // If not modified check if the land cover has already defined polylines
378 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetPolylines();
379 if ( aSeq.Length() > 0 )
381 // If there are already defined polylines then ask a user to confirm land cover recalculation
382 isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
383 tr( "POLYLINES_CHANGED" ),
384 tr( "CONFIRM_LAND_COVER_RECALCULATION" ),
385 QMessageBox::Yes | QMessageBox::No,
386 QMessageBox::No ) == QMessageBox::Yes );
390 isConfirmed = true; // No polylines - nothing to recalculate