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