1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "configpropertywidget.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 | |
16 | using namespace config; |
17 | |
18 | class DetailPropertyWidgetPrivate |
19 | { |
20 | friend class DetailPropertyWidget; |
21 | QComboBox *pyVersionComboBox{nullptr}; |
22 | QSharedPointer<ToolChainData> toolChainData; |
23 | }; |
24 | |
25 | DetailPropertyWidget::DetailPropertyWidget(QWidget *parent) |
26 | : QWidget(parent) |
27 | , d(new DetailPropertyWidgetPrivate()) |
28 | { |
29 | setupUI(); |
30 | initData(); |
31 | } |
32 | |
33 | DetailPropertyWidget::~DetailPropertyWidget() |
34 | { |
35 | if (d) |
36 | delete d; |
37 | } |
38 | |
39 | void DetailPropertyWidget::setupUI() |
40 | { |
41 | QVBoxLayout *vLayout = new QVBoxLayout(); |
42 | setLayout(vLayout); |
43 | |
44 | QHBoxLayout *hLayout = new QHBoxLayout(); |
45 | QLabel *label = new QLabel(QLabel::tr("JS interpreter: " )); |
46 | label->setFixedWidth(120); |
47 | d->pyVersionComboBox = new QComboBox(); |
48 | hLayout->addWidget(label); |
49 | hLayout->addWidget(d->pyVersionComboBox); |
50 | vLayout->addLayout(hLayout); |
51 | |
52 | vLayout->addStretch(10); |
53 | } |
54 | |
55 | void DetailPropertyWidget::initData() |
56 | { |
57 | d->toolChainData.reset(new ToolChainData()); |
58 | QString retMsg; |
59 | bool ret = d->toolChainData->readToolChainData(retMsg); |
60 | if (ret) { |
61 | const ToolChainData::ToolChains &data = d->toolChainData->getToolChanins(); |
62 | auto initComboBox = [](QComboBox *comboBox, const ToolChainData::ToolChains &data, const QString &type) { |
63 | int index = 0; |
64 | ToolChainData::Params params = data.value(type); |
65 | for (auto param : params) { |
66 | QString text = param.name + "(" + param.path + ")" ; |
67 | comboBox->insertItem(index, text); |
68 | comboBox->setItemData(index, QVariant::fromValue(param), Qt::UserRole + 1); |
69 | index++; |
70 | } |
71 | }; |
72 | |
73 | initComboBox(d->pyVersionComboBox, data, kJS); |
74 | } |
75 | } |
76 | |
77 | void DetailPropertyWidget::setValues(const config::ConfigureParam *param) |
78 | { |
79 | if (!param) |
80 | return; |
81 | |
82 | auto initComboBox = [](QComboBox *comboBox, const config::ItemInfo &itemInfo) { |
83 | int count = comboBox->count(); |
84 | for (int i = 0; i < count; i++) { |
85 | ToolChainData::ToolChainParam toolChainParam = qvariant_cast<ToolChainData::ToolChainParam>(comboBox->itemData(i, Qt::UserRole + 1)); |
86 | if (itemInfo.name == toolChainParam.name |
87 | && itemInfo.path == toolChainParam.path) { |
88 | comboBox->setCurrentIndex(i); |
89 | break; |
90 | } |
91 | } |
92 | }; |
93 | |
94 | initComboBox(d->pyVersionComboBox, param->jsVersion); |
95 | } |
96 | |
97 | void DetailPropertyWidget::getValues(config::ConfigureParam *param) |
98 | { |
99 | if (!param) |
100 | return; |
101 | |
102 | auto getValue = [](QComboBox *comboBox, ItemInfo &itemInfo){ |
103 | itemInfo.clear(); |
104 | int index = comboBox->currentIndex(); |
105 | if (index > -1) { |
106 | ToolChainData::ToolChainParam value = qvariant_cast<ToolChainData::ToolChainParam>(comboBox->itemData(index, Qt::UserRole + 1)); |
107 | itemInfo.name = value.name; |
108 | itemInfo.path = value.path; |
109 | } |
110 | }; |
111 | |
112 | getValue(d->pyVersionComboBox, param->jsVersion); |
113 | } |
114 | |
115 | class ConfigPropertyWidgetPrivate |
116 | { |
117 | friend class ConfigPropertyWidget; |
118 | |
119 | DetailPropertyWidget *detail{nullptr}; |
120 | QStandardItem *item{nullptr}; |
121 | dpfservice::ProjectInfo projectInfo; |
122 | }; |
123 | |
124 | ConfigPropertyWidget::ConfigPropertyWidget(const dpfservice::ProjectInfo &projectInfo, QStandardItem *item, QWidget *parent) |
125 | : PageWidget(parent) |
126 | , d(new ConfigPropertyWidgetPrivate()) |
127 | { |
128 | d->item = item; |
129 | d->projectInfo = projectInfo; |
130 | setupUI(); |
131 | initData(projectInfo); |
132 | } |
133 | |
134 | ConfigPropertyWidget::~ConfigPropertyWidget() |
135 | { |
136 | if (d) |
137 | delete d; |
138 | } |
139 | |
140 | void ConfigPropertyWidget::setupUI() |
141 | { |
142 | QVBoxLayout *vLayout = new QVBoxLayout(); |
143 | setLayout(vLayout); |
144 | |
145 | d->detail = new DetailPropertyWidget(); |
146 | vLayout->addWidget(d->detail); |
147 | vLayout->addStretch(10); |
148 | } |
149 | |
150 | void ConfigPropertyWidget::initData(const dpfservice::ProjectInfo &projectInfo) |
151 | { |
152 | ConfigureParam *param = ConfigUtil::instance()->getConfigureParamPointer(); |
153 | ConfigUtil::instance()->readConfig(ConfigUtil::instance()->getConfigPath(projectInfo.workspaceFolder()), *param); |
154 | d->detail->setValues(param); |
155 | param->kit = projectInfo.kitName(); |
156 | param->language = projectInfo.language(); |
157 | param->projectPath = projectInfo.workspaceFolder(); |
158 | } |
159 | |
160 | void ConfigPropertyWidget::saveConfig() |
161 | { |
162 | ConfigureParam *param = ConfigUtil::instance()->getConfigureParamPointer(); |
163 | d->detail->getValues(param); |
164 | |
165 | QString filePath = ConfigUtil::instance()->getConfigPath(param->projectPath); |
166 | ConfigUtil::instance()->saveConfig(filePath, *param); |
167 | |
168 | ConfigUtil::instance()->updateProjectInfo(d->projectInfo, param); |
169 | dpfservice::ProjectInfo::set(d->item, d->projectInfo); |
170 | } |
171 | |