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