-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchipsmodel.cpp
112 lines (97 loc) · 2.65 KB
/
chipsmodel.cpp
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
#include "chipsmodel.h"
#include <QImage>
ChipsModel::ChipsModel(QObject *parent)
: QAbstractListModel(parent)
{
generateData();
}
int ChipsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_data.count();
}
QVariant ChipsModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() > m_data.count())
return QVariant();
switch (role) {
case IdXRole:
return QVariant::fromValue(m_data[index.row()].idX);
case IdYRole:
return QVariant::fromValue(m_data[index.row()].idY);
case XPosRole:
return QVariant::fromValue(m_data[index.row()].xPos);
case YPosRole:
return QVariant::fromValue(m_data[index.row()].yPos);
case ColorRole:
return QVariant::fromValue(m_data[index.row()].color);
case SelectedRole:
return QVariant::fromValue(m_data[index.row()].selected);
default:
break;
}
return QVariant();
}
bool ChipsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.row() < 0 || index.row() > m_data.count())
return false;
switch (role) {
case IdXRole:
return false;
case IdYRole:
return false;
case XPosRole:
m_data[index.row()].xPos = value.toReal();
return true;
case YPosRole:
m_data[index.row()].xPos = value.toReal();
return true;
case ColorRole:
m_data[index.row()].color = value.value<QColor>();
return true;
case SelectedRole:
m_data[index.row()].selected = value.toBool();
return true;
default:
break;
}
return false;
}
QHash<int, QByteArray> ChipsModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[IdXRole] = "idX";
roles[IdYRole] = "idY";
roles[XPosRole] = "xPos";
roles[YPosRole] = "yPos";
roles[ColorRole] = "color";
roles[SelectedRole] = "selected";
return roles;
}
void ChipsModel::generateData()
{
QImage image(":/qt4logo.png");
// Populate scene
int xx = 0;
int nitems = 0;
for (int i = -11000; i < 11000; i += 110) {
++xx;
int yy = 0;
for (int j = -7000; j < 7000; j += 70) {
++yy;
qreal x = (i + 11000) / 22000.0;
qreal y = (j + 7000) / 14000.0;
QColor color(image.pixel(int(image.width() * x), int(image.height() * y)));
ChipData chip;
chip.color = color;
chip.idX = xx;
chip.idY = yy;
chip.xPos = i;
chip.yPos = j;
chip.selected = false;
m_data.append(chip);
++nitems;
}
}
}