1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "environmentwidget.h" |
6 | |
7 | #include <QVBoxLayout> |
8 | #include <QCheckBox> |
9 | #include <QHeaderView> |
10 | #include <QTableView> |
11 | |
12 | class EnvironmentModelPrivate |
13 | { |
14 | friend class EnvironmentModel; |
15 | QMap<QString, QString> envs; |
16 | }; |
17 | |
18 | EnvironmentModel::EnvironmentModel(QObject *parent) |
19 | : QAbstractTableModel(parent) |
20 | , d (new EnvironmentModelPrivate()) |
21 | { |
22 | |
23 | } |
24 | |
25 | EnvironmentModel::~EnvironmentModel() |
26 | { |
27 | if (d) |
28 | delete d; |
29 | } |
30 | |
31 | int EnvironmentModel::rowCount(const QModelIndex &) const |
32 | { |
33 | return d->envs.keys().size(); |
34 | } |
35 | |
36 | int EnvironmentModel::columnCount(const QModelIndex &) const |
37 | { |
38 | return kColumnCount; |
39 | } |
40 | |
41 | QVariant EnvironmentModel::data(const QModelIndex &index, int role) const |
42 | { |
43 | if (role == Qt::DisplayRole || role == Qt::EditRole) { |
44 | auto var = d->envs.keys()[index.row()]; |
45 | switch (index.column()) { |
46 | case kVaribale: |
47 | return var; |
48 | case kValue: |
49 | return d->envs.value(var); |
50 | default: |
51 | break; |
52 | } |
53 | } |
54 | return {}; |
55 | } |
56 | |
57 | QVariant EnvironmentModel::(int section, Qt::Orientation orientation, int role) const |
58 | { |
59 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
60 | switch (section) { |
61 | case kVaribale: |
62 | return QObject::tr("Variable" ); |
63 | case kValue: |
64 | return QObject::tr("Value" ); |
65 | default: |
66 | break; |
67 | } |
68 | } |
69 | return {}; |
70 | } |
71 | |
72 | void EnvironmentModel::append(const QString &key, const QString &value) |
73 | { |
74 | beginInsertRows({}, d->envs.keys().count(), d->envs.keys().count()); |
75 | d->envs.insert(key, value); |
76 | endInsertRows(); |
77 | } |
78 | |
79 | void EnvironmentModel::update(const QMap<QString, QString> &data) |
80 | { |
81 | beginResetModel(); |
82 | d->envs.clear(); |
83 | d->envs = data; |
84 | endResetModel(); |
85 | } |
86 | |
87 | const QMap<QString, QString> EnvironmentModel::getEnvironment() const |
88 | { |
89 | return d->envs; |
90 | } |
91 | |
92 | class EnvironmentWidgetPrivate |
93 | { |
94 | friend class EnvironmentWidget; |
95 | |
96 | QVBoxLayout *vLayout{nullptr}; |
97 | QTableView *tableView{nullptr}; |
98 | QCheckBox *checkBox{nullptr}; |
99 | EnvironmentModel *model{nullptr}; |
100 | |
101 | config::EnvironmentItem *envShadow{nullptr}; |
102 | }; |
103 | |
104 | |
105 | EnvironmentWidget::EnvironmentWidget(QWidget *parent) |
106 | : QWidget(parent) |
107 | , d(new EnvironmentWidgetPrivate) |
108 | { |
109 | setAutoFillBackground(true); |
110 | |
111 | if (!d->vLayout) |
112 | d->vLayout = new QVBoxLayout(); |
113 | this->setLayout(d->vLayout); |
114 | |
115 | if (!d->tableView) { |
116 | d->tableView = new QTableView(); |
117 | |
118 | // Initialize view |
119 | d->tableView->setShowGrid(false); |
120 | QHeaderView* = d->tableView->horizontalHeader(); |
121 | headerView->setSectionResizeMode(QHeaderView::ResizeToContents); |
122 | d->tableView->verticalHeader()->hide(); |
123 | } |
124 | |
125 | if (!d->model) |
126 | d->model = new EnvironmentModel(); |
127 | |
128 | d->tableView->setModel(d->model); |
129 | |
130 | if (!d->checkBox) |
131 | d->checkBox = new QCheckBox(); |
132 | |
133 | connect(d->checkBox, &QCheckBox::clicked, [this](){ |
134 | d->envShadow->enable = d->checkBox->isChecked(); |
135 | }); |
136 | |
137 | d->checkBox->setText(tr("Enable All Environment" )); |
138 | d->checkBox->setChecked(true); |
139 | d->vLayout->setSpacing(0); |
140 | d->vLayout->setMargin(0); |
141 | d->vLayout->addWidget(d->checkBox); |
142 | d->vLayout->addWidget(d->tableView); |
143 | } |
144 | |
145 | EnvironmentWidget::~EnvironmentWidget() |
146 | { |
147 | if(d) |
148 | delete d; |
149 | } |
150 | |
151 | void EnvironmentWidget::getValues(config::EnvironmentItem &env) |
152 | { |
153 | env.enable = d->checkBox->isChecked(); |
154 | env.environments = d->model->getEnvironment(); |
155 | } |
156 | |
157 | void EnvironmentWidget::setValues(const config::EnvironmentItem &env) |
158 | { |
159 | d->checkBox->setChecked(env.enable); |
160 | d->model->update(env.environments); |
161 | } |
162 | |
163 | void EnvironmentWidget::bindValues(config::EnvironmentItem *env) |
164 | { |
165 | d->envShadow = env; |
166 | d->checkBox->setChecked(env->enable); |
167 | d->model->update(env->environments); |
168 | } |
169 | |
170 | |
171 | |