From 6771da6de439d2567e3fdff4cca27915ea73ef68 Mon Sep 17 00:00:00 2001 From: Jannis Voelker Date: Wed, 13 Mar 2024 11:43:51 +0100 Subject: [PATCH] Add option to edit saved dashboard names --- src/CMakeLists.txt | 2 + src/backend.cpp | 32 ++++++++ src/backend.h | 3 + src/dashboarditemmodel.cpp | 17 ++++ src/dashboarditemmodel.h | 1 + src/icons/cancel.svg | 1 + src/icons/cancel.svg.license | 3 + src/icons/edit.svg | 1 + src/icons/edit.svg.license | 3 + src/qml/SettingsView.qml | 116 ++++++++++++++++++++++++++++ src/qml/style/ThemeSettingsView.qml | 1 + 11 files changed, 180 insertions(+) create mode 100644 src/icons/cancel.svg create mode 100644 src/icons/cancel.svg.license create mode 100644 src/icons/edit.svg create mode 100644 src/icons/edit.svg.license diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 09bf3e1..83248eb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -107,11 +107,13 @@ qt_add_resources(appOPC_UA_Browser "icons" font/SwanseaBold.ttf icons/arrow_right.svg icons/back.svg + icons/cancel.svg icons/checkmark.svg icons/connect.svg icons/dashboard.svg icons/delete.svg icons/disconnect.svg + icons/edit.svg icons/event.svg icons/expert.svg icons/forward.svg diff --git a/src/backend.cpp b/src/backend.cpp index 12b6b59..cb5285a 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -448,6 +448,38 @@ int BackEnd::instantiateDefaultVariableDashboard(const QString &name) return -1; } +void BackEnd::renameSavedVariableDashboard(const QString &previousName, const QString &newName) +{ + + QSettings settings; + const QString settingsGroupName = + Constants::SettingsKey::DashboardsVariables % QChar::fromLatin1('/') % previousName; + + if (previousName == newName + || !mSavedVariableDashboardsModel->stringList().contains(previousName) + || mSavedVariableDashboardsModel->stringList().contains(newName) + || !settings.contains(settingsGroupName)) + return; + + const auto nodeIds = settings.value(Constants::SettingsKey::DashboardsVariables + % QChar::fromLatin1('/') % previousName) + .toStringList(); + + removeSavedVariableDashboard(previousName); + + settings.setValue(Constants::SettingsKey::DashboardsVariables % QChar::fromLatin1('/') + % newName, + nodeIds); + addItemToStringListModel(mSavedVariableDashboardsModel, newName); + + mDashboardItemModel->renameItem(previousName, newName); +} + +bool BackEnd::hasSavedVariableDashboard(const QString &name) const +{ + return mSavedVariableDashboardsModel->stringList().contains(name); +} + int BackEnd::instantiateCompanionSpecVariableDashboard(const QString &name) { assert(mDashboardItemModel); diff --git a/src/backend.h b/src/backend.h index 740d311..d997c07 100644 --- a/src/backend.h +++ b/src/backend.h @@ -190,6 +190,9 @@ class BackEnd : public QObject Q_INVOKABLE void removeSavedVariableDashboard(const QString &name); Q_INVOKABLE void loadDashboard(const QString &name); Q_INVOKABLE int instantiateDefaultVariableDashboard(const QString &name); + Q_INVOKABLE void renameSavedVariableDashboard(const QString &previousName, + const QString &newName); + Q_INVOKABLE bool hasSavedVariableDashboard(const QString &name) const; Q_INVOKABLE void loadLastDashboardsFromSettings(); diff --git a/src/dashboarditemmodel.cpp b/src/dashboarditemmodel.cpp index 41a440b..6e89e7f 100644 --- a/src/dashboarditemmodel.cpp +++ b/src/dashboarditemmodel.cpp @@ -91,6 +91,23 @@ int DashboardItemModel::addItem(DashboardItem::DashboardType type, const QString return pos; } +void DashboardItemModel::renameItem(const QString &previousName, const QString &newName) +{ + const auto item = + std::find_if(mItems.begin(), mItems.end(), [previousName](const DashboardItem *item) { + return item->name() == previousName; + }); + + if (item == mItems.end()) + return; + + (*item)->setName(newName); + const auto index = createIndex(std::distance(mItems.begin(), item), 0); + emit dataChanged(index, index, QList() << DisplayNameRole); + + saveDashboardsToSettings(); +} + void DashboardItemModel::removeItem(int index) { // Add item is last item diff --git a/src/dashboarditemmodel.h b/src/dashboarditemmodel.h index 72f2700..b84cc26 100644 --- a/src/dashboarditemmodel.h +++ b/src/dashboarditemmodel.h @@ -31,6 +31,7 @@ class DashboardItemModel : public QAbstractListModel bool containsItem(const QString &name) const noexcept; int getIndexOfItem(const QString &name) const noexcept; Q_INVOKABLE int addItem(DashboardItem::DashboardType type, const QString &name = QString()); + void renameItem(const QString &previousName, const QString &newName); Q_INVOKABLE void removeItem(int index); void clearItems(); diff --git a/src/icons/cancel.svg b/src/icons/cancel.svg new file mode 100644 index 0000000..8504fbf --- /dev/null +++ b/src/icons/cancel.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/icons/cancel.svg.license b/src/icons/cancel.svg.license new file mode 100644 index 0000000..c6127c5 --- /dev/null +++ b/src/icons/cancel.svg.license @@ -0,0 +1,3 @@ +SPDX-FileCopyrightText: 2023 Google LLC + +SPDX-License-Identifier: Apache-2.0 diff --git a/src/icons/edit.svg b/src/icons/edit.svg new file mode 100644 index 0000000..cb81b11 --- /dev/null +++ b/src/icons/edit.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/icons/edit.svg.license b/src/icons/edit.svg.license new file mode 100644 index 0000000..c6127c5 --- /dev/null +++ b/src/icons/edit.svg.license @@ -0,0 +1,3 @@ +SPDX-FileCopyrightText: 2023 Google LLC + +SPDX-License-Identifier: Apache-2.0 diff --git a/src/qml/SettingsView.qml b/src/qml/SettingsView.qml index 84abce6..8a9afe4 100644 --- a/src/qml/SettingsView.qml +++ b/src/qml/SettingsView.qml @@ -264,6 +264,7 @@ Rectangle { spacing: 10 Text { + id: dashboardName Layout.fillWidth: true Layout.rightMargin: 5 Layout.leftMargin: 5 @@ -275,6 +276,22 @@ Rectangle { elide: Text.ElideRight } + IconImage { + Layout.alignment: Qt.AlignVCenter + sourceSize.width: 24 + sourceSize.height: 24 + source: "qrc:/icons/edit.svg" + color: view.theme.textColor + + MouseArea { + anchors.fill: parent + cursorShape: Qt.PointingHandCursor + onClicked: function() { + dashboardNameEditPopup.showEdit(display) + } + } + } + IconImage { Layout.alignment: Qt.AlignVCenter Layout.rightMargin: 10 @@ -621,4 +638,103 @@ Rectangle { } } } + + Popup { + id: dashboardNameEditPopup + modal: true + + implicitWidth: contentColumn.width + implicitHeight: contentColumn.height + padding: 0 + + clip: true + closePolicy: Popup.NoAutoClose + + anchors.centerIn: parent + + property string previousName + + function showEdit(currentName: string) { + previousName = currentName + nameTextEdit.text = currentName + nameTextEdit.cursorPosition = currentName.length + open() + nameTextEdit.forceActiveFocus() + } + + background: Rectangle { + radius: 3 + opacity: 0.8 + color: view.theme.popupBackground + } + + ColumnLayout { + width: view.width - 50 + id: contentColumn + Text { + padding: 3 + font { + pointSize: 12 + bold: true + } + color: view.theme.textColor + text: qsTranslate("Settings", "Enter new dashboard name") + } + + TextEdit { + Layout.maximumWidth: contentColumn.width + id: nameTextEdit + padding: 3 + font { + pointSize: 10 + } + color: view.theme.textColor + wrapMode: Text.WrapAtWordBoundaryOrAnywhere + cursorVisible: true + } + + RowLayout { + IconImage { + Layout.margins: 5 + Layout.alignment: Qt.AlignVCenter + sourceSize.width: 24 + sourceSize.height: 24 + source: "qrc:/icons/cancel.svg" + color: view.theme.textColor + + MouseArea { + anchors.fill: parent + cursorShape: Qt.PointingHandCursor + onClicked: function() { + dashboardNameEditPopup.close() + } + } + } + + Item { + Layout.fillWidth: true + height: 24 + } + + IconImage { + Layout.margins: 5 + Layout.alignment: Qt.AlignVCenter + sourceSize.width: 24 + sourceSize.height: 24 + source: "qrc:/icons/checkmark.svg" + color: enabled ? view.theme.textColor : "lightgrey" + enabled: nameTextEdit.text !== "" && !BackEnd.hasSavedVariableDashboard(nameTextEdit.text) + + MouseArea { + anchors.fill: parent + cursorShape: Qt.PointingHandCursor + onClicked: function() { + BackEnd.renameSavedVariableDashboard(dashboardNameEditPopup.previousName, nameTextEdit.text) + dashboardNameEditPopup.close() + } + } + } + } + } + } } diff --git a/src/qml/style/ThemeSettingsView.qml b/src/qml/style/ThemeSettingsView.qml index 66075fd..cb3f859 100644 --- a/src/qml/style/ThemeSettingsView.qml +++ b/src/qml/style/ThemeSettingsView.qml @@ -12,4 +12,5 @@ StyleDefinitions { property color backgroundListView: dark property color backgroundSelected: mediumDark property color textColor: foreground + property color popupBackground: accent }