-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchapteritem.cpp
More file actions
58 lines (50 loc) · 1.53 KB
/
chapteritem.cpp
File metadata and controls
58 lines (50 loc) · 1.53 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
#include "chapteritem.h"
void ChapterItem::setItemProperty(ChapterProperties prop, const QVariant & value)
{
setData(value, prop);
}
QVariant ChapterItem::data(int role) const
{
switch (role) {
case Qt::DisplayRole:
if (data(ChapterTitle).isValid()) {
return data(ChapterTitle);
}
if (hasChildren()) {
return QObject::tr("Table of Contents");
}
break;
}
return QStandardItem::data(role);
}
template<
class Trait,
std::enable_if_t <
std::is_same<ChapterItem *, Trait>::value
|| std::is_same<const ChapterItem *, Trait>::value,
bool
> = true
>
static void forEachTemplate(Trait root, std::function<void (Trait)> callback) {
if (!root) return;
callback(root);
if (root->hasChildren()) {
int childrenCount = root->rowCount();
for (int i = 0; i < childrenCount; i++) {
QStandardItem * nextItem = root->child(i);
ChapterItem * currentItem = static_cast<ChapterItem *>(nextItem);
forEachTemplate<Trait>(currentItem, callback);
}
}
}
// DFS iteration
void ChapterItem::forEach(ChapterItem *root, std::function<void (ChapterItem *)> callback)
{
forEachTemplate<ChapterItem *>(root, callback);
}
// DFS iteration
void ChapterItem::forEach(const ChapterItem *root,
std::function<void (const ChapterItem *)> callback)
{
forEachTemplate<const ChapterItem *>(root, callback);
}