]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_CalculationOp.cxx
Salome HOME
HYDROGUI_Zone class is introduced.
[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 #include <HYDROData_Object.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_Desktop.h>
45
46 #include <QApplication>
47
48 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
49 : HYDROGUI_Operation( theModule ),
50   myIsEdit( theIsEdit ),
51   myActiveViewManager( NULL ),
52   myPreviewViewManager( NULL )
53 {
54   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
55 }
56
57 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
58 {
59   closePreview();
60 }
61
62 void HYDROGUI_CalculationOp::startOperation()
63 {
64   HYDROGUI_Operation::startOperation();
65   
66   // Begin transaction
67   startDocOperation();
68
69   HYDROGUI_CalculationDlg* aPanel = 
70     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
71   if ( !aPanel )
72     return;
73
74   myRegionsList.clear();
75   aPanel->reset();
76
77   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
78
79   QStringList aSelectedObjects;
80
81   myEditedObject.Nullify();
82   if ( myIsEdit )
83   {
84     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
85     if ( !myEditedObject.IsNull() )
86     {
87       anObjectName = myEditedObject->GetName();
88       updateGeomObjectsList(aPanel);
89     }
90   }
91   else
92   {
93     myEditedObject =
94       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
95     myEditedObject->SetName(anObjectName);
96   }
97
98   aPanel->setObjectName( anObjectName );
99   aPanel->setEditedObject( myEditedObject );
100 //  aPanel->setSelectedGeomObjects( aSelectedObjects );
101
102   createPreview();
103 }
104
105 void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
106 {
107   Handle(HYDROData_Object) anObject;
108   Handle(HYDROData_Entity) anEntity;
109   QStringList aList;
110   // Update the list in the dialog
111   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
112   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
113   for ( ; anIter.More(); anIter.Next() )
114   {
115     anEntity = anIter.Value();
116     if ( !anEntity.IsNull() )
117     {
118       anObject = Handle(HYDROData_Object)::DownCast( anEntity );
119       if ( !anObject.IsNull() )
120       {
121         aList.append( anObject->GetName() );
122       }
123     }
124   }
125   thePanel->setSelectedGeomObjects( aList );
126 }
127
128 void HYDROGUI_CalculationOp::abortOperation()
129 {
130   closePreview();
131   // Abort transaction
132   abortDocOperation();
133   HYDROGUI_Operation::abortOperation();
134 }
135
136 void HYDROGUI_CalculationOp::commitOperation()
137 {
138   closePreview();
139   // Commit transaction
140   commitDocOperation();
141   HYDROGUI_Operation::commitOperation();
142 }
143
144 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
145 {
146   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
147
148   // Connect signals and slots
149   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
150   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
151   connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
152   connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
153
154   return aPanel;
155 }
156
157 void HYDROGUI_CalculationOp::onAddObjects()
158 {
159   // Add geometry objects selected in the module browser to the calculation case
160   Handle(HYDROData_Object) anObject;
161   Handle(HYDROData_Entity) anEntity;
162   QStringList aList;
163   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
164   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
165   {
166     anEntity = aSeq.Value( anIndex );
167     if ( !anEntity.IsNull() )
168     {
169       anObject = Handle(HYDROData_Object)::DownCast( anEntity );
170       if( !anObject.IsNull() )
171       {
172         if (myEditedObject->AddGeometryObject( anObject ))
173         {
174           aList.append( anObject->GetName() );
175         }
176       }
177     }
178   }
179   HYDROGUI_CalculationDlg* aPanel = 
180     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
181   if ( aPanel )
182   {
183     updateGeomObjectsList( aPanel );
184   }
185 }
186
187 void HYDROGUI_CalculationOp::onRemoveObjects()
188 {
189   // Remove selected objects from the calculation case
190   HYDROGUI_CalculationDlg* aPanel = 
191     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
192   if ( aPanel )
193   {
194     QStringList aList = aPanel->getSelectedGeomObjects();
195     Handle(HYDROData_Object) anObject;
196     Handle(HYDROData_Entity) anEntity;
197     for (int i = 0; i < aList.length(); i++)
198     {
199       anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
200       if ( !anEntity.IsNull() )
201       {
202         anObject = Handle(HYDROData_Object)::DownCast( anEntity );
203         if ( !anObject.IsNull() )
204         {
205           myEditedObject->RemoveGeometryObject( anObject );
206         }
207       }
208     }
209     updateGeomObjectsList( aPanel );
210   }
211 }
212
213 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
214                                            QString& theErrorMsg )
215 {
216   HYDROGUI_CalculationDlg* aPanel = 
217     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
218   if ( !aPanel )
219     return false;
220
221   //QString anObjectName = aPanel->getObjectName().simplified();
222   //if ( anObjectName.isEmpty() )
223   //{
224   //  theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
225   //  return false;
226   //}
227
228   //// check that there are no other objects with the same name in the document
229   //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
230   //{
231   //  Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
232   //  if ( !anObject.IsNull() )
233   //  {
234   //    theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
235   //    return false;
236   //  }
237   //}
238
239   //Handle(HYDROData_Document) aDocument = doc();
240
241   //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
242   //  Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
243   //if ( aCalculObj.IsNull() )
244   //  return false;
245
246   //aCalculObj->SetName( anObjectName );
247
248   //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
249   //HYDROData_SequenceOfObjects aGeomObjects = 
250   //  HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
251
252   theUpdateFlags = UF_Model;
253
254   return true;
255 }
256
257 void HYDROGUI_CalculationOp::onApply()
258 {
259   QApplication::setOverrideCursor( Qt::WaitCursor );
260
261   int anUpdateFlags = 0;
262   QString anErrorMsg;
263
264   bool aResult = false;
265   
266   try
267   {
268     aResult = processApply( anUpdateFlags, anErrorMsg );
269   }
270   catch ( Standard_Failure )
271   {
272     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
273     anErrorMsg = aFailure->GetMessageString();
274     aResult = false;
275   }
276   catch ( ... )
277   {
278     aResult = false;
279   }
280   
281   QApplication::restoreOverrideCursor();
282
283   if ( aResult )
284   {
285     module()->update( anUpdateFlags );
286     commit();
287   }
288   else
289   {
290     abort();
291     QString aMsg = tr( "INPUT_VALID_DATA" );
292     if( !anErrorMsg.isEmpty() )
293       aMsg.prepend( anErrorMsg + "\n" );
294     SUIT_MessageBox::critical( module()->getApp()->desktop(),
295                                tr( "INSUFFICIENT_INPUT_DATA" ),
296                                aMsg ); 
297   }
298 }
299
300 void HYDROGUI_CalculationOp::onSplitZones()
301 {
302   //myRegionsList.clear();
303
304   HYDROGUI_CalculationDlg* aPanel = 
305     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
306   if ( !aPanel )
307     return;
308
309   QApplication::setOverrideCursor( Qt::WaitCursor );
310
311   //QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
312   //HYDROData_SequenceOfObjects aGeomObjects = 
313   //  HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
314
315   //QStringList aResSplittedZones;
316
317   //HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
318   //  HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
319
320   //QStringList aUsedNames;
321
322   //HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
323   //while( anIter.hasNext() )
324   //{
325   //  Region aRegion;
326   //  aRegion.SplitData = anIter.next();
327
328   //  aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
329   //  aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
330
331   //  aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
332
333   //  aUsedNames.append( aRegion.RegionName );
334
335   //  aResSplittedZones.append( aRegion.RegionName );
336
337   //  myRegionsList.append( aRegion );
338   //}
339   //
340
341   myEditedObject->SplitGeometryObjects();
342   aPanel->setEditedObject( myEditedObject );
343   createPreview();
344
345   QApplication::restoreOverrideCursor();
346 }
347
348 void HYDROGUI_CalculationOp::createPreview()
349 {
350   LightApp_Application* anApp = module()->getApp();
351
352   if ( !myActiveViewManager )
353   {
354     if ( myRegionsList.isEmpty() )
355       return;
356
357     myActiveViewManager = anApp->activeViewManager();
358   }
359
360   if ( !myPreviewViewManager )
361   {
362     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
363       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
364     if ( myPreviewViewManager )
365     {
366       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
367                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
368
369       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
370       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
371     }
372   }
373
374   if ( !myPreviewViewManager )
375     return;
376
377   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
378   {
379     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
380     if ( !aCtx.IsNull() )
381     {
382       RegionsList::iterator anIter = myRegionsList.begin();
383       for ( ; anIter != myRegionsList.end(); ++anIter )
384       {
385         Region& aRegion = *anIter;
386         if ( aRegion.Shape )
387         {
388           aRegion.Shape->erase( false );
389           delete aRegion.Shape;
390         }
391
392         aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
393
394         aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
395         aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
396         aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
397       }
398
399       //Process the draw events for viewer
400       QApplication::processEvents();
401       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
402         vw->onTopView();
403     }
404   }
405 }
406
407 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
408 {
409   closePreview();
410 }
411
412 void HYDROGUI_CalculationOp::closePreview()
413 {
414   RegionsList::iterator anIter= myRegionsList.begin();
415   for ( ; anIter != myRegionsList.end(); ++anIter )
416   {
417     Region& aRegion = *anIter;
418     if ( aRegion.Shape )
419     {
420       aRegion.Shape->erase( false );
421       delete aRegion.Shape;
422       aRegion.Shape = NULL;
423     }
424   }
425
426   if( myPreviewViewManager )
427   {
428     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
429                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
430
431     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
432     myPreviewViewManager = NULL;
433   }
434
435   if( myActiveViewManager )
436   {
437     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
438     myActiveViewManager = NULL;
439   }
440 }
441
442