From e3e7503a7c00afeaa4d4b8d8f6d9494bf30440db Mon Sep 17 00:00:00 2001 From: "vladimir.kuznetsov" Date: Fri, 12 May 2023 11:36:09 +0800 Subject: [PATCH] added saving the selected server and protocol to the config --- client/ui/models/containers_model.cpp | 37 ++++--- client/ui/models/containers_model.h | 1 + client/ui/models/servers_model.cpp | 118 ++++++++++++++--------- client/ui/models/servers_model.h | 13 ++- client/ui/qml/Controls2/DropDownType.qml | 6 ++ client/ui/qml/Pages2/PageHome.qml | 82 +++++++++------- 6 files changed, 160 insertions(+), 97 deletions(-) diff --git a/client/ui/models/containers_model.cpp b/client/ui/models/containers_model.cpp index f56613c74..c7aa0ec1f 100644 --- a/client/ui/models/containers_model.cpp +++ b/client/ui/models/containers_model.cpp @@ -1,10 +1,8 @@ #include "containers_model.h" -ContainersModel::ContainersModel(std::shared_ptr settings, QObject *parent) : - m_settings(settings), - QAbstractListModel(parent) +ContainersModel::ContainersModel(std::shared_ptr settings, QObject *parent) : m_settings(settings), QAbstractListModel(parent) { - + setSelectedServerIndex(m_settings->defaultServerIndex()); } int ContainersModel::rowCount(const QModelIndex &parent) const @@ -13,14 +11,19 @@ int ContainersModel::rowCount(const QModelIndex &parent) const return ContainerProps::allContainers().size(); } -QHash ContainersModel::roleNames() const { - QHash roles; - roles[NameRole] = "name_role"; - roles[DescRole] = "desc_role"; - roles[DefaultRole] = "default_role"; - roles[ServiceTypeRole] = "service_type_role"; - roles[IsInstalledRole] = "is_installed_role"; - return roles; +bool ContainersModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (!index.isValid() || index.row() < 0 || index.row() >= ContainerProps::allContainers().size()) { + return false; + } + + if (role == DefaultRole) { + DockerContainer container = ContainerProps::allContainers().at(index.row()); + m_settings->setDefaultContainer(m_selectedServerIndex, container); + } + + emit dataChanged(index, index); + return true; } QVariant ContainersModel::data(const QModelIndex &index, int role) const @@ -67,3 +70,13 @@ QString ContainersModel::getCurrentlyInstalledContainerName() { return data(m_currentlyInstalledContainerIndex, NameRole).toString(); } + +QHash ContainersModel::roleNames() const { + QHash roles; + roles[NameRole] = "name_role"; + roles[DescRole] = "desc_role"; + roles[DefaultRole] = "default_role"; + roles[ServiceTypeRole] = "service_type_role"; + roles[IsInstalledRole] = "is_installed_role"; + return roles; +} diff --git a/client/ui/models/containers_model.h b/client/ui/models/containers_model.h index 9c409fc00..ba0fea57e 100644 --- a/client/ui/models/containers_model.h +++ b/client/ui/models/containers_model.h @@ -25,6 +25,7 @@ public: int rowCount(const QModelIndex &parent = QModelIndex()) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; Q_INVOKABLE void setSelectedServerIndex(int index); Q_INVOKABLE void setCurrentlyInstalledContainerIndex(int index); diff --git a/client/ui/models/servers_model.cpp b/client/ui/models/servers_model.cpp index 89287b7f1..44ed26197 100644 --- a/client/ui/models/servers_model.cpp +++ b/client/ui/models/servers_model.cpp @@ -2,34 +2,83 @@ ServersModel::ServersModel(std::shared_ptr settings, QObject *parent) : m_settings(settings), QAbstractListModel(parent) { - refresh(); -} -void ServersModel::refresh() -{ - beginResetModel(); - const QJsonArray &servers = m_settings->serversArray(); - int defaultServer = m_settings->defaultServerIndex(); - QVector serverListContent; - for(int i = 0; i < servers.size(); i++) { - ServerModelContent content; - auto server = servers.at(i).toObject(); - content.desc = server.value(config_key::description).toString(); - content.address = server.value(config_key::hostName).toString(); - if (content.desc.isEmpty()) { - content.desc = content.address; - } - content.isDefault = (i == defaultServer); - serverListContent.push_back(content); - } - m_data = serverListContent; - endResetModel(); } int ServersModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); - return static_cast(m_data.size()); + return static_cast(m_settings->serversCount()); +} + +bool ServersModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (!index.isValid() || index.row() < 0 + || index.row() >= static_cast(m_settings->serversCount())) { + return false; + } +// if (role == DescRole) { +// return m_data[index.row()].desc; +// } +// if (role == AddressRole) { +// return m_data[index.row()].address; +// } +// if (role == IsDefaultRole) { +// return m_data[index.row()].isDefault; +// } +} + +QVariant ServersModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() < 0 || index.row() >= static_cast(m_settings->serversCount())) { + return QVariant(); + } + + const QJsonArray &servers = m_settings->serversArray(); + const QJsonObject server = servers.at(index.row()).toObject(); + + if (role == DescRole) { + auto description = server.value(config_key::description).toString(); + if (description.isEmpty()) { + return server.value(config_key::hostName).toString(); + } + return description; + } + if (role == AddressRole) { + return server.value(config_key::hostName).toString(); + } + if (role == IsDefaultRole) { + return index.row() == m_settings->defaultServerIndex(); + } + return QVariant(); + + +// if (!index.isValid() || index.row() < 0 +// || index.row() >= static_cast(m_data.size())) { +// return QVariant(); +// } +// if (role == DescRole) { +// return m_data[index.row()].desc; +// } +// if (role == AddressRole) { +// return m_data[index.row()].address; +// } +// if (role == IsDefaultRole) { +// return m_data[index.row()].isDefault; +// } +// return QVariant(); +} + +void ServersModel::setDefaultServerIndex(int index) +{ +// beginResetModel(); + m_settings->setDefaultServer(index); + // endResetModel(); +} + +int ServersModel::getDefaultServerIndex() +{ + return m_settings->defaultServerIndex(); } QHash ServersModel::roleNames() const { @@ -39,28 +88,3 @@ QHash ServersModel::roleNames() const { roles[IsDefaultRole] = "is_default"; return roles; } - -QVariant ServersModel::data(const QModelIndex &index, int role) const -{ - if (!index.isValid() || index.row() < 0 - || index.row() >= static_cast(m_data.size())) { - return QVariant(); - } - if (role == DescRole) { - return m_data[index.row()].desc; - } - if (role == AddressRole) { - return m_data[index.row()].address; - } - if (role == IsDefaultRole) { - return m_data[index.row()].isDefault; - } - return QVariant(); -} - -void ServersModel::setDefaultServerIndex(int index) -{ - -} - - diff --git a/client/ui/models/servers_model.h b/client/ui/models/servers_model.h index c8c32b56d..cd46846b9 100644 --- a/client/ui/models/servers_model.h +++ b/client/ui/models/servers_model.h @@ -15,24 +15,27 @@ class ServersModel : public QAbstractListModel { Q_OBJECT public: - ServersModel(std::shared_ptr settings, QObject *parent = nullptr); -public: - enum SiteRoles { + enum ServersModelRoles { DescRole = Qt::UserRole + 1, AddressRole, IsDefaultRole }; - void refresh(); + ServersModel(std::shared_ptr settings, QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; +public slots: + void setDefaultServerIndex(int index); + int getDefaultServerIndex(); + protected: QHash roleNames() const override; private: - QVector m_data; std::shared_ptr m_settings; }; diff --git a/client/ui/qml/Controls2/DropDownType.qml b/client/ui/qml/Controls2/DropDownType.qml index ea6ca5fca..64013ba4b 100644 --- a/client/ui/qml/Controls2/DropDownType.qml +++ b/client/ui/qml/Controls2/DropDownType.qml @@ -17,6 +17,7 @@ Item { property string buttonImage: "qrc:/images/controls/chevron-down.svg" property string buttonImageColor: "#494B50" + property int buttonMaximumWidth property string defaultColor: "#1C1D21" @@ -70,8 +71,13 @@ Item { horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter + Layout.maximumWidth: buttonMaximumWidth ? buttonMaximumWidth : implicitWidth + color: root.textColor text: root.text + + wrapMode: Text.NoWrap + elide: Text.ElideRight } } diff --git a/client/ui/qml/Pages2/PageHome.qml b/client/ui/qml/Pages2/PageHome.qml index 3f81f1681..f4f4d13c3 100644 --- a/client/ui/qml/Pages2/PageHome.qml +++ b/client/ui/qml/Pages2/PageHome.qml @@ -21,8 +21,8 @@ PageBase { property string borderColor: "#2C2D30" - property string currentServerName: menuContent.currentItem.delegateData.desc - property string currentServerDescription: menuContent.currentItem.delegateData.address + property string currentServerName: serversMenuContent.currentItem.delegateData.desc + property string currentServerDescription: serversMenuContent.currentItem.delegateData.address Rectangle { id: buttonBackground @@ -108,7 +108,7 @@ PageBase { } ColumnLayout { - id: menuHeader + id: serversMenuHeader anchors.top: parent.top anchors.right: parent.right anchors.left: parent.left @@ -147,12 +147,13 @@ PageBase { } DropDownType { - id: protocolsDropDown + id: containersDropDown implicitHeight: 40 borderWidth: 0 buttonImageColor: "#0E0E11" + buttonMaximumWidth: 150 defaultColor: "#D7D8DB" @@ -162,27 +163,38 @@ PageBase { menuModel: proxyContainersModel + ButtonGroup { + id: containersRadioButtonGroup + } + menuDelegate: Item { - implicitWidth: menuContent.width - implicitHeight: radioButton.implicitHeight + implicitWidth: root.width + implicitHeight: containerRadioButton.implicitHeight RadioButton { - id: radioButton + id: containerRadioButton implicitWidth: parent.width - implicitHeight: radioButtonContent.implicitHeight + implicitHeight: containerRadioButtonContent.implicitHeight hoverEnabled: true - ButtonGroup.group: radioButtonGroup + ButtonGroup.group: containersRadioButtonGroup + + checked: { + if (modelData !== null) { + return modelData.default_role + } + return false + } indicator: Rectangle { anchors.fill: parent - color: radioButton.hovered ? "#2C2D30" : "#1C1D21" + color: containerRadioButton.hovered ? "#2C2D30" : "#1C1D21" } RowLayout { - id: radioButtonContent + id: containerRadioButtonContent anchors.fill: parent anchors.rightMargin: 16 @@ -191,7 +203,7 @@ PageBase { z: 1 Text { - id: text + id: containerRadioButtonText // todo remove dirty hack? text: { @@ -214,7 +226,7 @@ PageBase { Image { source: "qrc:/images/controls/check.svg" - visible: radioButton.checked + visible: containerRadioButton.checked width: 24 height: 24 @@ -223,14 +235,16 @@ PageBase { } onClicked: { - protocolsDropDown.text = text.text - protocolsDropDown.menuVisible = false + modelData.default_role = true + + containersDropDown.text = containerRadioButtonText.text + containersDropDown.menuVisible = false } } Component.onCompleted: { if (modelData !== null && modelData.default_role) { - protocolsDropDown.text = modelData.name_role + containersDropDown.text = modelData.name_role } } } @@ -256,7 +270,7 @@ PageBase { } FlickableType { - anchors.top: menuHeader.bottom + anchors.top: serversMenuHeader.bottom anchors.topMargin: 16 contentHeight: col.implicitHeight @@ -269,45 +283,46 @@ PageBase { spacing: 16 ButtonGroup { - id: radioButtonGroup + id: serversRadioButtonGroup } ListView { - id: menuContent + id: serversMenuContent width: parent.width - height: menuContent.contentItem.height + height: serversMenuContent.contentItem.height model: ServersModel - currentIndex: 0 + currentIndex: ServersModel.getDefaultServerIndex() clip: true - interactive: false delegate: Item { id: menuContentDelegate property variant delegateData: model - implicitWidth: menuContent.width - implicitHeight: radioButton.implicitHeight + implicitWidth: serversMenuContent.width + implicitHeight: serverRadioButton.implicitHeight RadioButton { - id: radioButton + id: serverRadioButton implicitWidth: parent.width - implicitHeight: radioButtonContent.implicitHeight + implicitHeight: serverRadioButtonContent.implicitHeight hoverEnabled: true - ButtonGroup.group: radioButtonGroup + checked: index === serversMenuContent.currentIndex + + ButtonGroup.group: serversRadioButtonGroup indicator: Rectangle { anchors.fill: parent - color: radioButton.hovered ? "#2C2D30" : "#1C1D21" + color: serverRadioButton.hovered ? "#2C2D30" : "#1C1D21" } RowLayout { - id: radioButtonContent + id: serverRadioButtonContent anchors.fill: parent anchors.rightMargin: 16 @@ -316,7 +331,7 @@ PageBase { z: 1 Text { - id: text + id: serverRadioButtonText text: desc color: "#D7D8DB" @@ -333,7 +348,7 @@ PageBase { Image { source: "qrc:/images/controls/check.svg" - visible: radioButton.checked + visible: serverRadioButton.checked width: 24 height: 24 @@ -342,10 +357,11 @@ PageBase { } onClicked: { - console.log(index) - ContainersModel.setSelectedServerIndex(index) root.currentServerName = desc root.currentServerDescription = address + + ServersModel.setDefaultServerIndex(index) + ContainersModel.setSelectedServerIndex(index) } } }