-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScalarSourceModel.cpp
More file actions
223 lines (177 loc) · 6 KB
/
ScalarSourceModel.cpp
File metadata and controls
223 lines (177 loc) · 6 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
#include "ScalarSourceModel.h"
#include <DataHierarchyItem.h>
#include <Application.h>
#include <Set.h>
using namespace mv;
using namespace mv::util;
ScalarSourceModel::ScalarSourceModel(QObject* parent /*= nullptr*/) :
QAbstractListModel(parent),
_showFullPathName(true)
{
}
int ScalarSourceModel::rowCount(const QModelIndex& parent /*= QModelIndex()*/) const
{
// Constant point size option plus the number of available datasets
return _datasets.count() + DefaultRow::DatasetStart;
}
int ScalarSourceModel::rowIndex(const Dataset<DatasetImpl>& dataset) const
{
// Only proceed if we have a valid dataset
if (!dataset.isValid())
return -1;
// Return the index of the dataset and add one for the constant point size option
return _datasets.indexOf(dataset) + DefaultRow::DatasetStart;
}
int ScalarSourceModel::columnCount(const QModelIndex& parent /*= QModelIndex()*/) const
{
return 1;
}
QVariant ScalarSourceModel::data(const QModelIndex& index, int role) const
{
// Get row/column of and smart pointer to the dataset
const auto row = index.row();
const auto column = index.column();
const auto scalarDataset = getDataset(row);
switch (role)
{
// Return ruler icon for constant point size and dataset icon otherwise
case Qt::DecorationRole:
{
if (row == DefaultRow::Constant)
return StyledIcon("ruler");
if (row == DefaultRow::Selection)
return StyledIcon("mouse-pointer");
if (row >= DefaultRow::DatasetStart)
return scalarDataset->icon();
break;
}
// Return 'Constant' for constant point size and dataset (full path) GUI name otherwise
case Qt::DisplayRole:
{
if (row >= DefaultRow::DatasetStart)
{
if (row == 2)
return scalarDataset->text();
else
return _showFullPathName ? scalarDataset->getLocation() : scalarDataset->text();
} else {
if (row == DefaultRow::Constant)
return "Constant";
if (row == DefaultRow::Selection)
return "Selection";
}
break;
}
default:
break;
}
return QVariant();
}
void ScalarSourceModel::addDataset(const Dataset<DatasetImpl>& dataset)
{
// Avoid duplicates
if (hasDataset(dataset))
return;
// Insert row into model
beginInsertRows(QModelIndex(), rowCount(), rowCount());
{
// Add the dataset
_datasets << dataset;
}
endInsertRows();
// Get smart pointer to last added dataset
auto& addedDataset = _datasets.last();
// Remove a dataset from the model when it is about to be deleted
connect(&addedDataset, &Dataset<DatasetImpl>::aboutToBeRemoved, this, [this, &addedDataset]() {
removeDataset(addedDataset);
});
// Notify others that the model has updated when the dataset GUI name changes
connect(addedDataset.get(), &DatasetImpl::textChanged, this, [this, &addedDataset]() {
// Get row index of the dataset
const auto colorDatasetRowIndex = rowIndex(addedDataset);
// Only proceed if we found a valid row index
if (colorDatasetRowIndex < 0)
return;
// Establish model index
const auto modelIndex = index(colorDatasetRowIndex, 0);
// Only proceed if we have a valid model index
if (!modelIndex.isValid())
return;
// Notify others that the data changed
emit dataChanged(modelIndex, modelIndex);
});
}
bool ScalarSourceModel::hasDataset(const Dataset<DatasetImpl>& dataset) const
{
return rowIndex(dataset) >= DefaultRow::DatasetStart;
}
void ScalarSourceModel::removeDataset(const Dataset<DatasetImpl>& dataset)
{
// Get row index of the dataset
const auto datasetRowIndex = rowIndex(dataset);
// Update model
beginRemoveRows(QModelIndex(), datasetRowIndex, datasetRowIndex);
{
// Remove dataset from internal vector
_datasets.removeOne(dataset);
}
endRemoveRows();
}
void ScalarSourceModel::removeAllDatasets()
{
// Remove row from model
beginRemoveRows(QModelIndex(), 0, rowCount() - DefaultRow::DatasetStart);
{
// Remove all datasets
_datasets.clear();
}
endRemoveRows();
// And update model data with altered datasets
updateData();
}
const Datasets& ScalarSourceModel::getDatasets() const
{
return _datasets;
}
Dataset<DatasetImpl> ScalarSourceModel::getDataset(const std::int32_t& rowIndex) const
{
// Return empty smart pointer when out of range
if (rowIndex < DefaultRow::DatasetStart || rowIndex > (DefaultRow::DatasetStart + _datasets.count()))
return Dataset<DatasetImpl>();
// Subtract the constant point size row
return _datasets[rowIndex - DefaultRow::DatasetStart];
}
void ScalarSourceModel::setDatasets(const Datasets& datasets)
{
// Notify others that the model layout is about to be changed
emit layoutAboutToBeChanged();
// Add datasets
for (const auto& dataset : datasets)
addDataset(dataset);
// Notify others that the model layout is changed
emit layoutChanged();
// And update model data with datasets
updateData();
}
bool ScalarSourceModel::getShowFullPathName() const
{
return _showFullPathName;
}
void ScalarSourceModel::setShowFullPathName(const bool& showFullPathName)
{
_showFullPathName = showFullPathName;
updateData();
}
void ScalarSourceModel::updateData()
{
// Update the datasets string list model
for (auto dataset : _datasets) {
// Continue if the dataset is not valid
if (!dataset.isValid())
continue;
// Get dataset model index
const auto datasetModelIndex = index(_datasets.indexOf(dataset), 0);
// Notify others that the data changed
emit dataChanged(datasetModelIndex, datasetModelIndex);
}
}