Salome HOME
Path to resource files corrected as in LINUX platform.
[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   aCalculObj->SetGeometryObjects( aGeomObjects );
169
170   theUpdateFlags = UF_Model;
171
172   return true;
173 }
174
175 void HYDROGUI_CalculationOp::onSplitZones()
176 {
177   myRegionsList.clear();
178
179   HYDROGUI_CalculationDlg* aPanel = 
180     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
181   if ( !aPanel )
182     return;
183
184   QApplication::setOverrideCursor( Qt::WaitCursor );
185
186   QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
187   HYDROData_SequenceOfObjects aGeomObjects = 
188     HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
189
190   QStringList aResSplittedZones;
191
192   HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
193     HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
194
195   QStringList aUsedNames;
196
197   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
198   while( anIter.hasNext() )
199   {
200     Region aRegion;
201     aRegion.SplitData = anIter.next();
202
203     aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
204     aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
205
206     aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
207
208     aUsedNames.append( aRegion.RegionName );
209
210     aResSplittedZones.append( aRegion.RegionName );
211
212     myRegionsList.append( aRegion );
213   }
214   
215   createPreview();
216
217   QApplication::restoreOverrideCursor();
218 }
219
220 void HYDROGUI_CalculationOp::createPreview()
221 {
222   LightApp_Application* anApp = module()->getApp();
223
224   if ( !myActiveViewManager )
225   {
226     if ( myRegionsList.isEmpty() )
227       return;
228
229     myActiveViewManager = anApp->activeViewManager();
230   }
231
232   if ( !myPreviewViewManager )
233   {
234     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
235       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
236     if ( myPreviewViewManager )
237     {
238       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
239                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
240
241       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
242       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
243     }
244   }
245
246   if ( !myPreviewViewManager )
247     return;
248
249   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
250   {
251     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
252     if ( !aCtx.IsNull() )
253     {
254       RegionsList::iterator anIter = myRegionsList.begin();
255       for ( ; anIter != myRegionsList.end(); ++anIter )
256       {
257         Region& aRegion = *anIter;
258         if ( aRegion.Shape )
259         {
260           aRegion.Shape->erase( false );
261           delete aRegion.Shape;
262         }
263
264         aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
265
266         aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
267         aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
268         aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
269       }
270
271       //Process the draw events for viewer
272       QApplication::processEvents();
273       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
274         vw->onTopView();
275     }
276   }
277 }
278
279 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
280 {
281   closePreview();
282 }
283
284 void HYDROGUI_CalculationOp::closePreview()
285 {
286   RegionsList::iterator anIter= myRegionsList.begin();
287   for ( ; anIter != myRegionsList.end(); ++anIter )
288   {
289     Region& aRegion = *anIter;
290     if ( aRegion.Shape )
291     {
292       aRegion.Shape->erase( false );
293       delete aRegion.Shape;
294       aRegion.Shape = NULL;
295     }
296   }
297
298   if( myPreviewViewManager )
299   {
300     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
301                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
302
303     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
304     myPreviewViewManager = NULL;
305   }
306
307   if( myActiveViewManager )
308   {
309     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
310     myActiveViewManager = NULL;
311   }
312 }
313
314