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