Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / ParaViewPlugin / pqAbstractFieldsWidget.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 "pqAbstractFieldsWidget.h"
22
23 #include "pqArrayListDomain.h"
24 #include "pqTreeWidget.h"
25 #include "pqTreeWidgetItemObject.h"
26
27 #include <QGridLayout>
28 #include <QHeaderView>
29 //-----------------------------------------------------------------------------
30 pqAbstractFieldsWidget::pqAbstractFieldsWidget(
31   vtkSMProxy *smproxy, vtkSMProperty * /*smproperty*/, QWidget *parentObject)
32 : Superclass(smproxy, parentObject)
33 {
34   this->NItems = 0;
35   this->visibleHeader = true;
36   this->setShowLabel(false);
37
38   // Grid Layout
39   QGridLayout* gridLayout = new QGridLayout(this);
40
41   // Tree widget
42   this->TreeWidget = new pqTreeWidget(this);
43   gridLayout->addWidget(this->TreeWidget);
44 }
45
46 //-----------------------------------------------------------------------------
47 pqAbstractFieldsWidget::~pqAbstractFieldsWidget()
48 {
49   delete this->TreeWidget;
50 }
51
52 void pqAbstractFieldsWidget::initializeTreeWidget(vtkSMProxy *smproxy, vtkSMProperty *smproperty)
53 {
54   // Load Tree Widget Items
55   this->loadTreeWidgetItems();
56
57   // Connect Property Domain to the fieldDomain property,
58   // so setFieldDomain is called when the domain changes.
59   vtkSMDomain* arraySelectionDomain = smproperty->GetDomain("array_list");
60   new pqArrayListDomain(this,"fieldsDomain", smproxy, smproperty, arraySelectionDomain);
61
62   // Connect property to field QProperty using a biderectionnal property link
63   this->addPropertyLink(this, "fields", SIGNAL(fieldsChanged()),
64                         smproxy, smproperty);
65
66   // Call slot when the tree is changed
67   QObject::connect(this->TreeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
68                    this, SLOT(onItemChanged(QTreeWidgetItem*, int)));
69
70 }
71
72 //-----------------------------------------------------------------------------
73 QSize pqAbstractFieldsWidget::sizeHint() const
74 {
75   // TreeWidget sizeHintForRow is too low, correcting to +3.
76   int pix = (this->TreeWidget->sizeHintForRow(0) + 3) * this->NItems;
77   int margin[4];
78   this->TreeWidget->getContentsMargins(margin, margin + 1, margin + 2, margin + 3);
79   int h =  pix + margin[1] + margin[3];
80   if (this->visibleHeader)
81     {
82     h += this->TreeWidget->header()->frameSize().height();
83     } 
84   h = std::min(300, h);
85   return QSize(156, h);
86 }
87
88 //-----------------------------------------------------------------------------
89 void pqAbstractFieldsWidget::onItemChanged(QTreeWidgetItem* /*item*/, int column) const
90 {
91   if (column != 0)
92     {
93     return;
94     }
95   emit fieldsChanged();
96 }
97
98 //-----------------------------------------------------------------------------
99 QList< QList< QVariant> > pqAbstractFieldsWidget::getFields() const
100 {
101   // Put together a Field list, using ItemMap
102   QList< QList< QVariant> > ret;
103   QList< QVariant > field;
104   QMap<QString, pqTreeWidgetItemObject*>::const_iterator it;
105   for (it = this->ItemMap.begin(); it != this->ItemMap.end(); it++)
106     {
107     field.clear();
108     field.append(it.key());
109     field.append(it.value()->isChecked());
110     ret.append(field);
111     }
112   return ret;
113 }
114
115 //-----------------------------------------------------------------------------
116 void pqAbstractFieldsWidget::setFields(QList< QList< QVariant> > fields)
117 {
118   // Update each item checkboxes, using fields names and ItemMap
119   QMap<QString, pqTreeWidgetItemObject*>::iterator it;
120   foreach (QList< QVariant> field, fields)
121     {
122     it = this->ItemMap.find(field[0].toString());
123     if (it == this->ItemMap.end())
124       {
125       qDebug("Found an unknow Field in pqAbstractFieldsWidget::setFields, ignoring");
126       continue;
127       }
128     it.value()->setChecked(field[1].toBool());
129     }
130 }
131
132 //-----------------------------------------------------------------------------
133 void pqAbstractFieldsWidget::setFieldsDomain(QList< QList< QVariant> > fields)
134 {
135   // Block signals so the reloading does not trigger
136   // UncheckPropertyModified event
137   this->blockSignals(true);
138
139   // Load the tree widget
140   this->loadTreeWidgetItems();
141
142   // Set fields checkboxes
143   this->setFields(fields);
144
145   // Restore signals
146   this->blockSignals(false);
147 }