-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColorSourceModel.cpp
More file actions
183 lines (136 loc) · 4.06 KB
/
ColorSourceModel.cpp
File metadata and controls
183 lines (136 loc) · 4.06 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
#include "ColorSourceModel.h"
#include <Application.h>
#include <Set.h>
using namespace mv;
using namespace mv::util;
QVariant ColorSourceModel::ConstantColorItem::data(int role /*= Qt::UserRole + 1*/) const
{
switch (role)
{
case Qt::DecorationRole:
return StyledIcon("palette");
case Qt::DisplayRole:
return "Constant";
default:
break;
}
return {};
}
QVariant ColorSourceModel::ScatterLayoutItem::data(int role /*= Qt::UserRole + 1*/) const
{
switch (role)
{
case Qt::DecorationRole:
return StyledIcon("palette");
case Qt::DisplayRole:
return "Scatter layout";
default:
break;
}
return {};
}
ColorSourceModel::DatasetItem::DatasetItem(Dataset<DatasetImpl> dataset, ColorSourceModel* colorSourceModel) :
_dataset(dataset),
_colorSourceModel(colorSourceModel)
{
Q_ASSERT(_dataset.isValid());
Q_ASSERT(_colorSourceModel);
if (!dataset.isValid() || !_colorSourceModel)
return;
connect(&_dataset, &Dataset<DatasetImpl>::aboutToBeRemoved, this, [this]() {
_colorSourceModel->removeDataset(_dataset);
});
connect(_dataset.get(), &DatasetImpl::textChanged, this, [this]() {
emitDataChanged();
});
connect(&_dataset, &Dataset<DatasetImpl>::dataChanged, this, [this]() {
_colorSourceModel->dataChanged(_dataset);
});
connect(_colorSourceModel, &ColorSourceModel::showFullPathNameChanged, this, [this]() {
emitDataChanged();
});
}
QVariant ColorSourceModel::DatasetItem::data(int role /*= Qt::UserRole + 1*/) const
{
switch (role)
{
case Qt::DecorationRole:
return _dataset->icon();
case Qt::DisplayRole:
return row() == 2 ? _dataset->text() : (_colorSourceModel->getShowFullPathName() ? _dataset->getLocation() : _dataset->text());
case 200:
return _dataset->getId();
default:
break;
}
return {};
}
mv::Dataset<mv::DatasetImpl>& ColorSourceModel::DatasetItem::getDataset()
{
return _dataset;
}
ColorSourceModel::ColorSourceModel(QObject* parent /*= nullptr*/) :
QStandardItemModel(parent),
_showFullPathName(true)
{
appendRow(new ConstantColorItem());
appendRow(new ScatterLayoutItem());
}
void ColorSourceModel::addDataset(Dataset<DatasetImpl> dataset)
{
appendRow(new DatasetItem(dataset, this));
}
void ColorSourceModel::removeDataset(Dataset<DatasetImpl> dataset)
{
const auto matches = match(index(0, 0), 200, dataset->getId(), 1, Qt::MatchExactly);
if (matches.isEmpty())
return;
removeRows(matches.first().row(), 1);
}
void ColorSourceModel::removeAllDatasets()
{
}
Datasets ColorSourceModel::getDatasets() const
{
const auto numberOfRows = rowCount();
if (numberOfRows <= 2)
return {};
Datasets datasets;
for (int rowIndex = 0; rowIndex < numberOfRows; ++rowIndex) {
auto datasetItem = dynamic_cast<DatasetItem*>(itemFromIndex(index(rowIndex, 0)));
if (datasetItem)
datasets << datasetItem->getDataset();
}
return datasets;
}
Dataset<DatasetImpl> ColorSourceModel::getDataset(std::int32_t rowIndex) const
{
if (rowIndex <= 1)
return {};
auto datasetItem = dynamic_cast<DatasetItem*>(itemFromIndex(index(rowIndex, 0)));
if (datasetItem)
return datasetItem->getDataset();
return {};
}
int ColorSourceModel::rowIndex(Dataset<DatasetImpl> dataset) const
{
if (!dataset.isValid())
return -1;
if (!hasIndex(0, 0))
return -1;
const auto matches = match(index(0, 0), 200, dataset->getId(), 1, Qt::MatchExactly);
if (matches.isEmpty())
return -1;
return matches.first().row();
}
bool ColorSourceModel::getShowFullPathName() const
{
return _showFullPathName;
}
void ColorSourceModel::setShowFullPathName(const bool& showFullPathName)
{
if (showFullPathName == _showFullPathName)
return;
_showFullPathName = showFullPathName;
emit showFullPathNameChanged(_showFullPathName);
}