Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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
32 #include "VISU_Result_i.hh"
33 #include "VISU_ScalarMapOnDeformedShape_i.hh"
34
35 #include "VISU_ScalarMapPL.hxx"
36 #include "VISU_ScalarMapOnDeformedShapePL.hxx"
37
38 #include "VISU_Convertor.hxx"
39
40 #include "SalomeApp_Module.h"
41 #include "LightApp_SelectionMgr.h"
42 #include "SUIT_Desktop.h"
43
44 #include "SALOME_ListIO.hxx"
45
46 #include "SALOMEDSClient_AttributeComment.hxx"
47 #include "SALOMEDSClient_AttributeName.hxx"
48
49 #include <qlayout.h>
50 #include <qtabwidget.h>
51
52 using namespace std;
53
54 #define MYDEBUG 0
55
56 /*!
57  * Constructor
58  */
59 VisuGUI_ScalarMapOnDeformedShapeDlg::VisuGUI_ScalarMapOnDeformedShapeDlg (SalomeApp_Module* theModule)
60     : QDialog(VISU::GetDesktop(theModule), "VisuGUI_ScalarMapOnDeformedShapeDlg", false,
61               WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
62       myIsAnimation(false),
63       myVisuGUI(theModule)
64 {
65   myPrs = NULL;
66   setCaption(tr("DLG_TITLE"));
67   setSizeGripEnabled(TRUE);
68
69   QVBoxLayout* TopLayout = new QVBoxLayout (this);
70   TopLayout->setSpacing(6);
71   TopLayout->setMargin(11);
72
73   QTabWidget* aTabBox = new QTabWidget (this);
74
75   // Scalar Map on Deformed shape pane
76   QVBox* aBox = new QVBox (this);
77   aBox->setMargin(11);
78   QFrame* TopGroup = new QFrame (aBox, "TopGroup");
79   TopGroup->setFrameStyle(QFrame::Box | QFrame::Sunken);
80   TopGroup->setLineWidth(1);
81   QGridLayout* TopGroupLayout = new QGridLayout (TopGroup);
82   TopGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
83   TopGroupLayout->setSpacing(6);
84   TopGroupLayout->setMargin(11);
85
86   //   Scale factor
87   QLabel* ScaleLabel = new QLabel (tr("SCALE_FACTOR"), TopGroup, "ScaleLabel");
88   TopGroupLayout->addWidget(ScaleLabel, 0, 0);
89
90   ScalFact = new QtxDblSpinBox (0.0, 1.0E+38, 0.1, TopGroup);
91   ScalFact->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
92   ScalFact->setPrecision(5);
93   ScalFact->setValue(0.1);
94   TopGroupLayout->addWidget(ScalFact, 0, 1);
95
96   // Fields combo box
97   QLabel* FieldLabel = new QLabel (tr("FIELD_ITEM"), TopGroup, "FieldLabel");
98   myFieldsCombo = new QComboBox (TopGroup,"Fields");
99
100   TopGroupLayout->addWidget(FieldLabel, 1, 0);
101   TopGroupLayout->addWidget(myFieldsCombo,1,1);
102
103   // TimeStamps combo box
104   QLabel* TimeStampLabel = new QLabel (tr("TIMESTAMP_ITEM"), TopGroup, "TimeStampLabel");
105   myTimeStampsCombo = new QComboBox (TopGroup,"TimeStamp");
106
107   TopGroupLayout->addWidget(TimeStampLabel, 2, 0);
108   TopGroupLayout->addWidget(myTimeStampsCombo,2,1);
109
110   //
111   aTabBox->addTab(aBox, tr("SCALAR_MAP_ON_DEFORMED_SHAPE_TAB"));
112
113   // Scalar bar pane
114   myScalarPane = new VisuGUI_ScalarBarPane (this, false);
115   myScalarPane->setMargin(5);
116
117   aTabBox->addTab(myScalarPane, tr("SCALAR_BAR_TAB"));
118
119   // Buttons
120   QGroupBox* GroupButtons = new QGroupBox (this, "GroupButtons");
121   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
122   GroupButtons->setColumnLayout(0, Qt::Vertical);
123   GroupButtons->layout()->setSpacing(0);
124   GroupButtons->layout()->setMargin(0);
125   QGridLayout* GroupButtonsLayout = new QGridLayout (GroupButtons->layout());
126   GroupButtonsLayout->setAlignment(Qt::AlignTop);
127   GroupButtonsLayout->setSpacing(6);
128   GroupButtonsLayout->setMargin(11);
129
130   QPushButton* buttonOk = new QPushButton (tr("&OK"), GroupButtons, "buttonOk");
131   buttonOk->setAutoDefault(TRUE);
132   buttonOk->setDefault(TRUE);
133   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
134
135   GroupButtonsLayout->addItem(new QSpacerItem (5, 5, QSizePolicy::Expanding,
136                                                QSizePolicy::Minimum), 0, 1);
137
138   QPushButton* buttonCancel = new QPushButton (tr("&Cancel") , GroupButtons, "buttonCancel");
139   buttonCancel->setAutoDefault(TRUE);
140   GroupButtonsLayout->addWidget(buttonCancel, 0, 2);
141
142   // Add Tab box and Buttons to the top layout
143   TopLayout->addWidget(aTabBox);
144   TopLayout->addWidget(GroupButtons);
145
146   // signals and slots connections
147   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(accept()));
148   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
149   connect(myFieldsCombo,     SIGNAL(activated(int)), this, SLOT(onFieldChanged(int)));
150   connect(myTimeStampsCombo, SIGNAL(activated(int)), this, SLOT(onTimeStampChanged(int)));
151 }
152
153 void VisuGUI_ScalarMapOnDeformedShapeDlg::initFromPrsObject
154              (VISU::ScalarMapOnDeformedShape_i* thePrs, bool isAnim)
155 {
156   myIsAnimation = isAnim;
157   myPrs = thePrs;
158
159   setFactor(myPrs->GetScale());
160   myTimeStampsCombo->setDisabled(isAnim);
161
162   QString aFieldName(myPrs->GetScalarFieldName());
163   float aIterFloat = GetFloatValueOfTimeStamp(myPrs->GetMeshName().c_str(),
164                                               myPrs->GetScalarFieldName().c_str(),
165                                               myPrs->GetScalarIteration(),
166                                               myPrs->GetScalarEntity());
167   QString aIteration(QString("%1").arg(aIterFloat));
168
169   if (myMeshFieldsTimes.size() == 0)
170   {
171     // find all fields and time stamps on it
172     _PTR(Study) aActiveStudy = VISU::GetCStudy(VISU::GetAppStudy(myVisuGUI));
173     LightApp_SelectionMgr* aSel = VISU::GetSelectionMgr(myVisuGUI);
174     SALOME_ListIO selected;
175     aSel->selectedObjects(selected);
176     if (selected.Extent() > 0) {
177       Handle(SALOME_InteractiveObject) aIO = selected.First();
178       if (aIO->hasEntry()) {
179         _PTR(SObject) aSObject = aActiveStudy->FindObjectID(aIO->getEntry());
180
181         _PTR(GenericAttribute) anTmpAttr;
182         if (aSObject->FindAttribute(anTmpAttr, "AttributeComment")) {
183           _PTR(AttributeComment) aTmpComment (anTmpAttr);
184           string aTmpComm = aTmpComment->Value();
185           QString aTmpstrIn (aTmpComm.c_str());
186           VISU::Storable::TRestoringMap aTmpaMap;
187           VISU::Storable::StrToMap(aTmpstrIn,aTmpaMap);
188           bool aTmpisExist;
189           VISU::VISUType aTmpType =
190             (VISU::VISUType)VISU::Storable::FindValue(aTmpaMap,"myType",&aTmpisExist).toInt();
191           switch(aTmpType){
192           case VISU::TTIMESTAMP:
193             {
194               aSObject = aSObject->GetFather();
195               aSObject = aSObject->GetFather();
196               break;
197             }
198           case VISU::TFIELD:
199             {
200               _PTR(SObject) newSObject;
201               if(aSObject->ReferencedObject(newSObject)) aSObject = newSObject;
202               aSObject = aSObject->GetFather();
203               break;
204             }
205           case VISU::TANIMATION:
206             {
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         }
221
222         aSObject = aSObject->GetFather();
223         aSObject = aSObject->GetFather();
224
225         mySelectionObj = aSObject;
226         CORBA::Object_var anObject = VISU::ClientSObjectToObject(mySelectionObj);
227         if (CORBA::is_nil(anObject)) {
228           mySelectionObj = mySelectionObj->GetFather();
229         }
230       }
231     }
232
233     if (mySelectionObj) {
234       _PTR(ChildIterator) aIter = aActiveStudy->NewChildIterator(mySelectionObj);
235
236       for (aIter->InitEx(true); aIter->More(); aIter->Next()) {
237         _PTR(SObject) aChildSObj = aIter->Value();
238         CORBA::Object_var aChildObject = VISU::ClientSObjectToObject(aChildSObj);
239
240         _PTR(GenericAttribute) anAttr;
241         if (aChildSObj->FindAttribute(anAttr, "AttributeComment")) {
242           _PTR(AttributeComment) aComment (anAttr);
243           string aComm = aComment->Value();
244           QString strIn (aComm.c_str());
245           VISU::Storable::TRestoringMap aMap;
246           VISU::Storable::StrToMap(strIn,aMap);
247           bool isExist;
248           VISU::VISUType aType = (VISU::VISUType)VISU::Storable::FindValue(aMap,"myType",&isExist).toInt();
249           if (isExist) {
250             if ((aType == VISU::TTIMESTAMP)) {
251               _PTR(GenericAttribute) aNameAttr;
252               if (aChildSObj->FindAttribute(aNameAttr, "AttributeName")) {
253                 _PTR(AttributeName) aName (aNameAttr);
254                 VISU::Prs3d_var aPrsObj = VISU::Prs3d::_narrow(aChildObject);
255                 QString aMeshName = VISU::getValue(aChildSObj, "myMeshName");
256                 if (QString(myPrs->GetMeshName().c_str()) != aMeshName) continue;
257                 myCurrMeshName = aMeshName;
258                 QString aFieldName = VISU::getValue(aChildSObj, "myFieldName");
259                 QString aTimeIter  = VISU::getValue(aChildSObj, "myTimeStampId");
260                 QString aEntity    = VISU::getValue(aChildSObj, "myEntityId");
261                 VISU::TEntity anEntity;
262                 switch (aEntity.toInt()) {
263                 case 0: anEntity = VISU::NODE_ENTITY; break;
264                 case 1: anEntity = VISU::EDGE_ENTITY; break;
265                 case 2: anEntity = VISU::FACE_ENTITY; break;
266                 case 3: anEntity = VISU::CELL_ENTITY; break;
267                 }
268                 TFT &aFieldsMap = myMeshFieldsTimes[aMeshName.latin1()];
269                 TFE aKey (aFieldName.latin1(), anEntity);
270                 aFieldsMap[aKey][aTimeIter.toInt()] = 
271                   GetFloatValueOfTimeStamp(myPrs->GetMeshName().c_str(),
272                                            aFieldName.latin1(),
273                                            aTimeIter.toInt(),
274                                            anEntity);
275               }
276             }
277           }
278         }
279       }
280     }
281
282     AddAllFieldNames();
283   }
284   myFieldsCombo->setCurrentText(aFieldName);
285   AddAllTimes(myFieldsCombo->currentText());
286   myTimeStampsCombo->setCurrentText(aIteration);
287   SetScalarField();
288
289   myScalarPane->initFromPrsObject(myPrs);
290 }
291
292 int VisuGUI_ScalarMapOnDeformedShapeDlg::storeToPrsObject
293             (VISU::ScalarMapOnDeformedShape_i* thePrs, bool theUpdateScalars, int theIter)
294 {
295   thePrs->SetScale(getFactor());
296
297   if (myIsAnimation)
298     thePrs->SetScalarField(myPrs->GetScalarMeshName().c_str(),
299                            getCurrentScalarFieldName().latin1(),
300                            theIter,
301                            myPrs->GetScalarEntity());
302   else
303     thePrs->SetScalarField(myPrs->GetScalarMeshName().c_str(),
304                            getCurrentScalarFieldName().latin1(),
305                            myTimeStampsCombo->currentItem() + 1,
306                            myPrs->GetScalarEntity());
307
308   if (theUpdateScalars) SetScalarField();
309
310   thePrs->ShowColored(true);
311
312   myScalarPane->storeToPrsObject(thePrs);
313
314   myPrs = thePrs;
315
316   return 1;
317 }
318
319 int VisuGUI_ScalarMapOnDeformedShapeDlg::getCurrentScalarFieldNamePos(){
320   return myFieldsCombo->currentItem();
321 }
322
323 QString VisuGUI_ScalarMapOnDeformedShapeDlg::getCurrentScalarFieldName(){
324   return myFieldsCombo->currentText();
325 }
326
327 int VisuGUI_ScalarMapOnDeformedShapeDlg::getCurrentScalarNbIterations(){
328   return myTimeStampsCombo->count();
329 }
330
331 VISU::TEntity VisuGUI_ScalarMapOnDeformedShapeDlg::getCurrentScalarEntity(){
332   VISU::TEntity anEntity;
333   TFT::const_iterator aIterField = myMeshFieldsTimes[myCurrMeshName].begin();
334   for(;aIterField != myMeshFieldsTimes[myCurrMeshName].end();aIterField++){
335     if (aIterField->first.first == myFieldsCombo->currentText()){
336       anEntity = aIterField->first.second;
337       break;
338     }
339   }
340   return anEntity;
341 }
342
343 void VisuGUI_ScalarMapOnDeformedShapeDlg::SetScalarField(){
344   SetScalarField(myTimeStampsCombo->currentItem()+1);
345 }
346
347 void VisuGUI_ScalarMapOnDeformedShapeDlg::SetScalarField(int theIter,QString theFieldName){
348
349   QString aFieldName;
350
351   if(theFieldName.isEmpty())
352     aFieldName = myFieldsCombo->currentText();
353   else
354     aFieldName = theFieldName;
355
356   VISU::TEntity anEntity = getCurrentScalarEntity();
357
358   myPrs->SetScalarField(myCurrMeshName.latin1(),
359                         aFieldName.latin1(),
360                         theIter,
361                         anEntity);
362
363   myScalarPane->storeToPrsObject(myPrs);
364   
365   UpdateScalarField();
366 }
367
368 void VisuGUI_ScalarMapOnDeformedShapeDlg::accept()
369 {
370   if (myScalarPane->check()){
371     myScalarPane->deletePreview();
372     QDialog::accept();
373   }
374 }
375
376 void VisuGUI_ScalarMapOnDeformedShapeDlg::reject()
377 {
378   myScalarPane->deletePreview();
379   QDialog::reject();
380 }
381
382 void VisuGUI_ScalarMapOnDeformedShapeDlg::AddAllFieldNames(){
383   TMF::const_iterator aIterMesh = myMeshFieldsTimes.begin();
384   for(;aIterMesh != myMeshFieldsTimes.end();aIterMesh++){
385     QString aMeshName = aIterMesh->first;
386     TFT::const_iterator aIterField = (aIterMesh->second).begin();
387     for(;aIterField != (aIterMesh->second).end(); aIterField++){
388       QString aFieldName = aIterField->first.first;
389       myFieldsCombo->insertItem(aFieldName);
390     }
391   }
392 }
393
394 void VisuGUI_ScalarMapOnDeformedShapeDlg::AddAllTimes(const QString& theFieldName){
395   TMF::const_iterator aIterMesh = myMeshFieldsTimes.begin();
396   for(;aIterMesh != myMeshFieldsTimes.end();aIterMesh++){
397     QString aMeshName = aIterMesh->first;
398     TFT::const_iterator aIterField = (aIterMesh->second).begin();
399     for(;aIterField != (aIterMesh->second).end(); aIterField++){
400       QString aFieldName = aIterField->first.first;
401       if(theFieldName != aFieldName) continue;
402       myTimeStampsCombo->clear();
403       TTL::const_iterator aTimeIter = (aIterField->second).begin();
404       for(;aTimeIter != (aIterField->second).end(); aTimeIter++)
405         myTimeStampsCombo->insertItem(QString("%1").arg(aTimeIter->second));
406       return;
407     }
408   }
409 }
410
411 void VisuGUI_ScalarMapOnDeformedShapeDlg::onFieldChanged(int){
412   AddAllTimes(myFieldsCombo->currentText());
413   SetScalarField();
414   UpdateScalarField();
415 }
416
417 void VisuGUI_ScalarMapOnDeformedShapeDlg::onTimeStampChanged(int){
418   SetScalarField();
419   UpdateScalarField();
420 }
421
422 void VisuGUI_ScalarMapOnDeformedShapeDlg::UpdateScalarField(){
423   myPrs->SetScale(getFactor());
424   float aRange[2];
425   myPrs->GetScalarMapOnDeformedShapePL()->Build();
426   myPrs->GetScalarMapOnDeformedShapePL()->Update();
427   myPrs->GetScalarMapOnDeformedShapePL()->GetSourceRange(aRange);
428   double aDoubleRange[2];
429   aDoubleRange[0] = double(aRange[0]);
430   aDoubleRange[1] = double(aRange[1]);
431   myScalarPane->initFromPrsObject(myPrs);
432   myScalarPane->setRange(aDoubleRange[0],aDoubleRange[1],myScalarPane->isIRange());
433 }
434
435 float VisuGUI_ScalarMapOnDeformedShapeDlg::GetFloatValueOfTimeStamp(const char* theMeshName,
436                                                                     const char* theFieldName,
437                                                                     const int   theIter,
438                                                                     const VISU::TEntity  theEntity){
439   float ret=1;
440   VISU::Result_i* theResult = myPrs->GetResult();
441   VISU::PField aField = theResult->GetInput()->GetField(theMeshName,theEntity,theFieldName);
442   if(!aField) return ret;
443   VISU::TValField& aValField = aField->myValField;
444   VISU::TValField::const_iterator aIter = aValField.find(theIter);
445   if(aIter != aValField.end()){
446     VISU::PValForTime aValForTime = aIter->second;
447     ret = float(aValForTime->myTime.first);
448   } else {
449     if(MYDEBUG){
450       cout << "Where are no aValForTime with iteration:"<<theIter<<endl;
451       cout << "\tThe possible interations is aValField.keys():";
452       VISU::TValField::const_iterator aTmp = aValField.begin();
453       for(;aTmp != aValField.end(); aTmp++)
454         cout << aTmp->first << ",";
455       cout << endl;
456     }
457   }
458   return ret;
459 }