-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdisplaytablemodel.cc
125 lines (115 loc) · 3.34 KB
/
displaytablemodel.cc
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
#include "displaytablemodel.hpp"
#include "starrating.h"
#include <QApplication>
#include <QStyle>
DisplayTableModel::DisplayTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
auto metaEnums = QMetaEnum::fromType<Property>();
for (int i = 0; i < metaEnums.keyCount(); ++i) {
m_headerDatas << metaEnums.key(i);
}
}
auto DisplayTableModel::data(const QModelIndex &index, int role) const -> QVariant
{
if (!index.isValid()) {
return {};
}
auto row = index.row();
auto col = index.column();
const auto &data = m_datas.at(row);
switch (role) {
case Qt::TextAlignmentRole: return Qt::AlignCenter;
case Qt::CheckStateRole:
if (ID == col) {
return data.checked() ? Qt::Checked : Qt::Unchecked;
}
break;
case Qt::DecorationRole:
if (ID == col) {
return qApp->style()->standardIcon(QStyle::SP_ComputerIcon);
}
break;
case Qt::WhatsThisRole:
case Qt::ToolTipRole:
case Qt::DisplayRole:
case Qt::EditRole: { //双击为空需添加
switch (col) {
case ID: return row;
case TITLE: return data.title();
case NUMBER: return data.number();
case STATE: return data.state();
case PROCESS: return data.process();
case RICHTEXT: return data.richText();
case DETAILS: return tr("Details");
default: break;
}
break;
case Qt::UserRole:
if (col == RATING) {
return QVariant::fromValue(StarRating(data.rating()));
}
return QVariant::fromValue(data);
}
default: break;
}
return {};
}
auto DisplayTableModel::setData(const QModelIndex &index, const QVariant &value, int role) -> bool
{
if (!index.isValid()) {
return false;
}
auto row = index.row();
auto col = index.column();
auto data = m_datas.at(row);
switch (role) {
case Qt::CheckStateRole:
if (ID == col) {
data.setChecked(value.toBool());
}
break;
case Qt::EditRole: {
switch (col) {
case TITLE: data.setTitle(value.toString()); break;
case NUMBER: data.setNumber(value.toInt()); break;
case STATE: data.setState(value.toString()); break;
case PROCESS: data.setProcess(value.toInt()); break;
case RATING: data.setRating(value.value<StarRating>().starCount()); break;
case RICHTEXT: data.setRichText(value.toString()); break;
default: break;
}
}
default: break;
}
emit dataChanged(index, index);
return true;
}
auto DisplayTableModel::headerData(int section, Qt::Orientation orientation, int role) const
-> QVariant
{
if (section < 0 || section >= m_headerDatas.size() || orientation != Qt::Horizontal) {
return {};
}
switch (role) {
case Qt::TextAlignmentRole: return Qt::AlignCenter;
case Qt::WhatsThisRole:
case Qt::ToolTipRole:
case Qt::DisplayRole: return m_headerDatas.at(section);
default: break;
}
return {};
}
Qt::ItemFlags DisplayTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) {
return {};
}
auto flags = QAbstractTableModel::flags(index);
if (index.column() == ID) {
flags |= Qt::ItemIsUserCheckable;
} else {
flags |= Qt::ItemIsEditable;
}
return flags;
}