1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "detailwidget.h" |
6 | #include "templateparser.h" |
7 | |
8 | #include "common/util/custompaths.h" |
9 | |
10 | #include <QLabel> |
11 | #include <QVBoxLayout> |
12 | #include <QPainter> |
13 | #include <QLineEdit> |
14 | #include <QComboBox> |
15 | #include <QPushButton> |
16 | #include <QFileDialog> |
17 | #include <QMessageBox> |
18 | |
19 | class DetailWidgetPrivate |
20 | { |
21 | friend class DetailWidget; |
22 | |
23 | QString templatePath; |
24 | QMap<QString, QLineEdit*> lineEditMap; |
25 | QMap<QString, QComboBox*> comboBoxMap; |
26 | WizardInfo wizardInfo; |
27 | }; |
28 | |
29 | DetailWidget::DetailWidget(QWidget *parent) |
30 | : QScrollArea(parent) |
31 | , d(new DetailWidgetPrivate()) |
32 | { |
33 | |
34 | } |
35 | |
36 | DetailWidget::DetailWidget(const QString &templatePath, QWidget *parent) |
37 | : QScrollArea(parent) |
38 | , d(new DetailWidgetPrivate()) |
39 | { |
40 | d->templatePath = templatePath; |
41 | if (!TemplateParser::readWizardConfig(d->templatePath, d->wizardInfo)) |
42 | return; |
43 | |
44 | QWidget *widget = new QWidget(); |
45 | QVBoxLayout *vLayout = new QVBoxLayout(); |
46 | widget->setLayout(vLayout); |
47 | |
48 | QLabel *titleLabel = new QLabel(d->wizardInfo.trDisplayName); |
49 | vLayout->addWidget(titleLabel); |
50 | vLayout->addSpacing(10); |
51 | |
52 | auto iter = d->wizardInfo.configures.begin(); |
53 | for (; iter != d->wizardInfo.configures.end(); ++iter) { |
54 | QHBoxLayout *hLayout = new QHBoxLayout(); |
55 | QLabel *label = new QLabel(iter->displayName + ":" ); |
56 | label->setFixedSize(120, 30); |
57 | hLayout->addWidget(label, 0, Qt::AlignLeft); |
58 | hLayout->setStretchFactor(label, 1); |
59 | |
60 | if ("lineEdit" == iter->type) { |
61 | QLineEdit *lineEdit = new QLineEdit(); |
62 | if (!iter->defaultValues.isEmpty()) { |
63 | lineEdit->setText(iter->defaultValues.at(0)); |
64 | } |
65 | hLayout->addWidget(lineEdit, 0, Qt::AlignLeft); |
66 | hLayout->setStretchFactor(lineEdit, 3); |
67 | |
68 | d->lineEditMap.insert(iter->key, lineEdit); |
69 | if (iter->browse) { |
70 | lineEdit->setFixedSize(300, 30); |
71 | lineEdit->setReadOnly(true); |
72 | |
73 | QPushButton *browse = new QPushButton(tr("Browse..." )); |
74 | browse->setFixedSize(100, 30); |
75 | hLayout->addWidget(browse, 0, Qt::AlignRight); |
76 | hLayout->setStretchFactor(browse, 1); |
77 | |
78 | connect(browse, &QPushButton::clicked, [=]() { |
79 | QString path = QFileDialog::getExistingDirectory(this, tr("Choose path" ), QDir::homePath()); |
80 | if (!path.isEmpty()) { |
81 | lineEdit->setText(path); |
82 | } |
83 | }); |
84 | } else { |
85 | lineEdit->setFixedSize(400, 30); |
86 | } |
87 | } else if ("comboBox" == iter->type) { |
88 | QComboBox *comboBox = new QComboBox(); |
89 | comboBox->setFixedSize(400, 30); |
90 | hLayout->addWidget(comboBox, 0, Qt::AlignLeft); |
91 | hLayout->setStretchFactor(comboBox, 3); |
92 | |
93 | if (!iter->defaultValues.isEmpty()) { |
94 | comboBox->addItems(iter->defaultValues); |
95 | comboBox->setCurrentIndex(0); |
96 | } |
97 | d->comboBoxMap.insert(iter->key, comboBox); |
98 | } |
99 | |
100 | vLayout->addLayout(hLayout); |
101 | } |
102 | |
103 | setWidget(widget); |
104 | } |
105 | |
106 | DetailWidget::~DetailWidget() |
107 | { |
108 | if (d) |
109 | delete d; |
110 | } |
111 | |
112 | bool DetailWidget::getGenParams(PojectGenParam ¶m) |
113 | { |
114 | foreach (auto key, d->lineEditMap.keys()) { |
115 | auto lineEdit = d->lineEditMap.value(key); |
116 | |
117 | if (lineEdit->text().trimmed().isEmpty()) { |
118 | QMessageBox::critical(this, "tip" , "The value of " + key + " is empty" ); |
119 | return false; |
120 | } |
121 | |
122 | param.settingParamMap.insert("%{" + key + "}" , lineEdit->text().trimmed()); |
123 | } |
124 | |
125 | foreach (auto key, d->comboBoxMap.keys()) { |
126 | auto comboBox = d->comboBoxMap.value(key); |
127 | param.settingParamMap.insert("%{" + key + "}" , comboBox->currentText().trimmed()); |
128 | } |
129 | |
130 | param.templatePath = CustomPaths::global(CustomPaths::Templates) |
131 | + QDir::separator() + d->templatePath; |
132 | param.generator = d->wizardInfo.generator; |
133 | if (d->wizardInfo.type == "project" ) { |
134 | param.type = Project; |
135 | } else if (d->wizardInfo.type == "file" ) { |
136 | param.type = File; |
137 | } |
138 | param.kit = d->wizardInfo.kit; |
139 | param.language = d->wizardInfo.language; |
140 | |
141 | return true; |
142 | } |
143 | |
144 | |
145 | |
146 | |