Salome HOME
Get rid of Qt classes in the data model.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ChannelOp.cxx
1 // Copyright (C) 2007-2013  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.
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_ChannelDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Shape.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_Polyline3D.h>
34 #include <HYDROData_Profile.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 #include <TopoDS.hxx>
43
44 HYDROGUI_ChannelOp::HYDROGUI_ChannelOp( HYDROGUI_Module* theModule,
45                                        const bool theIsEdit )
46 : HYDROGUI_Operation( theModule ),
47   myIsEdit( theIsEdit ),
48   myPreviewPrs( 0 )
49 {
50   setName( theIsEdit ? tr( "EDIT_CHANNEL" ) : tr( "CREATE_CHANNEL" ) );
51 }
52
53 HYDROGUI_ChannelOp::~HYDROGUI_ChannelOp()
54 {
55   erasePreview();
56 }
57
58 void HYDROGUI_ChannelOp::startOperation()
59 {
60   HYDROGUI_Operation::startOperation();
61
62   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
63
64   aPanel->blockSignals( true );
65
66   aPanel->reset();
67
68   myEditedObject.Nullify();
69
70   QString aSelectedGuideLine, aSelectedProfile;
71
72   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
73   if ( myIsEdit )
74   {
75     myEditedObject =
76       Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
77     if ( !myEditedObject.IsNull() )
78     {
79       anObjectName = myEditedObject->GetName();
80
81       Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
82       if ( !aRefGuideLine.IsNull() )
83         aSelectedGuideLine = aRefGuideLine->GetName();
84
85       Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
86       if ( !aRefProfile.IsNull() )
87         aSelectedProfile = aRefProfile->GetName();
88     }
89   }
90
91   // collect information about existing 3d polylines
92   QStringList aGuideLines;
93   HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
94   for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
95   {
96     Handle(HYDROData_Polyline3D) aPolyline3d = 
97       Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
98     if( !aPolyline3d.IsNull() )
99     {
100       TopoDS_Shape aShape = aPolyline3d->GetShape3D();
101       if( aShape.ShapeType()==TopAbs_WIRE )
102       {
103         TopoDS_Wire aWire = TopoDS::Wire( aShape );
104         if( !aWire.Closed() )
105           aGuideLines.append( aPolyline3d->GetName() );
106       }
107     }
108   }
109
110   // collect information about existing profiles
111   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE );
112
113   aPanel->setObjectName( anObjectName );
114
115   aPanel->setGuideLineNames( aGuideLines );
116   aPanel->setProfileNames( aProfiles );
117
118   aPanel->setGuideLineName( aSelectedGuideLine );
119   aPanel->setProfileName( aSelectedProfile );
120
121   aPanel->blockSignals( false );
122
123   onCreatePreview();
124 }
125
126 void HYDROGUI_ChannelOp::abortOperation()
127 {
128   erasePreview();
129   HYDROGUI_Operation::abortOperation();
130 }
131
132 void HYDROGUI_ChannelOp::commitOperation()
133 {
134   erasePreview();
135   HYDROGUI_Operation::commitOperation();
136 }
137
138 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
139 {
140   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
141   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
142   return aPanel;
143 }
144
145 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
146                                        QString& theErrorMsg )
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     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
207
208   module()->setIsToUpdate( myEditedObject );
209
210   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
211
212   return true;
213 }
214
215 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
216 {
217   return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
218 }
219
220 QColor HYDROGUI_ChannelOp::getDefaultFillingColor() const
221 {
222   return HYDROData_Channel::DefaultFillingColor();
223 }
224
225 QColor HYDROGUI_ChannelOp::getDefaultBorderColor() const
226 {
227   return HYDROData_Channel::DefaultBorderColor();
228 }
229
230 void HYDROGUI_ChannelOp::onCreatePreview()
231 {
232   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
233   if ( !aPanel )
234     return;
235
236   QString aGuideLineName = aPanel->getGuideLineName();
237   QString aProfileName = aPanel->getProfileName();
238   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
239   {
240     erasePreview();
241     return;
242   }
243
244   LightApp_Application* anApp = module()->getApp();
245   
246   if ( !getPreviewManager() )
247   {
248     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
249                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
250   }
251
252   OCCViewer_ViewManager* aViewManager = getPreviewManager();
253   if ( aViewManager && !myPreviewPrs )
254   {
255     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
256     {
257       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
258       if ( !aCtx.IsNull() )
259       {
260         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
261
262         QColor aFillingColor = getDefaultFillingColor();
263         QColor aBorderColor = getDefaultBorderColor();
264         if ( !myEditedObject.IsNull() )
265         {
266           aFillingColor = myEditedObject->GetFillingColor();
267           aBorderColor = myEditedObject->GetBorderColor();
268         }
269
270         myPreviewPrs->setFillingColor( aFillingColor, false, false );
271         myPreviewPrs->setBorderColor( aBorderColor, false, false );
272       }
273     }
274   }
275
276   if ( !aViewManager || !myPreviewPrs )
277     return;
278
279   Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
280     HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
281
282   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
283     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
284
285   HYDROData_Channel::PrsDefinition aPrsDef;
286   if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
287   {
288     erasePreview();
289     return;
290   }
291
292   myPreviewPrs->setShape( aPrsDef.myPrs2D );
293 }
294
295 void HYDROGUI_ChannelOp::erasePreview()
296 {
297   if( myPreviewPrs )
298   {
299     delete myPreviewPrs;
300     myPreviewPrs = 0;
301   }
302 }