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_ChannelOp.h"
21 #include "HYDROGUI_DataModel.h"
22 #include <HYDROGUI_DataObject.h>
23 #include "HYDROGUI_ChannelDlg.h"
24 #include "HYDROGUI_Module.h"
25 #include "HYDROGUI_Shape.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_Tool2.h"
28 #include "HYDROGUI_UpdateFlags.h"
30 #include <HYDROData_Iterator.h>
31 #include <HYDROData_Polyline3D.h>
32 #include <HYDROData_Profile.h>
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
37 #include <LightApp_Application.h>
38 #include <LightApp_UpdateFlags.h>
42 HYDROGUI_ChannelOp::HYDROGUI_ChannelOp( HYDROGUI_Module* theModule,
43 const bool theIsEdit )
44 : HYDROGUI_Operation( theModule ),
45 myIsEdit( theIsEdit ),
48 setName( theIsEdit ? tr( "EDIT_CHANNEL" ) : tr( "CREATE_CHANNEL" ) );
51 HYDROGUI_ChannelOp::~HYDROGUI_ChannelOp()
56 void HYDROGUI_ChannelOp::startOperation()
58 HYDROGUI_Operation::startOperation();
60 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
62 aPanel->blockSignals( true );
66 if ( isApplyAndClose() )
67 myEditedObject.Nullify();
69 QString aSelectedGuideLine, aSelectedProfile;
71 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
74 if ( isApplyAndClose() )
76 Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
77 if ( !myEditedObject.IsNull() )
79 anObjectName = myEditedObject->GetName();
81 Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
82 if ( !aRefGuideLine.IsNull() )
83 aSelectedGuideLine = aRefGuideLine->GetName();
85 Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
86 if ( !aRefProfile.IsNull() )
87 aSelectedProfile = aRefProfile->GetName();
91 // collect information about existing 3d polylines
92 QStringList aGuideLines;
93 HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
94 for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
96 Handle(HYDROData_Polyline3D) aPolyline3d =
97 Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
98 if( !aPolyline3d.IsNull() )
100 TopoDS_Shape aShape = aPolyline3d->GetShape3D();
101 if( aShape.ShapeType()==TopAbs_WIRE )
103 TopoDS_Wire aWire = TopoDS::Wire( aShape );
104 if( !aWire.Closed() )
105 aGuideLines.append( aPolyline3d->GetName() );
110 // collect information about existing profiles
111 QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE, false );
113 aPanel->setObjectName( anObjectName );
115 aPanel->setGuideLineNames( aGuideLines );
116 aPanel->setProfileNames( aProfiles );
118 aPanel->setGuideLineName( aSelectedGuideLine );
119 aPanel->setProfileName( aSelectedProfile );
121 if( !myEditedObject.IsNull() )
122 aPanel->setEquiDistance( myEditedObject->GetEquiDistance() );
124 aPanel->setEquiDistance( 1.0 );
126 aPanel->blockSignals( false );
131 void HYDROGUI_ChannelOp::abortOperation()
134 HYDROGUI_Operation::abortOperation();
137 void HYDROGUI_ChannelOp::commitOperation()
140 HYDROGUI_Operation::commitOperation();
143 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
145 HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
146 connect( aPanel, SIGNAL( CreatePreview() ), this, SLOT( onCreatePreview() ) );
150 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
151 QString& theErrorMsg,
152 QStringList& theBrowseObjectsEntries )
154 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
158 QString anObjectName = aPanel->getObjectName().simplified();
159 if ( anObjectName.isEmpty() )
161 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
165 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
167 // check that there are no other objects with the same name in the document
168 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
169 if( !anObject.IsNull() )
171 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
177 if ( myEditedObject.IsNull() )
178 myEditedObject = createNewObject(); // Create new data model object
180 myEditedObject->SetName( anObjectName );
184 myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
185 myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
188 QString aGuideLineName = aPanel->getGuideLineName();
189 QString aProfileName = aPanel->getProfileName();
190 /*if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
192 myEditedObject->RemoveGuideLine();
193 myEditedObject->RemoveProfile();
197 Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
198 HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
199 if ( aGuideLine.IsNull() )
201 theErrorMsg = tr( "GUIDE_LINE_IS_NOT_SELECTED" );
205 myEditedObject->RemoveGuideLine();
206 myEditedObject->SetGuideLine( aGuideLine );
208 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
209 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
210 if ( aProfile.IsNull() )
212 theErrorMsg = tr( "PROFILE_IS_NOT_SELECTED" );
216 myEditedObject->RemoveProfile();
217 myEditedObject->SetProfile( aProfile );
218 myEditedObject->SetEquiDistance( aPanel->getEquiDistance() );
221 if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
222 myEditedObject->Update();
228 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
229 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
230 theBrowseObjectsEntries.append( anEntry );
233 module()->setIsToUpdate( myEditedObject );
235 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
240 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
242 return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
245 void HYDROGUI_ChannelOp::onCreatePreview()
247 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
251 QString aGuideLineName = aPanel->getGuideLineName();
252 QString aProfileName = aPanel->getProfileName();
253 if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
259 LightApp_Application* anApp = module()->getApp();
261 if ( !getPreviewManager() )
263 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
264 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
267 OCCViewer_ViewManager* aViewManager = getPreviewManager();
268 if ( aViewManager && !myPreviewPrs )
270 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
272 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
273 if ( !aCtx.IsNull() )
275 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
277 QColor aFillingColor = Qt::blue;
278 QColor aBorderColor = Qt::transparent;
279 if ( !myEditedObject.IsNull() )
281 aFillingColor = myEditedObject->GetFillingColor();
282 aBorderColor = myEditedObject->GetBorderColor();
285 myPreviewPrs->setFillingColor( aFillingColor, false, false );
286 myPreviewPrs->setBorderColor( aBorderColor, false, false );
291 if ( !aViewManager || !myPreviewPrs )
294 Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
295 HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
297 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
298 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
300 HYDROData_Channel::PrsDefinition aPrsDef;
301 if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef, aPanel->getEquiDistance() ) )
307 myPreviewPrs->setShape( aPrsDef.myPrs2D );
310 void HYDROGUI_ChannelOp::erasePreview()