Salome HOME
98dcd686ccb6c1fad297a587dd8f1eb4f559bd26
[modules/visu.git] / src / VISUGUI / VisuGUI_ScalarMapOnDeformedShapeDlg.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : VisuGUI_ScalarMapOnDeformedShapeDlg.cxx
25 //  Author : Eugeny Nikolaev
26 //  Module : VISU
27
28 #include "VisuGUI_ScalarMapOnDeformedShapeDlg.h"
29
30 #include "VisuGUI_Tools.h"
31 #include "VisuGUI_InputPane.h"
32
33 #include "VISU_Result_i.hh"
34 #include "VISU_ScalarMapOnDeformedShape_i.hh"
35 #include "VISU_ColoredPrs3dFactory.hh"
36
37 #include "VISU_ScalarMapPL.hxx"
38 #include "VISU_ScalarMapOnDeformedShapePL.hxx"
39
40 #include "VISU_Convertor.hxx"
41
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 <qlayout.h>
56 #include <qtabwidget.h>
57
58 using namespace std;
59
60 #define MYDEBUG 0
61
62 /*!
63  * Constructor
64  */
65 VisuGUI_ScalarMapOnDeformedShapeDlg::VisuGUI_ScalarMapOnDeformedShapeDlg (SalomeApp_Module* theModule)
66   : VisuGUI_ScalarBarBaseDlg(theModule),
67     myIsAnimation(false),
68     myUpdateScalars(true),
69     myVisuGUI(theModule)
70 {
71   setCaption(tr("DLG_TITLE"));
72   setSizeGripEnabled(TRUE);
73
74   QVBoxLayout* TopLayout = new QVBoxLayout (this);
75   TopLayout->setSpacing(6);
76   TopLayout->setMargin(11);
77
78   myTabBox = new QTabWidget (this);
79
80   // Scalar Map on Deformed shape pane
81   QVBox* aBox = new QVBox (this);
82   aBox->setMargin(11);
83   QFrame* TopGroup = new QFrame (aBox, "TopGroup");
84   TopGroup->setFrameStyle(QFrame::Box | QFrame::Sunken);
85   TopGroup->setLineWidth(1);
86   QGridLayout* TopGroupLayout = new QGridLayout (TopGroup);
87   TopGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
88   TopGroupLayout->setSpacing(6);
89   TopGroupLayout->setMargin(11);
90
91   //   Scale factor
92   QLabel* ScaleLabel = new QLabel (tr("SCALE_FACTOR"), TopGroup, "ScaleLabel");
93   TopGroupLayout->addWidget(ScaleLabel, 0, 0);
94
95   ScalFact = new QtxDblSpinBox (0.0, 1.0E+38, 0.1, TopGroup);
96   ScalFact->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
97   ScalFact->setPrecision(38);
98   ScalFact->setValue(0.1);
99   TopGroupLayout->addWidget(ScalFact, 0, 1);
100
101   // Fields combo box
102   QLabel* FieldLabel = new QLabel (tr("FIELD_ITEM"), TopGroup, "FieldLabel");
103   myFieldsCombo = new QComboBox (TopGroup,"Fields");
104
105   TopGroupLayout->addWidget(FieldLabel, 1, 0);
106   TopGroupLayout->addWidget(myFieldsCombo,1,1);
107
108   // TimeStamps combo box
109   QLabel* TimeStampLabel = new QLabel (tr("TIMESTAMP_ITEM"), TopGroup, "TimeStampLabel");
110   myTimeStampsCombo = new QComboBox (TopGroup,"TimeStamp");
111
112   TopGroupLayout->addWidget(TimeStampLabel, 2, 0);
113   TopGroupLayout->addWidget(myTimeStampsCombo,2,1);
114
115   //
116   myTabBox->addTab(aBox, tr("SCALAR_MAP_ON_DEFORMED_SHAPE_TAB"));
117
118   // Scalar bar pane
119   myInputPane = new VisuGUI_InputPane(VISU::TSCALARMAPONDEFORMEDSHAPE, theModule, this);
120
121   myTabBox->addTab(GetScalarPane(), tr("SCALAR_BAR_TAB"));
122   myTabBox->addTab(myInputPane, tr("INPUT_TAB"));
123
124   // Buttons
125   QGroupBox* GroupButtons = new QGroupBox (this, "GroupButtons");
126   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
127   GroupButtons->setColumnLayout(0, Qt::Vertical);
128   GroupButtons->layout()->setSpacing(0);
129   GroupButtons->layout()->setMargin(0);
130   QGridLayout* GroupButtonsLayout = new QGridLayout (GroupButtons->layout());
131   GroupButtonsLayout->setAlignment(Qt::AlignTop);
132   GroupButtonsLayout->setSpacing(6);
133   GroupButtonsLayout->setMargin(11);
134
135   QPushButton* buttonOk = new QPushButton (tr("&OK"), GroupButtons, "buttonOk");
136   buttonOk->setAutoDefault(TRUE);
137   buttonOk->setDefault(TRUE);
138   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
139
140   GroupButtonsLayout->addItem(new QSpacerItem (5, 5, QSizePolicy::Expanding,
141                                                QSizePolicy::Minimum), 0, 1);
142
143   QPushButton* buttonCancel = new QPushButton (tr("&Cancel") , GroupButtons, "buttonCancel");
144   buttonCancel->setAutoDefault(TRUE);
145   GroupButtonsLayout->addWidget(buttonCancel, 0, 2);
146
147   QPushButton* buttonHelp = new QPushButton (tr("&Help") , GroupButtons, "buttonHelp");
148   buttonHelp->setAutoDefault(TRUE);
149   GroupButtonsLayout->addWidget(buttonHelp, 0, 3);
150
151   // Add Tab box and Buttons to the top layout
152   TopLayout->addWidget(myTabBox);
153   TopLayout->addWidget(GroupButtons);
154
155   // signals and slots connections
156   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(accept()));
157   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
158   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(onHelp()));
159   connect(myFieldsCombo,     SIGNAL(activated(int)), this, SLOT(onFieldChanged(int)));
160   connect(myTimeStampsCombo, SIGNAL(activated(int)), this, SLOT(onTimeStampChanged(int)));
161 }
162
163 VisuGUI_ScalarMapOnDeformedShapeDlg::~VisuGUI_ScalarMapOnDeformedShapeDlg()
164 {}
165
166 void VisuGUI_ScalarMapOnDeformedShapeDlg::initFromPrsObject( VISU::ColoredPrs3d_i* thePrs,
167                                                              bool theInit )
168 {
169   if( theInit )
170     myPrsCopy = VISU::TSameAsFactory<VISU::TSCALARMAPONDEFORMEDSHAPE>().Create(thePrs, VISU::ColoredPrs3d_i::EDoNotPublish);
171   setFactor(myPrsCopy->GetScale());
172   myTimeStampsCombo->setDisabled(myIsAnimation);
173
174   CORBA::String_var aFieldName(myPrsCopy->GetScalarFieldName());
175   QString aIteration = GetFloatValueOfTimeStamp(myPrsCopy->GetScalarEntity(),
176                                                 aFieldName.in(),
177                                                 myPrsCopy->GetScalarTimeStampNumber());
178   if (myEntity2Fields.size() == 0)
179   {
180     // find all fields and time stamps on it
181     _PTR(Study) aActiveStudy = VISU::GetCStudy(VISU::GetAppStudy(myVisuGUI));
182     LightApp_SelectionMgr* aSel = VISU::GetSelectionMgr(myVisuGUI);
183     SALOME_ListIO selected;
184     aSel->selectedObjects(selected);
185     if (selected.Extent() > 0) {
186       Handle(SALOME_InteractiveObject) aIO = selected.First();
187       if (aIO->hasEntry()) {
188         _PTR(SObject) aSObject = aActiveStudy->FindObjectID(aIO->getEntry());
189         VISU::VISUType aType = VISU::Storable::SObject2Type( aSObject );
190         switch(aType){
191         case VISU::TTIMESTAMP: {
192           aSObject = aSObject->GetFather();
193           aSObject = aSObject->GetFather();
194           break;
195         }
196         case VISU::TFIELD: {
197           _PTR(SObject) newSObject;
198           if(aSObject->ReferencedObject(newSObject)) aSObject = newSObject;
199           aSObject = aSObject->GetFather();
200           break;
201         }
202         case VISU::TANIMATION: {
203           _PTR(ChildIterator) aTmpIter = aActiveStudy->NewChildIterator(aSObject);
204           for (aTmpIter->InitEx(true); aTmpIter->More(); aTmpIter->Next()) {
205             _PTR(SObject) aTmpChildSObj = aTmpIter->Value();
206             _PTR(SObject) newSObject;
207             if(aTmpChildSObj->ReferencedObject(newSObject)){
208               aSObject = newSObject;
209               aSObject->GetFather();
210               break;
211             }
212           }
213           break;
214         }}
215         
216         aSObject = aSObject->GetFather();
217         aSObject = aSObject->GetFather();
218
219         mySelectionObj = aSObject;
220         CORBA::Object_var anObject = VISU::ClientSObjectToObject(mySelectionObj);
221         if (CORBA::is_nil(anObject)) {
222           mySelectionObj = mySelectionObj->GetFather();
223         }
224       }
225     }
226
227     if (mySelectionObj) {
228       _PTR(ChildIterator) aIter = aActiveStudy->NewChildIterator(mySelectionObj);
229
230       for (aIter->InitEx(true); aIter->More(); aIter->Next()) {
231         _PTR(SObject) aChildSObj = aIter->Value();
232         VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aChildSObj);
233         if (!aRestoringMap.empty()) {
234           VISU::VISUType aType = VISU::Storable::RestoringMap2Type(aRestoringMap);
235           if (aType == VISU::TTIMESTAMP) {
236             QString aMeshName = aRestoringMap["myMeshName"];
237             CORBA::String_var aName = myPrsCopy->GetMeshName();
238             if (aMeshName != aName.in())
239               continue;
240             QString aFieldName = aRestoringMap["myFieldName"];
241             QString aTimeIter  = aRestoringMap["myTimeStampId"];
242             QString aEntity    = aRestoringMap["myEntityId"];
243             VISU::Entity anEntity;
244             switch (aEntity.toInt()) {
245             case 0: anEntity = VISU::NODE; break;
246             case 1: anEntity = VISU::EDGE; break;
247             case 2: anEntity = VISU::FACE; break;
248             case 3: anEntity = VISU::CELL; break;
249             }
250             TFieldName2TimeStamps& aFieldName2TimeStamps = myEntity2Fields[anEntity];
251             TTimeStampNumber2Time& aTimeStampNumber2Time = aFieldName2TimeStamps[aFieldName];
252             aTimeStampNumber2Time[aTimeIter.toInt()] = 
253               GetFloatValueOfTimeStamp(anEntity,
254                                        aFieldName.latin1(),
255                                        aTimeIter.toInt());
256           }
257         }
258       }
259     }
260     AddAllFieldNames();
261   }
262   myFieldsCombo->setCurrentText(aFieldName.in());
263   AddAllTimes(myFieldsCombo->currentText());
264   myTimeStampsCombo->setCurrentText(aIteration);
265   SetScalarField( false );
266
267   VisuGUI_ScalarBarBaseDlg::initFromPrsObject(myPrsCopy, theInit);
268
269   if( !theInit )
270     return;
271
272   myInputPane->initFromPrsObject( myPrsCopy );
273   myTabBox->setCurrentPage( 0 );
274 }
275
276 int
277 VisuGUI_ScalarMapOnDeformedShapeDlg
278 ::storeToPrsObject(VISU::ColoredPrs3d_i* thePrs)
279 {
280   if(!myInputPane->check() || !GetScalarPane()->check())
281     return 0;
282   
283   int anIsOk = myInputPane->storeToPrsObject( myPrsCopy );
284   anIsOk &= GetScalarPane()->storeToPrsObject( myPrsCopy );
285   
286   myPrsCopy->SetScale(getFactor());
287
288   myPrsCopy->SetScalarField(myPrsCopy->GetScalarEntity(),
289                             getCurrentScalarFieldName().latin1(),
290                             myTimeStampID[ myTimeStampsCombo->currentItem() ]);
291
292   if(myUpdateScalars) 
293     SetScalarField( false );
294
295   VISU::TSameAsFactory<VISU::TSCALARMAPONDEFORMEDSHAPE>().Copy(myPrsCopy, thePrs);
296
297   return anIsOk;
298 }
299
300 int VisuGUI_ScalarMapOnDeformedShapeDlg::getCurrentScalarFieldNamePos(){
301   return myFieldsCombo->currentItem();
302 }
303
304 QString VisuGUI_ScalarMapOnDeformedShapeDlg::getCurrentScalarFieldName(){
305   return myFieldsCombo->currentText();
306 }
307
308 int VisuGUI_ScalarMapOnDeformedShapeDlg::getCurrentScalarNbIterations(){
309   return myTimeStampsCombo->count();
310 }
311
312 VISU::Entity
313 VisuGUI_ScalarMapOnDeformedShapeDlg
314 ::getCurrentScalarEntity()
315 {
316   VISU::Entity anEntity = VISU::Entity(-1);
317   TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
318   for(; anIter != myEntity2Fields.end(); anIter++){
319     const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
320     TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
321     for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
322       const QString& aFieldName = aFieldIter->first;
323       if (aFieldName == myFieldsCombo->currentText()) {
324         anEntity = anIter->first;
325         break;
326       }
327     }
328   }
329   return anEntity;
330 }
331
332 void VisuGUI_ScalarMapOnDeformedShapeDlg::SetScalarField( const bool save_scalar_pane ){
333   SetScalarField( myTimeStampID[ myTimeStampsCombo->currentItem() ], "", save_scalar_pane );
334 }
335
336 void
337 VisuGUI_ScalarMapOnDeformedShapeDlg
338 ::SetScalarField(int theIter,
339                  QString theFieldName, 
340                  const bool save_scalar_pane )
341 {
342   QApplication::setOverrideCursor(Qt::waitCursor);
343
344   if( save_scalar_pane )
345     GetScalarPane()->storeToPrsObject(myPrsCopy);
346
347   QString aFieldName;
348
349   if(theFieldName.isEmpty())
350     aFieldName = myFieldsCombo->currentText();
351   else
352     aFieldName = theFieldName;
353
354   VISU::Entity anEntity = getCurrentScalarEntity();
355   
356   myPrsCopy->SetScalarField(anEntity,
357                             aFieldName.latin1(),
358                             theIter);
359   
360   if( save_scalar_pane )
361     UpdateScalarField();
362
363   QApplication::restoreOverrideCursor();
364 }
365
366 void VisuGUI_ScalarMapOnDeformedShapeDlg::accept()
367 {
368   VisuGUI_ScalarBarBaseDlg::accept();
369 }
370
371 void VisuGUI_ScalarMapOnDeformedShapeDlg::reject()
372 {
373   VisuGUI_ScalarBarBaseDlg::reject();
374 }
375
376 QString VisuGUI_ScalarMapOnDeformedShapeDlg::GetContextHelpFilePath()
377 {
378   return "scalar_map_on_deformed_shape_page.html";
379 }
380
381 void VisuGUI_ScalarMapOnDeformedShapeDlg::AddAllFieldNames(){
382   TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
383   for(; anIter != myEntity2Fields.end(); anIter++){
384     const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
385     TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
386     for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
387       const QString& aFieldName = aFieldIter->first;
388       myFieldsCombo->insertItem(aFieldName);
389     }
390   }
391 }
392
393 void VisuGUI_ScalarMapOnDeformedShapeDlg::AddAllTimes(const QString& theFieldName){
394   TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
395   for(; anIter != myEntity2Fields.end(); anIter++){
396     const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
397     TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
398     for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
399       const QString& aFieldName = aFieldIter->first;
400       if(theFieldName != aFieldName) 
401         continue;
402
403       myTimeStampID.clear();
404       myTimeStampsCombo->clear();
405
406       const TTimeStampNumber2Time& aTimeStampNumber2Time = aFieldIter->second;
407       TTimeStampNumber2Time::const_iterator aTimeStampIter = aTimeStampNumber2Time.begin();
408       for(; aTimeStampIter != aTimeStampNumber2Time.end(); aTimeStampIter++){
409         int aTimeStampNumber = aTimeStampIter->first;
410         myTimeStampID.push_back(aTimeStampNumber);
411
412         QString aTimeStampTime = aTimeStampIter->second;
413         myTimeStampsCombo->insertItem(aTimeStampTime);
414       }
415       return;
416     }
417   }
418 }
419
420 void VisuGUI_ScalarMapOnDeformedShapeDlg::onFieldChanged(int){
421   AddAllTimes(myFieldsCombo->currentText());
422   SetScalarField();
423   UpdateScalarField();
424 }
425
426 void VisuGUI_ScalarMapOnDeformedShapeDlg::onTimeStampChanged(int){
427   SetScalarField();
428   UpdateScalarField();
429 }
430
431 void VisuGUI_ScalarMapOnDeformedShapeDlg::UpdateScalarField(){
432   GetScalarPane()->initFromPrsObject(myPrsCopy);
433 }
434
435 QString 
436 VisuGUI_ScalarMapOnDeformedShapeDlg
437 ::GetFloatValueOfTimeStamp(VISU::Entity theEntity,
438                            const std::string& theFieldName,
439                            int theTimeStampNumber)
440 {
441   QString aTime("");
442   VISU::TEntity anEntity = VISU::TEntity(theEntity);
443   VISU::Result_i* theResult = myPrsCopy->GetCResult();
444   VISU::Result_i::PInput anInput = theResult->GetInput();
445   VISU::PField aField = anInput->GetField(myPrsCopy->GetCMeshName(),
446                                           anEntity,
447                                           theFieldName);
448   if(!aField) 
449     return aTime;
450
451   VISU::TValField& aValField = aField->myValField;
452   VISU::TValField::const_iterator aIter = aValField.find(theTimeStampNumber);
453   if(aIter != aValField.end()){
454     VISU::PValForTime aValForTime = aIter->second;
455     aTime = VISU_Convertor::GenerateName(aValForTime->myTime).c_str();
456   }
457   return aTime;
458 }