1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // VISU VISUGUI : GUI of VISU component
21 // File : VisuGUI_DeformedShapeAndScalarMapDlg.cxx
22 // Author : Eugeny Nikolaev
25 #include "VisuGUI_DeformedShapeAndScalarMapDlg.h"
27 #include "VisuGUI_Tools.h"
28 #include "VisuGUI_InputPane.h"
30 #include "VISU_Result_i.hh"
31 #include "VISU_DeformedShapeAndScalarMap_i.hh"
32 #include "VISU_ColoredPrs3dFactory.hh"
34 #include "VISU_ScalarMapPL.hxx"
35 #include "VISU_DeformedShapeAndScalarMapPL.hxx"
37 #include "VISU_Convertor.hxx"
39 #include <SalomeApp_DoubleSpinBox.h>
40 #include <SalomeApp_Module.h>
41 #include <LightApp_Application.h>
42 #include <LightApp_SelectionMgr.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_MessageBox.h>
47 #include <SALOME_ListIO.hxx>
49 #include <SALOMEDSClient_AttributeString.hxx>
50 #include <SALOMEDSClient_AttributeName.hxx>
52 #include <QVBoxLayout>
53 #include <QHBoxLayout>
54 #include <QGridLayout>
57 #include <QPushButton>
67 VisuGUI_DeformedShapeAndScalarMapDlg::VisuGUI_DeformedShapeAndScalarMapDlg (SalomeApp_Module* theModule)
68 : VisuGUI_ScalarBarBaseDlg(theModule, true),
70 myUpdateScalars(true),
71 myIsScalarFieldValid(true),
74 setWindowTitle(tr("DLG_TITLE"));
75 setSizeGripEnabled(true);
77 QVBoxLayout* TopLayout = new QVBoxLayout (this);
78 TopLayout->setSpacing(6);
79 TopLayout->setMargin(11);
81 myTabBox = new QTabWidget (this);
83 // Scalar Map on Deformed shape pane
84 QWidget* aBox = new QWidget (this);
85 QVBoxLayout* aVBLay = new QVBoxLayout( aBox );
86 aVBLay->setMargin(11);
87 QFrame* TopGroup = new QFrame (aBox);
88 aVBLay->addWidget(TopGroup);
89 TopGroup->setFrameStyle(QFrame::Box | QFrame::Sunken);
90 TopGroup->setLineWidth(1);
91 QGridLayout* TopGroupLayout = new QGridLayout (TopGroup);
92 TopGroupLayout->setSpacing(6);
93 TopGroupLayout->setMargin(11);
96 QLabel* ScaleLabel = new QLabel (tr("SCALE_FACTOR"), TopGroup);
97 TopGroupLayout->addWidget(ScaleLabel, 0, 0);
99 ScalFact = new SalomeApp_DoubleSpinBox (TopGroup);
100 VISU::initSpinBox( ScalFact, 0., 1.0E+38, .1, "visual_data_precision" );
101 ScalFact->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
102 ScalFact->setValue(0.1);
103 TopGroupLayout->addWidget(ScalFact, 0, 1);
106 QLabel* FieldLabel = new QLabel (tr("FIELD_ITEM"), TopGroup);
107 myFieldsCombo = new QComboBox (TopGroup);
109 TopGroupLayout->addWidget(FieldLabel, 1, 0);
110 TopGroupLayout->addWidget(myFieldsCombo,1,1);
112 // TimeStamps combo box
113 QLabel* TimeStampLabel = new QLabel (tr("TIMESTAMP_ITEM"), TopGroup);
114 myTimeStampsCombo = new QComboBox (TopGroup);
116 TopGroupLayout->addWidget(TimeStampLabel, 2, 0);
117 TopGroupLayout->addWidget(myTimeStampsCombo,2,1);
118 TopGroupLayout->setRowStretch(3,5);
121 myTabBox->addTab(aBox, tr("DEFORMED_SHAPE_AND_SCALAR_MAP_TAB"));
124 myInputPane = new VisuGUI_InputPane(VISU::TDEFORMEDSHAPEANDSCALARMAP, theModule, this);
126 myTabBox->addTab(GetScalarPane(), tr("SCALAR_BAR_TAB"));
127 myTabBox->addTab(myInputPane, tr("INPUT_TAB"));
130 QGroupBox* GroupButtons = new QGroupBox (this);
131 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
132 GroupButtonsLayout->setSpacing(6);
133 GroupButtonsLayout->setMargin(11);
135 myButtonOk = new QPushButton (tr("&OK"), GroupButtons);
136 myButtonOk->setAutoDefault(true);
137 myButtonOk->setDefault(true);
138 QPushButton* buttonCancel = new QPushButton (tr("&Cancel") , GroupButtons);
139 buttonCancel->setAutoDefault(true);
140 QPushButton* buttonHelp = new QPushButton (tr("&Help") , GroupButtons);
141 buttonHelp->setAutoDefault(true);
143 GroupButtonsLayout->addWidget(myButtonOk);
144 GroupButtonsLayout->addSpacing(10);
145 GroupButtonsLayout->addStretch();
146 GroupButtonsLayout->addWidget(buttonCancel);
147 GroupButtonsLayout->addWidget(buttonHelp);
149 // Add Tab box and Buttons to the top layout
150 TopLayout->addWidget(myTabBox);
151 TopLayout->addWidget(GroupButtons);
153 // signals and slots connections
154 connect(myButtonOk, SIGNAL(clicked()), this, SLOT(accept()));
155 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
156 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(onHelp()));
157 connect(myFieldsCombo, SIGNAL(activated(int)), this, SLOT(onFieldChanged(int)));
158 connect(myTimeStampsCombo, SIGNAL(activated(int)), this, SLOT(onTimeStampChanged(int)));
161 VisuGUI_DeformedShapeAndScalarMapDlg::~VisuGUI_DeformedShapeAndScalarMapDlg()
165 void VisuGUI_DeformedShapeAndScalarMapDlg::initFromPrsObject( VISU::ColoredPrs3d_i* thePrs,
169 myPrsCopy = VISU::TSameAsFactory<VISU::TDEFORMEDSHAPEANDSCALARMAP>().Create(thePrs, VISU::ColoredPrs3d_i::EDoNotPublish);
170 setFactor(myPrsCopy->GetScale());
171 myTimeStampsCombo->setDisabled(myIsAnimation);
173 CORBA::String_var aFieldName(myPrsCopy->GetScalarFieldName());
174 QString aIteration = GetFloatValueOfTimeStamp(myPrsCopy->GetScalarEntity(),
176 myPrsCopy->GetScalarTimeStampNumber());
177 if (myEntity2Fields.size() == 0)
179 // find all fields and time stamps on it
180 _PTR(Study) aActiveStudy = VISU::GetCStudy(VISU::GetAppStudy(myVisuGUI));
181 LightApp_SelectionMgr* aSel = VISU::GetSelectionMgr(myVisuGUI);
182 SALOME_ListIO selected;
183 aSel->selectedObjects(selected);
184 if (selected.Extent() > 0) {
185 Handle(SALOME_InteractiveObject) aIO = selected.First();
186 if (aIO->hasEntry()) {
187 _PTR(SObject) aSObject = aActiveStudy->FindObjectID(aIO->getEntry());
188 VISU::VISUType aType = VISU::Storable::SObject2Type( aSObject );
190 case VISU::TTIMESTAMP: {
191 aSObject = aSObject->GetFather();
192 aSObject = aSObject->GetFather();
196 _PTR(SObject) newSObject;
197 if(aSObject->ReferencedObject(newSObject)) aSObject = newSObject;
198 aSObject = aSObject->GetFather();
201 case VISU::TANIMATION: {
202 _PTR(ChildIterator) aTmpIter = aActiveStudy->NewChildIterator(aSObject);
203 for (aTmpIter->InitEx(true); aTmpIter->More(); aTmpIter->Next()) {
204 _PTR(SObject) aTmpChildSObj = aTmpIter->Value();
205 _PTR(SObject) newSObject;
206 if(aTmpChildSObj->ReferencedObject(newSObject)){
207 aSObject = newSObject;
208 aSObject->GetFather();
215 aSObject = aSObject->GetFather();
216 aSObject = aSObject->GetFather();
218 mySelectionObj = aSObject;
219 CORBA::Object_var anObject = VISU::ClientSObjectToObject(mySelectionObj);
220 if (CORBA::is_nil(anObject)) {
221 mySelectionObj = mySelectionObj->GetFather();
226 if (mySelectionObj) {
227 _PTR(ChildIterator) aIter = aActiveStudy->NewChildIterator(mySelectionObj);
229 for (aIter->InitEx(true); aIter->More(); aIter->Next()) {
230 _PTR(SObject) aChildSObj = aIter->Value();
231 VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aChildSObj);
232 if (!aRestoringMap.empty()) {
233 VISU::VISUType aType = VISU::Storable::RestoringMap2Type(aRestoringMap);
234 if (aType == VISU::TTIMESTAMP) {
235 QString aMeshName = aRestoringMap["myMeshName"];
236 CORBA::String_var aName = myPrsCopy->GetMeshName();
237 if (aMeshName != aName.in())
239 QString aFieldName = aRestoringMap["myFieldName"];
240 QString aTimeIter = aRestoringMap["myTimeStampId"];
241 QString aEntity = aRestoringMap["myEntityId"];
242 VISU::Entity anEntity;
243 switch (aEntity.toInt()) {
244 case 0: anEntity = VISU::NODE; break;
245 case 1: anEntity = VISU::EDGE; break;
246 case 2: anEntity = VISU::FACE; break;
247 case 3: anEntity = VISU::CELL; break;
249 TFieldName2TimeStamps& aFieldName2TimeStamps = myEntity2Fields[anEntity];
250 TTimeStampNumber2Time& aTimeStampNumber2Time = aFieldName2TimeStamps[aFieldName];
251 aTimeStampNumber2Time[aTimeIter.toInt()] =
252 GetFloatValueOfTimeStamp(anEntity,
253 aFieldName.toLatin1().constData(),
261 int idx = myFieldsCombo->findText(aFieldName.in());
263 myFieldsCombo->setCurrentIndex(idx);
265 myFieldsCombo->addItem(aFieldName.in());
266 myFieldsCombo->setCurrentIndex(myFieldsCombo->count()-1);
268 AddAllTimes(myFieldsCombo->currentText());
269 idx = myTimeStampsCombo->findText(aIteration);
271 myTimeStampsCombo->setCurrentIndex(idx);
273 myTimeStampsCombo->addItem(aIteration);
274 myTimeStampsCombo->setCurrentIndex(myTimeStampsCombo->count()-1);
276 SetScalarField( false );
278 VisuGUI_ScalarBarBaseDlg::initFromPrsObject(myPrsCopy, theInit);
283 myInputPane->initFromPrsObject( myPrsCopy );
284 myTabBox->setCurrentIndex( 0 );
287 double VisuGUI_DeformedShapeAndScalarMapDlg::getFactor() const
289 return ScalFact->value();
292 void VisuGUI_DeformedShapeAndScalarMapDlg::setFactor(double theFactor)
295 if (fabs(theFactor) > std::numeric_limits<double>::epsilon()) {
296 int degree = int(log10(fabs(theFactor))) - 1;
297 if (fabs(theFactor) < 1) {
298 // as logarithm value is negative in this case
299 // and it is truncated to the bigger integer
302 step = pow(10., double(degree));
305 ScalFact->setSingleStep(step);
306 ScalFact->setValue(theFactor);
310 VisuGUI_DeformedShapeAndScalarMapDlg
311 ::storeToPrsObject(VISU::ColoredPrs3d_i* thePrs)
313 if(!myInputPane->check() || !GetScalarPane()->check())
316 int anIsOk = myInputPane->storeToPrsObject( myPrsCopy );
317 anIsOk &= GetScalarPane()->storeToPrsObject( myPrsCopy );
319 myPrsCopy->SetScale(getFactor());
321 myPrsCopy->SetScalarField(myPrsCopy->GetScalarEntity(),
322 getCurrentScalarFieldName().toLatin1().constData(),
323 myTimeStampID[ myTimeStampsCombo->currentIndex() ]);
326 SetScalarField( false );
328 VISU::TSameAsFactory<VISU::TDEFORMEDSHAPEANDSCALARMAP>().Copy(myPrsCopy, thePrs);
333 int VisuGUI_DeformedShapeAndScalarMapDlg::getCurrentScalarFieldNamePos(){
334 return myFieldsCombo->currentIndex();
337 QString VisuGUI_DeformedShapeAndScalarMapDlg::getCurrentScalarFieldName(){
338 return myFieldsCombo->currentText();
341 int VisuGUI_DeformedShapeAndScalarMapDlg::getCurrentScalarNbIterations(){
342 return myTimeStampsCombo->count();
346 VisuGUI_DeformedShapeAndScalarMapDlg
347 ::getCurrentScalarEntity()
349 VISU::Entity anEntity = VISU::Entity(-1);
350 TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
351 for(; anIter != myEntity2Fields.end(); anIter++){
352 const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
353 TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
354 for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
355 const QString& aFieldName = aFieldIter->first;
356 if (aFieldName == myFieldsCombo->currentText()) {
357 anEntity = anIter->first;
365 void VisuGUI_DeformedShapeAndScalarMapDlg::SetScalarField( const bool save_scalar_pane ){
366 SetScalarField( myTimeStampID[ myTimeStampsCombo->currentIndex() ], "", save_scalar_pane );
370 VisuGUI_DeformedShapeAndScalarMapDlg
371 ::SetScalarField(int theIter,
372 QString theFieldName,
373 const bool save_scalar_pane )
375 QApplication::setOverrideCursor(Qt::WaitCursor);
377 if( save_scalar_pane && IsScalarFieldValid() )
378 GetScalarPane()->storeToPrsObject(myPrsCopy);
382 if(theFieldName.isEmpty())
383 aFieldName = myFieldsCombo->currentText();
385 aFieldName = theFieldName;
387 VISU::Entity anEntity = getCurrentScalarEntity();
390 myPrsCopy->SetScalarField(anEntity,
391 aFieldName.toLatin1().constData(),
393 if( save_scalar_pane )
395 myIsScalarFieldValid = true;
396 } catch( std::exception& exc ) {
398 SUIT_MessageBox::warning( this, tr( "WRN_WARNING" ), tr( "ERR_SCALAR_DATA_INCONSISTENT" ) );
399 myIsScalarFieldValid = false;
403 QApplication::restoreOverrideCursor();
406 void VisuGUI_DeformedShapeAndScalarMapDlg::accept()
408 VisuGUI_ScalarBarBaseDlg::accept();
411 void VisuGUI_DeformedShapeAndScalarMapDlg::reject()
413 VisuGUI_ScalarBarBaseDlg::reject();
416 QString VisuGUI_DeformedShapeAndScalarMapDlg::GetContextHelpFilePath()
418 return "scalar_map_on_deformed_shape_page.html";
421 void VisuGUI_DeformedShapeAndScalarMapDlg::AddAllFieldNames(){
422 TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
423 for(; anIter != myEntity2Fields.end(); anIter++){
424 const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
425 TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
426 for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
427 const QString& aFieldName = aFieldIter->first;
428 myFieldsCombo->addItem(aFieldName);
433 void VisuGUI_DeformedShapeAndScalarMapDlg::AddAllTimes(const QString& theFieldName){
434 TEntity2Fields::const_iterator anIter = myEntity2Fields.begin();
435 int currentTimeStampId = (myTimeStampsCombo->count() > 0) ? myTimeStampsCombo->currentIndex() : 0;
436 for(; anIter != myEntity2Fields.end(); anIter++){
437 const TFieldName2TimeStamps& aFieldName2TimeStamps = anIter->second;
438 TFieldName2TimeStamps::const_iterator aFieldIter = aFieldName2TimeStamps.begin();
439 for(; aFieldIter != aFieldName2TimeStamps.end(); aFieldIter++){
440 const QString& aFieldName = aFieldIter->first;
441 if(theFieldName != aFieldName)
444 myTimeStampID.clear();
445 myTimeStampsCombo->clear();
447 const TTimeStampNumber2Time& aTimeStampNumber2Time = aFieldIter->second;
448 TTimeStampNumber2Time::const_iterator aTimeStampIter = aTimeStampNumber2Time.begin();
449 for(; aTimeStampIter != aTimeStampNumber2Time.end(); aTimeStampIter++){
450 int aTimeStampNumber = aTimeStampIter->first;
451 myTimeStampID.push_back(aTimeStampNumber);
453 QString aTimeStampTime = aTimeStampIter->second;
454 myTimeStampsCombo->addItem(aTimeStampTime);
456 if(currentTimeStampId >= myTimeStampID.size())
457 currentTimeStampId = myTimeStampID.size()-1;
458 myTimeStampsCombo->setCurrentIndex(currentTimeStampId);
464 void VisuGUI_DeformedShapeAndScalarMapDlg::onFieldChanged(int){
465 AddAllTimes(myFieldsCombo->currentText());
467 //UpdateScalarField(); // it is called from SetScalarField() method
470 void VisuGUI_DeformedShapeAndScalarMapDlg::onTimeStampChanged(int){
472 //UpdateScalarField(); // it is called from SetScalarField() method
475 void VisuGUI_DeformedShapeAndScalarMapDlg::UpdateScalarField(){
476 GetScalarPane()->initFromPrsObject(myPrsCopy);
480 VisuGUI_DeformedShapeAndScalarMapDlg
481 ::GetFloatValueOfTimeStamp(VISU::Entity theEntity,
482 const std::string& theFieldName,
483 int theTimeStampNumber)
486 VISU::TEntity anEntity = VISU::TEntity(theEntity);
487 VISU::Result_i* theResult = myPrsCopy->GetCResult();
488 VISU::Result_i::PInput anInput = theResult->GetInput();
489 VISU::PField aField = anInput->GetField(myPrsCopy->GetCMeshName(),
495 VISU::TValField& aValField = aField->myValField;
496 VISU::TValField::const_iterator aIter = aValField.find(theTimeStampNumber);
497 if(aIter != aValField.end()){
498 VISU::PValForTime aValForTime = aIter->second;
499 aTime = VISU_Convertor::GenerateName(aValForTime->myTime).c_str();
504 void VisuGUI_DeformedShapeAndScalarMapDlg::updateControls()
506 bool isScalarFieldValid = IsScalarFieldValid();
507 myTabBox->setTabEnabled( 1, isScalarFieldValid ); // "Scalar Bar" tab
508 myTabBox->setTabEnabled( 2, isScalarFieldValid ); // "Input" tab
509 myTimeStampsCombo->setEnabled( isScalarFieldValid );
510 myButtonOk->setEnabled( isScalarFieldValid );