forked from nuttyartist/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaglistmodel.cpp
More file actions
82 lines (74 loc) · 1.88 KB
/
Copy pathtaglistmodel.cpp
File metadata and controls
82 lines (74 loc) · 1.88 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
#include "taglistmodel.h"
#include "tagpool.h"
#include <QDebug>
#include "nodepath.h"
TagListModel::TagListModel(QObject *parent) :
QAbstractListModel(parent),
m_tagPool{nullptr}
{
}
void TagListModel::setTagPool(TagPool *tagPool)
{
beginResetModel();
m_tagPool = tagPool;
connect(tagPool, &TagPool::dataReseted, this, [this] {
beginResetModel();
updateTagData();
endResetModel();
});
endResetModel();
}
void TagListModel::setModelData(QSet<int> data)
{
if (!m_tagPool) {
qDebug() << __FUNCTION__ << "Tag pool is not init yet";
return;
}
beginResetModel();
m_ids = data;
updateTagData();
endResetModel();
}
void TagListModel::addTag(int tagId)
{
if (!m_tagPool) {
qDebug() << __FUNCTION__ << "Tag pool is not init yet";
return;
}
if (m_tagPool->contains(tagId)) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_data.append(m_tagPool->getTag(tagId));
endInsertRows();
} else {
qDebug() << __FUNCTION__ << "Tag is not in pool:" << tagId;
}
}
int TagListModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return m_data.count();
}
QVariant TagListModel::data(const QModelIndex & index, int role) const {
if (!index.isValid()) {
return QVariant();
}
const TagData &tag = m_data[index.row()];
if (role == IdRole) {
return tag.id();
} else if (role == NameRole) {
return tag.name();
} else if (role == ColorRole) {
return tag.color();
}
return QVariant();
}
void TagListModel::updateTagData()
{
m_data.clear();
for (const auto& id : QT_AS_CONST(m_ids)) {
if (m_tagPool->contains(id)) {
m_data.append(m_tagPool->getTag(id));
} else {
qDebug() << __FUNCTION__ << "Tag is not in pool:" << id;
}
}
}