Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / ParaViewPlugin / pqExtractGroupFieldsWidget.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 "pqExtractGroupFieldsWidget.h"
22
23 #include "vtkMEDReader.h"
24 #include "vtkExtractGroup.h"
25 #include "vtkPVMetaDataInformation.h"
26
27 #include "pqTreeWidget.h"
28 #include "pqTreeWidgetItemObject.h"
29 #include "vtkGraph.h"
30 #include "vtkNew.h"
31 #include "vtkStringArray.h"
32 #include "vtkDataSetAttributes.h"
33 #include "vtkTree.h"
34
35 #include <QStringList>
36
37 //-----------------------------------------------------------------------------
38 pqExtractGroupFieldsWidget::pqExtractGroupFieldsWidget(
39   vtkSMProxy *smproxy, vtkSMProperty *smproperty, QWidget *parentObject)
40 : Superclass(smproxy, smproperty, parentObject)
41 {
42   this->TreeWidget->setHeaderLabel("Groups And Families");
43   this->initializeTreeWidget(smproxy, smproperty);
44 }
45
46 //-----------------------------------------------------------------------------
47 pqExtractGroupFieldsWidget::~pqExtractGroupFieldsWidget()
48 {
49 }
50
51 //-----------------------------------------------------------------------------
52 void pqExtractGroupFieldsWidget::loadTreeWidgetItems()
53 {
54   // Recover Graph
55   vtkPVMetaDataInformation *info(vtkPVMetaDataInformation::New());
56   this->proxy()->GatherInformation(info);
57   vtkGraph* graph = vtkGraph::SafeDownCast(info->GetInformationData());
58   if(!graph)
59     {
60     return;
61     }
62
63   // Clear Tree Widget
64   this->TreeWidget->clear();
65
66   // Clear Item Map
67   this->ItemMap.clear();
68
69   // Create a tree
70   vtkNew<vtkTree> tree;
71   tree->CheckedShallowCopy(graph);
72   vtkStringArray* names = vtkStringArray::SafeDownCast(tree->GetVertexData()->GetAbstractArray("Names"));
73
74   vtkIdType root = tree->GetRoot();
75   vtkIdType mfg = tree->GetChild(root, 1); // MeshesFamsGrps
76
77   vtkIdType mesh = tree->GetChild(mfg, 0); // mesh
78   QString meshName = QString(names->GetValue(mesh));
79
80   this->NItems = 0;
81
82   vtkIdType grps = tree->GetChild(mesh, 0); // grps
83   pqTreeWidgetItemObject* grpsItem = new pqTreeWidgetItemObject(this->TreeWidget, QStringList());
84   this->NItems++;
85   grpsItem->setText(0, QString("Groups of \"" + meshName + "\""));
86
87   vtkIdType fams = tree->GetChild(mesh, 1); // fams
88   pqTreeWidgetItemObject* famsItem = new pqTreeWidgetItemObject(this->TreeWidget, QStringList());
89   this->NItems++;
90   famsItem->setText(0, QString("Families of \"" + meshName + "\""));
91
92   std::map<std::string, int> famDataTypeMap;
93
94   for (int i = 0; i < tree->GetNumberOfChildren(fams); i++)
95     {
96     // Familly Item
97     vtkIdType fam = tree->GetChild(fams, i);
98     pqTreeWidgetItemObject *famItem = new pqTreeWidgetItemObject(famsItem, QStringList());
99     this->NItems++;
100
101     // Familly name
102     std::string str = names->GetValue(fam);
103     const char* separator = vtkMEDReader::GetSeparator();
104     size_t pos = str.find(separator);
105     std::string name = str.substr(0, pos);
106     famItem->setText(0, QString(name.c_str()));
107
108     // Property Name
109     QString propertyName = QString(vtkExtractGroup::GetFamStart()) + QString(str.c_str());
110     this->ItemMap[propertyName] = famItem;
111
112     // Datatype flag
113     int dataTypeFlag = atoi(str.substr(pos + strlen(separator)).c_str());
114     famDataTypeMap[name] = dataTypeFlag;
115
116     // Checkbox
117     famItem->setFlags(famItem->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
118     famItem->setChecked(true);
119
120     // Tooltip
121     QString famToolTip(QString("%1 (%2)").arg(QString(name.c_str())).arg(dataTypeFlag));
122     famItem->setData(0, Qt::ToolTipRole, famToolTip);
123
124     // Pixmap
125     if (dataTypeFlag<0)
126       {
127       famItem->setData(0, Qt::DecorationRole,
128                        QPixmap(":/ParaViewResources/Icons/pqCellData16.png"));
129       }
130     if (dataTypeFlag>0)
131       {
132       famItem->setData(0, Qt::DecorationRole,
133                        QPixmap(":/ParaViewResources/Icons/pqPointData16.png"));
134       }
135     }
136
137   for (int i = 0; i < tree->GetNumberOfChildren(grps); i++)
138     {
139     // Grp Item
140     vtkIdType grp = tree->GetChild(grps, i);
141     pqTreeWidgetItemObject *grpItem = new pqTreeWidgetItemObject(grpsItem, QStringList());
142     this->NItems++;
143
144     // Group name
145     QString name = QString(names->GetValue(grp));
146     grpItem->setText(0, name);
147
148     // Property Name
149     QString propertyName = QString(vtkExtractGroup::GetGrpStart()) + name;
150     this->ItemMap[propertyName] = grpItem;
151
152     // Datatype flag
153     bool hasPoint = false;
154     bool hasCell = false;
155     int dataTypeFlag = 0;
156     for (int j = 0; j < tree->GetNumberOfChildren(grp); j++)
157       {
158       dataTypeFlag = famDataTypeMap[names->GetValue(tree->GetChild(grp, j))];
159       if (dataTypeFlag > 0)
160         {
161         hasPoint = true;
162         }
163       else if (dataTypeFlag < 0)
164         {
165         hasCell = true;
166         }
167       else
168         {
169         dataTypeFlag = 0;
170         break;
171         }
172
173       if (hasPoint && hasCell)
174         {
175         dataTypeFlag = 0;
176         break;
177         }
178       }
179
180     // Checkbox
181     grpItem->setFlags(grpItem->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
182     grpItem->setChecked(true);
183
184     // Tooltip
185     grpItem->setData(0, Qt::ToolTipRole, name);
186
187     // Pixmap
188     if (dataTypeFlag<0)
189       {
190       grpItem->setData(0, Qt::DecorationRole,
191                        QPixmap(":/ParaViewResources/Icons/pqCellData16.png"));
192       }
193     if (dataTypeFlag>0)
194       {
195       grpItem->setData(0, Qt::DecorationRole,
196                        QPixmap(":/ParaViewResources/Icons/pqPointData16.png"));
197       }
198     }
199
200   // Expand Widget
201   this->TreeWidget->expandAll();
202 }