aboutsummaryrefslogtreecommitdiff
path: root/src/documentoutlinemodel.hpp
blob: 0476892c22c2c62993a2c9efdaabb8ce1f7d86f7 (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
#ifndef DOCUMENTOUTLINEMODEL_HPP
#define DOCUMENTOUTLINEMODEL_HPP

#include <QAbstractItemModel>
#include <QList>

class DocumentOutlineModel :
    public QAbstractItemModel
{
    Q_OBJECT
public:
    DocumentOutlineModel();

    void clear();

    void beginBuild();

    void appendH1(QString const & title, QString const & anchor);

    void appendH2(QString const & title, QString const & anchor);

    void appendH3(QString const & title, QString const & anchor);

    void endBuild();

    QString getTitle(QModelIndex const & index) const;
    QString getAnchor(QModelIndex const & index) const;

public:
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;

    QModelIndex parent(const QModelIndex &child) const override;

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;


private:
    struct Node
    {
        Node * parent;
        QString title;
        QString anchor;
        int depth = 0;
        int index = 0;
        QList<Node> children;
    };

    Node root;

    Node & ensureLevel1();
    Node & ensureLevel2();
};

#endif // DOCUMENTOUTLINEMODEL_HPP