1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "kitsmanagerwidget.h"
6#include "services/option/toolchaindata.h"
7#include "common/util/custompaths.h"
8#include "common/toolchain/toolchain.h"
9
10#include <QDebug>
11#include <QDir>
12#include <QtCore/QVariant>
13#include <QtWidgets/QApplication>
14#include <QtWidgets/QComboBox>
15#include <QtWidgets/QGridLayout>
16#include <QtWidgets/QHBoxLayout>
17#include <QtWidgets/QLabel>
18#include <QtWidgets/QLineEdit>
19#include <QtWidgets/QListView>
20#include <QtWidgets/QPushButton>
21#include <QtWidgets/QScrollArea>
22#include <QtWidgets/QTabWidget>
23#include <QtWidgets/QVBoxLayout>
24#include <QtWidgets/QWidget>
25#include <QStringListModel>
26#include <QAbstractItemView>
27#include <QListWidget>
28#include <QJsonObject>
29
30class KitsManagerWidgetPrivate
31{
32 friend class KitsManagerWidget;
33 QListWidget *listWidget = nullptr;
34 QLineEdit *nameEidt = nullptr;
35 QComboBox *cbCXXComplier = nullptr;
36 QComboBox *cbCMake = nullptr;
37 QComboBox *cbCComplier = nullptr;
38 QComboBox *cbDebugger = nullptr;
39 QLabel *labelGeneratorExpression = nullptr;
40
41 QSharedPointer<ToolChainData> toolChainData;
42};
43
44KitsManagerWidget::KitsManagerWidget(QWidget *parent)
45 : PageWidget(parent)
46 , d(new KitsManagerWidgetPrivate)
47{
48 d->toolChainData.reset(new ToolChainData());
49 QString retMsg;
50 bool ret = d->toolChainData->readToolChainData(retMsg);
51 if (ret) {
52 qInfo() << retMsg;
53 }
54
55 setupUi();
56 updateUi();
57}
58
59KitsManagerWidget::~KitsManagerWidget()
60{
61 if (d) {
62 delete d;
63 }
64}
65
66void KitsManagerWidget::setupUi()
67{
68 auto centerLayout = new QGridLayout();
69 centerLayout->setSpacing(6);
70 centerLayout->setContentsMargins(0, 0, 11, 0);
71
72 // List tree.
73 d->listWidget = new QListWidget();
74 d->listWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
75 d->listWidget->addItem("Desktop");
76
77 auto detailGridLayout = new QGridLayout();
78 detailGridLayout->setSpacing(6);
79 d->cbCXXComplier = new QComboBox();
80
81 d->nameEidt = new QLineEdit();
82 d->nameEidt->setText(tr("Desktop"));
83 d->nameEidt->setEnabled(false);
84
85 d->cbCMake = new QComboBox();
86
87 auto labelCMake = new QLabel();
88 labelCMake->setText(tr("CMake Tool:"));
89
90 d->cbCComplier = new QComboBox();
91
92 d->cbDebugger = new QComboBox();
93
94 auto labelGenerator = new QLabel();
95 labelGenerator->setText(tr("CMake generator:CodeBlocks - Unix Makefiles"));
96
97 auto labelDebugger = new QLabel();
98 labelDebugger->setText(tr("Debugger:"));
99
100 d->labelGeneratorExpression = new QLabel();
101 d->labelGeneratorExpression->setText(QString());
102
103 auto Name = new QLabel();
104 Name->setText(tr("Name:"));
105
106 auto labelCCompiler = new QLabel();
107 labelCCompiler->setText("C Compiler:");
108
109 auto labelCXXCompiler = new QLabel();
110 labelCXXCompiler->setText("C++ Compiler:");
111
112 QFrame* separator = new QFrame();
113 separator->setFrameShape(QFrame::HLine);
114 separator->setFrameShadow(QFrame::Sunken);
115
116 centerLayout->addWidget(d->listWidget, 0, 0, 25, 1);
117 centerLayout->addWidget(Name, 1, 1, 1, 1);
118 centerLayout->addWidget(d->nameEidt, 1, 2, 1, 1);
119 centerLayout->addWidget(separator, 3, 1, 1, 2);
120 centerLayout->addWidget(labelCCompiler, 5, 1, 1, 1);
121 centerLayout->addWidget(d->cbCComplier, 5, 2, 1, 1);
122 centerLayout->addWidget(labelCXXCompiler, 7, 1, 1, 1);
123 centerLayout->addWidget(d->cbCXXComplier, 7, 2, 1, 1);
124 centerLayout->addWidget(labelDebugger, 9, 1, 1, 1);
125 centerLayout->addWidget(d->cbDebugger, 9, 2, 1, 1);
126 centerLayout->addWidget(labelCMake, 11, 1, 1, 1);
127 centerLayout->addWidget(d->cbCMake, 11, 2, 1, 1);
128 centerLayout->addWidget(labelGenerator, 13, 1, 1, 2);
129
130 setLayout(centerLayout);
131}
132
133void KitsManagerWidget::updateUi()
134{
135 const ToolChainData::ToolChains &data = d->toolChainData->getToolChanins();
136
137 // Update compiler combox.
138 auto updateComplier = [](QComboBox *cb, ToolChainData::Params &params) {
139 int i = 0;
140 for (auto p : params) {
141 QString text = p.name + "(" + p.path + ")";
142 cb->insertItem(i, text);
143 cb->setItemData(i, QVariant::fromValue(p), Qt::UserRole + 1);
144 i++;
145 }
146 };
147
148 ToolChainData::Params cParams = data.value(kCCompilers);
149 updateComplier(d->cbCComplier, cParams);
150
151 ToolChainData::Params cxxParams = data.value(kCXXCompilers);
152 updateComplier(d->cbCXXComplier, cxxParams);
153
154 // Update Debugger combox.
155 ToolChainData::Params debugger = data.value(kCCXXDebuggers);
156 updateComplier(d->cbDebugger, debugger);
157
158 // Update CMake combox.
159 ToolChainData::Params cmake = data.value(kCCXXBuildSystems);
160 updateComplier(d->cbCMake, cmake);
161}
162
163bool KitsManagerWidget::getControlValue(QMap<QString, QVariant> &map)
164{
165 KitConfig config;
166 config.name = d->nameEidt->text();
167
168 auto comboBoxValue = [](QComboBox *cb) {
169 int index = cb->currentIndex();
170 if (index > -1) {
171 return qvariant_cast<ToolChainData::ToolChainParam>(cb->itemData(index, Qt::UserRole + 1));
172 } else {
173 return ToolChainData::ToolChainParam();
174 }
175 };
176
177 config.ccompiler = comboBoxValue(d->cbCComplier);
178 config.cppcompiler = comboBoxValue(d->cbCXXComplier);
179 config.debugger = comboBoxValue(d->cbDebugger);
180 config.cmake = comboBoxValue(d->cbCMake);
181 config.cmakeGenerator = d->labelGeneratorExpression->text();
182
183 dataToMap(config, map);
184
185 return true;
186}
187
188void KitsManagerWidget::setControlValue(const QMap<QString, QVariant> &map)
189{
190 KitConfig config;
191 mapToData(map, config);
192
193 auto updateComplier = [](QComboBox *cb, ToolChainData::ToolChainParam &params) {
194 for (int i = 0; i < cb->count(); i++) {
195 ToolChainData::ToolChainParam data = qvariant_cast<ToolChainData::ToolChainParam>(cb->itemData(i, Qt::UserRole + 1));
196 if (data.name == params.name && data.path == params.path) {
197 cb->setCurrentIndex(i);
198 return;
199 }
200 }
201 };
202
203 updateComplier(d->cbCComplier, config.ccompiler);
204 updateComplier(d->cbCXXComplier, config.cppcompiler);
205 updateComplier(d->cbDebugger, config.debugger);
206 updateComplier(d->cbCMake, config.cmake);
207
208 //d->labelGeneratorExpression->setText(config.cmakeGenerator);
209}
210
211bool KitsManagerWidget::dataToMap(const KitConfig &config, QMap<QString, QVariant> &map)
212{
213 auto updateComplier = [](QMap<QString, QVariant> &map, const QString key, const ToolChainData::ToolChainParam &params) {
214 QMap<QString, QVariant> version;
215 version.insert("name", params.name);
216 version.insert("path", params.path);
217 map.insert(key, version);
218 };
219
220 updateComplier(map, "ccompiler", config.ccompiler);
221 updateComplier(map, "cppcompiler", config.cppcompiler);
222 updateComplier(map, "debugger", config.debugger);
223 updateComplier(map, "cmake", config.cmake);
224
225 map.insert("name", config.name);
226 map.insert("cmakeGenerator", config.cmakeGenerator);
227
228 return true;
229}
230
231bool KitsManagerWidget::mapToData(const QMap<QString, QVariant> &map, KitConfig &config)
232{
233 auto updateComplier = [](const QMap<QString, QVariant> &map, const QString key, ToolChainData::ToolChainParam &params) {
234 QMap<QString, QVariant> version = map.value(key).toMap();
235 params.name = version.value("name").toString();
236 params.path = version.value("path").toString();
237 };
238
239 updateComplier(map, "ccompiler", config.ccompiler);
240 updateComplier(map, "cppcompiler", config.cppcompiler);
241 updateComplier(map, "debugger", config.debugger);
242 updateComplier(map, "cmake", config.cmake);
243
244 config.name = map.value("name").toString();
245 config.cmakeGenerator = map.value("cmakeGenerator").toString();
246
247 return true;
248}
249
250void KitsManagerWidget::setUserConfig(const QMap<QString, QVariant> &map)
251{
252 setControlValue(map);
253}
254
255void KitsManagerWidget::getUserConfig(QMap<QString, QVariant> &map)
256{
257 getControlValue(map);
258}
259