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