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 "pqMEDReaderFieldsWidget.h"
23 #include "vtkMEDReader.h"
24 #include "vtkPVMetaDataInformation.h"
26 #include "pqTreeWidget.h"
27 #include "pqTreeWidgetItemObject.h"
28 #include "vtkDataSetAttributes.h"
31 #include "vtkStringArray.h"
34 #include <QStringList>
35 #include <QHeaderView>
37 //-----------------------------------------------------------------------------
38 pqMEDReaderFieldsWidget::pqMEDReaderFieldsWidget(
39 vtkSMProxy *smproxy, vtkSMProperty *smproperty, QWidget *parentObject)
40 : Superclass(smproxy, smproperty, parentObject)
42 this->TreeWidget->header()->hide();
43 this->visibleHeader = false;
44 this->TransmitToParent = true;
45 this->initializeTreeWidget(smproxy, smproperty);
48 //-----------------------------------------------------------------------------
49 pqMEDReaderFieldsWidget::~pqMEDReaderFieldsWidget()
53 //-----------------------------------------------------------------------------
54 void pqMEDReaderFieldsWidget::loadTreeWidgetItems()
57 this->ItemMap.clear();
60 this->TreeWidget->clear();
62 // Clear unique checked item vector
63 this->UniqueCheckedItems.clear();
65 // Recover meta data graph
66 vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
67 this->proxy()->GatherInformation(info);
68 vtkGraph* graph = vtkGraph::SafeDownCast(info->GetInformationData());
72 tree->CheckedShallowCopy(graph);
73 vtkStringArray* names =
74 vtkStringArray::SafeDownCast(tree->GetVertexData()->GetAbstractArray("Names"));
76 vtkIdType root = tree->GetRoot();
77 vtkIdType fst = tree->GetChild(root, 0); // FieldsStatusTree
81 for (int i = 1; i < tree->GetNumberOfChildren(fst); i += 2)
83 // Browse all interessting tree node
86 vtkIdType tsxId = tree->GetChild(fst, i);
87 vtkIdType tsId = tree->GetChild(fst, i - 1);
88 pqTreeWidgetItemObject *ts = new pqTreeWidgetItemObject(this->TreeWidget, QStringList());
90 QString tsxName = QString(names->GetValue(tsxId));
91 ts->setText(0, tsxName);
92 ts->setData(0, Qt::ToolTipRole, QString(names->GetValue(tsId)));
95 for (int maili = 0; maili < tree->GetNumberOfChildren(tsxId); maili++)
97 vtkIdType mailId = tree->GetChild(tsxId, maili);
98 pqTreeWidgetItemObject *mail = new pqTreeWidgetItemObject(ts, QStringList());
100 QString mailName = QString(names->GetValue(mailId));
101 mail->setText(0, mailName);
102 mail->setData(0, Qt::ToolTipRole, QString(names->GetValue(mailId)));
104 QString propertyBaseName = tsxName + "/" + mailName + "/";
107 for (int comsupi = 0; comsupi < tree->GetNumberOfChildren(mailId); comsupi++)
109 vtkIdType comSupId = tree->GetChild(mailId, comsupi);
110 pqTreeWidgetItemObject *comsup = new pqTreeWidgetItemObject(mail, QStringList());
112 QString comsupName = QString(names->GetValue(comSupId));
113 comsup->setText(0, comsupName);
116 vtkIdType geoTypeId = tree->GetChild(comSupId, 1);
117 QString comSupToolTipName(names->GetValue(comSupId));
118 for (int geoi = 0; geoi < tree->GetNumberOfChildren(geoTypeId); geoi++)
120 comSupToolTipName += QString("\n- %1").arg(
121 QString(names->GetValue(tree->GetChild(geoTypeId, geoi))));
123 comsup->setData(0, Qt::ToolTipRole, comSupToolTipName);
125 comsup->setFlags(comsup->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
126 comsup->setChecked(true);
127 QObject::connect(comsup, SIGNAL(checkedStateChanged(bool)), this, SLOT(updateChecks()));
128 this->UniqueCheckedItems.push_back(comsup);
130 QString fullComsupName = propertyBaseName + comsupName + "/";
132 vtkIdType arrId = tree->GetChild(comSupId, 0);
133 for (int arri = 0; arri < tree->GetNumberOfChildren(arrId); arri++)
135 pqTreeWidgetItemObject *array = new pqTreeWidgetItemObject(comsup, QStringList());
138 vtkIdType arrayId = tree->GetChild(arrId, arri);
139 std::string str = names->GetValue(arrayId);
140 this->ItemMap[fullComsupName + QString(str.c_str())] = array;
142 const char* separator = vtkMEDReader::GetSeparator();
143 size_t pos = str.find(separator);
144 std::string name = str.substr(0, pos);
146 array->setText(0, QString(name.c_str()));
147 array->setFlags(array->flags() | Qt::ItemIsUserCheckable);
148 array->setChecked(true);
151 if (tree->GetNumberOfChildren(arrayId) != 0)
154 font.setItalic(true);
155 font.setUnderline(true);
156 array->setData(0, Qt::FontRole, QVariant(font));
158 array->setData(0, Qt::ToolTipRole,
159 QString("Whole \" %1\" mesh").arg(name.c_str()));
160 array->setData(0, Qt::DecorationRole,
161 QPixmap(":/ParaViewResources/Icons/pqCellDataForWholeMesh16.png"));
166 std::string spatialDiscr = str.substr(pos + strlen(separator));
167 QString tooltip = QString(name.c_str() + QString(" (") +
168 spatialDiscr.c_str() + QString(")"));
169 array->setData(0, Qt::ToolTipRole, tooltip);
172 if (spatialDiscr == "P0")
174 pix.load(":/ParaViewResources/Icons/pqCellData16.png");
176 else if (spatialDiscr == "P1")
178 pix.load(":/ParaViewResources/Icons/pqPointData16.png");
180 else if (spatialDiscr == "GAUSS")
182 pix.load(":/ParaViewResources/Icons/pqQuadratureData16.png");
184 else if (spatialDiscr == "GSSNE")
186 pix.load(":/ParaViewResources/Icons/pqElnoData16.png");
188 array->setData(0, Qt::DecorationRole, pix);
191 // Connection and updating checks for each item
192 QObject::connect(array, SIGNAL(checkedStateChanged(bool)), this, SLOT(updateChecks()));
199 this->TreeWidget->expandAll();
202 //-----------------------------------------------------------------------------
203 void pqMEDReaderFieldsWidget::uncheckOtherUniqueItems(pqTreeWidgetItemObject* item)
205 // Uncheck all other items in vector
206 foreach (pqTreeWidgetItemObject* otherItems, this->UniqueCheckedItems)
208 if (otherItems != item)
210 otherItems->setCheckState(0, Qt::Unchecked);
215 //-----------------------------------------------------------------------------
216 void pqMEDReaderFieldsWidget::updateChecks()
218 // Call updateCheck on the sender
219 pqTreeWidgetItemObject* item = qobject_cast<pqTreeWidgetItemObject*>(QObject::sender());
220 this->updateChecks(item);
223 //-----------------------------------------------------------------------------
224 void pqMEDReaderFieldsWidget::updateChecks(pqTreeWidgetItemObject* item)
226 // When a Leaf item is checked, the parent will be checked (partially or not).
227 // Then other parents will be unchecked using uncheckOtherUniqueItems
228 // Then other parent leaf will be unchecked using updateChecks(parent)
230 // When a Parent item is checked, the leaf will be checked
231 // Then other parents will be unchecked using uncheckOtherUniqueItems
232 // Then other parent leaf will be unchecked using updateChecks(parent)
234 if (item->childCount() == 0)
236 // Only first level leaf will transmit checks to parent
237 if (this->TransmitToParent)
239 // Recover correct parent state
240 Qt::CheckState state = item->checkState(0);
241 pqTreeWidgetItemObject* parent =
242 dynamic_cast<pqTreeWidgetItemObject*>(item->QTreeWidgetItem::parent());
243 for (int i = 0; i < parent->childCount(); i++)
245 if (parent->child(i)->checkState(0) != state)
247 state = Qt::PartiallyChecked;
251 parent->setCheckState(0, state);
256 // Check/Uncheck childs, blocking looped call to slot
257 if (item->checkState(0) != Qt::PartiallyChecked)
259 this->TransmitToParent = false;
260 for (int i = 0; i < item->childCount(); i++)
262 pqTreeWidgetItemObject* leaf =
263 dynamic_cast<pqTreeWidgetItemObject*>(item->child(i));
264 leaf->setCheckState(0, item->checkState(0));
266 this->TransmitToParent = true;
269 // Uncheck other unique checked items
270 if (item->checkState(0) != Qt::Unchecked)
272 this->uncheckOtherUniqueItems(item);