This repository was archived by the owner on Feb 16, 2021. It is now read-only.
forked from pinkierton/harkach
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadModel.h
More file actions
106 lines (86 loc) · 2.56 KB
/
ThreadModel.h
File metadata and controls
106 lines (86 loc) · 2.56 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
#ifndef THREADMODEL_H
#define THREADMODEL_H
#include "ThreadObject.h"
#include <QAbstractListModel>
#include <QDebug>
#include <QObjectList>
#include <QQmlListProperty>
class ThreadModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit ThreadModel(QObject *parent = nullptr) : QAbstractListModel(parent)
{
}
~ThreadModel() {
qDeleteAll(mThreads);
mThreads.clear();
}
enum ThreadRoles {
NumRole = Qt::UserRole + 1,
PostsCountRole,
CommentRole,
SubjectRole,
TimeStampRole,
LasthitRole,
FilesRole
};
int rowCount(const QModelIndex & parent = QModelIndex()) const override {
Q_UNUSED(parent)
return mThreads.count();
}
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override {
if (index.row() < 0 || index.row() >= rowCount())
return QVariant();
using ThreadObjectPtr = ThreadObject*;
const ThreadObjectPtr& thread = mThreads.at(index.row());
if (role == NumRole)
return thread->num();
else if (role == PostsCountRole)
return thread->postsCount();
else if (role == CommentRole)
return thread->comment();
else if (role == SubjectRole)
return thread->subject();
else if (role == TimeStampRole)
return thread->timeStamp();
else if (role == LasthitRole)
return thread->lasthit();
else if (role == FilesRole) {
QObjectList files;
for (Attachment* att : thread->files()) {
files << att;
}
return QVariant::fromValue(files);
}
return QVariant();
}
void resetThreads() {
if (!mThreads.empty()) {
qDeleteAll(mThreads);
mThreads.clear();
}
beginResetModel();
endResetModel();
}
void setThreads(QList<ThreadObject*>&& threads) {
mThreads = threads;
beginInsertRows(QModelIndex(), 0, 0+rowCount()-1);
endInsertRows();
}
protected:
QHash<int, QByteArray> roleNames() const override {
return {
{NumRole, "post_num" },
{PostsCountRole, "posts_count"},
{CommentRole, "comment" },
{SubjectRole, "subject" },
{TimeStampRole, "time_stamp" },
{LasthitRole, "lasthit" },
{FilesRole, "files" }
};
}
private:
QList<ThreadObject*> mThreads;
};
#endif // THREADMODEL_H