| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "gradlewidget.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 | |
| 20 | class GradleWidgetPrivate |
| 21 | { |
| 22 | friend class GradleWidget; |
| 23 | |
| 24 | QRadioButton *useWrapper = nullptr; |
| 25 | QRadioButton *useLocal = nullptr; |
| 26 | QComboBox *localDetail = nullptr; |
| 27 | QSharedPointer<ToolChainData> toolChainData; |
| 28 | }; |
| 29 | |
| 30 | GradleWidget::GradleWidget(QWidget *parent) |
| 31 | : PageWidget(parent) |
| 32 | , d(new GradleWidgetPrivate()) |
| 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 | |
| 45 | GradleWidget::~GradleWidget() |
| 46 | { |
| 47 | if (d) { |
| 48 | delete d; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void GradleWidget::setupUi() |
| 53 | { |
| 54 | QVBoxLayout *vLayout = new QVBoxLayout(); |
| 55 | setLayout(vLayout); |
| 56 | |
| 57 | QLabel *label = new QLabel(QLabel::tr("Gradle distribution:" )); |
| 58 | d->useWrapper = new QRadioButton(tr("use Gradle wrapper" )); |
| 59 | |
| 60 | QHBoxLayout *localLayout = new QHBoxLayout(); |
| 61 | d->useLocal = new QRadioButton(tr("use Local installation, directory:" )); |
| 62 | d->useLocal->setFixedWidth(300); |
| 63 | d->useLocal->setChecked(true); |
| 64 | d->localDetail = new QComboBox(); |
| 65 | localLayout->addWidget(d->useLocal); |
| 66 | localLayout->addWidget(d->localDetail); |
| 67 | |
| 68 | QObject::connect(d->useLocal, &QRadioButton::setChecked, [this](){ |
| 69 | d->localDetail->setEnabled(d->useLocal->isChecked()); |
| 70 | }); |
| 71 | |
| 72 | vLayout->addWidget(label); |
| 73 | vLayout->addLayout(localLayout); |
| 74 | vLayout->addWidget(d->useWrapper); |
| 75 | vLayout->addStretch(); |
| 76 | } |
| 77 | |
| 78 | void GradleWidget::updateUi() |
| 79 | { |
| 80 | const ToolChainData::ToolChains &data = d->toolChainData->getToolChanins(); |
| 81 | ToolChainData::Params cParams = data.value(kGradle); |
| 82 | int i = 0; |
| 83 | for (auto param : cParams) { |
| 84 | QString text = param.name + "(" + param.path + ")" ; |
| 85 | d->localDetail->insertItem(i, text); |
| 86 | d->localDetail->setItemData(i, QVariant::fromValue(param), Qt::UserRole + 1); |
| 87 | i++; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool GradleWidget::getControlValue(QMap<QString, QVariant> &map) |
| 92 | { |
| 93 | GradleConfig config; |
| 94 | config.useWrapper = d->useWrapper->isChecked() ? "1" : "0" ; |
| 95 | config.useLocal = d->useLocal->isChecked() ? "1" : "0" ; |
| 96 | |
| 97 | int index = d->localDetail->currentIndex(); |
| 98 | if (index < 0) { |
| 99 | config.version = ToolChainData::ToolChainParam(); |
| 100 | } else { |
| 101 | config.version = qvariant_cast<ToolChainData::ToolChainParam>(d->localDetail->itemData(index, Qt::UserRole + 1)); |
| 102 | } |
| 103 | |
| 104 | dataToMap(config, map); |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | void GradleWidget::setControlValue(const QMap<QString, QVariant> &map) |
| 110 | { |
| 111 | GradleConfig config; |
| 112 | mapToData(map, config); |
| 113 | |
| 114 | int count = d->localDetail->count(); |
| 115 | for (int i = 0; i < count; i++) { |
| 116 | ToolChainData::ToolChainParam toolChainParam = qvariant_cast<ToolChainData::ToolChainParam>(d->localDetail->itemData(i, Qt::UserRole + 1)); |
| 117 | if (config.version.name == toolChainParam.name |
| 118 | && config.version.path == toolChainParam.path) { |
| 119 | d->localDetail->setCurrentIndex(i); |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | d->useWrapper->setChecked(config.useWrapper.toInt() == 1); |
| 125 | d->useLocal->setChecked(config.useLocal.toInt() == 1); |
| 126 | } |
| 127 | |
| 128 | bool GradleWidget::dataToMap(const GradleConfig &config, QMap<QString, QVariant> &map) |
| 129 | { |
| 130 | QMap<QString, QVariant> version; |
| 131 | version.insert("name" , config.version.name); |
| 132 | version.insert("path" , config.version.path); |
| 133 | |
| 134 | map.insert("version" , version); |
| 135 | map.insert("useWrapper" , config.useWrapper); |
| 136 | map.insert("useLocal" , config.useLocal); |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool GradleWidget::mapToData(const QMap<QString, QVariant> &map, GradleConfig &config) |
| 142 | { |
| 143 | QMap<QString, QVariant> version = map.value("version" ).toMap(); |
| 144 | config.version.name = version.value("name" ).toString(); |
| 145 | config.version.path = version.value("path" ).toString(); |
| 146 | |
| 147 | config.useWrapper = map.value("useWrapper" ).toString(); |
| 148 | config.useLocal = map.value("useLocal" ).toString(); |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | void GradleWidget::setUserConfig(const QMap<QString, QVariant> &map) |
| 154 | { |
| 155 | setControlValue(map); |
| 156 | } |
| 157 | |
| 158 | void GradleWidget::getUserConfig(QMap<QString, QVariant> &map) |
| 159 | { |
| 160 | getControlValue(map); |
| 161 | } |
| 162 | |