Salome HOME
1st step.
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / pqMEDReaderFieldsWidget.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 "pqMEDReaderFieldsWidget.h"
22
23 #include "vtkMEDReader.h"
24 #include "vtkPVMetaDataInformation.h"
25
26 #include "pqTreeWidget.h"
27 #include "pqTreeWidgetItemObject.h"
28 #include "vtkDataSetAttributes.h"
29 #include "vtkGraph.h"
30 #include "vtkNew.h"
31 #include "vtkStringArray.h"
32 #include "vtkTree.h"
33
34 #include <QStringList>
35 #include <QHeaderView>
36
37 //-----------------------------------------------------------------------------
38 pqMEDReaderFieldsWidget::pqMEDReaderFieldsWidget(
39   vtkSMProxy *smproxy, vtkSMProperty *smproperty, QWidget *parentObject)
40 : Superclass(smproxy, smproperty, parentObject)
41 {
42   this->TreeWidget->header()->hide();
43   this->visibleHeader = false;
44   this->TransmitToParent = true;
45   this->initializeTreeWidget(smproxy, smproperty);
46 }
47
48 //-----------------------------------------------------------------------------
49 pqMEDReaderFieldsWidget::~pqMEDReaderFieldsWidget()
50 {
51 }
52
53 //-----------------------------------------------------------------------------
54 void pqMEDReaderFieldsWidget::loadTreeWidgetItems()
55 {
56   //Clear Item Map
57   this->ItemMap.clear();
58
59   // Clear tree
60   this->TreeWidget->clear();
61
62   // Clear unique checked item vector
63   this->UniqueCheckedItems.clear();
64
65   // Recover meta data graph
66   vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
67   this->proxy()->GatherInformation(info);
68   vtkGraph* graph = vtkGraph::SafeDownCast(info->GetInformationData());
69
70   // Create a tree
71   vtkNew<vtkTree> tree;
72   tree->CheckedShallowCopy(graph);
73   vtkStringArray* names =
74     vtkStringArray::SafeDownCast(tree->GetVertexData()->GetAbstractArray("Names"));
75
76   vtkIdType root = tree->GetRoot();
77   vtkIdType fst = tree->GetChild(root, 0); // FieldsStatusTree
78
79   this->NItems = 0;
80   int nLeaves = 0;
81   for (int i = 1; i < tree->GetNumberOfChildren(fst); i += 2)
82     {
83     // Browse all interessting tree node
84
85     // TSX Node
86     vtkIdType tsxId = tree->GetChild(fst, i);
87     vtkIdType tsId = tree->GetChild(fst, i - 1);
88     pqTreeWidgetItemObject *ts = new pqTreeWidgetItemObject(this->TreeWidget, QStringList());
89     this->NItems++;
90     QString tsxName = QString(names->GetValue(tsxId));
91     ts->setText(0, tsxName);
92     ts->setData(0, Qt::ToolTipRole, QString(names->GetValue(tsId)));
93
94     // MAIL Node
95     vtkIdType mailId = tree->GetChild(tsxId, 0);
96     pqTreeWidgetItemObject *mail = new pqTreeWidgetItemObject(ts, QStringList());
97     this->NItems++;
98     QString mailName = QString(names->GetValue(mailId));
99     mail->setText(0, mailName);
100     mail->setData(0, Qt::ToolTipRole, QString(names->GetValue(mailId)));
101
102     QString propertyBaseName = tsxName + "/" + mailName + "/";
103
104     // ComsupX node
105     for (int comsupi = 0; comsupi < tree->GetNumberOfChildren(mailId); comsupi++)
106       {
107       vtkIdType comSupId = tree->GetChild(mailId, comsupi);
108       pqTreeWidgetItemObject *comsup = new pqTreeWidgetItemObject(mail, QStringList());
109       this->NItems++;
110       QString comsupName = QString(names->GetValue(comSupId));
111       comsup->setText(0, comsupName);
112
113       // ComSup tooltip
114       vtkIdType geoTypeId = tree->GetChild(comSupId, 1);
115       QString comSupToolTipName(names->GetValue(comSupId));
116       for (int geoi = 0; geoi < tree->GetNumberOfChildren(geoTypeId); geoi++)
117         {
118         comSupToolTipName += QString("\n- %1").arg(
119           QString(names->GetValue(tree->GetChild(geoTypeId, geoi))));
120         }
121       comsup->setData(0, Qt::ToolTipRole, comSupToolTipName);
122
123       comsup->setFlags(comsup->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
124       comsup->setChecked(true);
125       QObject::connect(comsup, SIGNAL(checkedStateChanged(bool)), this, SLOT(updateChecks()));
126       this->UniqueCheckedItems.push_back(comsup);
127
128       QString fullComsupName = propertyBaseName + comsupName + "/";
129       // Arrs node
130       vtkIdType arrId = tree->GetChild(comSupId, 0);
131       for (int arri = 0; arri < tree->GetNumberOfChildren(arrId); arri++)
132         {
133         pqTreeWidgetItemObject *array = new pqTreeWidgetItemObject(comsup, QStringList());
134         this->NItems++;
135
136         vtkIdType arrayId = tree->GetChild(arrId, arri);
137         std::string str = names->GetValue(arrayId);
138         this->ItemMap[fullComsupName + QString(str.c_str())] = array;
139
140         const char* separator = vtkMEDReader::GetSeparator();
141         size_t pos = str.find(separator);
142         std::string name = str.substr(0, pos);
143
144         array->setText(0, QString(name.c_str()));
145         array->setFlags(array->flags() | Qt::ItemIsUserCheckable);
146         array->setChecked(true);
147
148         // Special Field
149         if (tree->GetNumberOfChildren(arrayId) != 0)
150           {
151           QFont font;
152           font.setItalic(true);
153           font.setUnderline(true);
154           array->setData(0, Qt::FontRole, QVariant(font));
155
156           array->setData(0, Qt::ToolTipRole,
157                          QString("Whole \" %1\" mesh").arg(name.c_str()));
158           array->setData(0, Qt::DecorationRole,
159             QPixmap(":/ParaViewResources/Icons/pqCellDataForWholeMesh16.png"));
160           }
161         // Standard Field
162         else
163           {
164           std::string spatialDiscr = str.substr(pos + strlen(separator));
165           QString tooltip = QString(name.c_str() + QString(" (") +
166                                     spatialDiscr.c_str() + QString(")"));
167           array->setData(0, Qt::ToolTipRole, tooltip);
168
169           QPixmap pix;
170           if (spatialDiscr == "P0")
171             {
172             pix.load(":/ParaViewResources/Icons/pqCellData16.png");
173             }
174           else if (spatialDiscr == "P1")
175             {
176             pix.load(":/ParaViewResources/Icons/pqPointData16.png");
177             }
178           else if (spatialDiscr == "GAUSS")
179             {
180             pix.load(":/ParaViewResources/Icons/pqQuadratureData16.png");
181             }
182           else if (spatialDiscr == "GSSNE")
183             {
184             pix.load(":/ParaViewResources/Icons/pqElnoData16.png");
185             }
186           array->setData(0, Qt::DecorationRole, pix);
187           }
188
189         // Connection and updating checks for each item
190         QObject::connect(array, SIGNAL(checkedStateChanged(bool)), this, SLOT(updateChecks()));
191         nLeaves++;
192         }
193       }
194     }
195
196   // Expand tree
197   this->TreeWidget->expandAll();
198 }
199
200 //-----------------------------------------------------------------------------
201 void pqMEDReaderFieldsWidget::uncheckOtherUniqueItems(pqTreeWidgetItemObject* item)
202 {
203   // Uncheck all other items in vector
204   foreach (pqTreeWidgetItemObject* otherItems, this->UniqueCheckedItems)
205     {
206     if (otherItems != item)
207       {
208       otherItems->setCheckState(0, Qt::Unchecked);
209       }
210     }
211 }
212
213 //-----------------------------------------------------------------------------
214 void pqMEDReaderFieldsWidget::updateChecks()
215 {
216   // Call updateCheck on the sender
217   pqTreeWidgetItemObject* item = qobject_cast<pqTreeWidgetItemObject*>(QObject::sender());
218   this->updateChecks(item);
219 }
220
221 //-----------------------------------------------------------------------------
222 void pqMEDReaderFieldsWidget::updateChecks(pqTreeWidgetItemObject* item)
223 {
224   // When a Leaf item is checked, the parent will be checked (partially or not).
225   // Then other parents will be unchecked using uncheckOtherUniqueItems
226   // Then other parent leaf will be unchecked using updateChecks(parent)
227   //
228   // When a Parent item is checked, the leaf will be checked
229   // Then other parents will be unchecked using uncheckOtherUniqueItems
230   // Then other parent leaf will be unchecked using updateChecks(parent)
231
232   if (item->childCount() == 0)
233     {
234     // Only first level leaf will transmit checks to parent
235     if (this->TransmitToParent)
236       {
237       // Recover correct parent state
238       Qt::CheckState state = item->checkState(0);
239       pqTreeWidgetItemObject* parent =
240         dynamic_cast<pqTreeWidgetItemObject*>(item->QTreeWidgetItem::parent());
241       for (int i = 0; i < parent->childCount(); i++)
242         {
243         if (parent->child(i)->checkState(0) != state)
244           {
245           state = Qt::PartiallyChecked;
246           }
247         }
248       // Set Parent State
249       parent->setCheckState(0, state);
250       }
251     }
252   else
253     {
254     // Check/Uncheck childs, blocking looped call to slot
255     if (item->checkState(0) != Qt::PartiallyChecked)
256       {
257       this->TransmitToParent = false;
258       for (int i = 0; i < item->childCount(); i++)
259         {
260         pqTreeWidgetItemObject* leaf =
261           dynamic_cast<pqTreeWidgetItemObject*>(item->child(i));
262         leaf->setCheckState(0, item->checkState(0));
263         }
264       this->TransmitToParent = true;
265       }
266
267     // Uncheck other unique checked items
268     if (item->checkState(0) != Qt::Unchecked)
269       {
270       this->uncheckOtherUniqueItems(item);
271       }
272     }
273 }