File indexing completed on 2024-04-28 15:35:52

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.1-only or LGPL-3.0-only or LicenseRef-KDE-Accepted-LGPL
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <memory>
0008 #include <qabstractitemmodel.h>
0009 
0010 class TreeItem
0011 {
0012 public:
0013     explicit TreeItem(const QString &title, const QString &href, const QString &id, const QJsonArray &childs, TreeItem *parentItem = nullptr);
0014 
0015     void appendChild(std::unique_ptr<TreeItem> &&child);
0016 
0017     void clear();
0018     TreeItem *child(int row);
0019     int childCount() const;
0020     QVariant data(int role) const;
0021     int row() const;
0022     TreeItem *parentItem();
0023 
0024 private:
0025     // Position in treee
0026     std::vector<std::unique_ptr<TreeItem>> m_childItems;
0027     TreeItem *m_parentItem;
0028 
0029     // Content
0030     const QString m_title;
0031     const QString m_href;
0032     const QString m_id;
0033 };
0034 
0035 class TableOfContentModel : public QAbstractItemModel
0036 {
0037     Q_OBJECT
0038 
0039 public:
0040     explicit TableOfContentModel(QObject *parent = nullptr);
0041 
0042     enum ExtraRole {
0043         TitleRole = Qt::UserRole + 1,
0044         HrefRole,
0045         IdRole,
0046     };
0047 
0048     QHash<int, QByteArray> roleNames() const override;
0049     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0050     QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override;
0051     QModelIndex parent(const QModelIndex &index) const override;
0052     int rowCount(const QModelIndex &parent = {}) const override;
0053     int columnCount(const QModelIndex &parent = {}) const override;
0054 
0055     Q_INVOKABLE void importFromJson(const QByteArray &json);
0056 
0057 private:
0058     std::unique_ptr<TreeItem> m_rootItem;
0059 };