Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / ParaViewPlugin / pqMEDReaderTimesFlagsWidget.cxx
1 // Copyright (C) 2010-2022  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   if(!this->TimesVectWidget) // In case of error right at the begining of loading process (empty MED file)
81     return ;
82
83   const QMap<QString, VectBoolItem*>& items(this->TimesVectWidget->getItems());
84   QMap<QString, VectBoolItem*>::const_iterator it;
85   for (it = items.begin(); it != items.end(); it++)
86     {
87     QObject::connect(it.value(), SIGNAL(changed()), this, SLOT(onItemChanged()));
88     }
89 }
90
91 //-----------------------------------------------------------------------------
92 pqMEDReaderTimesFlagsWidget::~pqMEDReaderTimesFlagsWidget()
93 {
94   delete this->TimesVectWidget;
95 }
96
97 //-----------------------------------------------------------------------------
98 void pqMEDReaderTimesFlagsWidget::onItemChanged() const
99 {
100   emit timeStepsChanged();
101 }
102
103 //-----------------------------------------------------------------------------
104 QList< QList< QVariant> > pqMEDReaderTimesFlagsWidget::getTimeSteps() const
105 {
106   // Put together a TimeStep list, using ItemMap
107   QList< QList< QVariant> > ret;
108   QList< QVariant > timeStep;
109   if(!this->TimesVectWidget) // In case of error right at the begining of loading process (empty MED file)
110     return ret;
111   const QMap<QString, VectBoolItem*>& items(this->TimesVectWidget->getItems());
112   QMap<QString, VectBoolItem*>::const_iterator it;
113   for (it = items.begin(); it != items.end(); it++)
114     {
115     timeStep.clear();
116     timeStep.append(it.key());
117     timeStep.append(it.value()->isActivated());
118     ret.append(timeStep);
119     }
120   return ret;
121 }
122
123 //-----------------------------------------------------------------------------
124 void pqMEDReaderTimesFlagsWidget::setTimeSteps(QList< QList< QVariant> > timeSteps)
125 {
126   // Update each item checkboxes, using timeSteps names and ItemMap
127   const QMap<QString, VectBoolItem *>& items(this->TimesVectWidget->getItems());
128   QMap<QString, VectBoolItem*>::const_iterator it;
129   foreach (QList< QVariant> timeStep, timeSteps)
130     {
131     it = items.find(timeStep[0].toString());
132     if (it == items.end())
133       {
134       qDebug("Found an unknow TimeStep in pqMEDReaderTimesFlagsWidget::setTimeSteps, ignoring");
135       continue;
136       }
137     it.value()->activated(timeStep[1].toBool());
138     }
139 }
140
141 //-----------------------------------------------------------------------------
142 void pqMEDReaderTimesFlagsWidget::setTimeStepsDomain(QList< QList< QVariant> > timeSteps)
143 {
144   cout<<"TimeStepsDomai"<<endl;
145   // Block signals so the reloading does not trigger
146   // UncheckPropertyModified event
147   this->blockSignals(true);
148
149   // Load the tree widget
150   this->CreateTimesVectWidget();
151
152   // Set timeSteps checkboxes
153   this->setTimeSteps(timeSteps);
154
155   // Restore signals
156   this->blockSignals(false);
157 }
158
159 //-----------------------------------------------------------------------------
160 void pqMEDReaderTimesFlagsWidget::CreateTimesVectWidget()
161 {
162   // Recover Graph
163   vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
164   this->proxy()->GatherInformation(info);
165   vtkGraph* graph = vtkGraph::SafeDownCast(info->GetInformationData());
166
167   // Delete the widget
168   if (this->TimesVectWidget != NULL)
169     {
170     delete this->TimesVectWidget;
171     }
172
173   if(!graph)
174     return ;// In case of error right at the begining of loading process (empty MED file)
175
176   // (Re)cretate widget
177   this->TimesVectWidget = new VectBoolWidget(this,
178     pqMedReaderGraphUtils::getMaxNumberOfTS(graph));
179   this->TimesVectLayout->addWidget(this->TimesVectWidget);
180 }
181
182 //-----------------------------------------------------------------------------
183 void pqMEDReaderTimesFlagsWidget::UpdateTimeSteps()
184 {
185   // Recover property
186   vtkSMStringVectorProperty* prop = this->proxy()?
187     vtkSMStringVectorProperty::SafeDownCast(this->proxy()->GetProperty("FieldsStatus")) : NULL;
188   vtkIdType tsId = -1;
189   if (prop)
190     {
191     // Searching first activated leaf id
192     for (unsigned int i = 1; i < prop->GetNumberOfElements(); i += 2)
193       {
194       if (prop->GetElement(i)[0] == '1')
195         {
196         const char* leafString = prop->GetElement(i - 1);
197         const char* tmp = strchr(leafString, '/');
198         if (tmp)
199         {
200           size_t num = tmp - leafString;
201           char* dest = new char[num+1];
202           strncpy(dest, leafString, num);
203           dest[num] = '\0';
204           tsId = (vtkIdType)strtol(dest + 2, NULL, 10);
205           delete [] dest;
206         }
207         break;
208         }
209       }
210     }
211
212   if (tsId != -1 && tsId != this->CachedTsId)
213     {
214     // Recovering graph
215     this->CachedTsId = tsId;
216     vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
217     this->proxy()->GatherInformation(info);
218     vtkGraph* g = vtkGraph::SafeDownCast(info->GetInformationData());
219
220     // Updating times steps using leaf id
221     QStringList dts, its, tts;
222     if(g)
223       pqMedReaderGraphUtils::getCurrentTS(g, tsId, dts, its, tts);
224     if(this->TimesVectWidget)
225       this->TimesVectWidget->setItems(dts, its, tts);
226     }
227 }