-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorCountModel.cpp
More file actions
120 lines (106 loc) · 2.82 KB
/
ColorCountModel.cpp
File metadata and controls
120 lines (106 loc) · 2.82 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
#include "ColorCountModel.h"
#include <QColor>
#include <QDebug>
#include <QPixmap>
#include <QIcon>
ColorCountModel::ColorCountModel(QObject *parent)
: QAbstractItemModel(parent)
{
}
ColorCountModel::~ColorCountModel()
{
}
QModelIndex ColorCountModel::index(int row, int column,
const QModelIndex &parent) const
{
if(parent==QModelIndex()) {
return createIndex(row, column, 0);
} else {
return QModelIndex();
}
}
QModelIndex ColorCountModel::index(const QRgb& color) const
{
for(int i=0;i<m_colors.size();i++)
{
if(m_colors.at(i).color == color) {
return index(i, 0);
}
}
return QModelIndex();
}
QModelIndex ColorCountModel::parent(const QModelIndex &child) const
{
Q_UNUSED(child);
return QModelIndex();
}
void ColorCountModel::setColors(const QList<ColorCount>& colors)
{
//beginInsertRows(QModelIndex(), 0, colors.count()-m_colors.count());
m_colors = colors;
//endInsertRows();
emit layoutChanged();
//emit dataChanged(index(0, 0), index(m_colors.count(), 2));
}
int ColorCountModel::rowCount(const QModelIndex &parent) const
{
if(parent==QModelIndex()) {
return m_colors.size();
} else
return 0;
}
int ColorCountModel::columnCount(const QModelIndex &parent) const
{
if(parent==QModelIndex())
return 2;
else
return 0;
}
QVariant ColorCountModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= m_colors.size())
return QVariant();
if (index.column() >= 2)
return QVariant();
if (role == Qt::DisplayRole) {
if(index.column()==0) {
return QColor(m_colors.at(index.row()).color).name();
} else if(index.column()==1) {
return m_colors.at(index.row()).count;
} else {
return QVariant();
}
} else if(role==Qt::DecorationRole && index.column()==0) {
QColor color(m_colors.at(index.row()).color);
QPixmap iconPixmap(16, 16);
iconPixmap.fill(color);
QIcon icon(iconPixmap);
return icon;
} else
return QVariant();
}
QColor ColorCountModel::color(int row) const
{
if(row >= m_colors.count()) return QColor();
return m_colors.at(row).color;
}
QVariant ColorCountModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role == Qt::DisplayRole) {
if(orientation==Qt::Horizontal) {
if(section==0) {
return tr("Color");
} else if(section==1) {
return tr("Count");
} else {
return QVariant();
}
} else {
return QString(section+1);
}
} else
return QVariant();
}