Salome HOME
Merge branch 'BR_IMPROVEMENTS' of ssh://git.salome-platform.org/modules/hydro into...
[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   if ( isApplyAndClose() )
70     myEditedObject.Nullify();
71
72   QString aSelectedGuideLine, aSelectedProfile;
73
74   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
75   if ( myIsEdit )
76   {
77     if ( isApplyAndClose() )
78       myEditedObject =
79         Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
80     if ( !myEditedObject.IsNull() )
81     {
82       anObjectName = myEditedObject->GetName();
83
84       Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
85       if ( !aRefGuideLine.IsNull() )
86         aSelectedGuideLine = aRefGuideLine->GetName();
87
88       Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
89       if ( !aRefProfile.IsNull() )
90         aSelectedProfile = aRefProfile->GetName();
91     }
92   }
93
94   // collect information about existing 3d polylines
95   QStringList aGuideLines;
96   HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
97   for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
98   {
99     Handle(HYDROData_Polyline3D) aPolyline3d = 
100       Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
101     if( !aPolyline3d.IsNull() )
102     {
103       TopoDS_Shape aShape = aPolyline3d->GetShape3D();
104       if( aShape.ShapeType()==TopAbs_WIRE )
105       {
106         TopoDS_Wire aWire = TopoDS::Wire( aShape );
107         if( !aWire.Closed() )
108           aGuideLines.append( aPolyline3d->GetName() );
109       }
110     }
111   }
112
113   // collect information about existing profiles
114   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE, true );
115
116   aPanel->setObjectName( anObjectName );
117
118   aPanel->setGuideLineNames( aGuideLines );
119   aPanel->setProfileNames( aProfiles );
120
121   aPanel->setGuideLineName( aSelectedGuideLine );
122   aPanel->setProfileName( aSelectedProfile );
123
124   aPanel->blockSignals( false );
125
126   onCreatePreview();
127 }
128
129 void HYDROGUI_ChannelOp::abortOperation()
130 {
131   erasePreview();
132   HYDROGUI_Operation::abortOperation();
133 }
134
135 void HYDROGUI_ChannelOp::commitOperation()
136 {
137   erasePreview();
138   HYDROGUI_Operation::commitOperation();
139 }
140
141 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
142 {
143   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
144   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
145   return aPanel;
146 }
147
148 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
149                                        QString& theErrorMsg,
150                                        QStringList& theBrowseObjectsEntries )
151 {
152   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
153   if ( !aPanel )
154     return false;
155
156   QString anObjectName = aPanel->getObjectName().simplified();
157   if ( anObjectName.isEmpty() )
158   {
159     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
160     return false;
161   }
162
163   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
164   {
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() )
168     {
169       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
170       return false;
171     }
172   }
173
174   
175   if ( myEditedObject.IsNull() )
176     myEditedObject = createNewObject(); // Create new data model object
177
178   myEditedObject->SetName( anObjectName );
179
180   if ( !myIsEdit )
181   {
182     myEditedObject->SetFillingColor( getDefaultFillingColor() );
183     myEditedObject->SetBorderColor( getDefaultBorderColor() );
184   }
185
186   QString aGuideLineName = aPanel->getGuideLineName();
187   QString aProfileName = aPanel->getProfileName();
188   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
189   {
190     myEditedObject->RemoveGuideLine();
191     myEditedObject->RemoveProfile();
192   }
193   else
194   {
195     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
196       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
197     myEditedObject->SetGuideLine( aGuideLine );
198
199     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
200       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
201     myEditedObject->SetProfile( aProfile );
202   }
203
204   if ( myEditedObject->IsMustBeUpdated() )
205     myEditedObject->Update();
206
207   erasePreview();
208
209   if( !myIsEdit )
210   {
211     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
212     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
213     theBrowseObjectsEntries.append( anEntry );
214   }
215
216   module()->setIsToUpdate( myEditedObject );
217
218   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
219
220   return true;
221 }
222
223 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
224 {
225   return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
226 }
227
228 QColor HYDROGUI_ChannelOp::getDefaultFillingColor() const
229 {
230   return HYDROData_Channel::DefaultFillingColor();
231 }
232
233 QColor HYDROGUI_ChannelOp::getDefaultBorderColor() const
234 {
235   return HYDROData_Channel::DefaultBorderColor();
236 }
237
238 void HYDROGUI_ChannelOp::onCreatePreview()
239 {
240   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
241   if ( !aPanel )
242     return;
243
244   QString aGuideLineName = aPanel->getGuideLineName();
245   QString aProfileName = aPanel->getProfileName();
246   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
247   {
248     erasePreview();
249     return;
250   }
251
252   LightApp_Application* anApp = module()->getApp();
253   
254   if ( !getPreviewManager() )
255   {
256     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
257                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
258   }
259
260   OCCViewer_ViewManager* aViewManager = getPreviewManager();
261   if ( aViewManager && !myPreviewPrs )
262   {
263     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
264     {
265       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
266       if ( !aCtx.IsNull() )
267       {
268         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
269
270         QColor aFillingColor = getDefaultFillingColor();
271         QColor aBorderColor = getDefaultBorderColor();
272         if ( !myEditedObject.IsNull() )
273         {
274           aFillingColor = myEditedObject->GetFillingColor();
275           aBorderColor = myEditedObject->GetBorderColor();
276         }
277
278         myPreviewPrs->setFillingColor( aFillingColor, false, false );
279         myPreviewPrs->setBorderColor( aBorderColor, false, false );
280       }
281     }
282   }
283
284   if ( !aViewManager || !myPreviewPrs )
285     return;
286
287   Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
288     HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
289
290   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
291     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
292
293   HYDROData_Channel::PrsDefinition aPrsDef;
294   if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
295   {
296     erasePreview();
297     return;
298   }
299
300   myPreviewPrs->setShape( aPrsDef.myPrs2D );
301 }
302
303 void HYDROGUI_ChannelOp::erasePreview()
304 {
305   if( myPreviewPrs )
306   {
307     delete myPreviewPrs;
308     myPreviewPrs = 0;
309   }
310 }