]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_Prs3dTools.h
Salome HOME
Fix of issue 0020593: EDF 885 VISU: Unchecking Filter by Scalar does not remove white...
[modules/visu.git] / src / VISUGUI / VisuGUI_Prs3dTools.h
1 //  Copyright (C) 2007-2008  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 //  VISU VISUGUI : GUI of VISU component
23 //  File   : VisuGUI_Module.h
24 //  Author : 
25 //  Module : VISU
26 //
27 #ifndef VisuGUI_Prs3dTools_HeaderFile
28 #define VisuGUI_Prs3dTools_HeaderFile
29
30 #include "VISUConfig.hh"
31 #include "VisuGUI_Tools.h"
32 #include "VisuGUI_ViewTools.h"
33 #include "VISU_ColoredPrs3dFactory.hh"
34 #include "VISU_PipeLine.hxx"
35 #include "VisuGUI_DialogRunner.h"
36
37 #include <SUIT_Desktop.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <SPlot2d_ViewWindow.h>
41
42 #include <vtkRenderer.h>
43
44 namespace VISU
45 {
46   //---------------------------------------------------------------
47   template<class TPrs3d_i, class TViewer, class TDlg, int TIsDlgModal>
48   void
49   EditPrs3d(VisuGUI* theModule, 
50             Handle(SALOME_InteractiveObject)& theIO,
51             VISU::Prs3d_i* thePrs3d,
52             SVTK_ViewWindow* theViewWindow)
53   {
54     if (TPrs3d_i* aPrs3d = dynamic_cast<TPrs3d_i*>(thePrs3d)) {
55       bool isModal = TIsDlgModal;
56       if( ColoredPrs3d_i* aColoredPrs3d = dynamic_cast<ColoredPrs3d_i*>(aPrs3d) )
57         if( !aColoredPrs3d->IsTimeStampFixed() )
58           isModal = 0;
59
60       TDlg* aDlg = new TDlg (theModule);
61       aDlg->initFromPrsObject(aPrs3d, true);
62       if (runAndWait(aDlg,isModal)) {
63         if (!(aDlg->storeToPrsObject(aPrs3d))) {
64           delete aDlg;
65           return;
66         }
67         try {
68           aPrs3d->UpdateActors();
69         } catch (std::runtime_error& exc) {
70           aPrs3d->RemoveActors();
71
72           INFOS(exc.what());
73           SUIT_MessageBox::warning
74             (GetDesktop(theModule), QObject::tr("WRN_VISU"),
75              QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()) );
76         }
77         if (theViewWindow) {
78           theViewWindow->getRenderer()->ResetCameraClippingRange();
79           theViewWindow->Repaint();
80         }
81         // Optionally, create table and curves for cut lines
82         QApplication::setOverrideCursor(Qt::WaitCursor);
83         CreateCurves( theModule,
84                       dynamic_cast<VISU::CutLinesBase_i*>( aPrs3d ),
85                       aDlg,
86                       false ); // in edition mode
87         QApplication::restoreOverrideCursor();
88       }
89       delete aDlg;
90     }
91   }
92
93
94   //----------------------------------------------------------------------------
95   template<class TPrs3d_i, class TDlg, int TIsDlgModal>
96   void
97   EditPrs3d(VisuGUI* theModule,
98             Handle(SALOME_InteractiveObject)& theIO,
99             VISU::Prs3d_i* thePrs3d,
100             SVTK_ViewWindow* theViewWindow = NULL)
101   {
102     SVTK_ViewWindow* aViewWindow = theViewWindow;
103     if (!aViewWindow)
104       // Create SVTK_ViewWindow, if it does not exist
105       aViewWindow = GetViewWindow<SVTK_Viewer>(theModule);
106     if (aViewWindow) {
107       EditPrs3d<TPrs3d_i,SVTK_Viewer,TDlg,TIsDlgModal>(theModule, theIO, thePrs3d, aViewWindow);
108     }
109   }
110
111
112   //---------------------------------------------------------------
113   template<class TPrs3d_i>
114   TPrs3d_i*
115   CreatePrs3dFromFactory(VisuGUI* theModule,
116                          _PTR(SObject) theTimeStamp,
117                          const char* theMeshName,
118                          VISU::Entity theEntity,
119                          const char* theFieldName,
120                          int theTimeId,
121                          ColoredPrs3d_i::EPublishInStudyMode thePublishInStudyMode)
122   {
123     VISU::Result_var aResult;
124     if (CheckResult(theModule, theTimeStamp, aResult)){
125       QApplication::setOverrideCursor(Qt::WaitCursor);
126       TPrs3d_i* aPrs3d = NULL;
127       if(thePublishInStudyMode == VISU::ColoredPrs3d_i::EPublishUnderTimeStamp)
128         aPrs3d = VISU::CreatePrs3d<TPrs3d_i>(aResult,
129                                              theMeshName,
130                                              theEntity,
131                                              theFieldName,
132                                              theTimeId);
133       else
134       {
135         CORBA::Float anUsedMemory = 0.0;
136         CORBA::Float aRequiredMemory = 0.0;
137         VISU::ColoredPrs3dCache::EnlargeType anEnlargeType = 
138           VISU::GetRequiredCacheMemory<TPrs3d_i>(aResult,
139                                                  theMeshName,
140                                                  theEntity,
141                                                  theFieldName,
142                                                  theTimeId,
143                                                  anUsedMemory,
144                                                  aRequiredMemory);
145
146         if( anEnlargeType == VISU::ColoredPrs3dCache::IMPOSSIBLE )
147         {
148           size_t aMb = 1024 * 1024;
149           double aFreeMemory = double(VISU_PipeLine::GetAvailableMemory(8192*aMb)) / double(aMb);
150
151           CORBA::Float aNecessaryMemory = aRequiredMemory - aFreeMemory - anUsedMemory;
152           SUIT_MessageBox::warning(GetDesktop(theModule),
153                                    QObject::tr("WRN_VISU"),
154                                    QObject::tr("ERR_NO_MEMORY_TO_BUILD").arg(aNecessaryMemory) );
155           QApplication::restoreOverrideCursor();
156           return NULL;
157         }
158         else
159         {
160           if( anEnlargeType == VISU::ColoredPrs3dCache::ENLARGE )
161           {
162             if(SUIT_MessageBox::information(GetDesktop(theModule),
163                                       QObject::tr("WRN_VISU"),
164                                       QObject::tr("WRN_EXTRA_MEMORY_REQUIRED").arg(aRequiredMemory),
165                                       QObject::tr("&OK"), QObject::tr("&Cancel"),
166                                       0, 1) == 1)
167             {
168               QApplication::restoreOverrideCursor();
169               return NULL;
170             }
171           }
172           aPrs3d = VISU::CreateHolder2GetDeviceByType<TPrs3d_i>(aResult,
173                                                                 theMeshName,
174                                                                 theEntity,
175                                                                 theFieldName,
176                                                                 theTimeId,
177                                                                 anEnlargeType,
178                                                                 aRequiredMemory);
179         }
180       }
181       
182       QApplication::restoreOverrideCursor();
183       if(aPrs3d)
184         return aPrs3d;
185     }
186     SUIT_MessageBox::warning(GetDesktop(theModule),
187                              QObject::tr("WRN_VISU"),
188                              QObject::tr("ERR_CANT_BUILD_PRESENTATION") );
189
190     return NULL;
191   }
192
193
194   //---------------------------------------------------------------
195   template<class TPrs3d_i, class TViewer, class TDlg, int IsDlgModal>
196   TPrs3d_i*
197   CreateAndEditPrs3d(VisuGUI* theModule,
198                      _PTR(SObject) theTimeStamp,
199                      ColoredPrs3d_i::EPublishInStudyMode thePublishInStudyMode)
200   {
201     Storable::TRestoringMap aRestoringMap = Storable::GetStorableMap(theTimeStamp);
202     VISU::VISUType aType = VISU::Storable::RestoringMap2Type(aRestoringMap);
203     if ( aType != TTIMESTAMP )
204       return NULL;
205
206     QString aMeshName = aRestoringMap["myMeshName"];
207     QString anEntity = aRestoringMap["myEntityId"];
208     QString aFieldName = aRestoringMap["myFieldName"];
209     QString aTimeStampId = aRestoringMap["myTimeStampId"];
210
211     // Create new TViewWindow instance, if it does not exist.
212     typedef typename TViewer::TViewWindow TViewWindow;
213     TViewWindow* aViewWindow = GetViewWindow<TViewer>(theModule);
214
215     // Define free position for scalar bar.
216     int aPos = GetFreePositionOfDefaultScalarBar(theModule, aViewWindow);
217     GetResourceMgr()->setValue("VISU", "scalar_bar_position_num", aPos);
218
219     QApplication::setOverrideCursor(Qt::WaitCursor);
220
221     TPrs3d_i* aPrs3d =
222       CreatePrs3dFromFactory<TPrs3d_i>(theModule,
223                                        theTimeStamp,
224                                        (const char*)aMeshName.toLatin1(),
225                                        (Entity)anEntity.toInt(),
226                                        (const char*)aFieldName.toLatin1(),
227                                        aTimeStampId.toInt(),
228                                        thePublishInStudyMode);
229     
230     QApplication::restoreOverrideCursor();
231     if (aPrs3d) {
232       SUIT_ResourceMgr* aResourceMgr = GetResourceMgr();
233       int aValue = aResourceMgr->integerValue("VISU","BuildDefaultPrs3d",0);
234       if(aResourceMgr->booleanValue("VISU","display_only",false)){
235           theModule->OnEraseAll();
236         }
237       
238       if (!aValue) {
239         if (TDlg* aDlg = new TDlg(theModule)) { // dialog box in creation mode
240           aDlg->initFromPrsObject(aPrs3d, true);
241           if (runAndWait(aDlg,IsDlgModal) && (aDlg->storeToPrsObject(aPrs3d))) {
242             if(aResourceMgr->booleanValue("VISU","display_only",false)){
243               if(SPlot2d_Viewer* aPlot2d = GetPlot2dViewer(theModule, false)) aPlot2d->EraseAll();
244             } 
245             // Optionally, create table and curves for cut lines
246             QApplication::setOverrideCursor(Qt::WaitCursor);
247             CreateCurves( theModule,
248                           dynamic_cast<VISU::CutLinesBase_i*>( aPrs3d ),
249                           aDlg,
250                           true ); // in creation mode
251             UpdateObjBrowser(theModule,true,theTimeStamp);
252             QApplication::restoreOverrideCursor();
253             delete aDlg;
254           } else {
255             if ( theModule->application() )
256               DeletePrs3d(theModule,aPrs3d);
257             QApplication::restoreOverrideCursor();
258             delete aDlg;
259             return NULL;
260           }
261         }
262       }
263       // aViewWindow = GetViewWindow<TViewer>(theModule); IPAL 20125 dmv
264       // Display created presentation.
265       if (aViewWindow) {
266         PublishInView(theModule, aPrs3d, aViewWindow);
267         if(GetResourceMgr()->booleanValue("VISU","automatic_fit_all",false)){
268           aViewWindow->onFitAll();
269         }
270
271         AddScalarBarPosition(theModule, aViewWindow, aPrs3d, aPos);
272       }
273
274       return aPrs3d;
275     }
276
277     return NULL;
278   }
279
280
281   //---------------------------------------------------------------
282   template<class TPrs3d_i, class TViewer, class TDlg, int IsDlgModal>
283   TPrs3d_i*
284   CreatePrs3dInViewer(VisuGUI* theModule,
285                       _PTR(SObject) theTimeStampSObj,
286                       ColoredPrs3d_i::EPublishInStudyMode thePublishInStudyMode)
287   {
288     TPrs3d_i* aPrs = 0;
289     // Create new TViewWindow instance, if it does not exist.
290     typedef typename TViewer::TViewWindow TViewWindow;
291     if (!GetViewWindow<TViewer>(theModule))
292       return aPrs;
293
294     aPrs = CreateAndEditPrs3d<TPrs3d_i,TViewer,TDlg,IsDlgModal>
295               (theModule,theTimeStampSObj,thePublishInStudyMode);
296
297     theModule->application()->putInfo(QObject::tr("INF_DONE"));
298     return aPrs;
299   }
300
301
302   //----------------------------------------------------------------------------
303   template<class TPrs3d_i, class TDlg, int IsDlgModal>
304   TPrs3d_i*
305   CreatePrs3d(VisuGUI* theModule,
306               const QString& theDesiredViewerType = QString())
307   {
308     TPrs3d_i* aPrs = 0;
309     if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
310       return aPrs;
311
312     _PTR(SObject) aTimeStampSObj;
313     Handle(SALOME_InteractiveObject) anIO;
314     ColoredPrs3d_i::EPublishInStudyMode aPublishInStudyMode;
315     if (!CheckTimeStamp(theModule,aTimeStampSObj,anIO,aPublishInStudyMode))
316       return aPrs;
317
318     if(/*aPublishInStudyMode == */VISU::ColoredPrs3d_i::EPublishIndependently){
319       //      CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,0>
320       //        (theModule,aTimeStampSObj,aPublishInStudyMode);
321       aPrs = CreatePrs3dInViewer<TPrs3d_i,SVTK_Viewer,TDlg,0>
322                 (theModule,aTimeStampSObj,aPublishInStudyMode);
323       return aPrs;
324     }else{
325       if(theDesiredViewerType.isNull()){
326         if (/*SUIT_ViewManager* aViewManager = */theModule->getApp()->activeViewManager())
327           /*if (aViewManager->getType() == VVTK_Viewer::Type()){ 
328             aPrs = CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,IsDlgModal>
329                       (theModule,aTimeStampSObj,aPublishInStudyMode);
330             return aPrs;
331             }*/
332         aPrs = CreatePrs3dInViewer<TPrs3d_i,SVTK_Viewer,TDlg,IsDlgModal>
333                    (theModule,aTimeStampSObj,aPublishInStudyMode);
334       }else{
335         /*if(theDesiredViewerType == VVTK_Viewer::Type()) {
336           aPrs = CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,IsDlgModal>
337                      (theModule,aTimeStampSObj,aPublishInStudyMode);
338                      } else {*/
339           aPrs = CreatePrs3dInViewer<TPrs3d_i,SVTK_Viewer,TDlg,IsDlgModal>
340                      (theModule,aTimeStampSObj,aPublishInStudyMode);
341           //}
342       }
343     }
344     return aPrs;
345   }
346
347
348   //----------------------------------------------------------------------------
349   template<typename TInterface> 
350   typename TInterface::_var_type
351   GetInterface(CORBA::Object_ptr theObject)
352   {
353     if(!CORBA::is_nil(theObject))
354       return TInterface::_narrow(theObject);
355     return TInterface::_nil();
356   }
357   
358
359   //----------------------------------------------------------------------------
360   template<typename TServant> 
361   TServant*
362   GetServantInterface(CORBA::Object_ptr theObject)
363   {
364     if(!CORBA::is_nil(theObject)){
365       PortableServer::ServantBase_var aServant = GetServant(theObject);
366       return dynamic_cast<TServant*>(aServant.in());
367     }
368     return NULL;
369   }
370   
371
372   //----------------------------------------------------------------------------
373 }
374
375 #endif