Salome HOME
Merge from OCC_development_generic_2006
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : VisuGUI_Module.h
25 //  Author : 
26 //  Module : VISU
27 //  $Header$
28
29 #ifndef VisuGUI_Prs3dTools_HeaderFile
30 #define VisuGUI_Prs3dTools_HeaderFile
31
32 #include "VISU_Gen_i.hh"
33 #include "VisuGUI_ViewTools.h"
34 #include "VisuGUI_DialogRunner.h"
35
36 #include <vtkRenderer.h>
37
38 namespace VISU
39 {
40   class CutLines_i;
41
42   //---------------------------------------------------------------
43   inline
44   int
45   runAndWait( QDialog* dlg, const bool modal )
46   {
47     VisuGUI_DialogRunner r( dlg );
48     return r.run( modal );
49   }
50
51   //---------------------------------------------------------------
52   template<class TPrs3d_i, class TViewer, class TDlg, int TIsDlgModal>
53   void
54   EditPrs3d(VisuGUI* theModule, 
55             VISU::Prs3d_i* thePrs3d)
56   {
57     if(TPrs3d_i* aPrs3d = dynamic_cast<TPrs3d_i*>(thePrs3d)){
58       TDlg* aDlg = new TDlg (theModule);
59       aDlg->initFromPrsObject(aPrs3d);
60       if (runAndWait(aDlg,TIsDlgModal)) {
61         if (!(aDlg->storeToPrsObject(aPrs3d))) {
62           delete aDlg;
63           return;
64         }
65         try {
66           aPrs3d->UpdateActors();
67         } catch (std::runtime_error& exc) {
68           INFOS(exc.what());
69           SUIT_MessageBox::warn1
70             (GetDesktop(theModule), QObject::tr("WRN_VISU"),
71              QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
72              QObject::tr("BUT_OK"));
73
74           aPrs3d->RemoveActors();
75         }
76         typedef typename TViewer::TViewWindow TViewWindow;
77         if(TViewWindow* aViewWindow = GetViewWindow<TViewer>(theModule)){
78           aViewWindow->getRenderer()->ResetCameraClippingRange();
79           aViewWindow->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   template<class TPrs3d_i, class TDlg, int TIsDlgModal>
94   inline
95   void
96   EditPrs3d(VisuGUI* theModule, 
97             VISU::Prs3d_i* thePrs3d)
98   {
99     EditPrs3d<TPrs3d_i,SVTK_Viewer,TDlg,TIsDlgModal>(theModule,thePrs3d);
100   }
101
102   //---------------------------------------------------------------
103   template<class TPrs3d_i>
104   inline
105   TPrs3d_i*
106   CreatePrs3d(VisuGUI* theModule,
107               _PTR(SObject) theTimeStamp,
108               const char* theMeshName,
109               VISU::Entity theEntity,
110               const char* theFieldName,
111               int theTimeId)
112   {
113     VISU::Result_var aResult;
114     if (CheckResult(theModule,theTimeStamp,aResult)){
115       QApplication::setOverrideCursor(Qt::waitCursor);
116       TPrs3d_i* aPrs3d =
117         GetVisuGen(theModule)->template CreatePrs3d<TPrs3d_i>
118         (aResult,theMeshName,theEntity,theFieldName,theTimeId);
119       QApplication::restoreOverrideCursor();
120       if(aPrs3d)
121         return aPrs3d;
122     }
123     SUIT_MessageBox::warn1(GetDesktop(theModule),
124                            QObject::tr("WRN_VISU"),
125                            QObject::tr("ERR_CANT_BUILD_PRESENTATION"),
126                            QObject::tr("BUT_OK") );
127     return NULL;
128   }
129
130
131   //---------------------------------------------------------------
132   template<class TPrs3d_i, class TViewer, class TDlg, int IsDlgModal>
133   inline
134   bool
135   CreatePrs3d(VisuGUI* theModule,
136               _PTR(SObject) theTimeStamp,
137               const Handle(SALOME_InteractiveObject)& theIO,
138               const bool theIsCreateView = false)
139   {
140     Storable::TRestoringMap aMap = getMapOfValue(theTimeStamp);
141
142     bool isExist;
143     QString aType = Storable::FindValue(aMap,"myType",&isExist);
144     if(!isExist || aType.toInt() != TTIMESTAMP )
145       return false;
146
147     QString aMeshName = Storable::FindValue(aMap,"myMeshName",&isExist).latin1();
148     QString anEntity = Storable::FindValue(aMap,"myEntityId",&isExist).latin1();
149     QString aFieldName = Storable::FindValue(aMap,"myFieldName",&isExist).latin1();
150     QString aTimeStampId = Storable::FindValue(aMap,"myTimeStampId",&isExist).latin1();
151     //
152     int aPos = GetFreePositionOfDefaultScalarBar(theModule);
153     GetResourceMgr()->setValue("VISU","scalar_bar_position_num",aPos);
154     //
155     QApplication::setOverrideCursor(Qt::waitCursor);
156     
157     TPrs3d_i* aPrs3d =
158       CreatePrs3d<TPrs3d_i>(theModule,
159                             theTimeStamp,
160                             aMeshName.latin1(),
161                             (Entity)anEntity.toInt(),
162                             aFieldName.latin1(),
163                             aTimeStampId.toInt());
164     
165     QApplication::restoreOverrideCursor();
166     if(aPrs3d){
167       SUIT_ResourceMgr* aResourceMgr = GetResourceMgr();
168       int aValue = aResourceMgr->integerValue("VISU","BuildDefaultPrs3d",0);
169       if(!aValue){
170         if(TDlg* aDlg = new TDlg(theModule)){ // dialog box in creation mode
171           aDlg->initFromPrsObject(aPrs3d);
172
173             if(runAndWait(aDlg,IsDlgModal) && (aDlg->storeToPrsObject(aPrs3d))) {
174               // Optionally, create table and curves for cut lines
175               QApplication::setOverrideCursor(Qt::waitCursor);
176               CreateCurves( theModule,
177                             dynamic_cast<VISU::CutLines_i*>( aPrs3d ),
178                             aDlg,
179                             true ); // in creation mode
180               UpdateObjBrowser(theModule,true,theTimeStamp);
181               QApplication::restoreOverrideCursor();
182               delete aDlg;
183             } else {
184               DeletePrs3d(theModule,aPrs3d,theIO);
185               delete aDlg;
186               return false;
187             }
188         }
189       }
190       PublishInView<TViewer>(theModule,aPrs3d,true,theIsCreateView);
191
192       AddScalarBarPosition(theModule,aPrs3d,aPos);
193
194       return true;
195     }
196
197     return false;
198   }
199   
200
201   //---------------------------------------------------------------
202   template<class TPrs3d_i, class TViewer, class TDlg, int IsDlgModal>
203   inline
204   void
205   CreatePrs3d(VisuGUI* theModule,
206               const bool theIsCreateView = true)
207   {
208     if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
209       return;
210     
211     _PTR(SObject) aTimeStampSObj;
212     Handle(SALOME_InteractiveObject) anIO;
213     if(!CheckTimeStamp(theModule,aTimeStampSObj,&anIO))
214       return;
215     
216     if(!CreatePrs3d<TPrs3d_i,TViewer,TDlg,IsDlgModal>(theModule,aTimeStampSObj,anIO,theIsCreateView))
217       return;
218     
219     theModule->application()->putInfo(QObject::tr("INF_DONE"));
220     
221     typedef typename TViewer::TViewWindow TView;
222     if(TView* aView = GetViewWindow<TViewer>(theModule,theIsCreateView))
223       aView->onFitAll();
224   }
225
226   template<class TPrs3d_i, class TDlg, int IsDlgModal>
227   inline
228   void
229   CreatePrs3d(VisuGUI* theModule,
230               const bool theIsCreateView = true)
231   {
232     if(SUIT_ViewManager* aViewManager = theModule->getApp()->activeViewManager()){
233       QString aType = aViewManager->getType();
234       if(aType == SVTK_Viewer::Type())
235         CreatePrs3d<TPrs3d_i,SVTK_Viewer,TDlg,IsDlgModal>(theModule,theIsCreateView);
236       else if(aType == VVTK_Viewer::Type())
237         CreatePrs3d<TPrs3d_i,VVTK_Viewer,TDlg,IsDlgModal>(theModule,theIsCreateView);
238     }
239   }
240
241 }
242
243 #endif