Salome HOME
copyrights in HYDRO files are updated
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ChannelOp.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ChannelOp.h"
24
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"
32
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_Polyline3D.h>
35 #include <HYDROData_Profile.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42
43 #include <TopoDS.hxx>
44
45 HYDROGUI_ChannelOp::HYDROGUI_ChannelOp( HYDROGUI_Module* theModule,
46                                        const bool theIsEdit )
47 : HYDROGUI_Operation( theModule ),
48   myIsEdit( theIsEdit ),
49   myPreviewPrs( 0 )
50 {
51   setName( theIsEdit ? tr( "EDIT_CHANNEL" ) : tr( "CREATE_CHANNEL" ) );
52 }
53
54 HYDROGUI_ChannelOp::~HYDROGUI_ChannelOp()
55 {
56   erasePreview();
57 }
58
59 void HYDROGUI_ChannelOp::startOperation()
60 {
61   HYDROGUI_Operation::startOperation();
62
63   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
64
65   aPanel->blockSignals( true );
66
67   aPanel->reset();
68
69   myEditedObject.Nullify();
70
71   QString aSelectedGuideLine, aSelectedProfile;
72
73   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
74   if ( myIsEdit )
75   {
76     myEditedObject =
77       Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
78     if ( !myEditedObject.IsNull() )
79     {
80       anObjectName = myEditedObject->GetName();
81
82       Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
83       if ( !aRefGuideLine.IsNull() )
84         aSelectedGuideLine = aRefGuideLine->GetName();
85
86       Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
87       if ( !aRefProfile.IsNull() )
88         aSelectedProfile = aRefProfile->GetName();
89     }
90   }
91
92   // collect information about existing 3d polylines
93   QStringList aGuideLines;
94   HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
95   for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
96   {
97     Handle(HYDROData_Polyline3D) aPolyline3d = 
98       Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
99     if( !aPolyline3d.IsNull() )
100     {
101       TopoDS_Shape aShape = aPolyline3d->GetShape3D();
102       if( aShape.ShapeType()==TopAbs_WIRE )
103       {
104         TopoDS_Wire aWire = TopoDS::Wire( aShape );
105         if( !aWire.Closed() )
106           aGuideLines.append( aPolyline3d->GetName() );
107       }
108     }
109   }
110
111   // collect information about existing profiles
112   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE, true );
113
114   aPanel->setObjectName( anObjectName );
115
116   aPanel->setGuideLineNames( aGuideLines );
117   aPanel->setProfileNames( aProfiles );
118
119   aPanel->setGuideLineName( aSelectedGuideLine );
120   aPanel->setProfileName( aSelectedProfile );
121
122   aPanel->blockSignals( false );
123
124   onCreatePreview();
125 }
126
127 void HYDROGUI_ChannelOp::abortOperation()
128 {
129   erasePreview();
130   HYDROGUI_Operation::abortOperation();
131 }
132
133 void HYDROGUI_ChannelOp::commitOperation()
134 {
135   erasePreview();
136   HYDROGUI_Operation::commitOperation();
137 }
138
139 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
140 {
141   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
142   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
143   return aPanel;
144 }
145
146 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
147                                        QString& theErrorMsg,
148                                        QStringList& theBrowseObjectsEntries )
149 {
150   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
151   if ( !aPanel )
152     return false;
153
154   QString anObjectName = aPanel->getObjectName().simplified();
155   if ( anObjectName.isEmpty() )
156   {
157     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
158     return false;
159   }
160
161   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
162   {
163     // check that there are no other objects with the same name in the document
164     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
165     if( !anObject.IsNull() )
166     {
167       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
168       return false;
169     }
170   }
171
172   
173   if ( myEditedObject.IsNull() )
174     myEditedObject = createNewObject(); // Create new data model object
175
176   myEditedObject->SetName( anObjectName );
177
178   if ( !myIsEdit )
179   {
180     myEditedObject->SetFillingColor( getDefaultFillingColor() );
181     myEditedObject->SetBorderColor( getDefaultBorderColor() );
182   }
183
184   QString aGuideLineName = aPanel->getGuideLineName();
185   QString aProfileName = aPanel->getProfileName();
186   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
187   {
188     myEditedObject->RemoveGuideLine();
189     myEditedObject->RemoveProfile();
190   }
191   else
192   {
193     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
194       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
195     myEditedObject->SetGuideLine( aGuideLine );
196
197     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
198       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
199     myEditedObject->SetProfile( aProfile );
200   }
201
202   if ( myEditedObject->IsMustBeUpdated() )
203     myEditedObject->Update();
204
205   erasePreview();
206
207   if( !myIsEdit )
208   {
209     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
210     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
211     theBrowseObjectsEntries.append( anEntry );
212   }
213
214   module()->setIsToUpdate( myEditedObject );
215
216   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
217
218   return true;
219 }
220
221 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
222 {
223   return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
224 }
225
226 QColor HYDROGUI_ChannelOp::getDefaultFillingColor() const
227 {
228   return HYDROData_Channel::DefaultFillingColor();
229 }
230
231 QColor HYDROGUI_ChannelOp::getDefaultBorderColor() const
232 {
233   return HYDROData_Channel::DefaultBorderColor();
234 }
235
236 void HYDROGUI_ChannelOp::onCreatePreview()
237 {
238   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
239   if ( !aPanel )
240     return;
241
242   QString aGuideLineName = aPanel->getGuideLineName();
243   QString aProfileName = aPanel->getProfileName();
244   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
245   {
246     erasePreview();
247     return;
248   }
249
250   LightApp_Application* anApp = module()->getApp();
251   
252   if ( !getPreviewManager() )
253   {
254     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
255                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
256   }
257
258   OCCViewer_ViewManager* aViewManager = getPreviewManager();
259   if ( aViewManager && !myPreviewPrs )
260   {
261     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
262     {
263       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
264       if ( !aCtx.IsNull() )
265       {
266         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
267
268         QColor aFillingColor = getDefaultFillingColor();
269         QColor aBorderColor = getDefaultBorderColor();
270         if ( !myEditedObject.IsNull() )
271         {
272           aFillingColor = myEditedObject->GetFillingColor();
273           aBorderColor = myEditedObject->GetBorderColor();
274         }
275
276         myPreviewPrs->setFillingColor( aFillingColor, false, false );
277         myPreviewPrs->setBorderColor( aBorderColor, false, false );
278       }
279     }
280   }
281
282   if ( !aViewManager || !myPreviewPrs )
283     return;
284
285   Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
286     HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
287
288   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
289     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
290
291   HYDROData_Channel::PrsDefinition aPrsDef;
292   if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
293   {
294     erasePreview();
295     return;
296   }
297
298   myPreviewPrs->setShape( aPrsDef.myPrs2D );
299 }
300
301 void HYDROGUI_ChannelOp::erasePreview()
302 {
303   if( myPreviewPrs )
304   {
305     delete myPreviewPrs;
306     myPreviewPrs = 0;
307   }
308 }