Salome HOME
Suppress compiler warning.
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / pqMEDReaderTimesFlagsWidget.cxx
1 // Copyright (C) 2010-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay
20
21 #include "pqMEDReaderTimesFlagsWidget.h"
22
23 #include "VectBoolWidget.h"
24 #include "pqMEDReaderGraphUtils.h"
25 #include "vtkMEDReader.h"
26 #include "vtkPVMetaDataInformation.h"
27
28 #include "pqCoreUtilities.h"
29 #include "pqArrayListDomain.h"
30 #include "pqPropertiesPanel.h"
31 #include "vtkGraph.h"
32 #include "vtkGraphWriter.h"
33 #include "vtkInformationDataObjectMetaDataKey.h"
34 #include "vtkNew.h"
35 #include "vtkSMDoubleVectorProperty.h"
36 #include "vtkSMStringVectorProperty.h"
37 #include "vtkTree.h"
38
39 #include <QGridLayout>
40 #include <QStringList>
41
42 //-----------------------------------------------------------------------------
43 pqMEDReaderTimesFlagsWidget::pqMEDReaderTimesFlagsWidget(
44   vtkSMProxy *smproxy, vtkSMProperty *smproperty, QWidget *parentObject)
45 : Superclass(smproxy, parentObject)
46 {
47   this->CachedTsId = -1;
48   this->TimesVectWidget = NULL;
49   this->setShowLabel(false);
50
51   // Grid Layout
52   this->TimesVectLayout = new QGridLayout(this);
53
54   // VectBoolWidget
55   this->CreateTimesVectWidget();
56   this->UpdateTimeSteps();
57
58   // Connect timeStepStatus property to update time steps method
59   vtkSMProperty* prop = smproxy? smproxy->GetProperty("FieldsStatus") : NULL;
60   if (!prop)
61     {
62     qDebug("Could not locate property named 'FieldsStatus'. "
63            "pqMEDReaderTimesFlagsWidget will have no updated labels.");
64     }
65   else
66     {
67     pqCoreUtilities::connect(prop, vtkCommand::UncheckedPropertyModifiedEvent,
68                              this, SLOT(UpdateTimeSteps()));
69     }
70
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);
75
76   // Connect property to timeStep QProperty using a biderectionnal property link
77   this->addPropertyLink(this, "timeSteps", SIGNAL(timeStepsChanged()),
78                         smproxy, smproperty);
79
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++)
83     {
84     QObject::connect(it.value(), SIGNAL(changed()), this, SLOT(onItemChanged()));
85     }
86 }
87
88 //-----------------------------------------------------------------------------
89 pqMEDReaderTimesFlagsWidget::~pqMEDReaderTimesFlagsWidget()
90 {
91   delete this->TimesVectWidget;
92 }
93
94 //-----------------------------------------------------------------------------
95 void pqMEDReaderTimesFlagsWidget::onItemChanged() const
96 {
97   emit timeStepsChanged();
98 }
99
100 //-----------------------------------------------------------------------------
101 QList< QList< QVariant> > pqMEDReaderTimesFlagsWidget::getTimeSteps() const
102 {
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++)
109     {
110     timeStep.clear();
111     timeStep.append(it.key());
112     timeStep.append(it.value()->isActivated());
113     ret.append(timeStep);
114     }
115   return ret;
116 }
117
118 //-----------------------------------------------------------------------------
119 void pqMEDReaderTimesFlagsWidget::setTimeSteps(QList< QList< QVariant> > timeSteps)
120 {
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)
125     {
126     it = items.find(timeStep[0].toString());
127     if (it == items.end())
128       {
129       qDebug("Found an unknow TimeStep in pqMEDReaderTimesFlagsWidget::setTimeSteps, ignoring");
130       continue;
131       }
132     it.value()->activated(timeStep[1].toBool());
133     }
134 }
135
136 //-----------------------------------------------------------------------------
137 void pqMEDReaderTimesFlagsWidget::setTimeStepsDomain(QList< QList< QVariant> > timeSteps)
138 {
139   cout<<"TimeStepsDomai"<<endl;
140   // Block signals so the reloading does not trigger
141   // UncheckPropertyModified event
142   this->blockSignals(true);
143
144   // Load the tree widget
145   this->CreateTimesVectWidget();
146
147   // Set timeSteps checkboxes
148   this->setTimeSteps(timeSteps);
149
150   // Restore signals
151   this->blockSignals(false);
152 }
153
154 //-----------------------------------------------------------------------------
155 void pqMEDReaderTimesFlagsWidget::CreateTimesVectWidget()
156 {
157   // Recover Graph
158   vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
159   this->proxy()->GatherInformation(info);
160   vtkGraph* graph = vtkGraph::SafeDownCast(info->GetInformationData());
161
162   // Delete the widget
163   if (this->TimesVectWidget != NULL)
164     {
165     delete this->TimesVectWidget;
166     }
167
168   // (Re)cretate widget
169   this->TimesVectWidget = new VectBoolWidget(this,
170     pqMedReaderGraphUtils::getMaxNumberOfTS(graph));
171   this->TimesVectLayout->addWidget(this->TimesVectWidget);
172 }
173
174 //-----------------------------------------------------------------------------
175 void pqMEDReaderTimesFlagsWidget::UpdateTimeSteps()
176 {
177   // Recover property
178   vtkSMStringVectorProperty* prop = this->proxy()?
179     vtkSMStringVectorProperty::SafeDownCast(this->proxy()->GetProperty("FieldsStatus")) : NULL;
180   vtkIdType tsId = -1;
181   if (prop)
182     {
183     // Searching first activated leaf id
184     for (int i = 1; i < prop->GetNumberOfElements(); i += 2)
185       {
186       if (prop->GetElement(i)[0] == '1')
187         {
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);
193         dest[num] = '\0';
194         tsId = strtol(dest + 2, NULL, 10);
195         delete dest;
196         break;
197         }
198       }
199     }
200
201   if (tsId != -1 && tsId != this->CachedTsId)
202     {
203     // Recovering graph
204     this->CachedTsId = tsId;
205     vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
206     this->proxy()->GatherInformation(info);
207     vtkGraph* g = vtkGraph::SafeDownCast(info->GetInformationData());
208
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);
213     }
214 }