Salome HOME
Calculation case and it child objects methods has been revised.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.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_CalculationOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_CalculationDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <HYDROData_Polyline.h>
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_ImmersibleZone.h>
34
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewModel.h>
37 #include <OCCViewer_ViewWindow.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 #include <QApplication>
43
44 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
45 : HYDROGUI_Operation( theModule ),
46   myIsEdit( theIsEdit ),
47   myActiveViewManager( NULL ),
48   myPreviewViewManager( NULL )
49 {
50   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
51 }
52
53 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
54 {
55   closePreview();
56 }
57
58 void HYDROGUI_CalculationOp::startOperation()
59 {
60   HYDROGUI_Operation::startOperation();
61
62   HYDROGUI_CalculationDlg* aPanel = 
63     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
64   if ( !aPanel )
65     return;
66
67   myRegionsList.clear();
68   aPanel->reset();
69
70   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
71
72   QStringList aSelectedObjects;
73
74   myEditedObject.Nullify();
75   if ( myIsEdit )
76   {
77     myEditedObject = Handle(HYDROData_Calculation)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
78     if ( !myEditedObject.IsNull() )
79     {
80       anObjectName = myEditedObject->GetName();
81
82       HYDROData_SequenceOfObjects aRefObjects = myEditedObject->GetGeometryObjects();
83       HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
84       for ( ; anIter.More(); anIter.Next() )
85       {
86         Handle(HYDROData_Object) aRefbject = 
87           Handle(HYDROData_Object)::DownCast( anIter.Value() );
88         if ( aRefbject.IsNull() )
89           continue;
90
91         QString aRefObjectName = aRefbject->GetName();
92         if ( aRefObjectName.isEmpty() )
93           continue;
94
95         aSelectedObjects.append( aRefObjectName );
96       }
97     }
98   }
99
100   aPanel->setObjectName( anObjectName );
101   aPanel->setSelectedGeomObjects( aSelectedObjects );
102
103   createPreview();
104 }
105
106 void HYDROGUI_CalculationOp::abortOperation()
107 {
108   closePreview();
109
110   HYDROGUI_Operation::abortOperation();
111 }
112
113 void HYDROGUI_CalculationOp::commitOperation()
114 {
115   closePreview();
116
117   HYDROGUI_Operation::commitOperation();
118 }
119
120 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
121 {
122   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
123
124   // Connect signals and slots
125
126   return aPanel;
127 }
128
129 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
130                                            QString& theErrorMsg )
131 {
132   HYDROGUI_CalculationDlg* aPanel = 
133     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
134   if ( !aPanel )
135     return false;
136
137   QString anObjectName = aPanel->getObjectName().simplified();
138   if ( anObjectName.isEmpty() )
139   {
140     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
141     return false;
142   }
143
144   // check that there are no other objects with the same name in the document
145   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
146   {
147     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
148     if ( !anObject.IsNull() )
149     {
150       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
151       return false;
152     }
153   }
154
155   Handle(HYDROData_Document) aDocument = doc();
156
157   Handle(HYDROData_Calculation) aCalculObj = myIsEdit ? myEditedObject :
158     Handle(HYDROData_Calculation)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
159   if ( aCalculObj.IsNull() )
160     return false;
161
162   aCalculObj->SetName( anObjectName );
163
164   QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
165   HYDROData_SequenceOfObjects aGeomObjects = 
166     HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
167
168   theUpdateFlags = UF_Model;
169
170   return true;
171 }
172
173 void HYDROGUI_CalculationOp::onSplitZones()
174 {
175   myRegionsList.clear();
176
177   HYDROGUI_CalculationDlg* aPanel = 
178     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
179   if ( !aPanel )
180     return;
181
182   QApplication::setOverrideCursor( Qt::WaitCursor );
183
184   QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
185   HYDROData_SequenceOfObjects aGeomObjects = 
186     HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
187
188   QStringList aResSplittedZones;
189
190   HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
191     HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
192
193   QStringList aUsedNames;
194
195   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
196   while( anIter.hasNext() )
197   {
198     Region aRegion;
199     aRegion.SplitData = anIter.next();
200
201     aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
202     aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
203
204     aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
205
206     aUsedNames.append( aRegion.RegionName );
207
208     aResSplittedZones.append( aRegion.RegionName );
209
210     myRegionsList.append( aRegion );
211   }
212   
213   createPreview();
214
215   QApplication::restoreOverrideCursor();
216 }
217
218 void HYDROGUI_CalculationOp::createPreview()
219 {
220   LightApp_Application* anApp = module()->getApp();
221
222   if ( !myActiveViewManager )
223   {
224     if ( myRegionsList.isEmpty() )
225       return;
226
227     myActiveViewManager = anApp->activeViewManager();
228   }
229
230   if ( !myPreviewViewManager )
231   {
232     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
233       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
234     if ( myPreviewViewManager )
235     {
236       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
237                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
238
239       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
240       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
241     }
242   }
243
244   if ( !myPreviewViewManager )
245     return;
246
247   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
248   {
249     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
250     if ( !aCtx.IsNull() )
251     {
252       RegionsList::iterator anIter = myRegionsList.begin();
253       for ( ; anIter != myRegionsList.end(); ++anIter )
254       {
255         Region& aRegion = *anIter;
256         if ( aRegion.Shape )
257         {
258           aRegion.Shape->erase( false );
259           delete aRegion.Shape;
260         }
261
262         aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
263
264         aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
265         aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
266         aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
267       }
268
269       //Process the draw events for viewer
270       QApplication::processEvents();
271       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
272         vw->onTopView();
273     }
274   }
275 }
276
277 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
278 {
279   closePreview();
280 }
281
282 void HYDROGUI_CalculationOp::closePreview()
283 {
284   RegionsList::iterator anIter= myRegionsList.begin();
285   for ( ; anIter != myRegionsList.end(); ++anIter )
286   {
287     Region& aRegion = *anIter;
288     if ( aRegion.Shape )
289     {
290       aRegion.Shape->erase( false );
291       delete aRegion.Shape;
292       aRegion.Shape = NULL;
293     }
294   }
295
296   if( myPreviewViewManager )
297   {
298     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
299                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
300
301     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
302     myPreviewViewManager = NULL;
303   }
304
305   if( myActiveViewManager )
306   {
307     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
308     myActiveViewManager = NULL;
309   }
310 }
311
312