SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
elementtabledelegate.cpp
Go to the documentation of this file.
1// SedSAT3 – Sediment Source Assessment Tool, version 3
2// U.S. Geological Survey. See LICENSE.md and DISCLAIMER.md in the repository root.
3
5#include "QCheckBox"
6#include <QEvent>
7#include "QComboBox"
8#include "QLineEdit"
9#include "QLabel"
10
12 : QStyledItemDelegate(parent)
13{
14 Data=data;
15}
16
17bool ElementTableDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
18 const QStyleOptionViewItem &option, const QModelIndex &index)
19{
20 // One click toggles "Include in analysis". It used to open a check box
21 // editor that needed another click, and that started ticked even for
22 // excluded elements (it read "No" as true).
23 if (index.column() == 4 && event->type() == QEvent::MouseButtonRelease)
24 {
25 const bool included = (index.data(Qt::DisplayRole).toString() == "Yes");
26 model->setData(index, !included, Qt::EditRole);
27 return true;
28 }
29 return QStyledItemDelegate::editorEvent(event, model, option, index);
30}
31
32QWidget *ElementTableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
33 const QModelIndex &index) const
34{
35 if (index.column() == 0) return QStyledItemDelegate::createEditor(parent, option, index);
36
37
38 if (index.column()==1)
39 {
40 QComboBox *editor = new QComboBox(parent);
41 editor->addItem("Element"); editor->addItem("Isotope"); editor->addItem("Particle Size");editor->addItem("Organic Carbon");editor->addItem("Exclude");
42 string element_name = index.sibling(index.row(),0).data(Qt::DisplayRole).toString().toStdString();
43 if (Data->GetElementInformation(element_name)->Role == element_information::role::do_not_include)
44 editor->setCurrentText("Exclude");
45 if (Data->GetElementInformation(element_name)->Role == element_information::role::element)
46 editor->setCurrentText("Element");
47 if (Data->GetElementInformation(element_name)->Role == element_information::role::isotope)
48 editor->setCurrentText("Isotope");
49 if (Data->GetElementInformation(element_name)->Role == element_information::role::particle_size)
50 editor->setCurrentText("Particle Size");
51 if (Data->GetElementInformation(element_name)->Role == element_information::role::organic_carbon)
52 editor->setCurrentText("Organic Carbon");
53
54
55 return editor;
56 }
57 if (index.column()==2)
58 {
59 QComboBox *editor = new QComboBox(parent);
60 vector<string> element_names = Data->GetElementNames();
61 if (index.sibling(index.row(),1).data(Qt::DisplayRole)!="Isotope")
62 { editor->setEnabled(false);
63 editor->setCurrentText("");
64 }
65 else
66 editor->setEnabled(true);
67
68
69 for (unsigned int i=0; i<element_names.size(); i++)
70 if (element_names[i]!=index.sibling(index.row(),0).data(Qt::DisplayRole).toString().toStdString())
71 editor->addItem(QString::fromStdString(element_names[i]));
72 editor->setCurrentText(index.data().toString());
73 return editor;
74 }
75 if (index.column()==3)
76 {
77 QLineEdit *editor = new QLineEdit(parent);
78 if (index.sibling(index.row(),1).data(Qt::DisplayRole)!="Isotope")
79 { editor->setEnabled(false);
80 editor->setText("");
81 }
82 else
83 editor->setEnabled(true);
84
85 QString text = index.data(Qt::DisplayRole).toString();
86
87 editor->setText(text);
88 return editor;
89 }
90 if (index.column()==4)
91 return nullptr; // toggled with one click in editorEvent()
92 if (index.column()==5)
93 return QStyledItemDelegate::createEditor(parent, option, index); // free-text notes
94
95 return nullptr;
96
97}
98
99void ElementTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
100{
101 if (index.column()==5)
102 {
103 QStyledItemDelegate::setEditorData(editor, index);
104 return;
105 }
106 QVariant var = index.data(Qt::DisplayRole);
107 if (index.column()==0)
108 {
109 QLabel *label = static_cast<QLabel*>(editor);
110 label->setText(var.toString());
111 }
112 if (index.column()==1)
113 {
114 QComboBox *combo = static_cast<QComboBox*>(editor);
115 string element_name = index.sibling(index.row(),0).data(Qt::DisplayRole).toString().toStdString();
116 if (Data->GetElementInformation(element_name)->Role == element_information::role::do_not_include)
117 combo->setCurrentText("Exclude");
118 if (Data->GetElementInformation(element_name)->Role == element_information::role::element)
119 combo->setCurrentText("Element");
120 if (Data->GetElementInformation(element_name)->Role == element_information::role::isotope)
121 combo->setCurrentText("Isotope");
122 if (Data->GetElementInformation(element_name)->Role == element_information::role::particle_size)
123 combo->setCurrentText("Particle Size");
124 if (Data->GetElementInformation(element_name)->Role == element_information::role::organic_carbon)
125 combo->setCurrentText("Organic Carbon");
126
127 return;
128 }
129 if (index.column()==2)
130 {
131 QComboBox *combo = static_cast<QComboBox*>(editor);
132 combo->setCurrentText(var.toString());
133 }
134 if (index.column()==3)
135 {
136 QLineEdit *lineedit = static_cast<QLineEdit*>(editor);
137 lineedit->setText(var.toString());
138 }
139 if (index.column()==4)
140 {
141 QCheckBox *chkbox = static_cast<QCheckBox*>(editor);
142 if (var.toString()=="No")
143 chkbox->setChecked(false);
144 else
145 chkbox->setChecked(true);
146 }
147}
148void ElementTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
149 const QModelIndex &index) const
150{
151 if (index.column() == 0 || index.column() == 5)
152 {
153 QStyledItemDelegate::setModelData(editor, model, index);
154 return;
155 }
156 QString element = model->data(index.sibling(index.row(), 0)).toString();
157
158 if (index.column() == 1)
159 {
160 QComboBox *comboBox = static_cast<QComboBox*>(editor);
161 model->setData(index,comboBox->currentText());
162 return;
163 }
164 if (index.column() == 2)
165 {
166 QComboBox *comboBox = static_cast<QComboBox*>(editor);
167 model->setData(index,comboBox->currentText());
168 return;
169 }
170 if (index.column() == 3)
171 {
172 QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
173 model->setData(index,lineEdit->text());
174 return;
175 }
176 if (index.column() == 4)
177 {
178 QCheckBox *chkbox = static_cast<QCheckBox*>(editor);
179 // qDebug()<<chkbox->checkState();
180 if (chkbox->checkState()==Qt::CheckState::Checked)
181 model->setData(index,true);
182 else
183 model->setData(index,false);
184 return;
185 }
186}
187
189 const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
190{
191 editor->setGeometry(option.rect);
192}
193
194void ElementTableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
195 const QModelIndex &index) const
196{
197 QStyledItemDelegate::paint(painter, option, index);
198}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
Toggles the "Include in analysis" column with a single click.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setEditorData(QWidget *editor, const QModelIndex &index) const override
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
ElementTableDelegate(SourceSinkData *data, QObject *parent)
@ element
Standard chemical element concentration.
@ isotope
Isotopic ratio (e.g., 206Pb/207Pb).
@ organic_carbon
Organic carbon content.
@ particle_size
Particle size distribution parameter.
@ do_not_include
Exclude from all analyses.