1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "ninjawidget.h"
6#include "services/option/toolchaindata.h"
7#include "common/util/custompaths.h"
8#include "common/toolchain/toolchain.h"
9
10#include <QRadioButton>
11#include <QComboBox>
12#include <QVBoxLayout>
13#include <QHBoxLayout>
14#include <QLabel>
15#include <QHeaderView>
16#include <QDir>
17#include <QDebug>
18#include <QJsonObject>
19
20class NinjaWidgetPrivate
21{
22 friend class NinjaWidget;
23
24 QComboBox *homePathComboBox = nullptr;
25 QSharedPointer<ToolChainData> toolChainData;
26};
27
28NinjaWidget::NinjaWidget(QWidget *parent)
29 : PageWidget(parent)
30 , d(new NinjaWidgetPrivate())
31{
32 d->toolChainData.reset(new ToolChainData());
33 QString retMsg;
34 bool ret = d->toolChainData->readToolChainData(retMsg);
35 if (ret) {
36 qInfo() << retMsg;
37 }
38
39 setupUi();
40 updateUi();
41}
42
43NinjaWidget::~NinjaWidget()
44{
45 if (d) {
46 delete d;
47 }
48}
49
50void NinjaWidget::setupUi()
51{
52 QVBoxLayout *vLayout = new QVBoxLayout();
53 setLayout(vLayout);
54
55 QLabel *homePathLabel = new QLabel(QLabel::tr("Ninja path:"));
56 homePathLabel->setFixedWidth(120);
57 d->homePathComboBox = new QComboBox();
58
59 QHBoxLayout *homePathLayout = new QHBoxLayout();
60 homePathLayout->addWidget(homePathLabel);
61 homePathLayout->addWidget(d->homePathComboBox);
62
63 vLayout->addLayout(homePathLayout);
64 vLayout->addStretch();
65}
66
67void NinjaWidget::updateUi()
68{
69 const ToolChainData::ToolChains &data = d->toolChainData->getToolChanins();
70 ToolChainData::Params cParams = data.value(kNinja);
71 int i = 0;
72 for (auto param : cParams) {
73 QString text = param.name + "(" + param.path + ")";
74 d->homePathComboBox->insertItem(i, text);
75 d->homePathComboBox->setItemData(i, QVariant::fromValue(param), Qt::UserRole + 1);
76 i++;
77 }
78}
79
80bool NinjaWidget::getControlValue(QMap<QString, QVariant> &map)
81{
82 NinjaConfig config;
83 int index = d->homePathComboBox->currentIndex();
84 if (index < 0) {
85 config.version = ToolChainData::ToolChainParam();
86 } else {
87 config.version = qvariant_cast<ToolChainData::ToolChainParam>(d->homePathComboBox->itemData(index, Qt::UserRole + 1));
88 }
89
90 dataToMap(config, map);
91
92 return true;
93}
94
95void NinjaWidget::setControlValue(const QMap<QString, QVariant> &map)
96{
97 NinjaConfig config;
98 mapToData(map, config);
99
100 int count = d->homePathComboBox->count();
101 for (int i = 0; i < count; i++) {
102 ToolChainData::ToolChainParam toolChainParam = qvariant_cast<ToolChainData::ToolChainParam>(d->homePathComboBox->itemData(i, Qt::UserRole + 1));
103 if (config.version.name == toolChainParam.name
104 && config.version.path == toolChainParam.path) {
105 d->homePathComboBox->setCurrentIndex(i);
106 break;
107 }
108 }
109}
110
111bool NinjaWidget::dataToMap(const NinjaConfig &config, QMap<QString, QVariant> &map)
112{
113 QMap<QString, QVariant> version;
114 version.insert("name", config.version.name);
115 version.insert("path", config.version.path);
116
117 map.insert("version", version);
118
119 return true;
120}
121
122bool NinjaWidget::mapToData(const QMap<QString, QVariant> &map, NinjaConfig &config)
123{
124 QMap<QString, QVariant> version = map.value("version").toMap();
125 config.version.name = version.value("name").toString();
126 config.version.path = version.value("path").toString();
127
128 return true;
129}
130
131void NinjaWidget::setUserConfig(const QMap<QString, QVariant> &map)
132{
133 setControlValue(map);
134}
135
136void NinjaWidget::getUserConfig(QMap<QString, QVariant> &map)
137{
138 getControlValue(map);
139}
140