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