Salome HOME
Bug IPAL21228 SIGSEGV on exit from Salome with active GaussPointsDlg
[modules/visu.git] / src / VISUGUI / VisuGUI_DeformedShapeAndScalarMapDlg.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_DeformedShapeAndScalarMapDlg.cxx
24 //  Author : Eugeny Nikolaev
25 //  Module : VISU
26 //
27 #include "VisuGUI_DeformedShapeAndScalarMapDlg.h"
28
29 #include "VisuGUI_Tools.h"
30 #include "VisuGUI_InputPane.h"
31
32 #include "VISU_Result_i.hh"
33 #include "VISU_DeformedShapeAndScalarMap_i.hh"
34 #include "VISU_ColoredPrs3dFactory.hh"
35
36 #include "VISU_ScalarMapPL.hxx"
37 #include "VISU_DeformedShapeAndScalarMapPL.hxx"
38
39 #include "VISU_Convertor.hxx"
40
41 #include <QtxDoubleSpinBox.h>
42 #include <SalomeApp_Module.h>
43 #include <LightApp_Application.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <SUIT_Desktop.h>
46 #include <SUIT_ResourceMgr.h>
47 #include <SUIT_Session.h>
48 #include <SUIT_MessageBox.h>
49
50 #include <SALOME_ListIO.hxx>
51
52 #include <SALOMEDSClient_AttributeString.hxx>
53 #include <SALOMEDSClient_AttributeName.hxx>
54
55 #include <QVBoxLayout>
56 #include <QHBoxLayout>
57 #include <QGridLayout>
58 #include <QTabWidget>
59 #include <QComboBox>
60 #include <QPushButton>
61 #include <QLabel>
62
63 #include <limits>
64
65 #define MYDEBUG 0
66
67 /*!
68  * Constructor
69  */
70 VisuGUI_DeformedShapeAndScalarMapDlg::VisuGUI_DeformedShapeAndScalarMapDlg (SalomeApp_Module* theModule)
71   : VisuGUI_ScalarBarBaseDlg(theModule, true),
72     myIsAnimation(false),
73     myUpdateScalars(true),
74     myIsScalarFieldValid(true),
75     myVisuGUI(theModule)
76 {
77   setWindowTitle(tr("DLG_TITLE"));
78   setSizeGripEnabled(true);
79
80   QVBoxLayout* TopLayout = new QVBoxLayout (this);
81   TopLayout->setSpacing(6);
82   TopLayout->setMargin(11);
83
84   myTabBox = new QTabWidget (this);
85
86   // Scalar Map on Deformed shape pane
87   QWidget* aBox = new QWidget (this);
88   QVBoxLayout* aVBLay = new QVBoxLayout( aBox );
89   aVBLay->setMargin(11);
90   QFrame* TopGroup = new QFrame (aBox);
91   aVBLay->addWidget(TopGroup);
92   TopGroup->setFrameStyle(QFrame::Box | QFrame::Sunken);
93   TopGroup->setLineWidth(1);
94   QGridLayout* TopGroupLayout = new QGridLayout (TopGroup);
95   TopGroupLayout->setSpacing(6);
96   TopGroupLayout->setMargin(11);
97
98   //   Scale factor
99   QLabel* ScaleLabel = new QLabel (tr("SCALE_FACTOR"), TopGroup);
100   TopGroupLayout->addWidget(ScaleLabel, 0, 0);
101
102   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
103   int aPrecision = aResourceMgr->integerValue( "VISU", "floating_point_precision", 0 );
104
105   ScalFact = new QtxDoubleSpinBox (0, 1.0E+38, 0.1, aPrecision*(-1), 32, TopGroup);
106   ScalFact->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
107   ScalFact->setValue(0.1);
108   TopGroupLayout->addWidget(ScalFact, 0, 1);
109
110   // Fields combo box
111   QLabel* FieldLabel = new QLabel (tr("FIELD_ITEM"), TopGroup);
112   myFieldsCombo = new QComboBox (TopGroup);
113
114   TopGroupLayout->addWidget(FieldLabel, 1, 0);
115   TopGroupLayout->addWidget(myFieldsCombo,1,1);
116
117   // TimeStamps combo box
118   QLabel* TimeStampLabel = new QLabel (tr("TIMESTAMP_ITEM"), TopGroup);
119   myTimeStampsCombo = new QComboBox (TopGroup);
120
121   TopGroupLayout->addWidget(TimeStampLabel, 2, 0);
122   TopGroupLayout->addWidget(myTimeStampsCombo,2,1);
123   TopGroupLayout->setRowStretch(3,5);
124
125   //
126   myTabBox->addTab(aBox, tr("DEFORMED_SHAPE_AND_SCALAR_MAP_TAB"));
127
128   // Scalar bar pane
129   myInputPane = new VisuGUI_InputPane(VISU::TDEFORMEDSHAPEANDSCALARMAP, theModule, this);
130
131   myTabBox->addTab(GetScalarPane(), tr("SCALAR_BAR_TAB"));
132   myTabBox->addTab(myInputPane, tr("INPUT_TAB"));
133
134   // Buttons
135   QGroupBox* GroupButtons = new QGroupBox (this);
136   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
137   GroupButtonsLayout->setSpacing(6);
138   GroupButtonsLayout->setMargin(11);
139
140   myButtonOk = new QPushButton (tr("&OK"), GroupButtons);
141   myButtonOk->setAutoDefault(true);
142   myButtonOk->setDefault(true);
143   QPushButton* buttonCancel = new QPushButton (tr("&Cancel") , GroupButtons);
144   buttonCancel->setAutoDefault(true);
145   QPushButton* buttonHelp = new QPushButton (tr("&Help") , GroupButtons);
146   buttonHelp->setAutoDefault(true);
147
148   GroupButtonsLayout->addWidget(myButtonOk);
149   GroupButtonsLayout->addSpacing(10);
150   GroupButtonsLayout->addStretch();
151   GroupButtonsLayout->addWidget(buttonCancel);
152   GroupButtonsLayout->addWidget(buttonHelp);
153
154   // Add Tab box and Buttons to the top layout
155   TopLayout->addWidget(myTabBox);
156   TopLayout->addWidget(GroupButtons);
157
158   // signals and slots connections
159   connect(myButtonOk,   SIGNAL(clicked()), this, SLOT(accept()));
160   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
161   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(onHelp()));
162   connect(myFieldsCombo,     SIGNAL(activated(int)), this, SLOT(onFieldChanged(int)));
163   connect(myTimeStampsCombo, SIGNAL(activated(int)), this, SLOT(onTimeStampChanged(int)));
164 }
165
166 VisuGUI_DeformedShapeAndScalarMapDlg::~VisuGUI_DeformedShapeAndScalarMapDlg()
167 {
168 }
169
170 void VisuGUI_DeformedShapeAndScalarMapDlg::initFromPrsObject( VISU::ColoredPrs3d_i* thePrs,
171                                                               bool theInit )
172 {
173   if( theInit )
174     myPrsCopy = VISU::TSameAsFactory<VISU::TDEFORMEDSHAPEANDSCALARMAP>().Create(thePrs, VISU::ColoredPrs3d_i::EDoNotPublish);
175   setFactor(myPrsCopy->GetScale());
176   myTimeStampsCombo->setDisabled(myIsAnimation);
177
178   CORBA::String_var aFieldName(myPrsCopy->GetScalarFieldName());
179   QString aIteration = GetFloatValueOfTimeStamp(myPrsCopy->GetScalarEntity(),
180                                                 aFieldName.in(),
181                                                 myPrsCopy->GetScalarTimeStampNumber());
182   if (myEntity2Fields.size() == 0)
183   {
184     // find all fields and time stamps on it
185     _PTR(Study) aActiveStudy = VISU::GetCStudy(VISU::GetAppStudy(myVisuGUI));
186     LightApp_SelectionMgr* aSel = VISU::GetSelectionMgr(myVisuGUI);
187     SALOME_ListIO selected;
188     aSel->selectedObjects(selected);
189     if (selected.Extent() > 0) {
190       Handle(SALOME_InteractiveObject) aIO = selected.First();
191       if (aIO->hasEntry()) {
192         _PTR(SObject) aSObject = aActiveStudy->FindObjectID(aIO->getEntry());
193         VISU::VISUType aType = VISU::Storable::SObject2Type( aSObject );
194         switch(aType){
195         case VISU::TTIMESTAMP: {
196           aSObject = aSObject->GetFather();
197           aSObject = aSObject->GetFather();
198           break;
199         }
200         case VISU::TFIELD: {
201           _PTR(SObject) newSObject;
202           if(aSObject->ReferencedObject(newSObject)) aSObject = newSObject;
203           aSObject = aSObject->GetFather();
204           break;
205         }
206         case VISU::TANIMATION: {
207           _PTR(ChildIterator) aTmpIter = aActiveStudy->NewChildIterator(aSObject);
208           for (aTmpIter->InitEx(true); aTmpIter->More(); aTmpIter->Next()) {
209             _PTR(SObject) aTmpChildSObj = aTmpIter->Value();
210             _PTR(SObject) newSObject;
211             if(aTmpChildSObj->ReferencedObject(newSObject)){
212               aSObject = newSObject;
213               aSObject->GetFather();
214               break;
215             }
216           }
217           break;
218         }}
219         
220         aSObject = aSObject->GetFather();
221         aSObject = aSObject->GetFather();
222
223         mySelectionObj = aSObject;
224         CORBA::Object_var anObject = VISU::ClientSObjectToObject(mySelectionObj);
225         if (CORBA::is_nil(anObject)) {
226           mySelectionObj = mySelectionObj->GetFather();
227         }
228       }
229     }
230
231     if (mySelectionObj) {
232       _PTR(ChildIterator) aIter = aActiveStudy->NewChildIterator(mySelectionObj);
233
234       for (aIter->InitEx(true); aIter->More(); aIter->Next()) {
235         _PTR(SObject) aChildSObj = aIter->Value();
236         VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aChildSObj);
237         if (!aRestoringMap.empty()) {
238           VISU::VISUType aType = VISU::Storable::RestoringMap2Type(aRestoringMap);
239           if (aType == VISU::TTIMESTAMP) {
240             QString aMeshName = aRestoringMap["myMeshName"];
241             CORBA::String_var aName = myPrsCopy->GetMeshName();
242             if (aMeshName != aName.in())
243               continue;
244             QString aFieldName = aRestoringMap["myFieldName"];
245             QString aTimeIter  = aRestoringMap["myTimeStampId"];
246             QString aEntity    = aRestoringMap["myEntityId"];
247             VISU::Entity anEntity;
248             switch (aEntity.toInt()) {
249             case 0: anEntity = VISU::NODE; break;
250             case 1: anEntity = VISU::EDGE; break;
251             case 2: anEntity = VISU::FACE; break;
252             case 3: anEntity = VISU::CELL; break;
253             }
254             TFieldName2TimeStamps& aFieldName2TimeStamps = myEntity2Fields[anEntity];
255             TTimeStampNumber2Time& aTimeStampNumber2Time = aFieldName2TimeStamps[aFieldName];
256             aTimeStampNumber2Time[aTimeIter.toInt()] = 
257               GetFloatValueOfTimeStamp(anEntity,
258                                        aFieldName.toLatin1().constData(),
259                                        aTimeIter.toInt());
260           }
261         }
262       }
263     }
264     AddAllFieldNames();
265   }
266   int idx = myFieldsCombo->findText(aFieldName.in());
267   if (idx >= 0)
268     myFieldsCombo->setCurrentIndex(idx);
269   else {
270     myFieldsCombo->addItem(aFieldName.in());
271     myFieldsCombo->setCurrentIndex(myFieldsCombo->count()-1);
272   }
273   AddAllTimes(myFieldsCombo->currentText());
274   idx = myTimeStampsCombo->findText(aIteration);
275   if (idx >= 0)
276     myTimeStampsCombo->setCurrentIndex(idx);
277   else {
278     myTimeStampsCombo->addItem(aIteration);
279     myTimeStampsCombo->setCurrentIndex(myTimeStampsCombo->count()-1);
280   }
281   SetScalarField( false );
282
283   VisuGUI_ScalarBarBaseDlg::initFromPrsObject(myPrsCopy, theInit);
284
285   if( !theInit )
286     return;
287
288   myInputPane->initFromPrsObject( myPrsCopy );
289   myTabBox->setCurrentIndex( 0 );
290 }
291
292 double VisuGUI_DeformedShapeAndScalarMapDlg::getFactor() const
293 {
294   return ScalFact->value();
295 }
296
297 void VisuGUI_DeformedShapeAndScalarMapDlg::setFactor(double theFactor)
298 {
299   double step = 0.1;
300   if (fabs(theFactor) > std::numeric_limits<double>::epsilon()) {
301     int degree = int(log10(fabs(theFactor))) - 1;
302     if (fabs(theFactor) < 1) {
303       // as logarithm value is negative in this case
304       // and it is truncated to the bigger integer
305       degree -= 1;
306     }
307     step = pow(10., double(degree));
308   }
309
310   ScalFact->setSingleStep(step);
311   ScalFact->setValue(theFactor);
312 }
313
314 int
315 VisuGUI_DeformedShapeAndScalarMapDlg
316 ::storeToPrsObject(VISU::ColoredPrs3d_i* thePrs)
317 {
318   if(!myInputPane->check() || !GetScalarPane()->check())
319     return 0;
320   
321   int anIsOk = myInputPane->storeToPrsObject( myPrsCopy );
322   anIsOk &= GetScalarPane()->storeToPrsObject( myPrsCopy );
323   
324   myPrsCopy->SetScale(getFactor());
325
326   myPrsCopy->SetScalarField(myPrsCopy->GetScalarEntity(),
327                             getCurrentScalarFieldName().toLatin1().constData(),
328                             myTimeStampID[ myTimeStampsCombo->currentIndex() ]);
329
330   if(myUpdateScalars) 
331     SetScalarField( false );
332
333   VISU::TSameAsFactory<VISU::TDEFORMEDSHAPEANDSCALARMAP>().Copy(myPrsCopy, thePrs);
334
335   return anIsOk;
336 }
337
338 int VisuGUI_DeformedShapeAndScalarMapDlg::getCurrentScalarFieldNamePos(){
339   return myFieldsCombo->currentIndex();
340 }
341
342 QString VisuGUI_DeformedShapeAndScalarMapDlg::getCurrentScalarFieldName(){
343   return myFieldsCombo->currentText();
344 }
345
346 int VisuGUI_DeformedShapeAndScalarMapDlg::getCurrentScalarNbIterations(){
347   return myTimeStampsCombo->count();
348 }
349
350 VISU::Entity
351 VisuGUI_DeformedShapeAndScalarMapDlg
352 ::getCurrentScalarEntity()
353 {
354   VISU::Entity anEntity = VISU::Entity(-1);
355   TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
356   for(; anIter != myEntity2Fields.end(); anIter++){
357     const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
358     TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
359     for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
360       const QString& aFieldName = aFieldIter->first;
361       if (aFieldName == myFieldsCombo->currentText()) {
362         anEntity = anIter->first;
363         break;
364       }
365     }
366   }
367   return anEntity;
368 }
369
370 void VisuGUI_DeformedShapeAndScalarMapDlg::SetScalarField( const bool save_scalar_pane ){
371   SetScalarField( myTimeStampID[ myTimeStampsCombo->currentIndex() ], "", save_scalar_pane );
372 }
373
374 void
375 VisuGUI_DeformedShapeAndScalarMapDlg
376 ::SetScalarField(int theIter,
377                  QString theFieldName, 
378                  const bool save_scalar_pane )
379 {
380   QApplication::setOverrideCursor(Qt::WaitCursor);
381
382   if( save_scalar_pane && IsScalarFieldValid() )
383     GetScalarPane()->storeToPrsObject(myPrsCopy);
384
385   QString aFieldName;
386
387   if(theFieldName.isEmpty())
388     aFieldName = myFieldsCombo->currentText();
389   else
390     aFieldName = theFieldName;
391
392   VISU::Entity anEntity = getCurrentScalarEntity();
393   
394   try {
395     myPrsCopy->SetScalarField(anEntity,
396                               aFieldName.toLatin1().constData(),
397                               theIter);
398     if( save_scalar_pane )
399       UpdateScalarField();
400     myIsScalarFieldValid = true;
401   } catch( std::exception& exc ) {
402     INFOS( exc.what() );
403     SUIT_MessageBox::warning( this, tr( "WRN_WARNING" ), tr( "ERR_SCALAR_DATA_INCONSISTENT" ) );
404     myIsScalarFieldValid = false;
405   }
406   updateControls();
407
408   QApplication::restoreOverrideCursor();
409 }
410
411 void VisuGUI_DeformedShapeAndScalarMapDlg::accept()
412 {
413   VisuGUI_ScalarBarBaseDlg::accept();
414 }
415
416 void VisuGUI_DeformedShapeAndScalarMapDlg::reject()
417 {
418   VisuGUI_ScalarBarBaseDlg::reject();
419 }
420
421 QString VisuGUI_DeformedShapeAndScalarMapDlg::GetContextHelpFilePath()
422 {
423   return "scalar_map_on_deformed_shape_page.html";
424 }
425
426 void VisuGUI_DeformedShapeAndScalarMapDlg::AddAllFieldNames(){
427   TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
428   for(; anIter != myEntity2Fields.end(); anIter++){
429     const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
430     TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
431     for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
432       const QString& aFieldName = aFieldIter->first;
433       myFieldsCombo->addItem(aFieldName);
434     }
435   }
436 }
437
438 void VisuGUI_DeformedShapeAndScalarMapDlg::AddAllTimes(const QString& theFieldName){
439   TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
440   int currentTimeStampId = (myTimeStampsCombo->count() > 0) ? myTimeStampsCombo->currentIndex() : 0;
441   for(; anIter != myEntity2Fields.end(); anIter++){
442     const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
443     TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
444     for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
445       const QString& aFieldName = aFieldIter->first;
446       if(theFieldName != aFieldName) 
447         continue;
448
449       myTimeStampID.clear();
450       myTimeStampsCombo->clear();
451
452       const TTimeStampNumber2Time& aTimeStampNumber2Time = aFieldIter->second;
453       TTimeStampNumber2Time::const_iterator aTimeStampIter = aTimeStampNumber2Time.begin();
454       for(; aTimeStampIter != aTimeStampNumber2Time.end(); aTimeStampIter++){
455         int aTimeStampNumber = aTimeStampIter->first;
456         myTimeStampID.push_back(aTimeStampNumber);
457
458         QString aTimeStampTime = aTimeStampIter->second;
459         myTimeStampsCombo->addItem(aTimeStampTime);
460       }
461       if(currentTimeStampId >= myTimeStampID.size())
462         currentTimeStampId = myTimeStampID.size()-1;
463       myTimeStampsCombo->setCurrentIndex(currentTimeStampId);
464       return;
465     }
466   }
467 }
468
469 void VisuGUI_DeformedShapeAndScalarMapDlg::onFieldChanged(int){
470   AddAllTimes(myFieldsCombo->currentText());
471   SetScalarField();
472   //UpdateScalarField(); // it is called from SetScalarField() method
473 }
474
475 void VisuGUI_DeformedShapeAndScalarMapDlg::onTimeStampChanged(int){
476   SetScalarField();
477   //UpdateScalarField(); // it is called from SetScalarField() method
478 }
479
480 void VisuGUI_DeformedShapeAndScalarMapDlg::UpdateScalarField(){
481   GetScalarPane()->initFromPrsObject(myPrsCopy);
482 }
483
484 QString 
485 VisuGUI_DeformedShapeAndScalarMapDlg
486 ::GetFloatValueOfTimeStamp(VISU::Entity theEntity,
487                            const std::string& theFieldName,
488                            int theTimeStampNumber)
489 {
490   QString aTime("");
491   VISU::TEntity anEntity = VISU::TEntity(theEntity);
492   VISU::Result_i* theResult = myPrsCopy->GetCResult();
493   VISU::Result_i::PInput anInput = theResult->GetInput();
494   VISU::PField aField = anInput->GetField(myPrsCopy->GetCMeshName(),
495                                           anEntity,
496                                           theFieldName);
497   if(!aField) 
498     return aTime;
499
500   VISU::TValField& aValField = aField->myValField;
501   VISU::TValField::const_iterator aIter = aValField.find(theTimeStampNumber);
502   if(aIter != aValField.end()){
503     VISU::PValForTime aValForTime = aIter->second;
504     aTime = VISU_Convertor::GenerateName(aValForTime->myTime).c_str();
505   }
506   return aTime;
507 }
508
509 void VisuGUI_DeformedShapeAndScalarMapDlg::updateControls()
510 {
511   bool isScalarFieldValid = IsScalarFieldValid();
512   myTabBox->setTabEnabled( 1, isScalarFieldValid ); // "Scalar Bar" tab
513   myTabBox->setTabEnabled( 2, isScalarFieldValid ); // "Input" tab
514   myTimeStampsCombo->setEnabled( isScalarFieldValid );
515   myButtonOk->setEnabled( isScalarFieldValid );
516 }