summaryrefslogtreecommitdiff
path: root/examples/demos/documentviewer/jsonviewer.h
blob: c9291cd28594fc1dcd3700675ec4e8112e5e75b7 (plain)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#ifndef JSONVIEWER_H
#define JSONVIEWER_H

#include "abstractviewer.h"
#include <QJsonValue>
#include <QJsonDocument>
#include <QAbstractItemModel>

class QMainWindow;
class QTreeWidgetItem;
class QTreeView;
class QListWidget;
class QListWidgetItem;
class QLineEdit;

class JsonViewer : public AbstractViewer
{
    Q_GADGET
public:
    JsonViewer(QFile *file, QWidget *parent, QMainWindow *mainWindow);
    ~JsonViewer() override;

    QString viewerName() const override { return staticMetaObject.className(); };
    QByteArray saveState() const override;
    bool restoreState(QByteArray &) override;
    bool supportsOverview() const override { return true; }
    bool hasContent() const override;

#if defined(QT_ABSTRACTVIEWER_PRINTSUPPORT)
protected:
    void printDocument(QPrinter *printer) const override;
#endif // QT_ABSTRACTVIEWER_PRINTSUPPORT

private slots:
    void setupJsonUi();
    void onTopLevelItemClicked(QListWidgetItem *item);
    void onTopLevelItemDoubleClicked(QListWidgetItem *item);
    void onJsonMenuRequested(const QPoint &pos);
    void onBookmarkMenuRequested(const QPoint &pos);
    void onBookmarkAdded();
    void onBookmarkDeleted();

private:
    bool openJsonFile();

    QTreeView *m_tree;
    QListWidget *m_toplevel = nullptr;
    QJsonDocument m_root;

    int m_classId = -1;
    QLineEdit *m_searchKey = nullptr;
};

class JsonTreeItem
{
public:
    JsonTreeItem(JsonTreeItem *parent = nullptr);
    ~JsonTreeItem();
    void appendChild(JsonTreeItem *item);
    JsonTreeItem *child(int row);
    JsonTreeItem *parent();
    int childCount() const;
    int row() const;
    void setKey(const QString& key);
    void setValue(const QVariant& value);
    void setType(const QJsonValue::Type& type);
    QString key() const { return m_key; };
    QVariant value() const { return m_value; };
    QJsonValue::Type type() const { return m_type; };

    static JsonTreeItem* load(const QJsonValue& value, JsonTreeItem *parent = nullptr);

private:
    QString m_key;
    QVariant m_value;
    QJsonValue::Type m_type;
    QList<JsonTreeItem*> m_children;
    JsonTreeItem *m_parent = nullptr;
};

class JsonItemModel : public QAbstractItemModel
{
    Q_OBJECT
public:
    explicit JsonItemModel(QObject *parent = nullptr);
    JsonItemModel(const QJsonDocument& doc, QObject *parent = nullptr);
    ~JsonItemModel();
    QVariant data(const QModelIndex &index, int role) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex & = QModelIndex()) const override { return 2; };
    Qt::ItemFlags flags(const QModelIndex &index) const override;

private:
    JsonTreeItem *m_rootItem = nullptr;
    QStringList m_headers;
    static JsonTreeItem *itemFromIndex(const QModelIndex &index)
    {return static_cast<JsonTreeItem*>(index.internalPointer()); }
};

#endif //JSONVIEWER_H