Salome HOME
Creat\Edit stream operation.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.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_StreamOp.h"
24
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_Shape.h"
27 #include "HYDROGUI_StreamDlg.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <HYDROData_Document.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Profile.h>
34
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
38
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
42
43 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
44 : HYDROGUI_Operation( theModule ), 
45   myIsEdit( theIsEdit ),
46   myViewManager( NULL ),
47   myPreviewPrs( NULL )
48 {
49   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
50 }
51
52 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
53 {
54   erasePreview();
55 }
56
57 void HYDROGUI_StreamOp::startOperation()
58 {
59   HYDROGUI_Operation::startOperation();
60
61   // We start operation in the document
62   startDocOperation();
63
64   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
65
66   aPanel->blockSignals( true );
67
68   aPanel->reset();
69
70   if( myIsEdit )
71     myEditedObject = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
72   else 
73     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
74
75   QString aSelectedAxis;
76   QStringList aSelectedProfiles;
77
78   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
79   if ( myIsEdit && !myEditedObject.IsNull() )
80   {
81     anObjectName = myEditedObject->GetName();
82
83     Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
84     if ( !aHydraulicAxis.IsNull() )
85       aSelectedAxis = aHydraulicAxis->GetName();
86
87     HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
88     for ( int i = 1, n = aProfiles.Length(); i <= n; ++i )
89     {
90       Handle(HYDROData_Profile) aProfile = 
91         Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
92       if ( aProfile.IsNull() )
93         continue;
94
95       QString aProfileName = aProfile->GetName();
96       aSelectedProfiles << aProfileName;
97     }
98   }
99
100   QStringList anAxises = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY );
101
102   // Temporary, to be removed
103   if ( aSelectedProfiles.isEmpty() )
104   {
105     aSelectedProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE );
106   }
107
108   aPanel->setObjectName( anObjectName );
109   aPanel->setAxisNames( anAxises );
110   aPanel->setAxisName( aSelectedAxis );
111   aPanel->setSelectedProfiles( aSelectedProfiles );
112
113   aPanel->blockSignals( false );
114
115   onCreatePreview();
116 }
117
118 void HYDROGUI_StreamOp::abortOperation()
119 {
120   erasePreview();
121   abortDocOperation();
122
123   HYDROGUI_Operation::abortOperation();
124 }
125
126 void HYDROGUI_StreamOp::commitOperation()
127 {
128   erasePreview();
129
130   HYDROGUI_Operation::commitOperation();
131 }
132
133 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
134 {
135   HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
136   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
137   return aPanel;
138 }
139
140 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
141                                       QString& theErrorMsg )
142 {
143   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
144   if ( !aPanel )
145     return false;
146
147   QString anObjectName = aPanel->getObjectName().simplified();
148   if ( anObjectName.isEmpty() )
149   {
150     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
151     return false;
152   }
153
154   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
155   {
156     // check that there are no other objects with the same name in the document
157     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
158     if( !anObject.IsNull() )
159     {
160       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
161       return false;
162     }
163   }
164
165   if ( myEditedObject.IsNull() )
166     return false;
167
168   myEditedObject->SetName( anObjectName );
169
170   erasePreview();
171
172   if( !myIsEdit )
173     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
174
175   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
176
177   return true;
178 }
179
180 void HYDROGUI_StreamOp::onCreatePreview()
181 {
182   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
183   if ( !aPanel )
184     return;
185
186   QString anAxisName = aPanel->getAxisName();
187   QStringList aProfiles = aPanel->getSelectedProfiles();
188   if ( anAxisName.isEmpty() || aProfiles.isEmpty() )
189   {
190     erasePreview();
191     return;
192   }
193
194   if ( myEditedObject.IsNull() )
195     return;
196
197   // At first we remove all references from stream
198   myEditedObject->RemoveProfiles();
199
200   Handle(HYDROData_PolylineXY) anAxis = Handle(HYDROData_PolylineXY)::DownCast(
201     HYDROGUI_Tool::FindObjectByName( module(), anAxisName, KIND_POLYLINEXY ) );
202   myEditedObject->SetHydraulicAxis( anAxis );
203
204   HYDROData_SequenceOfObjects aProfileObjects =
205     HYDROGUI_Tool::FindObjectsByNames( module(), aProfiles, KIND_PROFILE );
206   for ( int i = 1, n = aProfileObjects.Length(); i <= n; ++i )
207   {
208     Handle(HYDROData_Profile) aProfile = 
209       Handle(HYDROData_Profile)::DownCast( aProfileObjects.Value( i ) );
210     if ( aProfile.IsNull() )
211       continue;
212
213     myEditedObject->AddProfile( aProfile );
214   }
215   
216   myEditedObject->Update();
217
218
219   LightApp_Application* anApp = module()->getApp();
220   if ( !myViewManager )
221     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
222       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
223
224   if ( myViewManager && !myPreviewPrs )
225   {
226     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
227     {
228       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
229       if ( !aCtx.IsNull() )
230         myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject );
231     }
232   }
233
234   if ( !myViewManager || !myPreviewPrs )
235     return;
236
237   myPreviewPrs->update();
238 }
239
240 void HYDROGUI_StreamOp::erasePreview()
241 {
242   if( myPreviewPrs )
243   {
244     delete myPreviewPrs;
245     myPreviewPrs = 0;
246   }
247 }