SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
elementstablemodel.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 "numericparsing.h"
6
7ElementTableModel::ElementTableModel(SourceSinkData *_data, QObject *parent) : QAbstractTableModel(parent)
8{
9 Data = _data;
10}
11int ElementTableModel::rowCount(const QModelIndex &/*index*/) const
12{
13 return Data->GetElementNames().size();;
14}
15int ElementTableModel::columnCount(const QModelIndex &/*index*/) const
16{
17 return 6;
18}
19QVariant ElementTableModel::data(const QModelIndex &index, int role) const
20{
21 if (role == Qt::DisplayRole)
22 { if (index.column()==0)
23 {
24 return QString::fromStdString(Data->GetElementNames()[index.row()]);
25 }
26 if (index.column()==1)
27 {
28 if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->Role == element_information::role::do_not_include)
29 return "Exclude";
30 if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->Role == element_information::role::element)
31 return "Element";
32 if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->Role == element_information::role::isotope)
33 return "Isotope";
34 if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->Role == element_information::role::particle_size)
35 return "Particle Size";
36 if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->Role == element_information::role::organic_carbon)
37 return "Organic Carbon";
38
39 }
40 if (index.column()==2)
41 {
42 if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->Role == element_information::role::isotope)
43 return QString::fromStdString(Data->GetElementInformation(Data->GetElementNames()[index.row()])->base_element);
44 else
45 return "";
46 }
47 if (index.column()==3)
48 {
49 if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->Role == element_information::role::isotope)
50 return QString::number(Data->GetElementInformation(Data->GetElementNames()[index.row()])->standard_ratio);
51 else
52 return "";
53 }
54 if (index.column()==4)
55 {
56 if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->include_in_analysis)
57 return "Yes";
58 else
59 return "No";
60 }
61 }
62 if ((role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole) && index.column()==5)
63 return QString::fromStdString(Data->GetElementInformation(Data->GetElementNames()[index.row()])->notes);
64 if (role == Qt::ItemIsEnabled)
65 {
66 if (index.column()==2 || index.column()==3)
67 { if (Data->GetElementInformation(Data->GetElementNames()[index.row()])->Role == element_information::role::isotope)
68 return true;
69 else
70 return false;
71 }
72 }
73 return QVariant();
74
75}
76QVariant ElementTableModel::headerData(int section, Qt::Orientation orientation, int role) const
77{
78 if (role == Qt::DisplayRole)
79 {
80 if (orientation == Qt::Horizontal)
81 { if (section==0)
82 {
83 return "Constituent";
84 }
85 if (section==1)
86 {
87 return "Constituent Type";
88 }
89 if (section==2)
90 {
91 return "Base Element";
92 }
93 if (section==3)
94 {
95 return "Standard isotope ratio";
96 }
97 if (section==4)
98 {
99 return "Include in analysis";
100 }
101 if (section==5)
102 {
103 return "Notes";
104 }
105 }
106 }
107 return QVariant();
108}
109bool ElementTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
110{
111 if (role==Qt::EditRole)
112 {
113
114 string element = Data->GetElementNames()[index.row()];
115 if (index.column()==1)
116 {
117 if (value.toString()=="Element")
118 Data->GetElementInformation(element)->Role = element_information::role::element;
119 if (value.toString()=="Isotope")
120 Data->GetElementInformation(element)->Role = element_information::role::isotope;
121 if (value.toString()=="Exclude")
122 Data->GetElementInformation(element)->Role = element_information::role::do_not_include;
123 if (value.toString()=="Particle Size")
124 Data->GetElementInformation(element)->Role = element_information::role::particle_size;
125 if (value.toString()=="Organic Carbon")
126 Data->GetElementInformation(element)->Role = element_information::role::organic_carbon;
127
128 }
129 if (index.column()==2)
130 {
131 Data->GetElementInformation(element)->base_element = value.toString().toStdString();
132 }
133 if (index.column()==3)
134 {
135 // Refuse an edit that is not a finite number, leaving the old value
136 double ratio = 0;
137 QString reason;
138 if (!parseNumericValue(value, ratio, reason))
139 return false;
140 Data->GetElementInformation(element)->standard_ratio = ratio;
141 }
142 if (index.column()==4)
143 {
144 // QVariant("No").toBool() is true, so text is compared explicitly
145 const bool include = (value.typeId() == QMetaType::Bool)
146 ? value.toBool()
147 : value.toString().compare("Yes", Qt::CaseInsensitive) == 0 || value.toString() == "true" || value.toString() == "1";
148 Data->GetElementInformation(element)->include_in_analysis = include;
149 }
150 if (index.column()==5)
151 {
152 Data->GetElementInformation(element)->notes = value.toString().toStdString();
153 }
154
155 }
156 QString result = "Done";
157 emit dataChanged(index, index); // so the view shows the change at once
158 emit editCompleted(result);
159 return true;
160}
161Qt::ItemFlags ElementTableModel::flags(const QModelIndex & index) const
162{
163 if (index.column()!=0 && index.column()!=4)
164 return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ;
165 else if (index.column()==4)
166 return Qt::ItemIsSelectable | Qt::ItemFlag::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsEditable ;
167 else
168 return Qt::ItemIsSelectable | Qt::ItemIsEnabled ;
169}
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
ElementTableModel(SourceSinkData *Data, QObject *parent=nullptr)
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void editCompleted(const QString &)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
SourceSinkData * Data
bool parseNumericValue(const QVariant &value, double &out, QString &error)
Parses a spreadsheet cell value as a finite number.
The one place where data values entering SedSAT3 are checked.
@ 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.