1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "mavenwidget.h"
6#include "services/option/toolchaindata.h"
7#include "common/util/custompaths.h"
8
9#include <QRadioButton>
10#include <QComboBox>
11#include <QVBoxLayout>
12#include <QHBoxLayout>
13#include <QLabel>
14#include <QHeaderView>
15#include <QLineEdit>
16#include <QPushButton>
17#include <QDebug>
18#include <QFileDialog>
19
20class MavenWidgetPrivate
21{
22 friend class MavenWidget;
23
24 QComboBox *homePathComboBox = nullptr;
25 QLineEdit *userSettingEdit = nullptr;
26 QLineEdit *localSettingEdit = nullptr;
27 QSharedPointer<ToolChainData> toolChainData;
28};
29
30MavenWidget::MavenWidget(QWidget *parent)
31 : PageWidget(parent)
32 , d(new MavenWidgetPrivate())
33{
34 d->toolChainData.reset(new ToolChainData());
35 QString retMsg;
36 bool ret = d->toolChainData->readToolChainData(retMsg);
37 if (ret) {
38 qInfo() << retMsg;
39 }
40
41 setupUi();
42 updateUi();
43}
44
45MavenWidget::~MavenWidget()
46{
47 if (d) {
48 delete d;
49 }
50}
51
52void MavenWidget::setupUi()
53{
54 QVBoxLayout *vLayout = new QVBoxLayout();
55 setLayout(vLayout);
56
57 QLabel *homePathLabel = new QLabel(QLabel::tr("Maven path:"));
58 homePathLabel->setFixedWidth(120);
59 d->homePathComboBox = new QComboBox();
60
61 QHBoxLayout *homePathLayout = new QHBoxLayout();
62 homePathLayout->addWidget(homePathLabel);
63 homePathLayout->addWidget(d->homePathComboBox);
64
65 QLabel *userSettingLabel = new QLabel(QLabel::tr("User Setting:"));
66 userSettingLabel->setFixedWidth(120);
67 d->userSettingEdit = new QLineEdit();
68 QPushButton *userSettingBtn = new QPushButton(QPushButton::tr("Browse"));
69 QHBoxLayout *userSettingLayout = new QHBoxLayout();
70 userSettingLayout->addWidget(userSettingLabel);
71 userSettingLayout->addWidget(d->userSettingEdit);
72 userSettingLayout->addWidget(userSettingBtn);
73
74 QLabel *localSettingLabel = new QLabel(QLabel::tr("Local Setting:"));
75 localSettingLabel->setFixedWidth(120);
76 d->localSettingEdit = new QLineEdit();
77 QPushButton *localSettingBtn = new QPushButton(QPushButton::tr("Browse"));
78 QHBoxLayout *localSettingLayout = new QHBoxLayout();
79 localSettingLayout->addWidget(localSettingLabel);
80 localSettingLayout->addWidget(d->localSettingEdit);
81 localSettingLayout->addWidget(localSettingBtn);
82
83 vLayout->addLayout(homePathLayout);
84 vLayout->addLayout(userSettingLayout);
85 vLayout->addLayout(localSettingLayout);
86 vLayout->addStretch();
87
88
89 QObject::connect(userSettingBtn, &QPushButton::clicked, [this](){
90 QFileDialog fileDialog;
91 QString filePath = fileDialog.getOpenFileName(nullptr, "Open Maven User Setting File", QString(), "*.xml");
92 if(!filePath.isEmpty()) {
93 d->userSettingEdit->setText(filePath);
94 }
95 });
96
97
98 QObject::connect(localSettingBtn, &QPushButton::clicked, [this](){
99 QFileDialog fileDialog;
100 QString filePath = fileDialog.getExistingDirectory(nullptr, "Open Maven Local Setting Folder", QString(), QFileDialog::DontResolveSymlinks);
101 if(!filePath.isEmpty()) {
102 d->localSettingEdit->setText(filePath);
103 }
104 });
105}
106
107void MavenWidget::updateUi()
108{
109 const ToolChainData::ToolChains &data = d->toolChainData->getToolChanins();
110 ToolChainData::Params cParams = data.value(kMaven);
111 int i = 0;
112 for (auto param : cParams) {
113 QString text = param.name + "(" + param.path + ")";
114 d->homePathComboBox->insertItem(i, text);
115 d->homePathComboBox->setItemData(i, QVariant::fromValue(param), Qt::UserRole + 1);
116 i++;
117 }
118}
119
120bool MavenWidget::getControlValue(QMap<QString, QVariant> &map)
121{
122 MavenConfig config;
123 int index = d->homePathComboBox->currentIndex();
124 if (index < 0) {
125 config.version = ToolChainData::ToolChainParam();
126 } else {
127 config.version = qvariant_cast<ToolChainData::ToolChainParam>(d->homePathComboBox->itemData(index, Qt::UserRole + 1));
128 }
129
130 config.userSetting = d->userSettingEdit->text();
131 config.localSetting = d->localSettingEdit->text();
132
133 dataToMap(config, map);
134
135 return true;
136}
137
138void MavenWidget::setControlValue(const QMap<QString, QVariant> &map)
139{
140 MavenConfig config;
141 mapToData(map, config);
142
143 int count = d->homePathComboBox->count();
144 for (int i = 0; i < count; i++) {
145 ToolChainData::ToolChainParam toolChainParam = qvariant_cast<ToolChainData::ToolChainParam>(d->homePathComboBox->itemData(i, Qt::UserRole + 1));
146 if (config.version.name == toolChainParam.name
147 && config.version.path == toolChainParam.path) {
148 d->homePathComboBox->setCurrentIndex(i);
149 break;
150 }
151 }
152
153 d->userSettingEdit->setText(config.userSetting);
154 d->localSettingEdit->setText(config.localSetting);
155}
156
157bool MavenWidget::dataToMap(const MavenConfig &config, QMap<QString, QVariant> &map)
158{
159 QMap<QString, QVariant> version;
160 version.insert("name", config.version.name);
161 version.insert("path", config.version.path);
162
163 map.insert("version", version);
164 map.insert("userSetting", config.userSetting);
165 map.insert("localSetting", config.localSetting);
166
167 return true;
168}
169
170bool MavenWidget::mapToData(const QMap<QString, QVariant> &map, MavenConfig &config)
171{
172 QMap<QString, QVariant> version = map.value("version").toMap();
173 config.version.name = version.value("name").toString();
174 config.version.path = version.value("path").toString();
175
176 config.userSetting = map.value("userSetting").toString();
177 config.localSetting = map.value("localSetting").toString();
178
179 return true;
180}
181
182void MavenWidget::setUserConfig(const QMap<QString, QVariant> &map)
183{
184 setControlValue(map);
185}
186
187void MavenWidget::getUserConfig(QMap<QString, QVariant> &map)
188{
189 getControlValue(map);
190}
191