-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScalarSourceModel.cpp
More file actions
251 lines (195 loc) · 6.01 KB
/
ScalarSourceModel.cpp
File metadata and controls
251 lines (195 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "ScalarSourceModel.h"
#include <DataHierarchyItem.h>
#include <Application.h>
#include <Set.h>
using namespace mv;
using namespace mv::util;
ScalarSourceModel::ScalarSourceModel(QObject* parent /*= nullptr*/) :
QStandardItemModel(parent),
_showFullPathName(true)
{
appendRow(Row(*this, {})); // Constant source
appendRow(Row(*this, {})); // Selection source
}
ScalarSourceModel::Item::Item(const ScalarSourceModel& scalarSourceModel, const mv::Dataset<>& scalarDataset) :
_scalarSourceModel(scalarSourceModel),
_scalarDataset(scalarDataset)
{
}
QVariant ScalarSourceModel::Item::data(int role) const
{
const auto rowIndex = row();
switch (role)
{
// Return ruler icon for constant point size and dataset icon otherwise
case Qt::DecorationRole:
{
if (rowIndex == DefaultRow::Constant)
return StyledIcon("ruler");
if (rowIndex == DefaultRow::Selection)
return StyledIcon("mouse-pointer");
if (rowIndex >= DefaultRow::DatasetStart)
return _scalarDataset->icon();
break;
}
// Return 'Constant' for constant point size and dataset (full path) GUI name otherwise
case Qt::DisplayRole:
{
if (rowIndex >= DefaultRow::DatasetStart)
{
if (rowIndex == 2)
return _scalarDataset->text();
else
return getScalarSourceModel().getShowFullPathName() ? getScalarDataset()->getLocation() : getScalarDataset()->getGuiName();
}
else {
if (rowIndex == DefaultRow::Constant)
return "Constant";
if (rowIndex == DefaultRow::Selection)
return "Selection";
}
}
default:
break;
}
return {};
}
const ScalarSourceModel& ScalarSourceModel::Item::getScalarSourceModel() const
{
return _scalarSourceModel;
}
const mv::Dataset<>& ScalarSourceModel::Item::getScalarDataset() const
{
return _scalarDataset;
}
ScalarSourceModel::NameItem::NameItem(const ScalarSourceModel& scalarSourceModel, const mv::Dataset<>& scalarDataset) :
Item(scalarSourceModel, scalarDataset)
{
connect(&const_cast<Dataset<>&>(getScalarDataset()), &Dataset<>::guiNameChanged, this, [this]() {
emitDataChanged();
});
}
QVariant ScalarSourceModel::NameItem::data(int role) const
{
const auto rowIndex = row();
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
{
if (rowIndex == DefaultRow::Constant)
return "Constant";
if (rowIndex == DefaultRow::Selection)
return "Selection";
if (rowIndex >= DefaultRow::DatasetStart)
return getScalarDataset()->getGuiName();
break;
}
case Qt::ToolTipRole:
{
if (rowIndex == DefaultRow::Constant)
return "Constant";
if (rowIndex == DefaultRow::Selection)
return "Selection";
if (rowIndex >= DefaultRow::DatasetStart)
return getScalarDataset()->getLocation();
break;
}
default:
break;
}
return Item::data(role);
}
QVariant ScalarSourceModel::IdItem::data(int role) const
{
const auto rowIndex = row();
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
{
if (rowIndex == DefaultRow::Constant || rowIndex == DefaultRow::Selection)
return "";
if (rowIndex >= DefaultRow::DatasetStart)
return getScalarDataset()->getId();
break;
}
case Qt::ToolTipRole:
{
if (rowIndex == DefaultRow::Constant || rowIndex == DefaultRow::Selection)
return "";
if (rowIndex >= DefaultRow::DatasetStart)
return "ID: " + getScalarDataset()->getId();
break;
}
default:
break;
}
return Item::data(role);
}
void ScalarSourceModel::addDataset(const Dataset<DatasetImpl>& dataset)
{
if (hasDataset(dataset))
return;
appendRow(Row(*this, dataset));
}
bool ScalarSourceModel::hasDataset(const Dataset<DatasetImpl>& dataset) const
{
if (!dataset.isValid())
return false;
else
return !match(index(0, static_cast<int>(Column::Id)), Qt::EditRole, dataset->getId(), 1, Qt::MatchExactly).isEmpty();
}
void ScalarSourceModel::removeDataset(const Dataset<DatasetImpl>& dataset)
{
if (!hasDataset(dataset))
return;
const auto matches = match(index(0, static_cast<int>(Column::Id)), Qt::EditRole, dataset->getId(), 1, Qt::MatchExactly);
if (!matches.isEmpty())
removeRow(matches.first().row());
}
void ScalarSourceModel::removeAllDatasets()
{
removeRows(DefaultRow::DatasetStart, rowCount() - DefaultRow::DatasetStart);
}
Datasets ScalarSourceModel::getDatasets() const
{
Datasets datasets;
for (int rowIndex = DefaultRow::DatasetStart; rowIndex < rowCount(); ++rowIndex)
{
if (auto item = dynamic_cast<Item*>(itemFromIndex(index(rowIndex, 0)))) {
const auto dataset = item->getScalarDataset();
if (dataset.isValid())
datasets.append(dataset);
}
}
return datasets;
}
Dataset<DatasetImpl> ScalarSourceModel::getDataset(const std::int32_t& rowIndex) const
{
if (auto item = dynamic_cast<Item*>(itemFromIndex(index(rowIndex, 0))))
return item->getScalarDataset();
return {};
}
void ScalarSourceModel::setDatasets(const Datasets& datasets)
{
for (const auto& dataset : datasets)
addDataset(dataset);
}
std::int32_t ScalarSourceModel::getRowIndex(const Dataset<DatasetImpl>& dataset) const
{
if (!dataset.isValid())
return -1;
const auto matches = match(index(0, static_cast<int>(Column::Id)), Qt::EditRole, dataset->getId(), 1, Qt::MatchExactly);
if (!matches.isEmpty())
return matches.first().row();
return -1;
}
bool ScalarSourceModel::getShowFullPathName() const
{
return _showFullPathName;
}
void ScalarSourceModel::setShowFullPathName(const bool& showFullPathName)
{
_showFullPathName = showFullPathName;
}