NewServerSettings qml rework

This commit is contained in:
Pokamest Nikak 2021-09-09 20:15:44 +03:00
parent 3175bc1e48
commit 40fa2d6779
34 changed files with 644 additions and 255 deletions

View file

@ -0,0 +1,51 @@
#include "all_containers_model.h"
AllContainersModel::AllContainersModel(QObject *parent) :
QAbstractListModel(parent)
{
}
int AllContainersModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return amnezia::allContainers().size();
}
QHash<int, QByteArray> AllContainersModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[DescRole] = "desc";
roles[TypeRole] = "is_vpn";
roles[InstalledRole] = "installed";
return roles;
}
QVariant AllContainersModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0
|| index.row() >= amnezia::allContainers().size()) {
return QVariant();
}
DockerContainer c = amnezia::allContainers().at(index.row());
if (role == NameRole) {
return containerHumanNames().value(c);
}
if (role == DescRole) {
return containerDescriptions().value(c);
}
if (role == TypeRole) {
return isContainerVpnType(c);
}
return QVariant();
}
void AllContainersModel::setServerData(const QJsonObject &server)
{
beginResetModel();
m_serverData = server;
endResetModel();
}

View file

@ -0,0 +1,36 @@
#ifndef ALL_CONTAINERS_MODEL_H
#define ALL_CONTAINERS_MODEL_H
#include <QAbstractListModel>
#include <QJsonObject>
#include <vector>
#include <utility>
#include "containers/containers_defs.h"
class AllContainersModel : public QAbstractListModel
{
Q_OBJECT
public:
AllContainersModel(QObject *parent = nullptr);
public:
enum SiteRoles {
NameRole = Qt::UserRole + 1,
DescRole,
TypeRole,
InstalledRole
};
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
void setServerData(const QJsonObject &server);
protected:
QHash<int, QByteArray> roleNames() const override;
private:
QJsonObject m_serverData;
};
#endif // ALL_CONTAINERS_MODEL_H

View file

@ -0,0 +1,55 @@
#include "servers_model.h"
ServersModel::ServersModel(QObject *parent) :
QAbstractListModel(parent)
{
}
void ServersModel::clearData()
{
beginResetModel();
content.clear();
endResetModel();
}
void ServersModel::setContent(const std::vector<ServerModelContent> &data)
{
beginResetModel();
content = data;
endResetModel();
}
int ServersModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return static_cast<int>(content.size());
}
QHash<int, QByteArray> ServersModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[DescRole] = "desc";
roles[AddressRole] = "address";
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>(content.size())) {
return QVariant();
}
if (role == DescRole) {
return content[index.row()].desc;
}
if (role == AddressRole) {
return content[index.row()].address;
}
if (role == IsDefaultRole) {
return content[index.row()].isDefault;
}
return QVariant();
}

View file

@ -0,0 +1,39 @@
#ifndef SERVERSMODEL_H
#define SERVERSMODEL_H
#include <QAbstractListModel>
#include <vector>
#include <utility>
struct ServerModelContent {
QString desc;
QString address;
bool isDefault;
};
class ServersModel : public QAbstractListModel
{
Q_OBJECT
public:
ServersModel(QObject *parent = nullptr);
public:
enum SiteRoles {
DescRole = Qt::UserRole + 1,
AddressRole,
IsDefaultRole
};
void clearData();
void setContent(const std::vector<ServerModelContent>& data);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
protected:
QHash<int, QByteArray> roleNames() const override;
private:
std::vector<ServerModelContent> content;
};
#endif // SERVERSMODEL_H

View file

@ -0,0 +1,86 @@
#include "sites_model.h"
SitesModel::SitesModel(Settings::RouteMode mode, QObject *parent)
: QAbstractListModel(parent),
m_mode(mode)
{
}
void SitesModel::resetCache()
{
beginResetModel();
m_ipsCache.clear();
m_cacheReady = false;
endResetModel();
}
int SitesModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
if (!m_cacheReady) genCache();
return m_ipsCache.size();
}
QVariant SitesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (!m_cacheReady) genCache();
if (role == SitesModel::UrlRole || role == SitesModel::IpRole) {
if (m_ipsCache.isEmpty()) return QVariant();
if (role == SitesModel::UrlRole) {
return m_ipsCache.at(index.row()).first;
}
if (role == SitesModel::IpRole) {
return m_ipsCache.at(index.row()).second;
}
}
// if (role == Qt::TextAlignmentRole && index.column() == 1) {
// return Qt::AlignRight;
// }
return QVariant();
}
QVariant SitesModel::data(int row, int column)
{
if (row < 0 || row >= rowCount() || column < 0 || column >= 2) {
return QVariant();
}
if (!m_cacheReady) genCache();
if (column == 0) {
return m_ipsCache.at(row).first;
}
if (column == 1) {
return m_ipsCache.at(row).second;
}
return QVariant();
}
void SitesModel::genCache() const
{
qDebug() << "SitesModel::genCache";
m_ipsCache.clear();
const QVariantMap &sites = m_settings.vpnSites(m_mode);
auto i = sites.constBegin();
while (i != sites.constEnd()) {
m_ipsCache.append(qMakePair(i.key(), i.value().toString()));
++i;
}
m_cacheReady= true;
}
QHash<int, QByteArray> SitesModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[UrlRole] = "url_path";
roles[IpRole] = "ip";
return roles;
}

View file

@ -0,0 +1,41 @@
#ifndef SITESMODEL_H
#define SITESMODEL_H
#include <QAbstractListModel>
#include "settings.h"
class SitesModel : public QAbstractListModel
{
Q_OBJECT
public:
enum SiteRoles {
UrlRole = Qt::UserRole + 1,
IpRole
};
explicit SitesModel(Settings::RouteMode mode, QObject *parent = nullptr);
void resetCache();
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant data(int row, int column);
protected:
QHash<int, QByteArray> roleNames() const override;
private:
void genCache() const;
private:
Settings::RouteMode m_mode;
Settings m_settings;
mutable QVector<QPair<QString, QString>> m_ipsCache;
mutable bool m_cacheReady = false;
};
#endif // SITESMODEL_H