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