1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "ninjaoptionwidget.h"
6#include "ninjawidget.h"
7
8#include "services/option/optionutils.h"
9#include "services/option/optiondatastruct.h"
10#include "services/option/optionmanager.h"
11
12#include <QHBoxLayout>
13#include <QTabWidget>
14
15class NinjaOptionWidgetPrivate {
16 QTabWidget* tabWidget = nullptr;
17
18 friend class NinjaOptionWidget;
19};
20
21NinjaOptionWidget::NinjaOptionWidget(QWidget *parent)
22 : PageWidget(parent)
23 , d(new NinjaOptionWidgetPrivate())
24{
25 QHBoxLayout *layout = new QHBoxLayout();
26 d->tabWidget = new QTabWidget();
27 layout->addWidget(d->tabWidget);
28
29 d->tabWidget->addTab(new NinjaWidget(), tr("Ninja"));
30 QObject::connect(d->tabWidget, &QTabWidget::currentChanged, [this]() {
31 readConfig();
32 });
33
34 setLayout(layout);
35}
36
37NinjaOptionWidget::~NinjaOptionWidget()
38{
39 if (d)
40 delete d;
41}
42
43void NinjaOptionWidget::saveConfig()
44{
45 for (int index = 0; index < d->tabWidget->count(); index++)
46 {
47 PageWidget *pageWidget = qobject_cast<PageWidget*>(d->tabWidget->widget(index));
48 if (pageWidget) {
49 QString itemNode = d->tabWidget->tabText(d->tabWidget->currentIndex());
50 QMap<QString, QVariant> map;
51 pageWidget->getUserConfig(map);
52 OptionUtils::writeJsonSection(OptionUtils::getJsonFilePath(),
53 option::CATEGORY_NINJA, itemNode, map);
54
55 OptionManager::getInstance()->updateData();
56 }
57 }
58}
59
60void NinjaOptionWidget::readConfig()
61{
62 for (int index = 0; index < d->tabWidget->count(); index++)
63 {
64 PageWidget *pageWidget = qobject_cast<PageWidget*>(d->tabWidget->widget(index));
65 if (pageWidget) {
66 QString itemNode = d->tabWidget->tabText(d->tabWidget->currentIndex());
67 QMap<QString, QVariant> map;
68 OptionUtils::readJsonSection(OptionUtils::getJsonFilePath(),
69 option::CATEGORY_NINJA, itemNode, map);
70 pageWidget->setUserConfig(map);
71 }
72 }
73}
74
75