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