Salome HOME
Fix of the following issues:
[modules/visu.git] / src / VISUGUI / VisuGUI_DeformedShapeDlg.cxx
1 //  Copyright (C) 2007-2008  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 //  VISU VISUGUI : GUI of VISU component
23 //  File   : VisuGUI_MagnitudeDlg.cxx
24 //  Author : Laurent CORNABE & Hubert ROLLAND
25 //  Module : VISU
26 //  $Header$
27 //
28 #include "VisuGUI_DeformedShapeDlg.h"
29
30 #include "VisuGUI_Tools.h"
31 #include "VisuGUI_InputPane.h"
32 #include "VisuGUI.h"
33 #include "VISU_ColoredPrs3dFactory.hh"
34 #include "VISU_DeformedShape_i.hh"
35 #include "VisuGUI_ViewTools.h"
36 #include "VISU_Gen_i.hh"
37 #include "VISU_Result_i.hh"
38 #include "VisuGUI_Prs3dTools.h"
39 #include "SVTK_ViewWindow.h"
40 #include "SalomeApp_Module.h"
41 #include "LightApp_Application.h"
42 #include "SUIT_Desktop.h"
43 #include "SUIT_Session.h"
44 #include "SUIT_MessageBox.h"
45 #include "SUIT_ResourceMgr.h"
46
47 #include <QLayout>
48 #include <QDialog>
49 #include <QGroupBox>
50 #include <QLabel>
51 #include <QPushButton>
52 #include <QTabWidget>
53 #include <QKeyEvent>
54
55 #include <math.h>
56 #include <limits>
57
58 using namespace std;
59
60 /*!
61  * Constructor
62  */
63 VisuGUI_DeformedShapeDlg::VisuGUI_DeformedShapeDlg (SalomeApp_Module* theModule)
64   : VisuGUI_ScalarBarBaseDlg(theModule)
65 {
66   setWindowTitle(tr("DLG_TITLE"));
67   setSizeGripEnabled(TRUE);
68   myModule = theModule;
69   isApplyed = false;
70
71   QVBoxLayout* TopLayout = new QVBoxLayout (this);
72   TopLayout->setSpacing(6);
73   TopLayout->setMargin(11);
74
75   myTabBox = new QTabWidget (this);
76
77   // Deformed shape pane
78   QWidget* aBox = new QWidget (this);
79   QVBoxLayout* aVBLay = new QVBoxLayout( aBox );
80   aVBLay->setMargin(11);
81   QFrame* TopGroup = new QFrame ( aBox );
82   aVBLay->addWidget( TopGroup );
83   TopGroup->setFrameStyle(QFrame::Box | QFrame::Sunken);
84   TopGroup->setLineWidth(1);
85   QGridLayout* TopGroupLayout = new QGridLayout (TopGroup);
86   TopGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
87   TopGroupLayout->setSpacing(6);
88   TopGroupLayout->setMargin(11);
89
90   //   Scale factor
91   QLabel* ScaleLabel = new QLabel (tr("SCALE_FACTOR"), TopGroup );
92   TopGroupLayout->addWidget(ScaleLabel, 0, 0);
93
94   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
95   int aPrecision = aResourceMgr->integerValue( "VISU", "floating_point_precision", 0 );
96
97   ScalFact = new QtxDoubleSpinBox (0, 1.0E+38, 0.1, aPrecision*(-1), 38, TopGroup);
98   ScalFact->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
99   ScalFact->setValue(0.1);
100   TopGroupLayout->addWidget(ScalFact, 0, 1);
101
102   //   Magnitude coloring
103   UseMagn = new QCheckBox (tr("MAGNITUDE_COLORING"), TopGroup);
104   UseMagn->setChecked(true);
105   TopGroupLayout->addWidget(UseMagn, 1, 0, 1, 2);
106
107   myTabBox->addTab(aBox, tr("DEFORMED_SHAPE_TAB"));
108
109   // Scalar bar pane
110   myInputPane = new VisuGUI_InputPane(VISU::TDEFORMEDSHAPE, theModule, this);
111
112   myTabBox->addTab(GetScalarPane(), tr("SCALAR_BAR_TAB"));
113   myTabBox->addTab(myInputPane, tr("INPUT_TAB"));
114
115   // Buttons
116   QGroupBox* GroupButtons = new QGroupBox (this);
117   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
118   //oupButtons->setColumnLayout(0, Qt::Vertical);
119   //GroupButtons->layout()->setSpacing(0);
120   //GroupButtons->layout()->setMargin(0);
121   QGridLayout* GroupButtonsLayout = new QGridLayout (GroupButtons);
122   GroupButtonsLayout->setAlignment(Qt::AlignTop);
123   GroupButtonsLayout->setSpacing(6);
124   GroupButtonsLayout->setMargin(11);
125
126   QPushButton* buttonOk = new QPushButton (tr("A&pply and Close"), GroupButtons);
127   buttonOk->setAutoDefault(TRUE);
128   buttonOk->setDefault(TRUE);
129   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
130
131   QPushButton* buttonApply = new QPushButton (tr("&Apply"), GroupButtons);
132   buttonApply->setAutoDefault(TRUE);
133   buttonApply->setDefault(TRUE);
134   GroupButtonsLayout->addWidget(buttonApply, 0, 1);
135
136   QPushButton* buttonCancel = new QPushButton (tr("BUT_CANCEL") , GroupButtons);
137   buttonCancel->setAutoDefault(TRUE);
138   GroupButtonsLayout->addWidget(buttonCancel, 0, 2);
139
140   QPushButton* buttonHelp = new QPushButton (tr("BUT_HELP") , GroupButtons);
141   buttonHelp->setAutoDefault(TRUE);
142   GroupButtonsLayout->addWidget(buttonHelp, 0, 3);
143
144   // Add Tab box and Buttons to the top layout
145   TopLayout->addWidget(myTabBox);
146   TopLayout->addWidget(GroupButtons);
147
148   // signals and slots connections
149   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(accept()));
150   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(onApply()));
151   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
152   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(onHelp()));
153 }
154
155 VisuGUI_DeformedShapeDlg::~VisuGUI_DeformedShapeDlg()
156 {}
157
158 void VisuGUI_DeformedShapeDlg::initFromPrsObject (VISU::ColoredPrs3d_i* thePrs,
159                                                   bool theInit)
160 {
161   if( theInit )
162     myPrsCopy = VISU::TSameAsFactory<VISU::TDEFORMEDSHAPE>().Create(thePrs, VISU::ColoredPrs3d_i::EDoNotPublish);
163
164   VisuGUI_ScalarBarBaseDlg::initFromPrsObject(myPrsCopy, theInit);
165   setFactor(myPrsCopy->GetScale());
166   UseMagn->setChecked(myPrsCopy->IsColored());
167
168   if (!theInit)
169     return;
170
171   myInputPane->initFromPrsObject(myPrsCopy);
172   myTabBox->setCurrentIndex(0);
173 }
174
175 void VisuGUI_DeformedShapeDlg::setFactor(double theFactor)
176 {
177   double step = 0.1;
178   if (fabs(theFactor) > numeric_limits<double>::epsilon()) {
179     int degree = int(log10(fabs(theFactor))) - 1;
180     if (fabs(theFactor) < 1) {
181       // as logarithm value is negative in this case
182       // and it is truncated to the bigger integer
183       degree -= 1;
184     }
185     step = pow(10., double(degree));
186   }
187
188   ScalFact->setSingleStep(step);
189   ScalFact->setValue(theFactor);
190 }
191
192 int VisuGUI_DeformedShapeDlg::storeToPrsObject(VISU::ColoredPrs3d_i* thePrs) 
193 {
194   if( !isValid() )
195     return 0;
196
197   int anIsOk = myInputPane->storeToPrsObject( myPrsCopy );
198   anIsOk &= GetScalarPane()->storeToPrsObject( myPrsCopy );
199
200   myPrsCopy->SetScale(getFactor());
201   myPrsCopy->ShowColored(isColored());
202
203   VISU::TSameAsFactory<VISU::TDEFORMEDSHAPE>().Copy(myPrsCopy, thePrs);
204
205   return anIsOk;
206 }
207
208 void VisuGUI_DeformedShapeDlg::updatePrsCopy()
209 {
210   SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>();
211   VisuGUI* aVisuGUI = dynamic_cast <VisuGUI*>(myModule);
212   if ( myPrsCopy->GetNumberOfActors() == 0 ) {
213     PublishInView(aVisuGUI, myPrsCopy, aViewWindow);
214     if(VISU::GetResourceMgr()->booleanValue("VISU","automatic_fit_all",false))
215       aViewWindow->onFitAll();
216     int aPos = VISU::GetFreePositionOfDefaultScalarBar(aVisuGUI, aViewWindow);
217     VISU::AddScalarBarPosition(aVisuGUI, aViewWindow, myPrsCopy, aPos);
218     } else {
219     try {
220       myPrsCopy->UpdateActors();
221     } catch (std::runtime_error& exc) {
222       myPrsCopy->RemoveActors();
223       
224       INFOS(exc.what());
225       SUIT_MessageBox::warning(VISU::GetDesktop(myModule),
226                                tr("WRN_VISU"),
227                                tr("ERR_CANT_BUILD_PRESENTATION") + ": " + exc.what());
228     }
229     aViewWindow->Repaint();
230   }
231 }
232
233 void VisuGUI_DeformedShapeDlg::accept()
234 {
235   VisuGUI_ScalarBarBaseDlg::done( isValid() );
236 }
237
238 void VisuGUI_DeformedShapeDlg::reject()
239 {
240   if ( isApplyed )
241     VisuGUI_ScalarBarBaseDlg::done( 1 );
242   else
243     VisuGUI_ScalarBarBaseDlg::reject();
244 }
245
246 QString VisuGUI_DeformedShapeDlg::GetContextHelpFilePath()
247 {
248   return "deformed_shape_page.html";
249 }
250
251 bool VisuGUI_DeformedShapeDlg::onApply()
252 {
253   if ( storeToPrsObject( myPrsCopy ) ) 
254   {
255     updatePrsCopy();
256     isApplyed = true;
257     return true;
258   }
259   return false;
260 }
261
262 ////////////////////////////////////////////////////////////////////////////////
263 //  Create and Edit
264 ////////////////////////////////////////////////////////////////////////////////
265
266 VISU::Prs3d_i* VisuGUI_DeformedShapeDlg::CreatePrs3d(VisuGUI* theModule)
267 {
268   VISU::DeformedShape_i* aPrs3d = NULL;
269   _PTR(SObject) aTimeStamp;
270   Handle(SALOME_InteractiveObject) anIO;
271   VISU::ColoredPrs3d_i::EPublishInStudyMode aPublishInStudyMode;
272
273   if (VISU::CheckTimeStamp(theModule, aTimeStamp, anIO, aPublishInStudyMode))
274     {
275       VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aTimeStamp);
276       VISU::VISUType aType = VISU::Storable::RestoringMap2Type(aRestoringMap);
277       if ( aType == VISU::TTIMESTAMP )
278         {
279   
280           QString aMeshName = aRestoringMap["myMeshName"];
281           QString anEntity = aRestoringMap["myEntityId"];
282           QString aFieldName = aRestoringMap["myFieldName"];
283           QString aTimeStampId = aRestoringMap["myTimeStampId"];
284           
285           //typedef VISU::DeformedShape_i TPrs3d_i; 
286           
287           aPrs3d =
288             VISU::CreatePrs3dFromFactory<VISU::DeformedShape_i>(theModule,
289                                                    aTimeStamp,
290                                                    aMeshName.toLatin1().data(),
291                                                    (VISU::Entity)anEntity.toInt(),
292                                                    aFieldName.toLatin1().data(),
293                                                    aTimeStampId.toInt(),
294                                                    aPublishInStudyMode);
295
296           if (aPrs3d) {
297             VisuGUI_DeformedShapeDlg* aDlg = new VisuGUI_DeformedShapeDlg (theModule);
298             aDlg->initFromPrsObject(aPrs3d, true);
299             aDlg->UseMagn->setChecked(true);
300             VisuGUI_DialogRunner r(aDlg);
301             int dlgResult = r.run( false );
302             
303             if ( dlgResult == 0 )
304               DeletePrs3d(theModule,aPrs3d);
305             else {
306               aDlg->storeToPrsObject( aPrs3d );
307               SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>();
308               PublishInView(theModule, aPrs3d, aViewWindow);
309               if(VISU::GetResourceMgr()->booleanValue("VISU","automatic_fit_all",false))
310                 aViewWindow->onFitAll();
311               int aPos = VISU::GetFreePositionOfDefaultScalarBar(theModule, aViewWindow);
312               VISU::AddScalarBarPosition(theModule, aViewWindow, aPrs3d, aPos);
313             }
314             
315             VISU::UpdateObjBrowser(theModule, true, aTimeStamp);
316             delete aDlg;
317           }
318         }
319     }
320   return aPrs3d;
321 }
322
323 ////////////////////////////////////////////////////////////////////////////////
324
325 void VisuGUI_DeformedShapeDlg::EditPrs3d(VisuGUI* theModule, VISU::Prs3d_i* thePrs3d, Handle(SALOME_InteractiveObject)& theIO)
326 {
327   typedef VISU::DeformedShape_i TPrs3d_i;
328
329   if (TPrs3d_i* aPrs3d = dynamic_cast<TPrs3d_i*>(thePrs3d)) {
330     VisuGUI_DeformedShapeDlg* aDlg = new VisuGUI_DeformedShapeDlg (theModule);
331     aDlg->initFromPrsObject(aPrs3d, true);
332     aDlg->updatePrsCopy();
333
334     //Hide thePrs3d from Viewer
335     SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>(theModule);
336     if (aViewWindow)
337       if (VISU_Actor* anActor = FindActor(aViewWindow, thePrs3d))
338         if (anActor->GetVisibility())
339           anActor->VisibilityOff();
340
341     VisuGUI_DialogRunner r(aDlg);
342     int dlgResult = r.run( false );
343     
344     if ( dlgResult != 0 )
345       aDlg->storeToPrsObject( aPrs3d );
346     
347     try {
348       thePrs3d->UpdateActors();
349     }
350     catch (std::runtime_error& exc) {
351       thePrs3d->RemoveActors();
352       
353       INFOS(exc.what());
354       SUIT_MessageBox::warning
355         (VISU::GetDesktop(theModule), QObject::tr("WRN_VISU"),
356          QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()));
357     }
358
359     if (aViewWindow)
360       if (VISU_Actor* anActor = FindActor(aViewWindow, thePrs3d))
361         if (!anActor->GetVisibility())
362           anActor->VisibilityOn();
363     
364     delete aDlg;    
365   }
366 }
367
368 ///////////////////////////////////////////////////////////////////////////////////
369 int VisuGUI_DeformedShapeDlg::isValid()
370 {
371   if(myInputPane->check() && GetScalarPane()->check())
372     return 1; 
373   else
374     return 0;
375 }