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