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 aPanel->blockSignals( false );
126 void HYDROGUI_ChannelOp::abortOperation()
129 HYDROGUI_Operation::abortOperation();
132 void HYDROGUI_ChannelOp::commitOperation()
135 HYDROGUI_Operation::commitOperation();
138 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
140 HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
141 connect( aPanel, SIGNAL( CreatePreview() ), this, SLOT( onCreatePreview() ) );
145 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
146 QString& theErrorMsg,
147 QStringList& theBrowseObjectsEntries )
149 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
153 QString anObjectName = aPanel->getObjectName().simplified();
154 if ( anObjectName.isEmpty() )
156 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
160 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
162 // check that there are no other objects with the same name in the document
163 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
164 if( !anObject.IsNull() )
166 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
172 if ( myEditedObject.IsNull() )
173 myEditedObject = createNewObject(); // Create new data model object
175 myEditedObject->SetName( anObjectName );
179 myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
180 myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
183 QString aGuideLineName = aPanel->getGuideLineName();
184 QString aProfileName = aPanel->getProfileName();
185 /*if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
187 myEditedObject->RemoveGuideLine();
188 myEditedObject->RemoveProfile();
192 Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
193 HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
194 if ( aGuideLine.IsNull() )
196 theErrorMsg = tr( "GUIDE_LINE_IS_NOT_SELECTED" );
200 myEditedObject->RemoveGuideLine();
201 myEditedObject->SetGuideLine( aGuideLine );
203 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
204 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
205 if ( aProfile.IsNull() )
207 theErrorMsg = tr( "PROFILE_IS_NOT_SELECTED" );
211 myEditedObject->RemoveProfile();
212 myEditedObject->SetProfile( aProfile );
215 if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
216 myEditedObject->Update();
222 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
223 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
224 theBrowseObjectsEntries.append( anEntry );
227 module()->setIsToUpdate( myEditedObject );
229 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
234 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
236 return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
239 void HYDROGUI_ChannelOp::onCreatePreview()
241 HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
245 QString aGuideLineName = aPanel->getGuideLineName();
246 QString aProfileName = aPanel->getProfileName();
247 if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
253 LightApp_Application* anApp = module()->getApp();
255 if ( !getPreviewManager() )
257 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
258 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
261 OCCViewer_ViewManager* aViewManager = getPreviewManager();
262 if ( aViewManager && !myPreviewPrs )
264 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
266 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
267 if ( !aCtx.IsNull() )
269 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
271 QColor aFillingColor = Qt::blue;
272 QColor aBorderColor = Qt::transparent;
273 if ( !myEditedObject.IsNull() )
275 aFillingColor = myEditedObject->GetFillingColor();
276 aBorderColor = myEditedObject->GetBorderColor();
279 myPreviewPrs->setFillingColor( aFillingColor, false, false );
280 myPreviewPrs->setBorderColor( aBorderColor, false, false );
285 if ( !aViewManager || !myPreviewPrs )
288 Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
289 HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
291 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
292 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
294 HYDROData_Channel::PrsDefinition aPrsDef;
295 if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
301 myPreviewPrs->setShape( aPrsDef.myPrs2D );
304 void HYDROGUI_ChannelOp::erasePreview()