]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_Prs3dTools.h
Salome HOME
IPAL20954 problem loading help pages
[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::CutLines_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::CutLines_i*>( aPrs3d ),
249                           aDlg,
250                           true ); // in creation mode
251             UpdateObjBrowser(theModule,true,theTimeStamp);
252             QApplication::restoreOverrideCursor();
253             delete aDlg;
254           } else {
255             DeletePrs3d(theModule,aPrs3d);
256             QApplication::restoreOverrideCursor();
257             delete aDlg;
258             return NULL;
259           }
260         }
261       }
262       // aViewWindow = GetViewWindow<TViewer>(theModule); IPAL 20125 dmv
263       // Display created presentation.
264       if (aViewWindow) {
265         PublishInView(theModule, aPrs3d, aViewWindow);
266         if(GetResourceMgr()->booleanValue("VISU","automatic_fit_all",false)){
267           aViewWindow->onFitAll();
268         }
269
270         AddScalarBarPosition(theModule, aViewWindow, aPrs3d, aPos);
271       }
272
273       return aPrs3d;
274     }
275
276     return NULL;
277   }
278
279
280   //---------------------------------------------------------------
281   template<class TPrs3d_i, class TViewer, class TDlg, int IsDlgModal>
282   TPrs3d_i*
283   CreatePrs3dInViewer(VisuGUI* theModule,
284                       _PTR(SObject) theTimeStampSObj,
285                       ColoredPrs3d_i::EPublishInStudyMode thePublishInStudyMode)
286   {
287     TPrs3d_i* aPrs = 0;
288     // Create new TViewWindow instance, if it does not exist.
289     typedef typename TViewer::TViewWindow TViewWindow;
290     if (!GetViewWindow<TViewer>(theModule))
291       return aPrs;
292
293     aPrs = CreateAndEditPrs3d<TPrs3d_i,TViewer,TDlg,IsDlgModal>
294               (theModule,theTimeStampSObj,thePublishInStudyMode);
295
296     theModule->application()->putInfo(QObject::tr("INF_DONE"));
297     return aPrs;
298   }
299
300
301   //----------------------------------------------------------------------------
302   template<class TPrs3d_i, class TDlg, int IsDlgModal>
303   TPrs3d_i*
304   CreatePrs3d(VisuGUI* theModule,
305               const QString& theDesiredViewerType = QString())
306   {
307     TPrs3d_i* aPrs = 0;
308     if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
309       return aPrs;
310
311     _PTR(SObject) aTimeStampSObj;
312     Handle(SALOME_InteractiveObject) anIO;
313     ColoredPrs3d_i::EPublishInStudyMode aPublishInStudyMode;
314     if (!CheckTimeStamp(theModule,aTimeStampSObj,anIO,aPublishInStudyMode))
315       return aPrs;
316
317     if(/*aPublishInStudyMode == */VISU::ColoredPrs3d_i::EPublishIndependently){
318       //      CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,0>
319       //        (theModule,aTimeStampSObj,aPublishInStudyMode);
320       aPrs = CreatePrs3dInViewer<TPrs3d_i,SVTK_Viewer,TDlg,0>
321                 (theModule,aTimeStampSObj,aPublishInStudyMode);
322       return aPrs;
323     }else{
324       if(theDesiredViewerType.isNull()){
325         if (/*SUIT_ViewManager* aViewManager = */theModule->getApp()->activeViewManager())
326           /*if (aViewManager->getType() == VVTK_Viewer::Type()){ 
327             aPrs = CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,IsDlgModal>
328                       (theModule,aTimeStampSObj,aPublishInStudyMode);
329             return aPrs;
330             }*/
331         aPrs = CreatePrs3dInViewer<TPrs3d_i,SVTK_Viewer,TDlg,IsDlgModal>
332                    (theModule,aTimeStampSObj,aPublishInStudyMode);
333       }else{
334         /*if(theDesiredViewerType == VVTK_Viewer::Type()) {
335           aPrs = CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,IsDlgModal>
336                      (theModule,aTimeStampSObj,aPublishInStudyMode);
337                      } else {*/
338           aPrs = CreatePrs3dInViewer<TPrs3d_i,SVTK_Viewer,TDlg,IsDlgModal>
339                      (theModule,aTimeStampSObj,aPublishInStudyMode);
340           //}
341       }
342     }
343     return aPrs;
344   }
345
346
347   //----------------------------------------------------------------------------
348   template<typename TInterface> 
349   typename TInterface::_var_type
350   GetInterface(CORBA::Object_ptr theObject)
351   {
352     if(!CORBA::is_nil(theObject))
353       return TInterface::_narrow(theObject);
354     return TInterface::_nil();
355   }
356   
357
358   //----------------------------------------------------------------------------
359   template<typename TServant> 
360   TServant*
361   GetServantInterface(CORBA::Object_ptr theObject)
362   {
363     if(!CORBA::is_nil(theObject)){
364       PortableServer::ServantBase_var aServant = GetServant(theObject);
365       return dynamic_cast<TServant*>(aServant.in());
366     }
367     return NULL;
368   }
369   
370
371   //----------------------------------------------------------------------------
372 }
373
374 #endif