2023-01-26 17:31:16 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
|
|
|
|
#ifndef JSONVIEWER_H
|
|
|
|
#define JSONVIEWER_H
|
|
|
|
|
2023-03-23 15:25:43 +00:00
|
|
|
#include "viewerinterfaces.h"
|
2023-06-20 11:39:08 +00:00
|
|
|
|
2023-01-26 17:31:16 +00:00
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
|
2023-06-07 14:11:25 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
2023-01-26 17:31:16 +00:00
|
|
|
class QTreeView;
|
|
|
|
class QListWidget;
|
|
|
|
class QListWidgetItem;
|
|
|
|
class QLineEdit;
|
2023-06-07 14:11:25 +00:00
|
|
|
QT_END_NAMESPACE
|
2023-12-14 16:53:04 +00:00
|
|
|
//! [pluginHeader]
|
2023-03-23 15:25:43 +00:00
|
|
|
class JsonViewer : public ViewerInterface
|
2023-01-26 17:31:16 +00:00
|
|
|
{
|
2023-03-23 15:25:43 +00:00
|
|
|
Q_OBJECT
|
|
|
|
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.DocumentViewer.ViewerInterface/1.0" FILE "jsonviewer.json")
|
|
|
|
Q_INTERFACES(ViewerInterface)
|
2023-01-26 17:31:16 +00:00
|
|
|
public:
|
2023-06-17 18:04:55 +00:00
|
|
|
JsonViewer();
|
2023-01-26 17:31:16 +00:00
|
|
|
~JsonViewer() override;
|
2023-12-14 16:53:04 +00:00
|
|
|
//! [pluginHeader]
|
2023-01-26 17:31:16 +00:00
|
|
|
|
2023-03-23 15:25:43 +00:00
|
|
|
void init(QFile *file, QWidget *parent, QMainWindow *mainWindow) override;
|
2023-12-14 16:53:04 +00:00
|
|
|
//! [pluginReimp]
|
2023-06-20 12:20:47 +00:00
|
|
|
QString viewerName() const override { return QLatin1StringView(staticMetaObject.className()); };
|
2023-03-23 15:25:43 +00:00
|
|
|
QStringList supportedMimeTypes() const override;
|
2023-12-14 16:53:04 +00:00
|
|
|
bool hasContent() const override;
|
|
|
|
bool supportsOverview() const override { return true; }
|
|
|
|
//! [pluginReimp]
|
2023-01-26 17:31:16 +00:00
|
|
|
QByteArray saveState() const override;
|
|
|
|
bool restoreState(QByteArray &) override;
|
documentviewer: Add retranslate() method to all plugins
Previously, only the imageviewer plugin had a retranslate() method,
which meant that when switching languages, UI texts in other plugins
(jsonviewer, txtviewer, pdfviewer) would not be updated.
This change adds retranslate() methods to all plugins that contain
translatable UI text:
- jsonviewer: Updates menu titles, toolbar titles, actions, tab titles,
and bookmark tooltips
- txtviewer: Updates menu titles, toolbar titles, and action texts/tooltips
- pdfviewer: Updates toolbar title, action texts/tooltips, and tab titles
Each plugin now stores references to UI elements that need retranslation
and implements the retranslate() method to update all translatable text
when the language is changed.
Fixes: QTBUG-138344
Pick-to: 6.10
Change-Id: I9d294694a42c2c48e03b72e28e9c0f4015cd5c80
Reviewed-by: Masoud Jami <masoud.jami@qt.io>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
2025-07-09 11:40:55 +00:00
|
|
|
void retranslate() override;
|
2023-01-26 17:31:16 +00:00
|
|
|
|
2025-07-09 14:25:49 +00:00
|
|
|
#ifdef DOCUMENTVIEWER_PRINTSUPPORT
|
2023-01-26 17:31:16 +00:00
|
|
|
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();
|
|
|
|
|
2023-12-14 16:53:04 +00:00
|
|
|
//! [pluginPrivateMembers]
|
2023-01-26 17:31:16 +00:00
|
|
|
private:
|
|
|
|
bool openJsonFile();
|
|
|
|
|
|
|
|
QTreeView *m_tree;
|
|
|
|
QListWidget *m_toplevel = nullptr;
|
|
|
|
QJsonDocument m_root;
|
documentviewer: Add retranslate() method to all plugins
Previously, only the imageviewer plugin had a retranslate() method,
which meant that when switching languages, UI texts in other plugins
(jsonviewer, txtviewer, pdfviewer) would not be updated.
This change adds retranslate() methods to all plugins that contain
translatable UI text:
- jsonviewer: Updates menu titles, toolbar titles, actions, tab titles,
and bookmark tooltips
- txtviewer: Updates menu titles, toolbar titles, and action texts/tooltips
- pdfviewer: Updates toolbar title, action texts/tooltips, and tab titles
Each plugin now stores references to UI elements that need retranslation
and implements the retranslate() method to update all translatable text
when the language is changed.
Fixes: QTBUG-138344
Pick-to: 6.10
Change-Id: I9d294694a42c2c48e03b72e28e9c0f4015cd5c80
Reviewed-by: Masoud Jami <masoud.jami@qt.io>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
2025-07-09 11:40:55 +00:00
|
|
|
QMenu *m_jsonMenu = nullptr;
|
|
|
|
QToolBar *m_jsonToolBar = nullptr;
|
|
|
|
QAction *m_expandAllAction = nullptr;
|
|
|
|
QAction *m_collapseAllAction = nullptr;
|
2023-01-26 17:31:16 +00:00
|
|
|
};
|
2023-12-14 16:53:04 +00:00
|
|
|
//! [pluginPrivateMembers]
|
2023-01-26 17:31:16 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
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; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
JsonTreeItem *m_rootItem = nullptr;
|
|
|
|
QStringList m_headers;
|
|
|
|
static JsonTreeItem *itemFromIndex(const QModelIndex &index)
|
|
|
|
{return static_cast<JsonTreeItem*>(index.internalPointer()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //JSONVIEWER_H
|