Salome HOME
Improvement Bug PAL10652
[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 "VisuGUI_Tools.h"
33 #include "VisuGUI_DialogRunner.h"
34
35 int runAndWait( QDialog* dlg, const bool modal )
36 {
37   VisuGUI_DialogRunner r( dlg );
38   return r.run( modal );
39 }
40
41 namespace VISU
42 {
43   class CutLines_i;
44
45   //---------------------------------------------------------------
46   template<class TPrs3d_i, class TDlg, int IsDlgModal >
47   void
48   EditPrs3d (SalomeApp_Module* theModule, VISU::Prs3d_i* thePrs3d)
49   {
50     TPrs3d_i* aPrsObject = dynamic_cast<TPrs3d_i*>(thePrs3d);
51     if (aPrsObject) {
52       TDlg* aDlg = new TDlg (theModule);
53       aDlg->initFromPrsObject(aPrsObject);
54       if (runAndWait(aDlg,IsDlgModal)) {
55         if (!(aDlg->storeToPrsObject(aPrsObject))) {
56           delete aDlg;
57           return;
58         }
59         RecreateActor(theModule, aPrsObject);
60         if (SVTK_ViewWindow* vw = GetViewWindow(theModule)) {
61           vw->getRenderer()->ResetCameraClippingRange();
62           vw->Repaint();
63         }
64         // Optionally, create table and curves for cut lines
65         QApplication::setOverrideCursor(Qt::waitCursor);
66         CreateCurves( theModule,
67                       dynamic_cast<VISU::CutLines_i*>( aPrsObject ),
68                       aDlg,
69                       false ); // in edition mode
70         QApplication::restoreOverrideCursor();
71       }
72       delete aDlg;
73     }
74   }
75
76
77   //---------------------------------------------------------------
78   template<class TPrs3d_i>
79   TPrs3d_i*
80   CreatePrs3d(SalomeApp_Module* theModule,
81               _PTR(SObject) theTimeStamp,
82               const char* theMeshName,
83               VISU::Entity theEntity,
84               const char* theFieldName,
85               int theTimeId)
86   {
87     VISU::Result_var aResult;
88     if (CheckResult(theModule,theTimeStamp,aResult)){
89       QApplication::setOverrideCursor(Qt::waitCursor);
90       typedef typename TPrs3d_i::TInterface TPrs3d;
91       typename TPrs3d::_var_type aPrs3d =
92         GetVisuGen(theModule)->template Prs3dOnField<TPrs3d_i>
93         (aResult,theMeshName,theEntity,theFieldName,theTimeId);
94       QApplication::restoreOverrideCursor();
95       if (!CORBA::is_nil(aPrs3d.in()))
96         return dynamic_cast<TPrs3d_i*>(VISU::GetServant(aPrs3d.in()).in());
97     }
98     SUIT_MessageBox::warn1(GetDesktop(theModule),
99                            QObject::tr("WRN_VISU"),
100                            QObject::tr("ERR_CANT_BUILD_PRESENTATION"),
101                            QObject::tr("BUT_OK") );
102     return NULL;
103   }
104
105
106   //---------------------------------------------------------------
107   template<class TPrs3d_i, class TDlg, int IsDlgModal>
108   bool
109   CreatePrs3d(SalomeApp_Module* theModule,
110               _PTR(SObject) theTimeStamp,
111               const Handle(SALOME_InteractiveObject)& theIO)
112   {
113     using namespace VISU;
114     Storable::TRestoringMap aMap = getMapOfValue(theTimeStamp);
115     bool isExist;
116     QString aType = Storable::FindValue(aMap,"myType",&isExist);
117     if(!isExist || aType.toInt() != TTIMESTAMP )
118       return false;
119     QString aMeshName = Storable::FindValue(aMap,"myMeshName",&isExist).latin1();
120     QString anEntity = Storable::FindValue(aMap,"myEntityId",&isExist).latin1();
121     QString aFieldName = Storable::FindValue(aMap,"myFieldName",&isExist).latin1();
122     QString aTimeStampId = Storable::FindValue(aMap,"myTimeStampId",&isExist).latin1();
123     //
124     SUIT_ResourceMgr* aResourceMgr = GetResourceMgr();
125     VisuGUI* aMod = dynamic_cast<VisuGUI*>(theModule);
126     int aPos = 1;
127     if (aMod) aPos = GetFreePositionOfDefaultScalarBar(aMod);
128     aResourceMgr->setValue("VISU","scalar_bar_position_num",aPos);
129     //
130     QApplication::setOverrideCursor(Qt::waitCursor);
131     
132     TPrs3d_i* aPrs3d =
133       CreatePrs3d<TPrs3d_i>(theModule,
134                             theTimeStamp,
135                             aMeshName.latin1(),
136                             (Entity)anEntity.toInt(),
137                             aFieldName.latin1(),
138                             aTimeStampId.toInt());
139     
140     QApplication::restoreOverrideCursor();
141     if(aPrs3d){
142       SUIT_ResourceMgr* aResourceMgr = GetResourceMgr();
143       int aValue = aResourceMgr->integerValue("VISU","BuildDefaultPrs3d",0);
144       if(!aValue){
145         if(TDlg* aDlg = new TDlg(theModule)){ // dialog box in creation mode
146           aDlg->initFromPrsObject(aPrs3d);
147
148             if(runAndWait(aDlg,IsDlgModal) && (aDlg->storeToPrsObject(aPrs3d))) {
149               // Optionally, create table and curves for cut lines
150               QApplication::setOverrideCursor(Qt::waitCursor);
151               CreateCurves( theModule,
152                             dynamic_cast<VISU::CutLines_i*>( aPrs3d ),
153                             aDlg,
154                             true ); // in creation mode
155               QApplication::restoreOverrideCursor();
156               delete aDlg;
157             } else {
158               DeletePrs3d(theModule,aPrs3d,theIO);
159               delete aDlg;
160               return false;
161             }
162         }
163       }
164       PublishInView(theModule,aPrs3d);
165       Prs3d_i* aPrs_i = dynamic_cast<Prs3d_i*>(aPrs3d);
166       if (aPrs_i != NULL && aMod != NULL)
167         AddScalarBarPosition(aMod,aPrs_i,aPos);
168       return true;
169     }
170     return false;
171   }
172   
173   
174   //---------------------------------------------------------------
175   template<class TPrs3d_i, class TDlg, int IsDlgModal>
176   void
177   CreatePrs3d(SalomeApp_Module* theModule)
178   {
179     if (CheckLock(GetCStudy(GetAppStudy(theModule))))
180       return;
181     
182     _PTR(SObject) aTimeStampSObj;
183     Handle(SALOME_InteractiveObject) anIO;
184     if (!CheckTimeStamp(theModule,aTimeStampSObj,&anIO))
185       return;
186     
187     if (!CreatePrs3d<TPrs3d_i,TDlg,IsDlgModal>(theModule,aTimeStampSObj,anIO))
188       return;
189     
190     theModule->application()->putInfo(QObject::tr("INF_DONE"));
191     theModule->updateObjBrowser();
192     UpdateObjBrowser(theModule);
193     
194     if (SVTK_ViewWindow* aView = GetViewWindow(theModule))
195       aView->onFitAll();
196   }
197
198 }
199
200 #endif