added saving the selected server and protocol to the config

This commit is contained in:
vladimir.kuznetsov 2023-05-12 11:36:09 +08:00
parent b66f4bf2be
commit e3e7503a7c
6 changed files with 160 additions and 97 deletions

View file

@ -1,10 +1,8 @@
#include "containers_model.h" #include "containers_model.h"
ContainersModel::ContainersModel(std::shared_ptr<Settings> settings, QObject *parent) : ContainersModel::ContainersModel(std::shared_ptr<Settings> settings, QObject *parent) : m_settings(settings), QAbstractListModel(parent)
m_settings(settings),
QAbstractListModel(parent)
{ {
setSelectedServerIndex(m_settings->defaultServerIndex());
} }
int ContainersModel::rowCount(const QModelIndex &parent) const int ContainersModel::rowCount(const QModelIndex &parent) const
@ -13,14 +11,19 @@ int ContainersModel::rowCount(const QModelIndex &parent) const
return ContainerProps::allContainers().size(); return ContainerProps::allContainers().size();
} }
QHash<int, QByteArray> ContainersModel::roleNames() const { bool ContainersModel::setData(const QModelIndex &index, const QVariant &value, int role)
QHash<int, QByteArray> roles; {
roles[NameRole] = "name_role"; if (!index.isValid() || index.row() < 0 || index.row() >= ContainerProps::allContainers().size()) {
roles[DescRole] = "desc_role"; return false;
roles[DefaultRole] = "default_role"; }
roles[ServiceTypeRole] = "service_type_role";
roles[IsInstalledRole] = "is_installed_role"; if (role == DefaultRole) {
return roles; 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 QVariant ContainersModel::data(const QModelIndex &index, int role) const
@ -67,3 +70,13 @@ QString ContainersModel::getCurrentlyInstalledContainerName()
{ {
return data(m_currentlyInstalledContainerIndex, NameRole).toString(); return data(m_currentlyInstalledContainerIndex, NameRole).toString();
} }
QHash<int, QByteArray> ContainersModel::roleNames() const {
QHash<int, QByteArray> 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;
}

View file

@ -25,6 +25,7 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override; 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; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
Q_INVOKABLE void setSelectedServerIndex(int index); Q_INVOKABLE void setSelectedServerIndex(int index);
Q_INVOKABLE void setCurrentlyInstalledContainerIndex(int index); Q_INVOKABLE void setCurrentlyInstalledContainerIndex(int index);

View file

@ -2,34 +2,83 @@
ServersModel::ServersModel(std::shared_ptr<Settings> settings, QObject *parent) : m_settings(settings), QAbstractListModel(parent) ServersModel::ServersModel(std::shared_ptr<Settings> 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<ServerModelContent> 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 int ServersModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
return static_cast<int>(m_data.size()); return static_cast<int>(m_settings->serversCount());
}
bool ServersModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || index.row() < 0
|| index.row() >= static_cast<int>(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<int>(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<int>(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<int, QByteArray> ServersModel::roleNames() const { QHash<int, QByteArray> ServersModel::roleNames() const {
@ -39,28 +88,3 @@ QHash<int, QByteArray> ServersModel::roleNames() const {
roles[IsDefaultRole] = "is_default"; roles[IsDefaultRole] = "is_default";
return roles; return roles;
} }
QVariant ServersModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0
|| index.row() >= static_cast<int>(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)
{
}

View file

@ -15,24 +15,27 @@ class ServersModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
public: public:
ServersModel(std::shared_ptr<Settings> settings, QObject *parent = nullptr); enum ServersModelRoles {
public:
enum SiteRoles {
DescRole = Qt::UserRole + 1, DescRole = Qt::UserRole + 1,
AddressRole, AddressRole,
IsDefaultRole IsDefaultRole
}; };
void refresh(); ServersModel(std::shared_ptr<Settings> settings, QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override; 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; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
public slots:
void setDefaultServerIndex(int index);
int getDefaultServerIndex();
protected: protected:
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
private: private:
QVector<ServerModelContent> m_data;
std::shared_ptr<Settings> m_settings; std::shared_ptr<Settings> m_settings;
}; };

View file

@ -17,6 +17,7 @@ Item {
property string buttonImage: "qrc:/images/controls/chevron-down.svg" property string buttonImage: "qrc:/images/controls/chevron-down.svg"
property string buttonImageColor: "#494B50" property string buttonImageColor: "#494B50"
property int buttonMaximumWidth
property string defaultColor: "#1C1D21" property string defaultColor: "#1C1D21"
@ -70,8 +71,13 @@ Item {
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
Layout.maximumWidth: buttonMaximumWidth ? buttonMaximumWidth : implicitWidth
color: root.textColor color: root.textColor
text: root.text text: root.text
wrapMode: Text.NoWrap
elide: Text.ElideRight
} }
} }

View file

@ -21,8 +21,8 @@ PageBase {
property string borderColor: "#2C2D30" property string borderColor: "#2C2D30"
property string currentServerName: menuContent.currentItem.delegateData.desc property string currentServerName: serversMenuContent.currentItem.delegateData.desc
property string currentServerDescription: menuContent.currentItem.delegateData.address property string currentServerDescription: serversMenuContent.currentItem.delegateData.address
Rectangle { Rectangle {
id: buttonBackground id: buttonBackground
@ -108,7 +108,7 @@ PageBase {
} }
ColumnLayout { ColumnLayout {
id: menuHeader id: serversMenuHeader
anchors.top: parent.top anchors.top: parent.top
anchors.right: parent.right anchors.right: parent.right
anchors.left: parent.left anchors.left: parent.left
@ -147,12 +147,13 @@ PageBase {
} }
DropDownType { DropDownType {
id: protocolsDropDown id: containersDropDown
implicitHeight: 40 implicitHeight: 40
borderWidth: 0 borderWidth: 0
buttonImageColor: "#0E0E11" buttonImageColor: "#0E0E11"
buttonMaximumWidth: 150
defaultColor: "#D7D8DB" defaultColor: "#D7D8DB"
@ -162,27 +163,38 @@ PageBase {
menuModel: proxyContainersModel menuModel: proxyContainersModel
ButtonGroup {
id: containersRadioButtonGroup
}
menuDelegate: Item { menuDelegate: Item {
implicitWidth: menuContent.width implicitWidth: root.width
implicitHeight: radioButton.implicitHeight implicitHeight: containerRadioButton.implicitHeight
RadioButton { RadioButton {
id: radioButton id: containerRadioButton
implicitWidth: parent.width implicitWidth: parent.width
implicitHeight: radioButtonContent.implicitHeight implicitHeight: containerRadioButtonContent.implicitHeight
hoverEnabled: true hoverEnabled: true
ButtonGroup.group: radioButtonGroup ButtonGroup.group: containersRadioButtonGroup
checked: {
if (modelData !== null) {
return modelData.default_role
}
return false
}
indicator: Rectangle { indicator: Rectangle {
anchors.fill: parent anchors.fill: parent
color: radioButton.hovered ? "#2C2D30" : "#1C1D21" color: containerRadioButton.hovered ? "#2C2D30" : "#1C1D21"
} }
RowLayout { RowLayout {
id: radioButtonContent id: containerRadioButtonContent
anchors.fill: parent anchors.fill: parent
anchors.rightMargin: 16 anchors.rightMargin: 16
@ -191,7 +203,7 @@ PageBase {
z: 1 z: 1
Text { Text {
id: text id: containerRadioButtonText
// todo remove dirty hack? // todo remove dirty hack?
text: { text: {
@ -214,7 +226,7 @@ PageBase {
Image { Image {
source: "qrc:/images/controls/check.svg" source: "qrc:/images/controls/check.svg"
visible: radioButton.checked visible: containerRadioButton.checked
width: 24 width: 24
height: 24 height: 24
@ -223,14 +235,16 @@ PageBase {
} }
onClicked: { onClicked: {
protocolsDropDown.text = text.text modelData.default_role = true
protocolsDropDown.menuVisible = false
containersDropDown.text = containerRadioButtonText.text
containersDropDown.menuVisible = false
} }
} }
Component.onCompleted: { Component.onCompleted: {
if (modelData !== null && modelData.default_role) { if (modelData !== null && modelData.default_role) {
protocolsDropDown.text = modelData.name_role containersDropDown.text = modelData.name_role
} }
} }
} }
@ -256,7 +270,7 @@ PageBase {
} }
FlickableType { FlickableType {
anchors.top: menuHeader.bottom anchors.top: serversMenuHeader.bottom
anchors.topMargin: 16 anchors.topMargin: 16
contentHeight: col.implicitHeight contentHeight: col.implicitHeight
@ -269,45 +283,46 @@ PageBase {
spacing: 16 spacing: 16
ButtonGroup { ButtonGroup {
id: radioButtonGroup id: serversRadioButtonGroup
} }
ListView { ListView {
id: menuContent id: serversMenuContent
width: parent.width width: parent.width
height: menuContent.contentItem.height height: serversMenuContent.contentItem.height
model: ServersModel model: ServersModel
currentIndex: 0 currentIndex: ServersModel.getDefaultServerIndex()
clip: true clip: true
interactive: false
delegate: Item { delegate: Item {
id: menuContentDelegate id: menuContentDelegate
property variant delegateData: model property variant delegateData: model
implicitWidth: menuContent.width implicitWidth: serversMenuContent.width
implicitHeight: radioButton.implicitHeight implicitHeight: serverRadioButton.implicitHeight
RadioButton { RadioButton {
id: radioButton id: serverRadioButton
implicitWidth: parent.width implicitWidth: parent.width
implicitHeight: radioButtonContent.implicitHeight implicitHeight: serverRadioButtonContent.implicitHeight
hoverEnabled: true hoverEnabled: true
ButtonGroup.group: radioButtonGroup checked: index === serversMenuContent.currentIndex
ButtonGroup.group: serversRadioButtonGroup
indicator: Rectangle { indicator: Rectangle {
anchors.fill: parent anchors.fill: parent
color: radioButton.hovered ? "#2C2D30" : "#1C1D21" color: serverRadioButton.hovered ? "#2C2D30" : "#1C1D21"
} }
RowLayout { RowLayout {
id: radioButtonContent id: serverRadioButtonContent
anchors.fill: parent anchors.fill: parent
anchors.rightMargin: 16 anchors.rightMargin: 16
@ -316,7 +331,7 @@ PageBase {
z: 1 z: 1
Text { Text {
id: text id: serverRadioButtonText
text: desc text: desc
color: "#D7D8DB" color: "#D7D8DB"
@ -333,7 +348,7 @@ PageBase {
Image { Image {
source: "qrc:/images/controls/check.svg" source: "qrc:/images/controls/check.svg"
visible: radioButton.checked visible: serverRadioButton.checked
width: 24 width: 24
height: 24 height: 24
@ -342,10 +357,11 @@ PageBase {
} }
onClicked: { onClicked: {
console.log(index)
ContainersModel.setSelectedServerIndex(index)
root.currentServerName = desc root.currentServerName = desc
root.currentServerDescription = address root.currentServerDescription = address
ServersModel.setDefaultServerIndex(index)
ContainersModel.setSelectedServerIndex(index)
} }
} }
} }