| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
|---|---|
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "jsontabwidget.h" |
| 6 | #include "jsondisplayview.h" |
| 7 | |
| 8 | #include <QHeaderView> |
| 9 | #include <QTableWidget> |
| 10 | #include <QStandardItemModel> |
| 11 | #include <QDebug> |
| 12 | |
| 13 | class JsonTabWidgetPrivate |
| 14 | { |
| 15 | friend class JsonTabWidget; |
| 16 | int findTab(QTabWidget *const tabWidget, const std::string tabText) const |
| 17 | { |
| 18 | for(int index = 0; index < tabWidget->count(); index ++){ |
| 19 | if (tabWidget->tabText(index).toStdString() == tabText) |
| 20 | return index; |
| 21 | } |
| 22 | return -1; |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | JsonTabWidget::JsonTabWidget(QWidget *parent) |
| 27 | : QTabWidget (parent) |
| 28 | , d (new JsonTabWidgetPrivate) |
| 29 | { |
| 30 | setTabPosition(QTabWidget::TabPosition::South); |
| 31 | qRegisterMetaType<Json::Value>("Json::Value"); |
| 32 | qRegisterMetaType<Json::Value>("Json::Value&"); |
| 33 | } |
| 34 | |
| 35 | JsonTabWidget::~JsonTabWidget() |
| 36 | { |
| 37 | if (d) |
| 38 | delete d; |
| 39 | } |
| 40 | |
| 41 | void JsonTabWidget::parseJson(const Json::Value &data) |
| 42 | { |
| 43 | if (!data.empty()) { |
| 44 | Json::Value result = data["result"]; |
| 45 | if (!result.empty()) { |
| 46 | for (auto name : result.getMemberNames()) { |
| 47 | if (name.empty()) |
| 48 | continue; |
| 49 | int tabIdx = d->findTab(this, name); |
| 50 | if (0 > tabIdx) { |
| 51 | tabIdx = addTab(new JsonDisplayView(this), QString::fromStdString(name)); |
| 52 | } |
| 53 | JsonDisplayView *curr = qobject_cast<JsonDisplayView*>(widget(tabIdx)); |
| 54 | if (curr) { |
| 55 | curr->parseJson(result[name]); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 |