Commit 212ec6ea authored by Arzhan's avatar Arzhan

FIX: VpnOrders model change

parent f7a8e2c8
#include "VpnOrder.h"
VpnOrder::VpnOrder(QString name, QString region, bool switchedOn,
int units, QString unitsType, double value,
QString token, bool unsafe, int speedLimit)
: m_name(name)
VpnOrder::VpnOrder(QObject *parent) : QObject(parent)
{
}
VpnOrder::VpnOrder(const VpnOrder &order)
{
this->setParent(order.parent());
setName(order.getName());
setRegion(order.getRegion());
setSwitchedOn(order.getSwitchedOn());
setUnits(order.getUnits());
setUnitsType(order.getUnitsType());
setValue(order.getValue());
setToken(order.getToken());
setUnsafe(order.getUnsafe());
setSpeedLimit(order.getSpeedLimit());
}
VpnOrder::VpnOrder(QString name, QString region, int units,
UnitsType unitsType, double value, QString token,
bool unsafe, int speedLimit, QObject *parent)
: QObject(parent)
, m_name(name)
, m_region(region)
, m_switchedOn(switchedOn)
, m_switchedOn(false)
, m_units(units)
, m_unitsType(unitsType)
, m_value(value)
......@@ -16,6 +36,21 @@ VpnOrder::VpnOrder(QString name, QString region, bool switchedOn,
}
VpnOrder &VpnOrder::operator=(const VpnOrder &order)
{
this->setParent(order.parent());
setName(order.getName());
setRegion(order.getRegion());
setSwitchedOn(order.getSwitchedOn());
setUnits(order.getUnits());
setUnitsType(order.getUnitsType());
setValue(order.getValue());
setToken(order.getToken());
setUnsafe(order.getUnsafe());
setSpeedLimit(order.getSpeedLimit());
return *this;
}
QString VpnOrder::getName() const
{
return m_name;
......@@ -36,12 +71,12 @@ int VpnOrder::getUnits() const
return m_units;
}
QString VpnOrder::getUnitsType() const
VpnOrder::UnitsType VpnOrder::getUnitsType() const
{
return m_unitsType;
}
int VpnOrder::getValue() const
double VpnOrder::getValue() const
{
return m_value;
}
......@@ -61,4 +96,51 @@ int VpnOrder::getSpeedLimit() const
return m_speedLimit;
}
void VpnOrder::setName(QString value)
{
m_name = value;
}
void VpnOrder::setRegion(QString value)
{
m_region = value;
}
void VpnOrder::setSwitchedOn(bool value)
{
m_switchedOn = value;
emit switchedOnChanged(value);
}
void VpnOrder::setUnits(int value)
{
m_units = value;
}
void VpnOrder::setUnitsType(UnitsType value)
{
m_unitsType = value;
}
void VpnOrder::setValue(double value)
{
m_value = value;
}
void VpnOrder::setToken(QString value)
{
m_token = value;
}
void VpnOrder::setUnsafe(bool value)
{
m_unsafe = value;
}
void VpnOrder::setSpeedLimit(int value)
{
m_speedLimit = value;
}
......@@ -3,30 +3,67 @@
#include <QObject>
class VpnOrder
class VpnOrder : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(bool switchedOn READ getSwitchedOn WRITE setSwitchedOn NOTIFY switchedOnChanged)
enum UnitsType
{
Seconds = 0,
Minutes,
Hours,
Days,
Kilobyte,
Megabyte,
Gigabyte
};
Q_ENUM(UnitsType);
VpnOrder(QObject *parent = nullptr);
VpnOrder (const VpnOrder &order);
VpnOrder(QString name, QString region, int units,
UnitsType unitsType, double value, QString token,
bool unsafe = false, int speedLimit = -1, QObject *parent = nullptr);
VpnOrder& operator=(const VpnOrder& order);
QString getName() const;
QString getRegion() const;
bool getSwitchedOn() const;
int getUnits() const;
UnitsType getUnitsType() const;
double getValue() const;
QString getToken() const;
bool getUnsafe() const;
int getSpeedLimit() const;
void setName(QString value);
void setRegion(QString value);
void setSwitchedOn(bool value);
void setUnits(int value);
void setUnitsType(UnitsType value);
void setValue(double value);
void setToken(QString value);
void setUnsafe(bool value);
void setSpeedLimit(int value);
private:
QString m_name;
QString m_region;
bool m_switchedOn;
int m_units;
QString m_unitsType;
UnitsType m_unitsType;
double m_value;
QString m_token;
bool m_unsafe;
int m_speedLimit;
public:
VpnOrder(QString name, QString region, bool switchedOn,
int units, QString unitsType, double value,
QString token, bool unsafe, int speedLimit);
QString getName() const;
QString getRegion() const;
bool getSwitchedOn() const;
int getUnits() const;
QString getUnitsType() const;
int getValue() const;
QString getToken() const;
bool getUnsafe() const;
int getSpeedLimit() const;
signals:
void switchedOnChanged(const bool& value);
};
#endif // VPNORDER_H
#include "VpnOrdersModel.h"
VpnOrdersModel* VpnOrdersModel::m_instance = nullptr;
VpnOrdersModel::VpnOrdersModel(QObject *parent)
: QAbstractListModel(parent)
{
VpnOrder order1{"Auto server", "Europe", false, 567, "seconds", 63, "LSDO", false, 1};
VpnOrder order2{"Auto Asia", "England", false, 3600, "hours", 0.1, "LFDF", false, -1};
VpnOrder order3{"Auto Europe", "Asia", false, 1230, "minutes", 48, "ASAD", true, 1};
VpnOrder order4{"Auto USA", "Germany", false, 854, "hours", 135.688, "APSF", true, -1};
VpnOrder order5{"Auto server", "Auto", false, 6532, "seconds", 891.5, "POAS", false, 1};
VpnOrder order6{"Auto server", "Russian", false, 3521, "minutes", 79, "CSDC", true, 1};
VpnOrder order7{"Auto server", "Kazakhstan", false, 655, "seconds", 123.56, "AFSG", false, -1};
VpnOrder order8{"Auto server", "USA", false, 186, "minutes", 0.56, "AWER", true, 1};
VpnOrder order9{"Auto server", "Canada", false, 10, "hours", 1.153, "NDGF", false, -1};
m_aOrders.append(order1);
m_aOrders.append(order2);
m_aOrders.append(order3);
m_aOrders.append(order4);
m_aOrders.append(order5);
m_aOrders.append(order6);
m_aOrders.append(order7);
m_aOrders.append(order8);
m_aOrders.append(order9);
}
VpnOrder order1("Auto server", "Europe", 567, VpnOrder::Seconds, 63, "LSDO", false, 1);
VpnOrder order2("Auto Asia", "England", 3600, VpnOrder::Hours, 0.1, "LFDF", false, -1);
VpnOrder order3("Auto Europe", "Asia", 1230, VpnOrder::Minutes, 48, "ASAD", true, 1);
VpnOrder order4("Auto USA", "Germany", 854, VpnOrder::Hours, 135.688, "APSF", true, -1);
VpnOrder order5("Auto server", "Auto", 6532, VpnOrder::Seconds, 891.5, "POAS", false, 1);
VpnOrder order6("Auto server", "Russian", 3521, VpnOrder::Minutes, 79, "CSDC", true, 1);
VpnOrder order7("Auto server", "Kazakhstan", 655, VpnOrder::Seconds, 123.56, "AFSG", false, -1);
VpnOrder order8("Auto server", "USA", 186, VpnOrder::Minutes, 0.56, "AWER", true, 1);
VpnOrder order9("Auto server", "Canada", 10, VpnOrder::Hours, 1.153, "NDGF", false, -1);
void VpnOrdersModel::sortByToken()
{
std::sort(m_aOrders.begin(), m_aOrders.end(), [](const VpnOrder &a, const VpnOrder &b)
{
return a.getToken() < b.getToken();
});
emit dataChanged(createIndex(0, 0), createIndex(m_aOrders.length(), 0));
}
order1.setSwitchedOn(true);
order2.setSwitchedOn(true);
order4.setSwitchedOn(true);
order5.setSwitchedOn(true);
order7.setSwitchedOn(true);
void VpnOrdersModel::sortByRegion()
{
std::sort(m_aOrders.begin(), m_aOrders.end(), [](const VpnOrder &a, const VpnOrder &b)
{
return a.getRegion() < b.getRegion();
});
emit dataChanged(createIndex(0, 0), createIndex(m_aOrders.length(), 0));
m_vpnOrders.append(order1);
m_vpnOrders.append(order2);
m_vpnOrders.append(order3);
m_vpnOrders.append(order4);
m_vpnOrders.append(order5);
m_vpnOrders.append(order6);
m_vpnOrders.append(order7);
m_vpnOrders.append(order8);
m_vpnOrders.append(order9);
}
VpnOrdersModel *VpnOrdersModel::getInstance()
{
static VpnOrdersModel *instance = new VpnOrdersModel;
return instance;
if (m_instance == nullptr)
m_instance = new VpnOrdersModel;
return m_instance;
}
int VpnOrdersModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_aOrders.count();
return m_vpnOrders.count();
}
QVariant VpnOrdersModel::data(const QModelIndex &index, int role) const
......@@ -62,21 +54,43 @@ QVariant VpnOrdersModel::data(const QModelIndex &index, int role) const
switch (role)
{
case nameDisplayRole: return m_aOrders.at(index.row()).getName();
case regionDisplayRole: return m_aOrders.at(index.row()).getToken();
case switchedOnDisplayRole: return m_aOrders.at(index.row()).getSwitchedOn();
case unitsDisplayRole: return m_aOrders.at(index.row()).getUnits();
case unitsTypeDisplayRole: return m_aOrders.at(index.row()).getUnitsType();
case valueDisplayRole: return m_aOrders.at(index.row()).getValue();
case tokenDisplayRole: return m_aOrders.at(index.row()).getToken();
case unsafeDisplayRole: return m_aOrders.at(index.row()).getUnsafe();
case speedLimitDisplayRole: return m_aOrders.at(index.row()).getSpeedLimit();
case nameDisplayRole: return m_vpnOrders.at(index.row()).getName();
case regionDisplayRole: return m_vpnOrders.at(index.row()).getToken();
case switchedOnDisplayRole: return m_vpnOrders.at(index.row()).getSwitchedOn();
case unitsDisplayRole: return m_vpnOrders.at(index.row()).getUnits();
case unitsTypeDisplayRole: return m_vpnOrders.at(index.row()).getUnitsType();
case valueDisplayRole: return m_vpnOrders.at(index.row()).getValue();
case tokenDisplayRole: return m_vpnOrders.at(index.row()).getToken();
case unsafeDisplayRole: return m_vpnOrders.at(index.row()).getUnsafe();
case speedLimitDisplayRole: return m_vpnOrders.at(index.row()).getSpeedLimit();
default: break;
}
return QVariant();
}
bool VpnOrdersModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
switch (role)
{
case nameDisplayRole: m_vpnOrders[index.row()].setName(value.toString()); break;
case regionDisplayRole: m_vpnOrders[index.row()].setRegion(value.toString()); break;
case switchedOnDisplayRole: m_vpnOrders[index.row()].setSwitchedOn(value.toBool()); break;
case unitsDisplayRole: m_vpnOrders[index.row()].setUnits(value.toInt()); break;
case unitsTypeDisplayRole: m_vpnOrders[index.row()].setUnitsType(VpnOrder::UnitsType(value.toInt())); break;
case valueDisplayRole: m_vpnOrders[index.row()].setValue(value.toDouble()); break;
case tokenDisplayRole: m_vpnOrders[index.row()].setToken(value.toString()); break;
case unsafeDisplayRole: m_vpnOrders[index.row()].setUnsafe(value.toBool()); break;
case speedLimitDisplayRole: m_vpnOrders[index.row()].setSpeedLimit(value.toInt()); break;
default: return false;
}
emit dataChanged(createIndex(0, 0), createIndex(m_vpnOrders.length(), 0));
return true;
}
QHash<int, QByteArray> VpnOrdersModel::roleNames() const
{
static const QHash<int, QByteArray> roles
......@@ -94,3 +108,34 @@ QHash<int, QByteArray> VpnOrdersModel::roleNames() const
return roles;
}
VpnOrdersModel::SortType VpnOrdersModel::sortType()
{
return m_sortType;
}
void VpnOrdersModel::setSortType(VpnOrdersModel::SortType type)
{
m_sortType = type;
switch (m_sortType)
{
case SortByRegion:
std::sort(m_vpnOrders.begin(), m_vpnOrders.end(),
[](const VpnOrder &a, const VpnOrder &b)
{
return a.getRegion() < b.getRegion();
});
break;
case SortByToken:
std::sort(m_vpnOrders.begin(), m_vpnOrders.end(),
[](const VpnOrder &a, const VpnOrder &b)
{
return a.getToken() < b.getToken();
});
break;
}
emit dataChanged(createIndex(0, 0), createIndex(m_vpnOrders.length(), 0));
emit sortTypeChanged(type);
}
......@@ -3,22 +3,16 @@
#include <QObject>
#include <QAbstractListModel>
#include <algorithm>
#include <QList>
#include <algorithm>
#include "VpnOrder.h"
class VpnOrdersModel : public QAbstractListModel
{
Q_OBJECT
QList <VpnOrder> m_aOrders;
explicit VpnOrdersModel(QObject *parent = nullptr);
public:
Q_INVOKABLE void sortByToken();
Q_INVOKABLE void sortByRegion();
Q_PROPERTY(SortType sortType READ sortType WRITE setSortType NOTIFY sortTypeChanged)
enum VpnOrderRole
{
......@@ -30,16 +24,36 @@ public:
valueDisplayRole,
tokenDisplayRole,
unsafeDisplayRole,
speedLimitDisplayRole,
speedLimitDisplayRole
};
static VpnOrdersModel *getInstance();
enum SortType
{
SortByRegion,
SortByToken
};
Q_ENUM(SortType);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
static VpnOrdersModel *getInstance();
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
SortType sortType();
void setSortType(SortType type);
private:
static VpnOrdersModel* m_instance;
QList <VpnOrder> m_vpnOrders;
SortType m_sortType;
explicit VpnOrdersModel(QObject *parent = nullptr);
signals:
void sortTypeChanged(SortType);
};
#endif // VPNORDERSMODEL_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment