added saving the selected server and protocol to the config
This commit is contained in:
parent
b66f4bf2be
commit
e3e7503a7c
6 changed files with 160 additions and 97 deletions
|
@ -1,10 +1,8 @@
|
|||
#include "containers_model.h"
|
||||
|
||||
ContainersModel::ContainersModel(std::shared_ptr<Settings> settings, QObject *parent) :
|
||||
m_settings(settings),
|
||||
QAbstractListModel(parent)
|
||||
ContainersModel::ContainersModel(std::shared_ptr<Settings> 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<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;
|
||||
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<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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -2,34 +2,83 @@
|
|||
|
||||
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
|
||||
{
|
||||
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 {
|
||||
|
@ -39,28 +88,3 @@ QHash<int, QByteArray> 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<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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,24 +15,27 @@ class ServersModel : public QAbstractListModel
|
|||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ServersModel(std::shared_ptr<Settings> settings, QObject *parent = nullptr);
|
||||
public:
|
||||
enum SiteRoles {
|
||||
enum ServersModelRoles {
|
||||
DescRole = Qt::UserRole + 1,
|
||||
AddressRole,
|
||||
IsDefaultRole
|
||||
};
|
||||
|
||||
void refresh();
|
||||
ServersModel(std::shared_ptr<Settings> 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<int, QByteArray> roleNames() const override;
|
||||
|
||||
private:
|
||||
QVector<ServerModelContent> m_data;
|
||||
std::shared_ptr<Settings> m_settings;
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue