1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ChannelOp.h"
25 #include "HYDROGUI_DataModel.h"
26 #include <HYDROGUI_DataObject.h>
27 #include "HYDROGUI_ChannelDlg.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Shape.h"
30 #include "HYDROGUI_Tool.h"
31 #include "HYDROGUI_UpdateFlags.h"
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_Polyline3D.h>
35 #include <HYDROData_Profile.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
45 HYDROGUI_ChannelOp::HYDROGUI_ChannelOp( HYDROGUI_Module* theModule,
46 const bool theIsEdit )
47 : HYDROGUI_Operation( theModule ),
48 myIsEdit( theIsEdit ),
51 setName( theIsEdit ? tr( "EDIT_CHANNEL" ) : tr( "CREATE_CHANNEL" ) );
54 HYDROGUI_ChannelOp::~HYDROGUI_ChannelOp()
59 void HYDROGUI_ChannelOp::startOperation()
61 HYDROGUI_Operation::startOperation();
63 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
65 aPanel->blockSignals( true );
69 if ( isApplyAndClose() )
70 myEditedObject.Nullify();
72 QString aSelectedGuideLine, aSelectedProfile;
74 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
77 if ( isApplyAndClose() )
79 Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
80 if ( !myEditedObject.IsNull() )
82 anObjectName = myEditedObject->GetName();
84 Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
85 if ( !aRefGuideLine.IsNull() )
86 aSelectedGuideLine = aRefGuideLine->GetName();
88 Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
89 if ( !aRefProfile.IsNull() )
90 aSelectedProfile = aRefProfile->GetName();
94 // collect information about existing 3d polylines
95 QStringList aGuideLines;
96 HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
97 for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
99 Handle(HYDROData_Polyline3D) aPolyline3d =
100 Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
101 if( !aPolyline3d.IsNull() )
103 TopoDS_Shape aShape = aPolyline3d->GetShape3D();
104 if( aShape.ShapeType()==TopAbs_WIRE )
106 TopoDS_Wire aWire = TopoDS::Wire( aShape );
107 if( !aWire.Closed() )
108 aGuideLines.append( aPolyline3d->GetName() );
113 // collect information about existing profiles
114 QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE, true );
116 aPanel->setObjectName( anObjectName );
118 aPanel->setGuideLineNames( aGuideLines );
119 aPanel->setProfileNames( aProfiles );
121 aPanel->setGuideLineName( aSelectedGuideLine );
122 aPanel->setProfileName( aSelectedProfile );
124 aPanel->blockSignals( false );
129 void HYDROGUI_ChannelOp::abortOperation()
132 HYDROGUI_Operation::abortOperation();
135 void HYDROGUI_ChannelOp::commitOperation()
138 HYDROGUI_Operation::commitOperation();
141 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
143 HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
144 connect( aPanel, SIGNAL( CreatePreview() ), this, SLOT( onCreatePreview() ) );
148 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
149 QString& theErrorMsg,
150 QStringList& theBrowseObjectsEntries )
152 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
156 QString anObjectName = aPanel->getObjectName().simplified();
157 if ( anObjectName.isEmpty() )
159 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
163 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
165 // check that there are no other objects with the same name in the document
166 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
167 if( !anObject.IsNull() )
169 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
175 if ( myEditedObject.IsNull() )
176 myEditedObject = createNewObject(); // Create new data model object
178 myEditedObject->SetName( anObjectName );
182 myEditedObject->SetFillingColor( getDefaultFillingColor() );
183 myEditedObject->SetBorderColor( getDefaultBorderColor() );
186 QString aGuideLineName = aPanel->getGuideLineName();
187 QString aProfileName = aPanel->getProfileName();
188 if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
190 myEditedObject->RemoveGuideLine();
191 myEditedObject->RemoveProfile();
195 Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
196 HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
197 myEditedObject->SetGuideLine( aGuideLine );
199 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
200 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
201 myEditedObject->SetProfile( aProfile );
204 if ( myEditedObject->IsMustBeUpdated() )
205 myEditedObject->Update();
211 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
212 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
213 theBrowseObjectsEntries.append( anEntry );
216 module()->setIsToUpdate( myEditedObject );
218 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
223 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
225 return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
228 QColor HYDROGUI_ChannelOp::getDefaultFillingColor() const
230 return HYDROData_Channel::DefaultFillingColor();
233 QColor HYDROGUI_ChannelOp::getDefaultBorderColor() const
235 return HYDROData_Channel::DefaultBorderColor();
238 void HYDROGUI_ChannelOp::onCreatePreview()
240 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
244 QString aGuideLineName = aPanel->getGuideLineName();
245 QString aProfileName = aPanel->getProfileName();
246 if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
252 LightApp_Application* anApp = module()->getApp();
254 if ( !getPreviewManager() )
256 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
257 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
260 OCCViewer_ViewManager* aViewManager = getPreviewManager();
261 if ( aViewManager && !myPreviewPrs )
263 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
265 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
266 if ( !aCtx.IsNull() )
268 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
270 QColor aFillingColor = getDefaultFillingColor();
271 QColor aBorderColor = getDefaultBorderColor();
272 if ( !myEditedObject.IsNull() )
274 aFillingColor = myEditedObject->GetFillingColor();
275 aBorderColor = myEditedObject->GetBorderColor();
278 myPreviewPrs->setFillingColor( aFillingColor, false, false );
279 myPreviewPrs->setBorderColor( aBorderColor, false, false );
284 if ( !aViewManager || !myPreviewPrs )
287 Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
288 HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
290 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
291 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
293 HYDROData_Channel::PrsDefinition aPrsDef;
294 if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
300 myPreviewPrs->setShape( aPrsDef.myPrs2D );
303 void HYDROGUI_ChannelOp::erasePreview()