| 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 <QProcessEnvironment> |
| 10 | #include <QHeaderView> |
| 11 | |
| 12 | |
| 13 | const QString ENABLE_ALL_ENV = EnvironmentWidget::tr("Enable All Environment" ); |
| 14 | |
| 15 | class EnvironmentModel; |
| 16 | class EnvironmentWidgetPrivate |
| 17 | { |
| 18 | friend class EnvironmentWidget; |
| 19 | QVBoxLayout *vLayout = nullptr; |
| 20 | QTableView *tableView = nullptr; |
| 21 | QCheckBox *checkBox = nullptr; |
| 22 | EnvironmentModel *model = nullptr; |
| 23 | }; |
| 24 | |
| 25 | class EnvironmentModel : public QAbstractTableModel |
| 26 | { |
| 27 | public: |
| 28 | enum ColumnType |
| 29 | { |
| 30 | kVaribale, |
| 31 | kValue, |
| 32 | kColumnCount |
| 33 | }; |
| 34 | |
| 35 | explicit EnvironmentModel(QObject *parent = nullptr) |
| 36 | : QAbstractTableModel(parent) |
| 37 | , envs(QProcessEnvironment::systemEnvironment()) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | int rowCount(const QModelIndex &) const override |
| 42 | { |
| 43 | return envs.keys().size(); |
| 44 | } |
| 45 | |
| 46 | int columnCount(const QModelIndex &) const override |
| 47 | { |
| 48 | return kColumnCount; |
| 49 | } |
| 50 | |
| 51 | QVariant data(const QModelIndex &index, int role) const override |
| 52 | { |
| 53 | if (role == Qt::DisplayRole || role == Qt::EditRole) { |
| 54 | auto var = envs.keys()[index.row()]; |
| 55 | switch (index.column()) { |
| 56 | case kVaribale: |
| 57 | return var; |
| 58 | case kValue: |
| 59 | return envs.value(var); |
| 60 | default: |
| 61 | ; // do nothing |
| 62 | } |
| 63 | } |
| 64 | return {}; |
| 65 | } |
| 66 | |
| 67 | QVariant (int section, Qt::Orientation orientation, int role) const override |
| 68 | { |
| 69 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 70 | switch (section) { |
| 71 | case kVaribale: |
| 72 | return QObject::tr("Variable" ); |
| 73 | case kValue: |
| 74 | return QObject::tr("Value" ); |
| 75 | default: |
| 76 | ; // do nothing. |
| 77 | } |
| 78 | } |
| 79 | return {}; |
| 80 | } |
| 81 | |
| 82 | void append(const QString &key, const QString &value) |
| 83 | { |
| 84 | beginInsertRows({}, envs.keys().count(), envs.keys().count()); |
| 85 | envs.insert(key, value); |
| 86 | endInsertRows(); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | QProcessEnvironment envs; |
| 91 | }; |
| 92 | |
| 93 | EnvironmentWidget::EnvironmentWidget(QWidget *parent) |
| 94 | : PageWidget(parent) |
| 95 | , d(new EnvironmentWidgetPrivate) |
| 96 | { |
| 97 | setAutoFillBackground(true); |
| 98 | |
| 99 | if (!d->vLayout) |
| 100 | d->vLayout = new QVBoxLayout(); |
| 101 | this->setLayout(d->vLayout); |
| 102 | |
| 103 | if (!d->tableView) { |
| 104 | d->tableView = new QTableView(); |
| 105 | |
| 106 | // Initialize view |
| 107 | d->tableView->setShowGrid(false); |
| 108 | QHeaderView* = d->tableView->horizontalHeader(); |
| 109 | headerView->setSectionResizeMode(QHeaderView::ResizeToContents); |
| 110 | d->tableView->verticalHeader()->hide(); |
| 111 | } |
| 112 | |
| 113 | if (!d->model) |
| 114 | d->model = new EnvironmentModel(); |
| 115 | |
| 116 | d->tableView->setModel(d->model); |
| 117 | |
| 118 | if (!d->checkBox) |
| 119 | d->checkBox = new QCheckBox(); |
| 120 | d->checkBox->setText(ENABLE_ALL_ENV); |
| 121 | d->checkBox->setChecked(true); |
| 122 | d->vLayout->setSpacing(0); |
| 123 | d->vLayout->setMargin(5); |
| 124 | d->vLayout->addWidget(d->tableView); |
| 125 | d->vLayout->addWidget(d->checkBox); |
| 126 | } |
| 127 | |
| 128 | EnvironmentWidget::~EnvironmentWidget() |
| 129 | { |
| 130 | if(d) |
| 131 | delete d; |
| 132 | } |
| 133 | |