aboutsummaryrefslogtreecommitdiff
path: root/documentoutlinemodel.cpp
blob: 1787d9e3175c555e4ca491d26b8a11972ca82e25 (plain) (blame)
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
#include "documentoutlinemodel.hpp"

#include <QModelIndex>

DocumentOutlineModel::DocumentOutlineModel() :
    QAbstractItemModel(),
    root()
{

}

void DocumentOutlineModel::beginBuild()
{
    beginResetModel();
    root = Node {
        nullptr,
        "<ROOT>",
        0,
        QVector<Node> { },
};
}

void DocumentOutlineModel::appendH1(const QString &title)
{
    root.children.append(Node {
        &root,
        title,
        1,
        QVector<Node> { },
    });
}

void DocumentOutlineModel::appendH2(const QString &title)
{
    if(root.children.size() == 0) {
        root.children.append(Node {
            &root,
            "<missing layer>",
            1,
            QVector<Node> { },
        });
    }
    auto & parent = root.children.last();
    parent.children.append(Node {
        &parent,
        title,
        2,
        QVector<Node> { },
    });
}

void DocumentOutlineModel::appendH3(const QString &title)
{

}

void DocumentOutlineModel::endBuild()
{
    endResetModel();
}

QModelIndex DocumentOutlineModel::index(int row, int column, const QModelIndex &parent) const
{
    if (not hasIndex(row, column, parent))
        return QModelIndex();

    Node const * parentItem;

    if (!parent.isValid())
        parentItem = &this->root;
    else
        parentItem = static_cast<Node*>(parent.internalPointer());

    Node const * childItem = &parentItem->children[row];
    if (childItem)
        return createIndex(row, column, reinterpret_cast<quintptr>(childItem));
    return QModelIndex();

}

QModelIndex DocumentOutlineModel::parent(const QModelIndex &child) const
{
    if (!child.isValid())
        return QModelIndex();

    Node const  *childItem = static_cast<Node const *>(child.internalPointer());
    Node const * parent = childItem->parent;

    if (parent == &root)
        return QModelIndex();

    return createIndex(
        parent - parent->parent->children.data(),
        0,
        reinterpret_cast<quintptr>(parent));
}

int DocumentOutlineModel::rowCount(const QModelIndex &parent) const
{
    Node const *parentItem;
    if (parent.column() > 0)
        return 0;

    if (!parent.isValid())
        parentItem = &root;
    else
        parentItem = static_cast<Node const *>(parent.internalPointer());

    return parentItem->children.size();
}

int DocumentOutlineModel::columnCount(const QModelIndex &parent) const
{
    return 1;
}

QVariant DocumentOutlineModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.column() != 0)
        return QVariant();

    if (role != Qt::DisplayRole)
        return QVariant();

    Node const *item = static_cast<Node const*>(index.internalPointer());

    return item->title;
}