-
Notifications
You must be signed in to change notification settings - Fork 0
/
propertymodel.cpp
174 lines (159 loc) · 4.4 KB
/
propertymodel.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "propertymodel.h"
#include "tracksmodel.h"
PropertyModel::PropertyModel(TracksModel *database, QObject *parent):
QAbstractItemModel(parent), mDatabase(database), mRecord(NULL)
{
connect(mDatabase, SIGNAL(dbChanged()), this, SLOT(updateRowList()));
connect(mDatabase, SIGNAL(recordChanged()), this, SLOT(updateRecord()));
connect(mDatabase, SIGNAL(recordSelected(Record*)),
this, SLOT(setRecord(Record*)));
updateRowList();
}
QModelIndex PropertyModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid())
return QModelIndex();
return createIndex(row, column);
}
QModelIndex PropertyModel::parent(const QModelIndex &/*child*/) const
{
return QModelIndex();
}
int PropertyModel::rowCount(const QModelIndex &parent) const
{
if (!mRecord || parent.isValid())
return 0;
return mPropNames.size() + fixedRowNumber;
}
int PropertyModel::columnCount(const QModelIndex &/*parent*/) const
{
return columnNumber;
}
QVariant PropertyModel::data(const QModelIndex &index, int role) const
{
if (!isValidIndex(index) || !mRecord)
return QVariant();
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case nameColumn:
switch(index.row()) {
case titleRow: return tr("Title");
case artistRow: return tr("Artist");
case albumRow: return tr("Album");
case tagsRow: return tr("Tags");
default: return mPropNames[index.row() - fixedRowNumber];
}
case valueColumn:
switch(index.row()) {
case titleRow: return mRecord->title;
case artistRow: return mRecord->artist;
case albumRow: return mRecord->album;
case tagsRow: return mRecord->tags().join(", ");
default: {
QVariant prop = mRecord->property(
mPropNames[index.row() - fixedRowNumber]);
if (prop.type() == QVariant::String)
return prop;
else if (prop.type() == QVariant::StringList)
return prop.toStringList().join(", ");
else
return QVariant();
}
}
}
case Qt::EditRole:
if (index.column() != valueColumn)
return QVariant();
switch(index.row()) {
case titleRow: return mRecord->title;
case artistRow: return mRecord->artist;
case albumRow: return mRecord->album;
case tagsRow: return mRecord->tags();
default: return mRecord->property(
mPropNames[index.row() - fixedRowNumber]);
}
case Qt::UserRole:
if (index.column() != valueColumn)
return QVariant();
switch(index.row()) {
case titleRow:
case artistRow:
case albumRow:
return QVariant();
case tagsRow:
return mDatabase->allTags();
default: {
QString pName = mPropNames[index.row() - fixedRowNumber];
if (mDatabase->propertyType(pName)->type == Property::Category)
return mDatabase->allCategories(pName);
else
return QVariant();
}
}
}
return QVariant();
}
bool PropertyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!isValidIndex(index) || !mRecord)
return false;
if (role != Qt::EditRole || index.column() != valueColumn)
return false;
switch(index.row()) {
case titleRow: mRecord->title = value.toString(); break;
case artistRow: mRecord->artist = value.toString(); break;
case albumRow: mRecord->album = value.toString(); break;
case tagsRow: mRecord->setProperty(QString(), value);
default: {
if (!mRecord->setProperty(
mPropNames[index.row() - fixedRowNumber],
value))
return false;
break;
}
}
mDatabase->updateRecord();
emit dataChanged(index, index);
return true;
}
QVariant PropertyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical || role != Qt::DisplayRole)
return QVariant();
switch (section) {
case nameColumn: return tr("Name");
case valueColumn: return tr("Value");
default:;
}
return QVariant();
}
Qt::ItemFlags PropertyModel::flags(const QModelIndex &index) const
{
if (index.column() == nameColumn)
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
else
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
}
bool PropertyModel::isValidIndex(const QModelIndex &index) const
{
return !index.parent().isValid() &&
index.column() >= 0 && index.column() < columnCount() &&
index.row() >= 0 && index.row() < rowCount();
}
void PropertyModel::updateRowList()
{
beginResetModel();
mPropNames = mDatabase->allProperties();
endResetModel();
}
void PropertyModel::updateRecord()
{
setRecord(mRecord);
}
void PropertyModel::setRecord(Record *rec)
{
beginResetModel();
mRecord = rec;
endResetModel();
}