Salome HOME
refs #568: Land Cover: a draft of data model and implementation of dialog box and...
[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     if ( aGuideLine.IsNull() )
194     {
195       theErrorMsg = tr( "GUIDE_LINE_IS_NOT_SELECTED" );
196       return false;
197     }
198
199     myEditedObject->RemoveGuideLine();
200     myEditedObject->SetGuideLine( aGuideLine );
201
202     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
203       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
204     if ( aProfile.IsNull() )
205     {
206       theErrorMsg = tr( "PROFILE_IS_NOT_SELECTED" );
207       return false;
208     }
209
210     myEditedObject->RemoveProfile();
211     myEditedObject->SetProfile( aProfile );
212   }
213
214   if ( myEditedObject->IsMustBeUpdated() )
215     myEditedObject->Update();
216
217   erasePreview();
218
219   if( !myIsEdit )
220   {
221     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
222     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
223     theBrowseObjectsEntries.append( anEntry );
224   }
225
226   module()->setIsToUpdate( myEditedObject );
227
228   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
229
230   return true;
231 }
232
233 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
234 {
235   return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
236 }
237
238 QColor HYDROGUI_ChannelOp::getDefaultFillingColor() const
239 {
240   return HYDROData_Channel::DefaultFillingColor();
241 }
242
243 QColor HYDROGUI_ChannelOp::getDefaultBorderColor() const
244 {
245   return HYDROData_Channel::DefaultBorderColor();
246 }
247
248 void HYDROGUI_ChannelOp::onCreatePreview()
249 {
250   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
251   if ( !aPanel )
252     return;
253
254   QString aGuideLineName = aPanel->getGuideLineName();
255   QString aProfileName = aPanel->getProfileName();
256   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
257   {
258     erasePreview();
259     return;
260   }
261
262   LightApp_Application* anApp = module()->getApp();
263   
264   if ( !getPreviewManager() )
265   {
266     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
267                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
268   }
269
270   OCCViewer_ViewManager* aViewManager = getPreviewManager();
271   if ( aViewManager && !myPreviewPrs )
272   {
273     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
274     {
275       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
276       if ( !aCtx.IsNull() )
277       {
278         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
279
280         QColor aFillingColor = getDefaultFillingColor();
281         QColor aBorderColor = getDefaultBorderColor();
282         if ( !myEditedObject.IsNull() )
283         {
284           aFillingColor = myEditedObject->GetFillingColor();
285           aBorderColor = myEditedObject->GetBorderColor();
286         }
287
288         myPreviewPrs->setFillingColor( aFillingColor, false, false );
289         myPreviewPrs->setBorderColor( aBorderColor, false, false );
290       }
291     }
292   }
293
294   if ( !aViewManager || !myPreviewPrs )
295     return;
296
297   Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
298     HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
299
300   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
301     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
302
303   HYDROData_Channel::PrsDefinition aPrsDef;
304   if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
305   {
306     erasePreview();
307     return;
308   }
309
310   myPreviewPrs->setShape( aPrsDef.myPrs2D );
311 }
312
313 void HYDROGUI_ChannelOp::erasePreview()
314 {
315   if( myPreviewPrs )
316   {
317     delete myPreviewPrs;
318     myPreviewPrs = 0;
319   }
320 }