1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "configureprojpane.h"
6
7#include "cmake/project/mainframe/cmakeasynparse.h"
8#include "cmake/project/mainframe/cmakeprojectgenerator.h"
9
10#include "services/option/optionmanager.h"
11
12#include <QtCore/QVariant>
13#include <QtWidgets/QApplication>
14#include <QtWidgets/QCheckBox>
15#include <QtWidgets/QGridLayout>
16#include <QtWidgets/QHBoxLayout>
17#include <QtWidgets/QLineEdit>
18#include <QtWidgets/QPushButton>
19#include <QtWidgets/QWidget>
20#include <QtWidgets/QSpacerItem>
21#include <QFileDialog>
22#include <QUrl>
23#include <QJsonArray>
24#include <QJsonObject>
25#include <QJsonDocument>
26#include <QTimer>
27#include <QRadioButton>
28#include <QButtonGroup>
29#include <QComboBox>
30
31// TODO(mozart):should get from kitmanager.
32static const char *kDefaultKitName = "Desktop";
33
34using namespace config;
35
36class ConfigureProjPanePrivate
37{
38 friend class ConfigureProjPane;
39
40 QComboBox *kitComboBox{nullptr};
41
42 QRadioButton *radioDebug{nullptr};
43 QRadioButton *radioRelease{nullptr};
44
45 QLineEdit *lineEditDebug{nullptr};
46 QLineEdit *lineEditRelease{nullptr};
47
48 QPushButton *btnRWithDInfo{nullptr};
49 QPushButton *btnConfigure{nullptr};
50
51 QButtonGroup *group{nullptr};
52
53 ConfigureParam *cfgItem{nullptr};
54};
55
56ConfigureProjPane::ConfigureProjPane(const QString &language,
57 const QString &workspace,
58 QWidget *parent)
59 : QWidget(parent)
60 , d(new ConfigureProjPanePrivate)
61{
62 d->cfgItem = ConfigUtil::instance()->getConfigureParamPointer();
63 d->cfgItem->clear();
64 d->cfgItem->language = language;
65 d->cfgItem->workspace = workspace;
66
67 setupUI();
68 updateUI();
69
70 connect(ConfigUtil::instance(), QOverload<const dpfservice::ProjectInfo &>::of(&ConfigUtil::configureDone),
71 [this](const dpfservice::ProjectInfo &info) {
72 QString propertyPath = ConfigUtil::instance()->getConfigPath(d->cfgItem->workspace);
73 config::ConfigUtil::instance()->saveConfig(propertyPath, *d->cfgItem);
74 dpfservice::ProjectInfo projectInfo = info;
75 config::ConfigUtil::instance()->updateProjectInfo(projectInfo, d->cfgItem);
76 emit configureDone(projectInfo);
77 });
78}
79
80ConfigureProjPane::~ConfigureProjPane()
81{
82 if (d)
83 delete d;
84}
85
86void ConfigureProjPane::setupUI()
87{
88 auto btnSignalConnect = [this](QPushButton *btn, QLineEdit *lineEdit){
89 connect(btn, &QPushButton::clicked, [=](){
90 QString outputDirectory = QFileDialog::getExistingDirectory(this, "Output directory");
91 if (!outputDirectory.isEmpty()) {
92 lineEdit->setText(outputDirectory);
93 }
94 });
95 };
96
97 QLabel *kitLabel = new QLabel(tr("Select kit: "));
98 kitLabel->setFixedWidth(100);
99 d->kitComboBox = new QComboBox();
100 d->kitComboBox->addItem(kDefaultKitName);
101 d->kitComboBox->setCurrentIndex(0);
102 QHBoxLayout *hLayoutKit = new QHBoxLayout();
103 hLayoutKit->addWidget(kitLabel, 0, Qt::AlignLeft);
104 hLayoutKit->addWidget(d->kitComboBox, 0, Qt::AlignLeft);
105 hLayoutKit->addStretch(10);
106
107 d->radioDebug = new QRadioButton("Debug");
108 d->radioDebug->setFixedWidth(100);
109 auto btnDebug = new QPushButton(tr("Browse..."));
110 d->lineEditDebug = new QLineEdit();
111 d->lineEditDebug->setMinimumWidth(400);
112 btnSignalConnect(btnDebug, d->lineEditDebug);
113 QHBoxLayout *hLayoutDebug = new QHBoxLayout();
114 hLayoutDebug->addWidget(d->radioDebug);
115 hLayoutDebug->addWidget(d->lineEditDebug);
116 hLayoutDebug->addWidget(btnDebug);
117
118 d->radioRelease = new QRadioButton("Release");
119 d->radioRelease->setFixedWidth(100);
120 auto btnRelease = new QPushButton(tr("Browse..."));
121 d->lineEditRelease = new QLineEdit();
122 d->lineEditDebug->setMinimumWidth(400);
123 btnSignalConnect(btnRelease, d->lineEditRelease);
124 QHBoxLayout *hLayoutRelease = new QHBoxLayout();
125 hLayoutRelease->addWidget(d->radioRelease);
126 hLayoutRelease->addWidget(d->lineEditRelease);
127 hLayoutRelease->addWidget(btnRelease);
128
129 auto btnConfigure = new QPushButton(tr("Configure"));
130 btnConfigure->connect(btnConfigure, &QPushButton::clicked,
131 this, &ConfigureProjPane::slotConfigure);
132
133 QHBoxLayout *hLayoutBottom = new QHBoxLayout();
134 hLayoutBottom->addStretch(10);
135 hLayoutBottom->addWidget(btnConfigure, 0, Qt::AlignRight);
136
137 QVBoxLayout *vLayout = new QVBoxLayout();
138 vLayout->addLayout(hLayoutKit);
139 vLayout->addSpacing(10);
140 vLayout->addLayout(hLayoutDebug);
141 vLayout->addLayout(hLayoutRelease);
142 vLayout->addSpacing(10);
143 vLayout->addLayout(hLayoutBottom);
144 setLayout(vLayout);
145
146 d->group = new QButtonGroup();
147 d->group->addButton(d->radioDebug, 0);
148 d->group->addButton(d->radioRelease, 1);
149 d->radioDebug->setChecked(true);
150}
151
152void ConfigureProjPane::resetUI()
153{
154 d->kitComboBox->clear();
155 d->radioDebug->setChecked(true);
156 d->lineEditDebug->clear();
157 d->lineEditRelease->clear();
158}
159
160void ConfigureProjPane::updateUI()
161{
162 resetUI();
163
164 d->kitComboBox->addItem(kDefaultKitName);
165
166 if (d->cfgItem->workspace.isEmpty())
167 return;
168
169 QDir folder(d->cfgItem->workspace);
170 QString folderName = folder.dirName();
171 folder.cdUp();
172 QString upDirectory = folder.path();
173
174 auto folerPath = [=](QString buildType) -> QString {
175 return (upDirectory + QDir::separator() + "build" + "-" + folderName + "-" + kDefaultKitName + "-" + buildType);
176 };
177
178 d->lineEditDebug->setText(folerPath(ConfigUtil::instance()->getNameFromType(Debug)));
179 d->lineEditRelease->setText(folerPath(ConfigUtil::instance()->getNameFromType(Release)));
180}
181
182void ConfigureProjPane::slotConfigure()
183{
184 QList<ConfigType> initType = {Debug, Release};
185 foreach (auto type, initType) {
186 BuildConfigure buildConfigure;
187 buildConfigure.type = type;
188 // init output directory
189 if (Debug == type) {
190 buildConfigure.directory = d->lineEditDebug->text().trimmed();
191 } else if (Release == type) {
192 buildConfigure.directory = d->lineEditRelease->text().trimmed();
193 }
194 // init build steps
195 for (int i = 0; i < StepCount; i++) {
196 StepItem item;
197 item.type = static_cast<StepType>(i);
198 buildConfigure.steps.push_back(item);
199 }
200 // init environment
201 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
202 foreach (auto key, env.keys()) {
203 buildConfigure.env.environments.insert(key, env.value(key));
204 }
205
206 d->cfgItem->buildConfigures.push_back(buildConfigure);
207 }
208
209 d->cfgItem->defaultType = ConfigUtil::instance()->getTypeFromName(d->group->checkedButton()->text());
210 d->cfgItem->tempSelType = d->cfgItem->defaultType;
211 d->cfgItem->kit = d->kitComboBox->currentText();
212
213 ConfigUtil::instance()->configProject(d->cfgItem);
214}
215