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_UpdateFlags.h"
29 #include <HYDROData_Iterator.h>
30 #include <HYDROData_Polyline3D.h>
31 #include <HYDROData_Profile.h>
33 #include <OCCViewer_ViewManager.h>
34 #include <OCCViewer_ViewModel.h>
36 #include <LightApp_Application.h>
37 #include <LightApp_UpdateFlags.h>
41 HYDROGUI_ChannelOp::HYDROGUI_ChannelOp( HYDROGUI_Module* theModule,
42 const bool theIsEdit )
43 : HYDROGUI_Operation( theModule ),
44 myIsEdit( theIsEdit ),
47 setName( theIsEdit ? tr( "EDIT_CHANNEL" ) : tr( "CREATE_CHANNEL" ) );
50 HYDROGUI_ChannelOp::~HYDROGUI_ChannelOp()
55 void HYDROGUI_ChannelOp::startOperation()
57 HYDROGUI_Operation::startOperation();
59 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
61 aPanel->blockSignals( true );
65 myEditedObject.Nullify();
67 QString aSelectedGuideLine, aSelectedProfile;
69 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
73 Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
74 if ( !myEditedObject.IsNull() )
76 anObjectName = myEditedObject->GetName();
78 Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
79 if ( !aRefGuideLine.IsNull() )
80 aSelectedGuideLine = aRefGuideLine->GetName();
82 Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
83 if ( !aRefProfile.IsNull() )
84 aSelectedProfile = aRefProfile->GetName();
88 // collect information about existing 3d polylines
89 QStringList aGuideLines;
90 HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
91 for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
93 Handle(HYDROData_Polyline3D) aPolyline3d =
94 Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
95 if( !aPolyline3d.IsNull() )
97 TopoDS_Shape aShape = aPolyline3d->GetShape3D();
98 if( aShape.ShapeType()==TopAbs_WIRE )
100 TopoDS_Wire aWire = TopoDS::Wire( aShape );
101 if( !aWire.Closed() )
102 aGuideLines.append( aPolyline3d->GetName() );
107 // collect information about existing profiles
108 QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE, false );
110 aPanel->setObjectName( anObjectName );
112 aPanel->setGuideLineNames( aGuideLines );
113 aPanel->setProfileNames( aProfiles );
115 aPanel->setGuideLineName( aSelectedGuideLine );
116 aPanel->setProfileName( aSelectedProfile );
118 aPanel->blockSignals( false );
123 void HYDROGUI_ChannelOp::abortOperation()
126 HYDROGUI_Operation::abortOperation();
129 void HYDROGUI_ChannelOp::commitOperation()
132 HYDROGUI_Operation::commitOperation();
135 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
137 HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
138 connect( aPanel, SIGNAL( CreatePreview() ), this, SLOT( onCreatePreview() ) );
142 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
143 QString& theErrorMsg,
144 QStringList& theBrowseObjectsEntries )
146 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
150 QString anObjectName = aPanel->getObjectName().simplified();
151 if ( anObjectName.isEmpty() )
153 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
157 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
159 // check that there are no other objects with the same name in the document
160 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
161 if( !anObject.IsNull() )
163 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
169 if ( myEditedObject.IsNull() )
170 myEditedObject = createNewObject(); // Create new data model object
172 myEditedObject->SetName( anObjectName );
176 myEditedObject->SetFillingColor( getDefaultFillingColor() );
177 myEditedObject->SetBorderColor( getDefaultBorderColor() );
180 QString aGuideLineName = aPanel->getGuideLineName();
181 QString aProfileName = aPanel->getProfileName();
182 if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
184 myEditedObject->RemoveGuideLine();
185 myEditedObject->RemoveProfile();
189 Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
190 HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
191 myEditedObject->SetGuideLine( aGuideLine );
193 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
194 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
195 myEditedObject->SetProfile( aProfile );
198 if ( myEditedObject->IsMustBeUpdated() )
199 myEditedObject->Update();
205 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
206 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
207 theBrowseObjectsEntries.append( anEntry );
210 module()->setIsToUpdate( myEditedObject );
212 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
217 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
219 return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
222 QColor HYDROGUI_ChannelOp::getDefaultFillingColor() const
224 return HYDROData_Channel::DefaultFillingColor();
227 QColor HYDROGUI_ChannelOp::getDefaultBorderColor() const
229 return HYDROData_Channel::DefaultBorderColor();
232 void HYDROGUI_ChannelOp::onCreatePreview()
234 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
238 QString aGuideLineName = aPanel->getGuideLineName();
239 QString aProfileName = aPanel->getProfileName();
240 if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
246 LightApp_Application* anApp = module()->getApp();
248 if ( !getPreviewManager() )
250 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
251 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
254 OCCViewer_ViewManager* aViewManager = getPreviewManager();
255 if ( aViewManager && !myPreviewPrs )
257 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
259 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
260 if ( !aCtx.IsNull() )
262 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
264 QColor aFillingColor = getDefaultFillingColor();
265 QColor aBorderColor = getDefaultBorderColor();
266 if ( !myEditedObject.IsNull() )
268 aFillingColor = myEditedObject->GetFillingColor();
269 aBorderColor = myEditedObject->GetBorderColor();
272 myPreviewPrs->setFillingColor( aFillingColor, false, false );
273 myPreviewPrs->setBorderColor( aBorderColor, false, false );
278 if ( !aViewManager || !myPreviewPrs )
281 Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
282 HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
284 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
285 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
287 HYDROData_Channel::PrsDefinition aPrsDef;
288 if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
294 myPreviewPrs->setShape( aPrsDef.myPrs2D );
297 void HYDROGUI_ChannelOp::erasePreview()