File size: 4,912 Bytes
5cee033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2008, Pino Toscano <[email protected]>
 * Copyright (C) 2018, Adam Reichold <[email protected]>
 * Copyright (C) 2019, Albert Astals Cid <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "toc.h"

#include <poppler-qt6.h>

#include <QtGui/QStandardItemModel>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QTreeWidget>

#include <QDebug>

struct Node
{
    Node(Poppler::OutlineItem &&item, int row, Node *parent) : m_row(row), m_parent(parent), m_item(std::move(item)) { }

    ~Node() { qDeleteAll(m_children); }

    Node(const Node &) = delete;
    Node &operator=(const Node &) = delete;

    int m_row;
    Node *m_parent;
    Poppler::OutlineItem m_item;
    QVector<Node *> m_children;
};

class TocModel : public QAbstractItemModel
{
    Q_OBJECT
public:
    TocModel(QVector<Poppler::OutlineItem> &&items, QObject *parent) : QAbstractItemModel(parent)
    {
        for (int i = 0; i < items.count(); ++i) {
            m_topItems << new Node(std::move(items[i]), i, nullptr);
        }
    }

    ~TocModel() override { qDeleteAll(m_topItems); }

    QVariant data(const QModelIndex &index, int role) const override
    {
        if (role != Qt::DisplayRole) {
            return {};
        }

        Node *n = static_cast<Node *>(index.internalPointer());
        return n->m_item.name();
    }

    QModelIndex index(int row, int column, const QModelIndex &parent) const override
    {
        Node *p = static_cast<Node *>(parent.internalPointer());
        const QVector<Node *> &children = p ? p->m_children : m_topItems;

        return createIndex(row, column, children[row]);
    }

    QModelIndex parent(const QModelIndex &child) const override
    {
        Node *n = static_cast<Node *>(child.internalPointer());
        if (n->m_parent == nullptr) {
            return QModelIndex();
        } else {
            return createIndex(n->m_parent->m_row, 0, n->m_parent);
        }
    }

    int rowCount(const QModelIndex &parent) const override
    {
        Node *n = static_cast<Node *>(parent.internalPointer());
        if (!n) {
            return m_topItems.count();
        }

        if (n->m_children.isEmpty() && !n->m_item.isNull()) {
            QVector<Poppler::OutlineItem> items = n->m_item.children();
            for (int i = 0; i < items.count(); ++i) {
                n->m_children << new Node(std::move(items[i]), i, n);
            }
        }

        return n->m_children.count();
    }

    bool hasChildren(const QModelIndex &parent) const override
    {
        Node *n = static_cast<Node *>(parent.internalPointer());
        if (!n) {
            return true;
        }

        return n->m_item.hasChildren();
    }

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

private:
    QVector<Node *> m_topItems;
};

TocDock::TocDock(QWidget *parent) : AbstractInfoDock(parent)
{
    m_tree = new QTreeView(this);
    setWidget(m_tree);
    m_tree->setAlternatingRowColors(true);
    m_tree->header()->hide();
    setWindowTitle(tr("TOC"));
    m_tree->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
}

TocDock::~TocDock() { }

void TocDock::expandItemModels(const QModelIndex &parent)
{
    TocModel *model = static_cast<TocModel *>(m_tree->model());
    for (int i = 0; i < model->rowCount(parent); ++i) {
        QModelIndex index = model->index(i, 0, parent);
        Node *n = static_cast<Node *>(index.internalPointer());
        if (n->m_item.isOpen()) {
            m_tree->setExpanded(index, true);
            expandItemModels(index);
        }
    }
}

void TocDock::fillInfo()
{
    auto outline = document()->outline();
    if (!outline.isEmpty()) {
        TocModel *model = new TocModel(std::move(outline), this);
        m_tree->setModel(model);

        expandItemModels(QModelIndex());
    } else {
        QStandardItemModel *model = new QStandardItemModel(this);
        QStandardItem *item = new QStandardItem(tr("No TOC"));
        item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
        model->appendRow(item);
        m_tree->setModel(model);
    }
}

void TocDock::documentClosed()
{
    m_tree->setModel(nullptr);
    AbstractInfoDock::documentClosed();
}

#include "toc.moc"