From a8db5f01b1dd04627bdbe5a95e59e91732dc27d0 Mon Sep 17 00:00:00 2001 From: liuzhangjian Date: Fri, 9 Aug 2024 14:48:34 +0800 Subject: [PATCH] fix: [Env view] Fixed issues with environment variable view 1.The application crashed while changing the variable name 2.After the variable name is modified, the modified item cannot be locked Bug: https://pms.uniontech.com/bug-view-255803.html Log: fix bug --- src/common/util/namevaluemodel.cpp | 199 ++++++++++++++++++ src/common/util/namevaluemodel.h | 45 ++++ .../binarytools/mainframe/environmentview.cpp | 165 ++------------- .../binarytools/mainframe/environmentview.h | 36 +--- .../cxx/cmake/project/properties/configutil.h | 4 +- .../project/properties/environmentwidget.cpp | 167 ++------------- .../project/properties/environmentwidget.h | 34 +-- src/plugins/option/optioncore/CMakeLists.txt | 8 +- .../mainframe/environmentwidget.cpp | 143 ------------- .../optioncore/mainframe/environmentwidget.h | 24 --- .../mainframe/optionenvironmentgenerator.cpp | 15 -- .../mainframe/optionenvironmentgenerator.h | 19 -- src/plugins/option/optioncore/optioncore.cpp | 11 +- 13 files changed, 302 insertions(+), 568 deletions(-) create mode 100644 src/common/util/namevaluemodel.cpp create mode 100644 src/common/util/namevaluemodel.h delete mode 100644 src/plugins/option/optioncore/mainframe/environmentwidget.cpp delete mode 100644 src/plugins/option/optioncore/mainframe/environmentwidget.h delete mode 100644 src/plugins/option/optioncore/mainframe/optionenvironmentgenerator.cpp delete mode 100644 src/plugins/option/optioncore/mainframe/optionenvironmentgenerator.h diff --git a/src/common/util/namevaluemodel.cpp b/src/common/util/namevaluemodel.cpp new file mode 100644 index 000000000..0fc45018a --- /dev/null +++ b/src/common/util/namevaluemodel.cpp @@ -0,0 +1,199 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "namevaluemodel.h" + +class NameValueModelPrivate +{ +public: + int indexOf(const QString &name); + int findInsertPosition(const QString &name); + +public: + QVariantMap itemMap; +}; + +int NameValueModelPrivate::indexOf(const QString &name) +{ + for (int i = 0; i < itemMap.size(); ++i) { + if (itemMap.keys()[i] == name) + return i; + } + + return -1; +} + +int NameValueModelPrivate::findInsertPosition(const QString &name) +{ + auto iter = itemMap.cbegin(); + int i = 0; + for (; iter != itemMap.cend(); ++iter, ++i) { + if (iter.key().compare(name, Qt::CaseSensitive) > 0) + return i; + } + + return itemMap.size(); +} + +NameValueModel::NameValueModel(QObject *parent) + : QAbstractTableModel(parent), + d(new NameValueModelPrivate) +{ +} + +NameValueModel::~NameValueModel() +{ + delete d; +} + +int NameValueModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) + return 0; + + return d->itemMap.size(); +} + +int NameValueModel::columnCount(const QModelIndex &parent) const +{ + if (parent.isValid()) + return 0; + + return 2; +} + +QVariant NameValueModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() >= d->itemMap.size()) + return QVariant(); + + auto name = d->itemMap.keys()[index.row()]; + switch (role) { + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::ToolTipRole: + if (index.column() == 0) + return name; + + if (index.column() == 1) + return d->itemMap.value(name); + break; + } + return QVariant(); +} + +bool NameValueModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (!index.isValid() || role != Qt::EditRole) + return false; + + // ignore changes to already set values: + if (data(index, role) == value) + return true; + + const QString oldName = data(this->index(index.row(), 0, QModelIndex())).toString(); + const QString oldValue = data(this->index(index.row(), 1, QModelIndex()), Qt::EditRole).toString(); + if (index.column() == 0) { + //fail if a variable with the same name already exists + const QString &newName = value.toString(); + if (newName.isEmpty() || newName.contains('=')) + return false; + // Does the new name exist already? + if (d->itemMap.contains(newName) || newName.isEmpty()) + return false; + + removeItem(index); + QModelIndex newIndex = addItem(newName, oldValue); // add the new variable + emit focusIndex(newIndex); // hint to focus on the name + return true; + } else if (index.column() == 1) { + d->itemMap[oldName] = value; + emit dataChanged(index, index); + return true; + } + return false; +} + +Qt::ItemFlags NameValueModel::flags(const QModelIndex &index) const +{ + Q_UNUSED(index) + return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled; +} + +QVariant NameValueModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Vertical || role != Qt::DisplayRole) + return QVariant(); + return section == 0 ? tr("Variable") : tr("Value"); +} + +void NameValueModel::setItems(const QVariantMap &items) +{ + beginResetModel(); + d->itemMap = items; + endResetModel(); +} + +QVariantMap NameValueModel::items() const +{ + return d->itemMap; +} + +QModelIndex NameValueModel::addItem() +{ + return addItem(tr(""), tr("")); +} + +QModelIndex NameValueModel::addItem(const QString &name, const QVariant &value) +{ + int pos = d->indexOf(name); + if (pos >= 0 && pos < d->itemMap.size()) + return index(pos, 0, QModelIndex()); + + int insertPos = d->findInsertPosition(name); + beginInsertRows(QModelIndex(), insertPos, insertPos); + d->itemMap.insert(name, value); + endInsertRows(); + + return index(insertPos, 0, QModelIndex()); +} + +void NameValueModel::removeItem(const QModelIndex &index) +{ + auto name = data(this->index(index.row(), 0, QModelIndex())).toString(); + removeItem(name); +} + +void NameValueModel::removeItem(const QString &variable) +{ + int pos = d->indexOf(variable); + if (pos < 0) + return; + + beginRemoveRows(QModelIndex(), pos, pos); + d->itemMap.remove(variable); + endRemoveRows(); +} + +void NameValueModel::clear() +{ + beginResetModel(); + d->itemMap.clear(); + endResetModel(); +} + +QString NameValueModel::variableFromIndex(const QModelIndex &index) const +{ + auto it = std::next(d->itemMap.cbegin(), index.row()); + return it.key(); +} + +QModelIndex NameValueModel::indexFromVariable(const QString &name) const +{ + int pos = d->indexOf(name); + if (pos == -1) + return {}; + + return index(pos, 0); +} diff --git a/src/common/util/namevaluemodel.h b/src/common/util/namevaluemodel.h new file mode 100644 index 000000000..95892cc6a --- /dev/null +++ b/src/common/util/namevaluemodel.h @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef NAMEVALUEMODEL_H +#define NAMEVALUEMODEL_H + +#include + +class NameValueModelPrivate; +class NameValueModel : public QAbstractTableModel +{ + Q_OBJECT +public: + explicit NameValueModel(QObject *parent = nullptr); + ~NameValueModel() override; + + int rowCount(const QModelIndex &parent) const override; + int columnCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + QVariant headerData(int section, + Qt::Orientation orientation, + int role = Qt::DisplayRole) const override; + + void setItems(const QVariantMap &items); + QVariantMap items() const; + QModelIndex addItem(); + QModelIndex addItem(const QString &name, const QVariant &value); + void removeItem(const QModelIndex &index); + void removeItem(const QString &variable); + void clear(); + + QString variableFromIndex(const QModelIndex &index) const; + QModelIndex indexFromVariable(const QString &name) const; + +signals: + void focusIndex(const QModelIndex &index); + +private: + NameValueModelPrivate *const d; +}; + +#endif // NAMEVALUEMODEL_H diff --git a/src/plugins/binarytools/mainframe/environmentview.cpp b/src/plugins/binarytools/mainframe/environmentview.cpp index ffbd84450..e8f393708 100644 --- a/src/plugins/binarytools/mainframe/environmentview.cpp +++ b/src/plugins/binarytools/mainframe/environmentview.cpp @@ -5,6 +5,9 @@ #include "environmentview.h" #include "binarytoolsconfigview.h" +#include "common/util/namevaluemodel.h" +#include "base/baseitemdelegate.h" + #include #include #include @@ -17,143 +20,14 @@ #include DWIDGET_USE_NAMESPACE -class EnvironmentModelPrivate -{ - friend class EnvironmentModel; - QMap envs; -}; - -EnvironmentModel::EnvironmentModel(QObject *parent) - : QAbstractTableModel(parent), d(new EnvironmentModelPrivate()) -{ -} - -EnvironmentModel::~EnvironmentModel() -{ - if (d) - delete d; -} - -int EnvironmentModel::rowCount(const QModelIndex &) const -{ - return d->envs.keys().size(); -} - -int EnvironmentModel::columnCount(const QModelIndex &) const -{ - return ColumnCount; -} - -QVariant EnvironmentModel::data(const QModelIndex &index, int role) const -{ - if (role == Qt::DisplayRole || role == Qt::EditRole) { - auto var = d->envs.keys()[index.row()]; - switch (index.column()) { - case Key: - return var; - case Value: - return d->envs.value(var); - default: - break; - } - } - return {}; -} - -bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - if (!index.isValid() || role != Qt::EditRole) - return false; - - if (data(index, role) == value) - return true; - - const QString oldName = data(this->index(index.row(), 0, QModelIndex())).toString(); - const QString oldValue = data(this->index(index.row(), 1, QModelIndex()), Qt::EditRole).toString(); - QMap map = d->envs; - if (index.column() == Key) { - const QString newName = value.toString(); - if (newName.isEmpty() || newName.contains("=")) - return false; - if (map.contains(newName) || newName.isEmpty()) - return false; - map.remove(oldName); - map.insert(value.toString(), oldValue); - } else if (index.column() == Value) { - const QString stringValue = value.toString(); - auto var = map.keys()[index.row()]; - map[var] = stringValue; - } - update(map); - emit dataChanged(index, index); - return true; -} - -Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const -{ - Q_UNUSED(index); - return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable; -} - -QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - switch (section) { - case Key: - return QObject::tr("Variable"); - case Value: - return QObject::tr("Value"); - default: - break; - } - } - return {}; -} - -QModelIndex EnvironmentModel::append(const QString &key, const QVariant &value) -{ - beginResetModel(); - d->envs.insert(key, value); - endResetModel(); - - for (int row = 0; row < d->envs.count(); row++) { - if (index(row, 0).data() == key) - return index(row, 0); - } - return {}; -} - -void EnvironmentModel::remove(QModelIndex &index) -{ - if (d->envs.keys().isEmpty() || index.row() < 0 || index.row() >= d->envs.count()) - return; - - beginResetModel(); - QString key = d->envs.keys()[index.row()]; - d->envs.remove(key); - endResetModel(); -} - -void EnvironmentModel::update(const QMap &data) -{ - beginResetModel(); - d->envs.clear(); - d->envs = data; - endResetModel(); -} - -const QMap EnvironmentModel::getEnvironment() const -{ - return d->envs; -} class EnvironmentViewPrivate { friend class EnvironmentView; + NameValueModel model; QVBoxLayout *vLayout = nullptr; DTableView *tableView = nullptr; - EnvironmentModel *model = nullptr; BinaryToolsConfigView *configView = nullptr; DIconButton *appendButton = nullptr; DIconButton *deleteButton = nullptr; @@ -184,30 +58,27 @@ EnvironmentView::EnvironmentView(DWidget *parent) d->tableView->setAlternatingRowColors(true); } d->vLayout->addWidget(d->tableView); - - if (!d->model) - d->model = new EnvironmentModel(); - - d->tableView->setModel(d->model); + d->tableView->setModel(&d->model); + d->tableView->setItemDelegate(new BaseItemDelegate(d->tableView)); //append d->appendButton = new DIconButton(this); d->appendButton->setIcon(QIcon::fromTheme("binarytools_add")); - d->appendButton->setIconSize({16, 16}); + d->appendButton->setIconSize({ 16, 16 }); d->appendButton->setFlat(true); d->appendButton->setToolTip(tr("append")); //Delete d->deleteButton = new DIconButton(this); d->deleteButton->setIcon(QIcon::fromTheme("binarytools_reduce")); - d->deleteButton->setIconSize({16, 16}); + d->deleteButton->setIconSize({ 16, 16 }); d->deleteButton->setFlat(true); d->deleteButton->setToolTip(tr("reduce")); //Reset d->resetButton = new DIconButton(this); d->resetButton->setIcon(QIcon::fromTheme("binarytools_reset")); - d->resetButton->setIconSize({16, 16}); + d->resetButton->setIconSize({ 16, 16 }); d->resetButton->setFlat(true); d->resetButton->setToolTip(tr("reset")); @@ -230,6 +101,7 @@ EnvironmentView::EnvironmentView(DWidget *parent) initModel(); + connect(&d->model, &NameValueModel::focusIndex, this, &EnvironmentView::handleFocusIndex); connect(d->appendButton, &DPushButton::clicked, this, &EnvironmentView::appendRow); connect(d->deleteButton, &DPushButton::clicked, this, &EnvironmentView::deleteRow); connect(d->resetButton, &DPushButton::clicked, this, &EnvironmentView::initModel); @@ -243,6 +115,13 @@ void EnvironmentView::disableDleteButton() d->deleteButton->setEnabled(false); } +void EnvironmentView::handleFocusIndex(const QModelIndex &index) +{ + d->tableView->setCurrentIndex(index); + d->tableView->setFocus(); + d->tableView->scrollTo(index, QTableView::PositionAtTop); +} + EnvironmentView::~EnvironmentView() { if (d) @@ -251,19 +130,19 @@ EnvironmentView::~EnvironmentView() const QMap EnvironmentView::getEnvironment() { - return d->model->getEnvironment(); + return d->model.items(); } void EnvironmentView::appendRow() { - auto index = d->model->append("", ""); + auto index = d->model.addItem(); d->tableView->setCurrentIndex(index); } void EnvironmentView::deleteRow() { QModelIndex index = d->tableView->currentIndex(); - d->model->remove(index); + d->model.removeItem(index); } void EnvironmentView::initModel() @@ -275,13 +154,13 @@ void EnvironmentView::initModel() envs.insert(key, value); } - d->model->update(envs); + d->model.setItems(envs); emit deleteSignal(false); } void EnvironmentView::setValue(const QMap &map) { - d->model->update(map); + d->model.setItems(map); } QMap EnvironmentView::defaultEnvironment() diff --git a/src/plugins/binarytools/mainframe/environmentview.h b/src/plugins/binarytools/mainframe/environmentview.h index 6a7e19def..35b2410f0 100644 --- a/src/plugins/binarytools/mainframe/environmentview.h +++ b/src/plugins/binarytools/mainframe/environmentview.h @@ -8,37 +8,6 @@ #include #include -class EnvironmentModelPrivate; -class EnvironmentModel : public QAbstractTableModel -{ - Q_OBJECT -public: - enum ColumnType - { - Key, - Value, - ColumnCount - }; - - explicit EnvironmentModel(QObject *parent = nullptr); - ~EnvironmentModel() override; - - int rowCount(const QModelIndex &) const override; - int columnCount(const QModelIndex &) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - Qt::ItemFlags flags(const QModelIndex &index) const override; - QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - - QModelIndex append(const QString &key, const QVariant &value); - void remove(QModelIndex &index); - void update(const QMap &data); - const QMap getEnvironment() const; - -private: - EnvironmentModelPrivate *const d; -}; - class EnvironmentViewPrivate; class EnvironmentView : public QWidget { @@ -57,12 +26,13 @@ class EnvironmentView : public QWidget public slots: void disableDleteButton(); + void handleFocusIndex(const QModelIndex &index); + signals: void deleteSignal(bool enable); private: - EnvironmentViewPrivate *const d; }; -#endif // ENVIRONMENTVIEW_H +#endif // ENVIRONMENTVIEW_H diff --git a/src/plugins/cxx/cmake/project/properties/configutil.h b/src/plugins/cxx/cmake/project/properties/configutil.h index 54ce4304f..560996310 100644 --- a/src/plugins/cxx/cmake/project/properties/configutil.h +++ b/src/plugins/cxx/cmake/project/properties/configutil.h @@ -58,7 +58,7 @@ struct StepItem struct EnvironmentItem { bool enable = true; - QMap environments; + QVariantMap environments; EnvironmentItem() { @@ -69,7 +69,7 @@ struct EnvironmentItem { QStringList envList; for (auto it = environments.begin(); it != environments.end(); it++) { - envList.append(it.key() + "=" + it.value()); + envList.append(it.key() + "=" + it.value().toString()); } return envList; } diff --git a/src/plugins/cxx/cmake/project/properties/environmentwidget.cpp b/src/plugins/cxx/cmake/project/properties/environmentwidget.cpp index f93e39b18..6b37bcf2b 100644 --- a/src/plugins/cxx/cmake/project/properties/environmentwidget.cpp +++ b/src/plugins/cxx/cmake/project/properties/environmentwidget.cpp @@ -4,6 +4,9 @@ #include "environmentwidget.h" +#include "base/baseitemdelegate.h" +#include "common/util/namevaluemodel.h" + #include #include #include @@ -17,139 +20,6 @@ DWIDGET_USE_NAMESPACE const char kLoggingRules[] = "QT_LOGGING_RULES"; -class EnvironmentModelPrivate -{ - friend class EnvironmentModel; - QMap envs; -}; - -EnvironmentModel::EnvironmentModel(QObject *parent) - : QAbstractTableModel(parent) - , d (new EnvironmentModelPrivate()) -{ - -} - -EnvironmentModel::~EnvironmentModel() -{ - if (d) - delete d; -} - -int EnvironmentModel::rowCount(const QModelIndex &) const -{ - return d->envs.keys().size(); -} - -int EnvironmentModel::columnCount(const QModelIndex &) const -{ - return kColumnCount; -} - -QVariant EnvironmentModel::data(const QModelIndex &index, int role) const -{ - if (role == Qt::DisplayRole || role == Qt::EditRole) { - auto var = d->envs.keys()[index.row()]; - switch (index.column()) { - case kVaribale: - return var; - case kValue: - return d->envs.value(var); - default: - break; - } - } - return {}; -} - -bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - if (!index.isValid() || role != Qt::EditRole) - return false; - - if (data(index, role) == value) - return true; - - const QString oldName = data(this->index(index.row(), 0, QModelIndex()), Qt::EditRole).toString(); - const QString oldValue = data(this->index(index.row(), 1, QModelIndex()), Qt::EditRole).toString(); - QMap map = d->envs; - if (index.column() == kVaribale) { - const QString newName = value.toString(); - if (newName.isEmpty() || newName.contains("=") || map.contains(newName)) - return false; - map.remove(oldName); - map.insert(value.toString(), oldValue); - } else if (index.column() == kValue) { - const QString stringValue = value.toString(); - auto var = map.keys()[index.row()]; - map[var] = stringValue; - } - update(map); - emit dataChanged(index, index); - return true; -} - -Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const -{ - Q_UNUSED(index); - return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable; -} - -QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - switch (section) { - case kVaribale: - return QObject::tr("Variable"); - case kValue: - return QObject::tr("Value"); - default: - break; - } - } - return {}; -} - -QModelIndex EnvironmentModel::append(const QString &key, const QString &value) -{ - auto list = d->envs.keys(); - list.append(key); - qSort(list); - int pos = list.indexOf(key); - - beginInsertRows(QModelIndex(), pos, pos); - d->envs.insert(key, value); - endInsertRows(); - emit dataChanged(index(pos, 0), index(pos, 1)); - return index(pos, 0); -} - -void EnvironmentModel::remove(QModelIndex &index) -{ - if (d->envs.keys().isEmpty() || index.row() < 0 || index.row() >= d->envs.count()) - return; - int row = index.row(); - beginRemoveRows(QModelIndex(), row, row); - QString key = d->envs.keys()[index.row()]; - d->envs.remove(key); - endRemoveRows(); - emit dataChanged(index, index); -} - -void EnvironmentModel::update(const QMap &data) -{ - beginResetModel(); - d->envs.clear(); - d->envs = data; - endResetModel(); - emit dataChanged(index(0, 0), index(d->envs.count() - 1, 1)); -} - -const QMap EnvironmentModel::getEnvironment() const -{ - return d->envs; -} - class EnvironmentWidgetPrivate { friend class EnvironmentWidget; @@ -160,7 +30,7 @@ class EnvironmentWidgetPrivate DIconButton *appendButton = nullptr; DIconButton *deleteButton = nullptr; DIconButton *resetButton = nullptr; - EnvironmentModel *model{nullptr}; + NameValueModel model; config::EnvironmentItem *envShadow{nullptr}; }; @@ -190,11 +60,11 @@ EnvironmentWidget::EnvironmentWidget(QWidget *parent, EnvType type) d->tableView->verticalHeader()->hide(); } - if (!d->model) - d->model = new EnvironmentModel(); - connect(d->model, &EnvironmentModel::dataChanged, this, &EnvironmentWidget::envUpdated); + connect(&d->model, &NameValueModel::dataChanged, this, &EnvironmentWidget::envUpdated); + connect(&d->model, &NameValueModel::focusIndex, this, &EnvironmentWidget::handleFocusIndex); - d->tableView->setModel(d->model); + d->tableView->setModel(&d->model); + d->tableView->setItemDelegate(new BaseItemDelegate(d->tableView)); // add enable env check box. if (!d->enableEnvCB) @@ -257,42 +127,49 @@ EnvironmentWidget::~EnvironmentWidget() void EnvironmentWidget::appendRow() { - auto index = d->model->append("", ""); + auto index = d->model.addItem(); d->tableView->setCurrentIndex(index); } void EnvironmentWidget::deleteRow() { QModelIndex index = d->tableView->currentIndex(); - d->model->remove(index); + d->model.removeItem(index); } void EnvironmentWidget::initModel() { - QMap envs; + QVariantMap envs; QStringList keys = QProcessEnvironment::systemEnvironment().keys(); for (auto key : keys) { QString value = QProcessEnvironment::systemEnvironment().value(key); envs.insert(key, value); } - d->model->update(envs); + d->model.setItems(envs); } void EnvironmentWidget::getValues(config::EnvironmentItem &env) { env.enable = d->enableEnvCB->isChecked(); - env.environments = d->model->getEnvironment(); + env.environments = d->model.items(); } void EnvironmentWidget::setValues(const config::EnvironmentItem &env) { d->enableEnvCB->setChecked(env.enable); - d->model->update(env.environments); + d->model.setItems(env.environments); } void EnvironmentWidget::updateEnvList(config::EnvironmentItem *env) { d->envShadow = env; d->enableEnvCB->setChecked(env->enable); - d->model->update(env->environments); + d->model.setItems(env->environments); +} + +void EnvironmentWidget::handleFocusIndex(const QModelIndex &index) +{ + d->tableView->setCurrentIndex(index); + d->tableView->setFocus(); + d->tableView->scrollTo(index, QTableView::PositionAtTop); } diff --git a/src/plugins/cxx/cmake/project/properties/environmentwidget.h b/src/plugins/cxx/cmake/project/properties/environmentwidget.h index 7e76c17dd..8c5c09204 100644 --- a/src/plugins/cxx/cmake/project/properties/environmentwidget.h +++ b/src/plugins/cxx/cmake/project/properties/environmentwidget.h @@ -15,37 +15,6 @@ enum EnvType { RunCfg }; -class EnvironmentModelPrivate; -class EnvironmentModel : public QAbstractTableModel -{ - Q_OBJECT -public: - enum ColumnType - { - kVaribale, - kValue, - kColumnCount - }; - - explicit EnvironmentModel(QObject *parent = nullptr); - ~EnvironmentModel() override; - - int rowCount(const QModelIndex &) const override; - int columnCount(const QModelIndex &) const override; - QVariant data(const QModelIndex &index, int role) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - Qt::ItemFlags flags(const QModelIndex &index) const override; - - QModelIndex append(const QString &key, const QString &value); - void remove(QModelIndex &index); - void update(const QMap &data); - const QMap getEnvironment() const; - -private: - EnvironmentModelPrivate *const d; -}; - class EnvironmentWidgetPrivate; class EnvironmentWidget : public DTK_WIDGET_NAMESPACE::DFrame { @@ -63,6 +32,9 @@ class EnvironmentWidget : public DTK_WIDGET_NAMESPACE::DFrame void setValues(const config::EnvironmentItem &env); void updateEnvList(config::EnvironmentItem *env); +public slots: + void handleFocusIndex(const QModelIndex &index); + signals: void envUpdated(); diff --git a/src/plugins/option/optioncore/CMakeLists.txt b/src/plugins/option/optioncore/CMakeLists.txt index 09d07cd6e..b57a4c75e 100644 --- a/src/plugins/option/optioncore/CMakeLists.txt +++ b/src/plugins/option/optioncore/CMakeLists.txt @@ -5,13 +5,13 @@ project(optioncore) find_package(Qt5 COMPONENTS Xml REQUIRED) set(CXX_CPP - mainframe/environmentwidget.cpp + mainframe/shortcutsettingwidget.cpp mainframe/persistentsettings.cpp mainframe/optionsdialog.cpp mainframe/optiondefaultkeeper.cpp mainframe/profilesettingwidget.cpp - mainframe/optionenvironmentgenerator.cpp + mainframe/optionprofilesettinggenerator.cpp mainframe/optionshortcutsettinggenerator.cpp mainframe/navigationdelegate.cpp @@ -21,13 +21,13 @@ set(CXX_CPP ) set(CXX_H - mainframe/environmentwidget.h + mainframe/shortcutsettingwidget.h mainframe/persistentsettings.h mainframe/optionsdialog.h mainframe/optiondefaultkeeper.h mainframe/profilesettingwidget.h - mainframe/optionenvironmentgenerator.h + mainframe/optionprofilesettinggenerator.h mainframe/optionshortcutsettinggenerator.h mainframe/navigationdelegate.h diff --git a/src/plugins/option/optioncore/mainframe/environmentwidget.cpp b/src/plugins/option/optioncore/mainframe/environmentwidget.cpp deleted file mode 100644 index b60e432bf..000000000 --- a/src/plugins/option/optioncore/mainframe/environmentwidget.cpp +++ /dev/null @@ -1,143 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#include "environmentwidget.h" - -#include -#include -#include - -#include -#include - -DWIDGET_USE_NAMESPACE - -const QString ENABLE_ALL_ENV = EnvironmentWidget::tr("Enable All Environment"); - -class EnvironmentModel; -class EnvironmentWidgetPrivate -{ - friend class EnvironmentWidget; - QVBoxLayout *vLayout = nullptr; - DTableView *tableView = nullptr; - DCheckBox *checkBox = nullptr; - EnvironmentModel *model = nullptr; -}; - -class EnvironmentModel : public QAbstractTableModel -{ -public: - enum ColumnType - { - kVaribale, - kValue, - kColumnCount - }; - - explicit EnvironmentModel(QObject *parent = nullptr) - : QAbstractTableModel(parent) - , envs(QProcessEnvironment::systemEnvironment()) - { - } - - int rowCount(const QModelIndex &) const override - { - return envs.keys().size(); - } - - int columnCount(const QModelIndex &) const override - { - return kColumnCount; - } - - QVariant data(const QModelIndex &index, int role) const override - { - if (role == Qt::DisplayRole || role == Qt::EditRole) { - auto var = envs.keys()[index.row()]; - switch (index.column()) { - case kVaribale: - return var; - case kValue: - return envs.value(var); - default: - ; // do nothing - } - } - return {}; - } - - QVariant headerData(int section, Qt::Orientation orientation, int role) const override - { - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - switch (section) { - case kVaribale: - return QObject::tr("Variable"); - case kValue: - return QObject::tr("Value"); - default: - ; // do nothing. - } - } - return {}; - } - - void append(const QString &key, const QString &value) - { - beginInsertRows({}, envs.keys().count(), envs.keys().count()); - envs.insert(key, value); - endInsertRows(); - } - -private: - QProcessEnvironment envs; -}; - -EnvironmentWidget::EnvironmentWidget(QWidget *parent) - : PageWidget(parent) - , d(new EnvironmentWidgetPrivate) -{ - if (!d->vLayout) - d->vLayout = new QVBoxLayout(); - this->setLayout(d->vLayout); - - if (!d->tableView) { - d->tableView = new DTableView(); - // Initialize view - d->tableView->setShowGrid(false); - d->tableView->setAlternatingRowColors(true); - d->tableView->setFrameShape(QFrame::NoFrame); - - DHeaderView *headerView = d->tableView->horizontalHeader(); - headerView->setDefaultAlignment(Qt::AlignLeft); - headerView->setSectionResizeMode(QHeaderView::ResizeToContents); - d->tableView->verticalHeader()->hide(); - } - - if (!d->model) - d->model = new EnvironmentModel(); - - d->tableView->setModel(d->model); - - if (!d->checkBox) - d->checkBox = new DCheckBox(); - d->checkBox->setText(ENABLE_ALL_ENV); - d->checkBox->setChecked(true); - - auto mainFrame = new DFrame(this); - mainFrame->setFixedHeight(428); - auto mainLayout = new QVBoxLayout(mainFrame); - mainFrame->setLayout(mainLayout); - mainLayout->addWidget(d->tableView); - mainLayout->addWidget(d->checkBox); - - d->vLayout->setSpacing(0); - d->vLayout->setMargin(5); - d->vLayout->addWidget(mainFrame); -} - -EnvironmentWidget::~EnvironmentWidget() -{ - if(d) - delete d; -} diff --git a/src/plugins/option/optioncore/mainframe/environmentwidget.h b/src/plugins/option/optioncore/mainframe/environmentwidget.h deleted file mode 100644 index 25be8005b..000000000 --- a/src/plugins/option/optioncore/mainframe/environmentwidget.h +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef ENVIRONMENTWIDGET_H -#define ENVIRONMENTWIDGET_H - -#include -#include - -class EnvironmentWidgetPrivate; -class EnvironmentWidget : public PageWidget -{ - Q_OBJECT - -public: - explicit EnvironmentWidget(QWidget *parent = nullptr); - virtual ~EnvironmentWidget(); - -private: - EnvironmentWidgetPrivate *const d; -}; - -#endif // ENVIRONMENTWIDGET_H diff --git a/src/plugins/option/optioncore/mainframe/optionenvironmentgenerator.cpp b/src/plugins/option/optioncore/mainframe/optionenvironmentgenerator.cpp deleted file mode 100644 index 2e4bba8e1..000000000 --- a/src/plugins/option/optioncore/mainframe/optionenvironmentgenerator.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#include "optionenvironmentgenerator.h" -#include "environmentwidget.h" - -OptionEnvironmentGenerator::OptionEnvironmentGenerator() -{ -} - -QWidget *OptionEnvironmentGenerator::optionWidget() -{ - return new EnvironmentWidget(); -} diff --git a/src/plugins/option/optioncore/mainframe/optionenvironmentgenerator.h b/src/plugins/option/optioncore/mainframe/optionenvironmentgenerator.h deleted file mode 100644 index eed35a609..000000000 --- a/src/plugins/option/optioncore/mainframe/optionenvironmentgenerator.h +++ /dev/null @@ -1,19 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef OPTIONENVIRONMENTGENERATOR_H -#define OPTIONENVIRONMENTGENERATOR_H - -#include "services/option/optiongenerator.h" - -class OptionEnvironmentGenerator : public dpfservice::OptionGenerator -{ - Q_OBJECT -public: - OptionEnvironmentGenerator(); - inline static QString kitName() {return QObject::tr("Environment");} - virtual QWidget *optionWidget() override; -}; - -#endif // OPTIONENVIRONMENTGENERATOR_H diff --git a/src/plugins/option/optioncore/optioncore.cpp b/src/plugins/option/optioncore/optioncore.cpp index 3fcfe3034..729b5c257 100644 --- a/src/plugins/option/optioncore/optioncore.cpp +++ b/src/plugins/option/optioncore/optioncore.cpp @@ -4,7 +4,6 @@ #include "optioncore.h" #include "mainframe/optiondefaultkeeper.h" -#include "mainframe/optionenvironmentgenerator.h" #include "mainframe/optionprofilesettinggenerator.h" #include "mainframe/optionshortcutsettinggenerator.h" @@ -20,8 +19,6 @@ #include -static QStringList generalKits {}; - using namespace dpfservice; DWIDGET_USE_NAMESPACE void OptionCore::initialize() @@ -51,12 +48,8 @@ bool OptionCore::start() OptionService *optionService = ctx.service(OptionService::name()); if (optionService) { - generalKits << OptionEnvironmentGenerator::kitName() - << OptionShortcutsettingGenerator::kitName() - << OptionProfilesettingGenerator::kitName(); - optionService->implGenerator(option::GROUP_GENERAL, generalKits[0]); - optionService->implGenerator(option::GROUP_GENERAL, generalKits[1]); - optionService->implGenerator(option::GROUP_GENERAL, generalKits[2]); + optionService->implGenerator(option::GROUP_GENERAL, OptionShortcutsettingGenerator::kitName()); + optionService->implGenerator(option::GROUP_GENERAL, OptionProfilesettingGenerator::kitName()); using namespace std::placeholders; optionService->showOptionDialog = std::bind(&OptionsDialog::showAtItem, OptionDefaultKeeper::getOptionDialog(), _1);