1 // Copyright (C) 2010-2015 CEA/DEN, EDF R&D
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, or (at your option) any later version.
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
19 // Author : Anthony Geay
21 #include "pqMEDReaderTimesFlagsWidget.h"
23 #include "VectBoolWidget.h"
24 #include "pqMEDReaderGraphUtils.h"
25 #include "vtkMEDReader.h"
26 #include "vtkPVMetaDataInformation.h"
28 #include "pqCoreUtilities.h"
29 #include "pqArrayListDomain.h"
30 #include "pqPropertiesPanel.h"
32 #include "vtkGraphWriter.h"
33 #include "vtkInformationDataObjectMetaDataKey.h"
35 #include "vtkSMDoubleVectorProperty.h"
36 #include "vtkSMStringVectorProperty.h"
39 #include <QGridLayout>
40 #include <QStringList>
42 //-----------------------------------------------------------------------------
43 pqMEDReaderTimesFlagsWidget::pqMEDReaderTimesFlagsWidget(
44 vtkSMProxy *smproxy, vtkSMProperty *smproperty, QWidget *parentObject)
45 : Superclass(smproxy, parentObject)
47 this->CachedTsId = -1;
48 this->TimesVectWidget = NULL;
49 this->setShowLabel(false);
52 this->TimesVectLayout = new QGridLayout(this);
55 this->CreateTimesVectWidget();
56 this->UpdateTimeSteps();
58 // Connect timeStepStatus property to update time steps method
59 vtkSMProperty* prop = smproxy? smproxy->GetProperty("FieldsStatus") : NULL;
62 qDebug("Could not locate property named 'FieldsStatus'. "
63 "pqMEDReaderTimesFlagsWidget will have no updated labels.");
67 pqCoreUtilities::connect(prop, vtkCommand::UncheckedPropertyModifiedEvent,
68 this, SLOT(UpdateTimeSteps()));
71 // Connect Property Domain to the timeStepDomain property,
72 // so setTimeStepDomain is called when the domain changes.
73 vtkSMDomain* arraySelectionDomain = smproperty->GetDomain("array_list");
74 new pqArrayListDomain(this,"timeStepsDomain", smproxy, smproperty, arraySelectionDomain);
76 // Connect property to timeStep QProperty using a biderectionnal property link
77 this->addPropertyLink(this, "timeSteps", SIGNAL(timeStepsChanged()),
80 const QMap<QString, VectBoolItem*>& items(this->TimesVectWidget->getItems());
81 QMap<QString, VectBoolItem*>::const_iterator it;
82 for (it = items.begin(); it != items.end(); it++)
84 QObject::connect(it.value(), SIGNAL(changed()), this, SLOT(onItemChanged()));
88 //-----------------------------------------------------------------------------
89 pqMEDReaderTimesFlagsWidget::~pqMEDReaderTimesFlagsWidget()
91 delete this->TimesVectWidget;
94 //-----------------------------------------------------------------------------
95 void pqMEDReaderTimesFlagsWidget::onItemChanged() const
97 emit timeStepsChanged();
100 //-----------------------------------------------------------------------------
101 QList< QList< QVariant> > pqMEDReaderTimesFlagsWidget::getTimeSteps() const
103 // Put together a TimeStep list, using ItemMap
104 QList< QList< QVariant> > ret;
105 QList< QVariant > timeStep;
106 const QMap<QString, VectBoolItem*>& items(this->TimesVectWidget->getItems());
107 QMap<QString, VectBoolItem*>::const_iterator it;
108 for (it = items.begin(); it != items.end(); it++)
111 timeStep.append(it.key());
112 timeStep.append(it.value()->isActivated());
113 ret.append(timeStep);
118 //-----------------------------------------------------------------------------
119 void pqMEDReaderTimesFlagsWidget::setTimeSteps(QList< QList< QVariant> > timeSteps)
121 // Update each item checkboxes, using timeSteps names and ItemMap
122 const QMap<QString, VectBoolItem *>& items(this->TimesVectWidget->getItems());
123 QMap<QString, VectBoolItem*>::const_iterator it;
124 foreach (QList< QVariant> timeStep, timeSteps)
126 it = items.find(timeStep[0].toString());
127 if (it == items.end())
129 qDebug("Found an unknow TimeStep in pqMEDReaderTimesFlagsWidget::setTimeSteps, ignoring");
132 it.value()->activated(timeStep[1].toBool());
136 //-----------------------------------------------------------------------------
137 void pqMEDReaderTimesFlagsWidget::setTimeStepsDomain(QList< QList< QVariant> > timeSteps)
139 cout<<"TimeStepsDomai"<<endl;
140 // Block signals so the reloading does not trigger
141 // UncheckPropertyModified event
142 this->blockSignals(true);
144 // Load the tree widget
145 this->CreateTimesVectWidget();
147 // Set timeSteps checkboxes
148 this->setTimeSteps(timeSteps);
151 this->blockSignals(false);
154 //-----------------------------------------------------------------------------
155 void pqMEDReaderTimesFlagsWidget::CreateTimesVectWidget()
158 vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
159 this->proxy()->GatherInformation(info);
160 vtkGraph* graph = vtkGraph::SafeDownCast(info->GetInformationData());
163 if (this->TimesVectWidget != NULL)
165 delete this->TimesVectWidget;
168 // (Re)cretate widget
169 this->TimesVectWidget = new VectBoolWidget(this,
170 pqMedReaderGraphUtils::getMaxNumberOfTS(graph));
171 this->TimesVectLayout->addWidget(this->TimesVectWidget);
174 //-----------------------------------------------------------------------------
175 void pqMEDReaderTimesFlagsWidget::UpdateTimeSteps()
178 vtkSMStringVectorProperty* prop = this->proxy()?
179 vtkSMStringVectorProperty::SafeDownCast(this->proxy()->GetProperty("FieldsStatus")) : NULL;
183 // Searching first activated leaf id
184 for (int i = 1; i < prop->GetNumberOfElements(); i += 2)
186 if (prop->GetElement(i)[0] == '1')
188 const char* leafString = prop->GetElement(i - 1);
189 const char* tmp = strchr(leafString, '/');
190 size_t num = tmp - leafString;
191 char* dest = new char[num];
192 strncpy(dest, leafString, num);
194 tsId = strtol(dest + 2, NULL, 10);
201 if (tsId != -1 && tsId != this->CachedTsId)
204 this->CachedTsId = tsId;
205 vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
206 this->proxy()->GatherInformation(info);
207 vtkGraph* g = vtkGraph::SafeDownCast(info->GetInformationData());
209 // Updating times steps using leaf id
210 QStringList dts, its, tts;
211 pqMedReaderGraphUtils::getCurrentTS(g, tsId, dts, its, tts);
212 this->TimesVectWidget->setItems(dts, its, tts);