diff options
Diffstat (limited to 'src/model/ThreadListModel.cpp')
-rw-r--r-- | src/model/ThreadListModel.cpp | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/src/model/ThreadListModel.cpp b/src/model/ThreadListModel.cpp new file mode 100644 index 0000000..02f0092 --- /dev/null +++ b/src/model/ThreadListModel.cpp | |||
@@ -0,0 +1,153 @@ | |||
1 | #include <sstream> | ||
2 | #include <QQmlEngine> | ||
3 | #include <QDebug> | ||
4 | |||
5 | #include "ThreadListModel.h" | ||
6 | |||
7 | |||
8 | ThreadListModel::ThreadListModel (const QList<Message *> &data, QObject * parent) | ||
9 | : QAbstractListModel (parent), | ||
10 | m_data (data) | ||
11 | { | ||
12 | QListIterator<Message *> it (m_data); | ||
13 | while (it.hasNext ()) | ||
14 | { | ||
15 | rowAdded (it.next ()); | ||
16 | } | ||
17 | } | ||
18 | |||
19 | |||
20 | int | ||
21 | ThreadListModel::rowCount (const QModelIndex & parent) const | ||
22 | { | ||
23 | Q_UNUSED (parent); | ||
24 | return m_data.size (); | ||
25 | } | ||
26 | |||
27 | |||
28 | QVariant | ||
29 | ThreadListModel::data (const QModelIndex & index, int role) const | ||
30 | { | ||
31 | int idx = index.row (); | ||
32 | if (idx < 0 || m_data.count () <= idx) | ||
33 | return QVariant::Invalid; | ||
34 | |||
35 | Message *msg = m_data[idx]; | ||
36 | |||
37 | switch (role) | ||
38 | { | ||
39 | #if FIXME | ||
40 | case NAME: | ||
41 | return msg->name (); | ||
42 | case PUBKEY: | ||
43 | return msg->pubKeyString (); | ||
44 | case TYPE: | ||
45 | return msg->type (); | ||
46 | case DESC: | ||
47 | return msg->desc (); | ||
48 | #endif | ||
49 | default: | ||
50 | return QVariant::Invalid; | ||
51 | } | ||
52 | } | ||
53 | |||
54 | |||
55 | QHash<int, QByteArray> | ||
56 | ThreadListModel::roleNames () const | ||
57 | { | ||
58 | QHash<int, QByteArray> roles; | ||
59 | |||
60 | roles[TITLE] = "title"; | ||
61 | roles[TEXT] = "text"; | ||
62 | roles[TYPE] = "type"; | ||
63 | roles[PUBKEY] = "pubKey"; | ||
64 | |||
65 | return roles; | ||
66 | } | ||
67 | |||
68 | |||
69 | /** | ||
70 | * A message was added to the source data. | ||
71 | */ | ||
72 | void | ||
73 | ThreadListModel::rowAdded (Message *msg) | ||
74 | { | ||
75 | int idx = m_data.count () - 1; | ||
76 | qDebug () << "ThreadListModel::rowAdded: " << idx; | ||
77 | |||
78 | m_thread += new ThreadModel (msg, this); | ||
79 | |||
80 | beginInsertRows (QModelIndex (), idx, idx); | ||
81 | endInsertRows (); | ||
82 | |||
83 | connect (msg, &Message::modified, | ||
84 | this, &ThreadListModel::rowModified); | ||
85 | |||
86 | connect (msg, &Message::childAdded, | ||
87 | m_thread[idx], &ThreadModel::rowAdded); | ||
88 | |||
89 | } | ||
90 | |||
91 | |||
92 | void | ||
93 | ThreadListModel::rowModified (Message *msg) | ||
94 | { | ||
95 | QModelIndex index = this->index (msg->index ()); | ||
96 | emit dataChanged (index, index); | ||
97 | } | ||
98 | |||
99 | |||
100 | int | ||
101 | ThreadListModel::count () | ||
102 | { | ||
103 | return m_data.count (); | ||
104 | } | ||
105 | |||
106 | |||
107 | Message * | ||
108 | ThreadListModel::get (int index) | ||
109 | { | ||
110 | if (index < 0 || count () < index) | ||
111 | return NULL; | ||
112 | |||
113 | Message *msg = m_data[index]; | ||
114 | |||
115 | //Set the ownership so QML don't delete it. | ||
116 | QQmlEngine::setObjectOwnership (msg, QQmlEngine::CppOwnership); | ||
117 | |||
118 | return msg; | ||
119 | } | ||
120 | |||
121 | |||
122 | Message * | ||
123 | ThreadListModel::get (QModelIndex index) | ||
124 | { | ||
125 | if (count () < index.row ()) | ||
126 | return NULL; | ||
127 | |||
128 | return m_data[index.row ()]; | ||
129 | } | ||
130 | |||
131 | |||
132 | #if FIXME | ||
133 | bool | ||
134 | ThreadListModel::contains (QString id) | ||
135 | { | ||
136 | return m_lookupIndex.contains (id); | ||
137 | } | ||
138 | |||
139 | |||
140 | Message * | ||
141 | ThreadListModel::get (QString key) | ||
142 | { | ||
143 | if (m_lookupIndex.contains (key)) | ||
144 | { | ||
145 | Message *msg = m_data[m_lookupIndex[key]]; | ||
146 | |||
147 | return msg; | ||
148 | |||
149 | } | ||
150 | else | ||
151 | return NULL; | ||
152 | } | ||
153 | #endif | ||