Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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
28 #ifndef VisuGUI_Prs3dTools_HeaderFile
29 #define VisuGUI_Prs3dTools_HeaderFile
30
31 #include "VISU_Gen_i.hh"
32 #include "VisuGUI_ViewTools.h"
33 #include "VisuGUI_DialogRunner.h"
34
35 #include <vtkRenderer.h>
36
37 namespace VISU
38 {
39   class CutLines_i;
40
41   //---------------------------------------------------------------
42   inline
43   int
44   runAndWait( QDialog* dlg, const bool modal )
45   {
46     VisuGUI_DialogRunner r( dlg );
47     return r.run( modal );
48   }
49
50   //---------------------------------------------------------------
51   template<class TPrs3d_i, class TViewer, class TDlg, int TIsDlgModal>
52   void
53   EditPrs3d(VisuGUI* theModule, 
54             VISU::Prs3d_i* thePrs3d,
55             SVTK_ViewWindow* theViewWindow)
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           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   template<class TPrs3d_i, class TDlg, int TIsDlgModal>
93   inline
94   void
95   EditPrs3d(VisuGUI* theModule,
96             Handle(SALOME_InteractiveObject)& theIO,
97             VISU::Prs3d_i* thePrs3d,
98             SVTK_ViewWindow* theViewWindow = NULL)
99   {
100     SVTK_ViewWindow* aViewWindow = theViewWindow;
101     if (!aViewWindow)
102       // Create SVTK_ViewWindow, if it does not exist
103       aViewWindow = GetViewWindow<SVTK_Viewer>(theModule);
104     if (aViewWindow) {
105       EditPrs3d<TPrs3d_i,SVTK_Viewer,TDlg,TIsDlgModal>(theModule, thePrs3d, aViewWindow);
106       aViewWindow->highlight(theIO, 1);
107     }
108   }
109
110   //---------------------------------------------------------------
111   template<class TPrs3d_i>
112   inline
113   TPrs3d_i*
114   CreatePrs3d(VisuGUI* theModule,
115               _PTR(SObject) theTimeStamp,
116               const char* theMeshName,
117               VISU::Entity theEntity,
118               const char* theFieldName,
119               int theTimeId)
120   {
121     VISU::Result_var aResult;
122     if (CheckResult(theModule,theTimeStamp,aResult)){
123       QApplication::setOverrideCursor(Qt::waitCursor);
124       TPrs3d_i* aPrs3d =
125         GetVisuGen(theModule)->template CreatePrs3d<TPrs3d_i>
126         (aResult,theMeshName,theEntity,theFieldName,theTimeId);
127       QApplication::restoreOverrideCursor();
128       if(aPrs3d)
129         return aPrs3d;
130     }
131     SUIT_MessageBox::warn1(GetDesktop(theModule),
132                            QObject::tr("WRN_VISU"),
133                            QObject::tr("ERR_CANT_BUILD_PRESENTATION"),
134                            QObject::tr("BUT_OK") );
135     return NULL;
136   }
137
138
139   //---------------------------------------------------------------
140   template<class TPrs3d_i, class TViewer, class TDlg, int IsDlgModal>
141   inline
142   bool
143   CreatePrs3d(VisuGUI* theModule,
144               _PTR(SObject) theTimeStamp,
145               const Handle(SALOME_InteractiveObject)& theIO)
146   {
147     Storable::TRestoringMap aMap = getMapOfValue(theTimeStamp);
148
149     bool isExist;
150     QString aType = Storable::FindValue(aMap,"myType",&isExist);
151     if (!isExist || aType.toInt() != TTIMESTAMP )
152       return false;
153
154     QString aMeshName = Storable::FindValue(aMap,"myMeshName",&isExist).latin1();
155     QString anEntity = Storable::FindValue(aMap,"myEntityId",&isExist).latin1();
156     QString aFieldName = Storable::FindValue(aMap,"myFieldName",&isExist).latin1();
157     QString aTimeStampId = Storable::FindValue(aMap,"myTimeStampId",&isExist).latin1();
158
159     // Create new TViewWindow instance, if it does not exist.
160     typedef typename TViewer::TViewWindow TViewWindow;
161     TViewWindow* aViewWindow = GetViewWindow<TViewer>(theModule);
162
163     // Define free position for scalar bar.
164     int aPos = GetFreePositionOfDefaultScalarBar(theModule, aViewWindow);
165     GetResourceMgr()->setValue("VISU", "scalar_bar_position_num", aPos);
166
167     QApplication::setOverrideCursor(Qt::waitCursor);
168
169     TPrs3d_i* aPrs3d =
170       CreatePrs3d<TPrs3d_i>(theModule,
171                             theTimeStamp,
172                             aMeshName.latin1(),
173                             (Entity)anEntity.toInt(),
174                             aFieldName.latin1(),
175                             aTimeStampId.toInt());
176
177     QApplication::restoreOverrideCursor();
178     if (aPrs3d) {
179       SUIT_ResourceMgr* aResourceMgr = GetResourceMgr();
180       int aValue = aResourceMgr->integerValue("VISU","BuildDefaultPrs3d",0);
181       if (!aValue) {
182         if (TDlg* aDlg = new TDlg(theModule)) { // dialog box in creation mode
183           aDlg->initFromPrsObject(aPrs3d);
184
185             if (runAndWait(aDlg,IsDlgModal) && (aDlg->storeToPrsObject(aPrs3d))) {
186               // Optionally, create table and curves for cut lines
187               QApplication::setOverrideCursor(Qt::waitCursor);
188               CreateCurves( theModule,
189                             dynamic_cast<VISU::CutLines_i*>( aPrs3d ),
190                             aDlg,
191                             true ); // in creation mode
192               UpdateObjBrowser(theModule,true,theTimeStamp);
193               QApplication::restoreOverrideCursor();
194               delete aDlg;
195             } else {
196               DeletePrs3d(theModule,aPrs3d,theIO);
197               delete aDlg;
198               return false;
199             }
200         }
201       }
202
203       // Display created presentation.
204       if (aViewWindow) {
205         PublishInView(theModule, aPrs3d, aViewWindow, /*highlight = */true);
206         aViewWindow->onFitAll();
207
208         AddScalarBarPosition(theModule, aViewWindow, aPrs3d, aPos);
209       }
210
211       return true;
212     }
213
214     return false;
215   }
216
217
218   //---------------------------------------------------------------
219   template<class TPrs3d_i, class TViewer, class TDlg, int IsDlgModal>
220   inline
221   void
222   CreatePrs3d(VisuGUI* theModule)
223   {
224     if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
225       return;
226
227     _PTR(SObject) aTimeStampSObj;
228     Handle(SALOME_InteractiveObject) anIO;
229     if (!CheckTimeStamp(theModule,aTimeStampSObj,&anIO))
230       return;
231
232     // Create new TViewWindow instance, if it does not exist.
233     typedef typename TViewer::TViewWindow TViewWindow;
234     if (!GetViewWindow<TViewer>(theModule))
235       return;
236
237     if (!CreatePrs3d<TPrs3d_i,TViewer,TDlg,IsDlgModal>(theModule,aTimeStampSObj,anIO))
238       return;
239
240     theModule->application()->putInfo(QObject::tr("INF_DONE"));
241   }
242
243   template<class TPrs3d_i, class TDlg, int IsDlgModal>
244   inline
245   void
246   CreatePrs3d(VisuGUI* theModule)
247   {
248     if (SUIT_ViewManager* aViewManager = theModule->getApp()->activeViewManager()) {
249       QString aType = aViewManager->getType();
250       if (aType == VVTK_Viewer::Type())
251         CreatePrs3d<TPrs3d_i,VVTK_Viewer,TDlg,IsDlgModal>(theModule);
252       else
253         CreatePrs3d<TPrs3d_i,SVTK_Viewer,TDlg,IsDlgModal>(theModule);
254     }
255   }
256 }
257
258 #endif