Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / VISUGUI / VisuGUI_Prs3dTools.h
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  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 //
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 <SPlot2d_ViewWindow.h>
39
40 #include <vtkRenderer.h>
41
42 namespace VISU
43 {
44   //---------------------------------------------------------------
45   template<class TPrs3d_i, class TViewer, class TDlg, int TIsDlgModal>
46   void
47   EditPrs3d(VisuGUI* theModule, 
48             Handle(SALOME_InteractiveObject)& theIO,
49             VISU::Prs3d_i* thePrs3d,
50             SVTK_ViewWindow* theViewWindow)
51   {
52     if (TPrs3d_i* aPrs3d = dynamic_cast<TPrs3d_i*>(thePrs3d)) {
53       bool isModal = TIsDlgModal;
54       if( ColoredPrs3d_i* aColoredPrs3d = dynamic_cast<ColoredPrs3d_i*>(aPrs3d) )
55         if( !aColoredPrs3d->IsTimeStampFixed() )
56           isModal = 0;
57
58       TDlg* aDlg = new TDlg (theModule);
59       aDlg->initFromPrsObject(aPrs3d, true);
60       if (runAndWait(aDlg,isModal)) {
61         if (!(aDlg->storeToPrsObject(aPrs3d))) {
62           delete aDlg;
63           return;
64         }
65         try {
66           aPrs3d->UpdateActors();
67         } catch (std::runtime_error& exc) {
68           aPrs3d->RemoveActors();
69
70           INFOS(exc.what());
71           SUIT_MessageBox::warn1
72             (GetDesktop(theModule), QObject::tr("WRN_VISU"),
73              QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
74              QObject::tr("BUT_OK"));
75         }
76         if (theViewWindow) {
77           theViewWindow->getRenderer()->ResetCameraClippingRange();
78           theViewWindow->Repaint();
79         }
80         // Optionally, create table and curves for cut lines
81         QApplication::setOverrideCursor(Qt::waitCursor);
82         CreateCurves( theModule,
83                       dynamic_cast<VISU::CutLines_i*>( aPrs3d ),
84                       aDlg,
85                       false ); // in edition mode
86         QApplication::restoreOverrideCursor();
87       }
88       delete aDlg;
89     }
90   }
91
92
93   //----------------------------------------------------------------------------
94   template<class TPrs3d_i, class TDlg, int TIsDlgModal>
95   void
96   EditPrs3d(VisuGUI* theModule,
97             Handle(SALOME_InteractiveObject)& theIO,
98             VISU::Prs3d_i* thePrs3d,
99             SVTK_ViewWindow* theViewWindow = NULL)
100   {
101     SVTK_ViewWindow* aViewWindow = theViewWindow;
102     if (!aViewWindow)
103       // Create SVTK_ViewWindow, if it does not exist
104       aViewWindow = GetViewWindow<SVTK_Viewer>(theModule);
105     if (aViewWindow) {
106       EditPrs3d<TPrs3d_i,SVTK_Viewer,TDlg,TIsDlgModal>(theModule, theIO, thePrs3d, aViewWindow);
107     }
108   }
109
110
111   //---------------------------------------------------------------
112   template<class TPrs3d_i>
113   TPrs3d_i*
114   CreatePrs3dFromFactory(VisuGUI* theModule,
115                          _PTR(SObject) theTimeStamp,
116                          const char* theMeshName,
117                          VISU::Entity theEntity,
118                          const char* theFieldName,
119                          int theTimeId,
120                          ColoredPrs3d_i::EPublishInStudyMode thePublishInStudyMode)
121   {
122     VISU::Result_var aResult;
123     if (CheckResult(theModule, theTimeStamp, aResult)){
124       QApplication::setOverrideCursor(Qt::waitCursor);
125       TPrs3d_i* aPrs3d = NULL;
126       if(thePublishInStudyMode == VISU::ColoredPrs3d_i::EPublishUnderTimeStamp)
127         aPrs3d = VISU::CreatePrs3d<TPrs3d_i>(aResult,
128                                              theMeshName,
129                                              theEntity,
130                                              theFieldName,
131                                              theTimeId);
132       else
133       {
134         CORBA::Float anUsedMemory = 0.0;
135         CORBA::Float aRequiredMemory = 0.0;
136         VISU::ColoredPrs3dCache::EnlargeType anEnlargeType = 
137           VISU::GetRequiredCacheMemory<TPrs3d_i>(aResult,
138                                                  theMeshName,
139                                                  theEntity,
140                                                  theFieldName,
141                                                  theTimeId,
142                                                  anUsedMemory,
143                                                  aRequiredMemory);
144
145         if( anEnlargeType == VISU::ColoredPrs3dCache::IMPOSSIBLE )
146         {
147           size_t aMb = 1024 * 1024;
148           double aFreeMemory = double(VISU_PipeLine::GetAvailableMemory(2048*aMb)) / double(aMb);
149
150           CORBA::Float aNecessaryMemory = aRequiredMemory - aFreeMemory - anUsedMemory;
151           SUIT_MessageBox::warn1(GetDesktop(theModule),
152                                  QObject::tr("WRN_VISU"),
153                                  QObject::tr("ERR_NO_MEMORY_TO_BUILD").arg(aNecessaryMemory),
154                                  QObject::tr("&OK") );
155           QApplication::restoreOverrideCursor();
156           return NULL;
157         }
158         else
159         {
160           if( anEnlargeType == VISU::ColoredPrs3dCache::ENLARGE )
161           {
162             if(SUIT_MessageBox::info2(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, 0) == 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::warn1(GetDesktop(theModule),
187                            QObject::tr("WRN_VISU"),
188                            QObject::tr("ERR_CANT_BUILD_PRESENTATION"),
189                            QObject::tr("BUT_OK") );
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                                        aMeshName.latin1(),
225                                        (Entity)anEntity.toInt(),
226                                        aFieldName.latin1(),
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);
263       // Display created presentation.
264       if (aViewWindow) {
265         PublishInView(theModule, aPrs3d, aViewWindow);
266         aViewWindow->onFitAll();
267
268         AddScalarBarPosition(theModule, aViewWindow, aPrs3d, aPos);
269       }
270
271       return aPrs3d;
272     }
273
274     return NULL;
275   }
276
277
278   //---------------------------------------------------------------
279   template<class TPrs3d_i, class TViewer, class TDlg, int IsDlgModal>
280   void
281   CreatePrs3dInViewer(VisuGUI* theModule,
282                       _PTR(SObject) theTimeStampSObj,
283                       ColoredPrs3d_i::EPublishInStudyMode thePublishInStudyMode)
284   {
285     // Create new TViewWindow instance, if it does not exist.
286     typedef typename TViewer::TViewWindow TViewWindow;
287     if (!GetViewWindow<TViewer>(theModule))
288       return;
289
290     CreateAndEditPrs3d<TPrs3d_i,TViewer,TDlg,IsDlgModal>
291       (theModule,theTimeStampSObj,thePublishInStudyMode);
292
293     theModule->application()->putInfo(QObject::tr("INF_DONE"));
294   }
295
296
297   //----------------------------------------------------------------------------
298   template<class TPrs3d_i, class TDlg, int IsDlgModal>
299   void
300   CreatePrs3d(VisuGUI* theModule,
301               const QString& theDesiredViewerType = QString())
302   {
303     if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
304       return;
305
306     _PTR(SObject) aTimeStampSObj;
307     Handle(SALOME_InteractiveObject) anIO;
308     ColoredPrs3d_i::EPublishInStudyMode aPublishInStudyMode;
309     if (!CheckTimeStamp(theModule,aTimeStampSObj,anIO,aPublishInStudyMode))
310       return;
311
312     if(aPublishInStudyMode == VISU::ColoredPrs3d_i::EPublishIndependently){
313       CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,0>
314         (theModule,aTimeStampSObj,aPublishInStudyMode);
315       return;
316     }else{
317       if(theDesiredViewerType.isNull()){
318         if (SUIT_ViewManager* aViewManager = theModule->getApp()->activeViewManager())
319           if (aViewManager->getType() == VVTK_Viewer::Type()){ 
320             CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,IsDlgModal>
321               (theModule,aTimeStampSObj,aPublishInStudyMode);
322             return;
323           }
324         CreatePrs3dInViewer<TPrs3d_i,SVTK_Viewer,TDlg,IsDlgModal>
325           (theModule,aTimeStampSObj,aPublishInStudyMode);
326       }else{
327         if(theDesiredViewerType == VVTK_Viewer::Type())
328           CreatePrs3dInViewer<TPrs3d_i,VVTK_Viewer,TDlg,IsDlgModal>
329             (theModule,aTimeStampSObj,aPublishInStudyMode);
330         else
331           CreatePrs3dInViewer<TPrs3d_i,SVTK_Viewer,TDlg,IsDlgModal>
332             (theModule,aTimeStampSObj,aPublishInStudyMode);
333       }
334     }
335   }
336
337
338   //----------------------------------------------------------------------------
339   template<typename TInterface> 
340   typename TInterface::_var_type
341   GetInterface(CORBA::Object_ptr theObject)
342   {
343     if(!CORBA::is_nil(theObject))
344       return TInterface::_narrow(theObject);
345     return TInterface::_nil();
346   }
347   
348
349   //----------------------------------------------------------------------------
350   template<typename TServant> 
351   TServant*
352   GetServantInterface(CORBA::Object_ptr theObject)
353   {
354     if(!CORBA::is_nil(theObject)){
355       PortableServer::ServantBase_var aServant = GetServant(theObject);
356       return dynamic_cast<TServant*>(aServant.in());
357     }
358     return NULL;
359   }
360   
361
362   //----------------------------------------------------------------------------
363 }
364
365 #endif