| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "gradleconfigpropertywidget.h" |
| 6 | |
| 7 | #include "services/option/toolchaindata.h" |
| 8 | #include "common/toolchain/toolchain.h" |
| 9 | #include "common/widget/pagewidget.h" |
| 10 | |
| 11 | #include <QComboBox> |
| 12 | #include <QVBoxLayout> |
| 13 | #include <QLabel> |
| 14 | #include <QLineEdit> |
| 15 | #include <QCheckBox> |
| 16 | #include <QPushButton> |
| 17 | #include <QFileDialog> |
| 18 | |
| 19 | static const QString kJrePath = QObject::tr("jre path" ); |
| 20 | static const QString kJreExecute = QObject::tr("jre execute" ); |
| 21 | static const QString kLaunchConfigPath = QObject::tr("launch config" ); |
| 22 | static const QString kLaunchPackageFile = QObject::tr("launch package" ); |
| 23 | static const QString kDapPackageFile = QObject::tr("dap package" ); |
| 24 | static const int kHeadlineWidth = 120; |
| 25 | |
| 26 | class GradleDetailPropertyWidgetPrivate |
| 27 | { |
| 28 | friend class GradleDetailPropertyWidget; |
| 29 | QComboBox *jdkVersionComboBox{nullptr}; |
| 30 | QComboBox *gradleVersionComboBox{nullptr}; |
| 31 | QLineEdit *mainClass{nullptr}; |
| 32 | QSharedPointer<ToolChainData> toolChainData; |
| 33 | QCheckBox *detailBox{nullptr}; |
| 34 | QLineEdit *jreEdit{nullptr}; |
| 35 | QLineEdit *jreExecuteEdit{nullptr}; |
| 36 | QLineEdit *launchCfgPathEdit{nullptr}; |
| 37 | QLineEdit *lanuchCfgFileEdit{nullptr}; |
| 38 | QLineEdit *dapPackageFileEdit{nullptr}; |
| 39 | }; |
| 40 | |
| 41 | GradleDetailPropertyWidget::GradleDetailPropertyWidget(QWidget *parent) |
| 42 | : QWidget(parent) |
| 43 | , d(new GradleDetailPropertyWidgetPrivate()) |
| 44 | { |
| 45 | setupUI(); |
| 46 | initData(); |
| 47 | } |
| 48 | |
| 49 | GradleDetailPropertyWidget::~GradleDetailPropertyWidget() |
| 50 | { |
| 51 | if (d) |
| 52 | delete d; |
| 53 | } |
| 54 | |
| 55 | void GradleDetailPropertyWidget::setupUI() |
| 56 | { |
| 57 | QVBoxLayout *vLayout = new QVBoxLayout(); |
| 58 | setLayout(vLayout); |
| 59 | |
| 60 | QHBoxLayout *hLayout = new QHBoxLayout(); |
| 61 | QLabel *label = new QLabel(QLabel::tr("JDK version:" )); |
| 62 | label->setFixedWidth(kHeadlineWidth); |
| 63 | d->jdkVersionComboBox = new QComboBox(); |
| 64 | hLayout->addWidget(label); |
| 65 | hLayout->addWidget(d->jdkVersionComboBox); |
| 66 | vLayout->addLayout(hLayout); |
| 67 | |
| 68 | hLayout = new QHBoxLayout(); |
| 69 | label = new QLabel(QLabel::tr("Gradle Version: " )); |
| 70 | label->setFixedWidth(kHeadlineWidth); |
| 71 | d->gradleVersionComboBox = new QComboBox(); |
| 72 | hLayout->addWidget(label); |
| 73 | hLayout->addWidget(d->gradleVersionComboBox); |
| 74 | vLayout->addLayout(hLayout); |
| 75 | |
| 76 | hLayout = new QHBoxLayout(); |
| 77 | label = new QLabel(QLabel::tr("Main Class:" )); |
| 78 | label->setFixedWidth(kHeadlineWidth); |
| 79 | d->mainClass = new QLineEdit(); |
| 80 | d->mainClass->setPlaceholderText(tr("Input main class" )); |
| 81 | hLayout->addWidget(label); |
| 82 | hLayout->addWidget(d->mainClass); |
| 83 | vLayout->addLayout(hLayout); |
| 84 | vLayout->addStretch(10); |
| 85 | |
| 86 | hLayout = new QHBoxLayout(); |
| 87 | label = new QLabel(QLabel::tr("Detail output:" )); |
| 88 | label->setFixedWidth(kHeadlineWidth); |
| 89 | d->detailBox = new QCheckBox(); |
| 90 | hLayout->addWidget(label); |
| 91 | hLayout->addWidget(d->detailBox); |
| 92 | hLayout->setAlignment(Qt::AlignLeft); |
| 93 | vLayout->addLayout(hLayout); |
| 94 | |
| 95 | // Dap plugins config. |
| 96 | auto addGroupWidgets = [this](QVBoxLayout *vLayout, const QString &headLine, QWidget *widget){ |
| 97 | |
| 98 | QHBoxLayout *hLayout = new QHBoxLayout(); |
| 99 | QLabel *label = new QLabel(headLine + ":" ); |
| 100 | label->setFixedWidth(kHeadlineWidth); |
| 101 | |
| 102 | QPushButton *btnBrowser = new QPushButton(this); |
| 103 | btnBrowser->setText(tr("Browse..." )); |
| 104 | btnBrowser->setObjectName(headLine); |
| 105 | hLayout->addWidget(label); |
| 106 | hLayout->addWidget(widget); |
| 107 | hLayout->addWidget(btnBrowser); |
| 108 | vLayout->addLayout(hLayout); |
| 109 | |
| 110 | connect(btnBrowser, &QPushButton::clicked, this, &GradleDetailPropertyWidget::browserFileDialog); |
| 111 | }; |
| 112 | |
| 113 | d->jreEdit = new QLineEdit(this); |
| 114 | d->jreExecuteEdit = new QLineEdit(this); |
| 115 | d->launchCfgPathEdit = new QLineEdit(this); |
| 116 | d->lanuchCfgFileEdit = new QLineEdit(this); |
| 117 | d->dapPackageFileEdit = new QLineEdit(this); |
| 118 | |
| 119 | addGroupWidgets(vLayout, kJrePath, d->jreEdit); |
| 120 | addGroupWidgets(vLayout, kJreExecute, d->jreExecuteEdit); |
| 121 | addGroupWidgets(vLayout, kLaunchConfigPath, d->launchCfgPathEdit); |
| 122 | addGroupWidgets(vLayout, kLaunchPackageFile, d->lanuchCfgFileEdit); |
| 123 | addGroupWidgets(vLayout, kDapPackageFile,d->dapPackageFileEdit); |
| 124 | } |
| 125 | |
| 126 | void GradleDetailPropertyWidget::initData() |
| 127 | { |
| 128 | d->toolChainData.reset(new ToolChainData()); |
| 129 | QString retMsg; |
| 130 | bool ret = d->toolChainData->readToolChainData(retMsg); |
| 131 | if (ret) { |
| 132 | const ToolChainData::ToolChains &data = d->toolChainData->getToolChanins(); |
| 133 | auto initComboBox = [](QComboBox *comboBox, const ToolChainData::ToolChains &data, const QString &type) { |
| 134 | int index = 0; |
| 135 | ToolChainData::Params params = data.value(type); |
| 136 | for (auto param : params) { |
| 137 | QString text = param.name + "(" + param.path + ")" ; |
| 138 | comboBox->insertItem(index, text); |
| 139 | comboBox->setItemData(index, QVariant::fromValue(param), Qt::UserRole + 1); |
| 140 | index++; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | initComboBox(d->jdkVersionComboBox, data, kJDK); |
| 145 | initComboBox(d->gradleVersionComboBox, data, kGradle); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void GradleDetailPropertyWidget::setValues(const gradleConfig::ConfigureParam *param) |
| 150 | { |
| 151 | if (!param) |
| 152 | return; |
| 153 | |
| 154 | auto initComboBox = [](QComboBox *comboBox, const gradleConfig::ItemInfo &itemInfo) { |
| 155 | int count = comboBox->count(); |
| 156 | for (int i = 0; i < count; i++) { |
| 157 | ToolChainData::ToolChainParam toolChainParam = qvariant_cast<ToolChainData::ToolChainParam>(comboBox->itemData(i, Qt::UserRole + 1)); |
| 158 | if (itemInfo.name == toolChainParam.name |
| 159 | && itemInfo.path == toolChainParam.path) { |
| 160 | comboBox->setCurrentIndex(i); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | initComboBox(d->jdkVersionComboBox, param->jdkVersion); |
| 167 | initComboBox(d->gradleVersionComboBox, param->gradleVersion); |
| 168 | d->mainClass->setText(param->mainClass); |
| 169 | d->detailBox->setChecked(param->detailInfo); |
| 170 | d->jreEdit->setText(param->jrePath); |
| 171 | d->jreExecuteEdit->setText(param->jreExecute); |
| 172 | d->launchCfgPathEdit->setText(param->launchConfigPath); |
| 173 | d->lanuchCfgFileEdit->setText(param->launchPackageFile); |
| 174 | d->dapPackageFileEdit->setText(param->dapPackageFile); |
| 175 | } |
| 176 | |
| 177 | void GradleDetailPropertyWidget::getValues(gradleConfig::ConfigureParam *param) |
| 178 | { |
| 179 | if (!param) |
| 180 | return; |
| 181 | |
| 182 | auto getValue = [](QComboBox *comboBox, gradleConfig::ItemInfo &itemInfo){ |
| 183 | itemInfo.clear(); |
| 184 | int index = comboBox->currentIndex(); |
| 185 | if (index > -1) { |
| 186 | ToolChainData::ToolChainParam value = qvariant_cast<ToolChainData::ToolChainParam>(comboBox->itemData(index, Qt::UserRole + 1)); |
| 187 | itemInfo.name = value.name; |
| 188 | itemInfo.path = value.path; |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | getValue(d->jdkVersionComboBox, param->jdkVersion); |
| 193 | getValue(d->gradleVersionComboBox, param->gradleVersion); |
| 194 | param->mainClass = d->mainClass->text(); |
| 195 | param->detailInfo = d->detailBox->isChecked(); |
| 196 | param->jrePath = d->jreEdit->text(); |
| 197 | param->jreExecute = d->jreExecuteEdit->text(); |
| 198 | param->launchConfigPath = d->launchCfgPathEdit->text(); |
| 199 | param->launchPackageFile = d->lanuchCfgFileEdit->text(); |
| 200 | param->dapPackageFile = d->dapPackageFileEdit->text(); |
| 201 | } |
| 202 | |
| 203 | void GradleDetailPropertyWidget::browserFileDialog() |
| 204 | { |
| 205 | QObject *senderObj = qobject_cast<QObject *>(sender()); |
| 206 | QString senderName = senderObj->objectName(); |
| 207 | |
| 208 | auto showDirDialog = [this](QLineEdit *widget){ |
| 209 | QString result = QFileDialog::getExistingDirectory(this, tr("Open Directory" ), |
| 210 | widget->text(), |
| 211 | QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 212 | if (!result.isEmpty()) { |
| 213 | widget->setText(result); |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | auto showFileDialog = [this](QLineEdit *widget){ |
| 218 | QString result = QFileDialog::getOpenFileName(this, tr("Select File" ), widget->text()); |
| 219 | if (!result.isEmpty()) |
| 220 | widget->setText(result); |
| 221 | }; |
| 222 | |
| 223 | if (senderName == kJrePath) { |
| 224 | showDirDialog(d->jreEdit); |
| 225 | } else if (senderName == kJreExecute) { |
| 226 | showFileDialog(d->jreExecuteEdit); |
| 227 | } else if (senderName == kLaunchConfigPath) { |
| 228 | showDirDialog(d->launchCfgPathEdit); |
| 229 | } else if (senderName == kLaunchPackageFile) { |
| 230 | showFileDialog(d->lanuchCfgFileEdit); |
| 231 | } else if (senderName == kDapPackageFile) { |
| 232 | showFileDialog(d->dapPackageFileEdit); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | class GradleConfigPropertyWidgetPrivate |
| 237 | { |
| 238 | friend class GradleConfigPropertyWidget; |
| 239 | |
| 240 | GradleDetailPropertyWidget *detail{nullptr}; |
| 241 | QStandardItem *item{nullptr}; |
| 242 | dpfservice::ProjectInfo projectInfo; |
| 243 | }; |
| 244 | |
| 245 | GradleConfigPropertyWidget::GradleConfigPropertyWidget(const dpfservice::ProjectInfo &projectInfo, QStandardItem *item, QWidget *parent) |
| 246 | : PageWidget(parent) |
| 247 | , d(new GradleConfigPropertyWidgetPrivate()) |
| 248 | { |
| 249 | d->item = item; |
| 250 | d->projectInfo = projectInfo; |
| 251 | setupUI(); |
| 252 | initData(projectInfo); |
| 253 | } |
| 254 | |
| 255 | GradleConfigPropertyWidget::~GradleConfigPropertyWidget() |
| 256 | { |
| 257 | if (d) |
| 258 | delete d; |
| 259 | } |
| 260 | |
| 261 | void GradleConfigPropertyWidget::setupUI() |
| 262 | { |
| 263 | QVBoxLayout *vLayout = new QVBoxLayout(); |
| 264 | setLayout(vLayout); |
| 265 | |
| 266 | d->detail = new GradleDetailPropertyWidget(); |
| 267 | vLayout->addWidget(d->detail); |
| 268 | vLayout->addStretch(10); |
| 269 | } |
| 270 | |
| 271 | void GradleConfigPropertyWidget::initData(const dpfservice::ProjectInfo &projectInfo) |
| 272 | { |
| 273 | gradleConfig::ConfigureParam *param = gradleConfig::ConfigUtil::instance()->getConfigureParamPointer(); |
| 274 | d->detail->setValues(param); |
| 275 | param->kit = projectInfo.kitName(); |
| 276 | param->language = projectInfo.language(); |
| 277 | param->projectPath = projectInfo.workspaceFolder(); |
| 278 | } |
| 279 | |
| 280 | void GradleConfigPropertyWidget::saveConfig() |
| 281 | { |
| 282 | gradleConfig::ConfigureParam *param = gradleConfig::ConfigUtil::instance()->getConfigureParamPointer(); |
| 283 | d->detail->getValues(param); |
| 284 | |
| 285 | QString filePath = gradleConfig::ConfigUtil::instance()->getConfigPath(param->projectPath); |
| 286 | gradleConfig::ConfigUtil::instance()->saveConfig(filePath, *param); |
| 287 | |
| 288 | gradleConfig::ConfigUtil::instance()->updateProjectInfo(d->projectInfo, param); |
| 289 | dpfservice::ProjectInfo::set(d->item, d->projectInfo); |
| 290 | } |
| 291 | |