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