-
Notifications
You must be signed in to change notification settings - Fork 4
/
palettemodel.cpp
152 lines (120 loc) · 3.08 KB
/
palettemodel.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
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
#include "color.h"
#include "colormanager.h"
#include "palettemodel.h"
#define COLUMN_ID 0
#define COLUMN_NAME 1
PaletteModel::PaletteModel(QObject *parent)
: QAbstractItemModel(parent)
{
list_ = NULL;
}
PaletteModel::~PaletteModel()
{
}
void PaletteModel::setColorManager(ColorManager *cm)
{
disconnect(this, SLOT(colorAppended()));
disconnect(this, SLOT(colorInserted(int)));
disconnect(this, SLOT(colorDeleted(int)));
disconnect(this, SLOT(colorSwapped(int, int)));
if (cm) {
list_ = &cm->colorList();
connect(cm, SIGNAL(colorAppended()),
this, SLOT(colorAppended()));
connect(cm, SIGNAL(colorInserted(int)),
this, SLOT(colorInserted(int)));
connect(cm, SIGNAL(colorDeleted(int)), this,
SLOT(colorDeleted(int)));
connect(cm, SIGNAL(colorSwapped(int, int)), this,
SLOT(colorSwapped(int, int)));
reset();
} else {
list_ = NULL;
}
}
int PaletteModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
int PaletteModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
if (!list_)
return 0;
return list_->size();
}
QModelIndex PaletteModel::parent(const QModelIndex &index) const
{
Q_UNUSED(index);
return QModelIndex();
}
QModelIndex PaletteModel::index(int row, int column,
const QModelIndex &parent) const
{
/* there are no children */
if (parent.isValid())
return QModelIndex();
return createIndex(row, column);
}
QVariant PaletteModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
Q_UNUSED(orientation);
if (role == Qt::DisplayRole) {
if (section == 0)
return tr("Color No.");
else if (section == 1)
return tr("Name");
}
return QVariant();
}
QVariant PaletteModel::data(const QModelIndex &index, int role) const
{
/* there are no children */
if (index.parent().isValid())
return QVariant();
if (!list_)
return QVariant();
const Color *c = (*list_)[index.row()];
if (!c)
return QVariant();
if (role == Qt::DisplayRole) {
if (index.column() == COLUMN_ID)
return c->id();
else if (index.column() == COLUMN_NAME)
return c->name();
} else if (role == Qt::TextAlignmentRole) {
if (index.column() == COLUMN_ID)
return Qt::AlignHCenter;
} else if (role == Qt::DecorationRole) {
if (index.column() == COLUMN_NAME)
return c->color();
}
return QVariant();
}
void PaletteModel::resetModel()
{
reset();
}
void PaletteModel::colorAppended()
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
endInsertRows();
}
void PaletteModel::colorInserted(int before)
{
beginInsertRows(QModelIndex(), before, before);
endInsertRows();
}
void PaletteModel::colorDeleted(int index)
{
beginRemoveRows(QModelIndex(), index, index);
endRemoveRows();
}
void PaletteModel::colorSwapped(int index1, int index2)
{
emit dataChanged(index(index1, 0), index(index1, columnCount()));
emit dataChanged(index(index2, 0), index(index2, columnCount()));
}