Refactoring finished

This commit is contained in:
Blue 2019-04-03 21:15:36 +03:00
parent 2bcee521c5
commit d14883ad91
14 changed files with 120 additions and 143 deletions

View file

@ -15,7 +15,10 @@ Models::Account::~Account()
void Models::Account::setState(int p_state)
{
state = p_state;
if (state != p_state) {
state = p_state;
emit changed(2);
}
}
QString Models::Account::getLogin() const
@ -40,17 +43,26 @@ int Models::Account::getState() const
void Models::Account::setLogin(const QString& p_login)
{
login = p_login;
if (login != p_login) {
login = p_login;
emit changed(3);
}
}
void Models::Account::setPassword(const QString& p_password)
{
password = p_password;
if (password != p_password) {
password = p_password;
emit changed(4);
}
}
void Models::Account::setServer(const QString& p_server)
{
server = p_server;
if (server != p_server) {
server = p_server;
emit changed(1);
}
}
QVariant Models::Account::data(int column) const
@ -61,7 +73,7 @@ QVariant Models::Account::data(int column) const
case 1:
return server;
case 2:
return state;
return Shared::ConnectionStateNames[state];
case 3:
return login;
case 4:
@ -75,3 +87,18 @@ int Models::Account::columnCount() const
{
return 5;
}
void Models::Account::update(const QString& field, const QVariant& value)
{
if (field == "name") {
setName(value.toString());
} else if (field == "server") {
setServer(value.toString());
} else if (field == "login") {
setLogin(value.toString());
} else if (field == "password") {
setPassword(value.toString());
} else if (field == "state") {
setState(value.toInt());
}
}

View file

@ -1,7 +1,9 @@
#ifndef MODELS_ACCOUNT_H
#define MODELS_ACCOUNT_H
#include "../../global.h"
#include "item.h"
#include <QVariant>
namespace Models {
class Account : public Item {
@ -24,6 +26,8 @@ namespace Models {
QVariant data(int column) const override;
int columnCount() const override;
void update(const QString& field, const QVariant& value);
private:
QString login;
QString password;

View file

@ -21,28 +21,16 @@ Models::Accounts::~Accounts()
}
QVariant Models::Accounts::data ( const QModelIndex& index, int role ) const
QVariant Models::Accounts::data (const QModelIndex& index, int role) const
{
QVariant answer;
switch (role) {
case Qt::DisplayRole: {
const Account& acc = accs[index.row()];
switch (index.column()) {
case 0:
answer = acc.name;
break;
case 1:
answer = acc.server;
break;
case 2:
answer = Shared::ConnectionStateNames[acc.state];
break;
}
}
case Qt::DisplayRole:
answer = accs[index.row()]->data(index.column());
break;
case Qt::DecorationRole:
if (index.column() == 2) {
answer = QIcon::fromTheme(Shared::ConnectionStateThemeIcons[accs[index.row()].state]);
answer = QIcon::fromTheme(Shared::ConnectionStateThemeIcons[accs[index.row()]->getState()]);
}
break;
default:
@ -71,39 +59,25 @@ QVariant Models::Accounts::headerData(int section, Qt::Orientation orientation,
}
void Models::Accounts::addAccount(const QMap<QString, QVariant>& map)
void Models::Accounts::addAccount(Account* account)
{
beginInsertRows(QModelIndex(), accs.size(), accs.size());
accs.push_back({
map.value("name").toString(),
map.value("server").toString(),
map.value("login").toString(),
map.value("password").toString(),
map.value("state").toInt()
});
accs.push_back(account);
connect(account, SIGNAL(changed(int)), this, SLOT(onAccountChanged(int)));
endInsertRows();
}
void Models::Accounts::updateAccount(const QString& account, const QString& field, const QVariant& value)
void Models::Accounts::onAccountChanged(int column)
{
for (int i = 0; i < accs.size(); ++i) {
Account& acc = accs[i];
if (acc.name == account) {
if (field == "name") {
acc.name = value.toString();
emit dataChanged(createIndex(i, 0), createIndex(i, 0));
} else if (field == "server") {
acc.server = value.toString();
emit dataChanged(createIndex(i, 1), createIndex(i, 1));
} else if (field == "login") {
acc.login = value.toString();
} else if (field == "password") {
acc.password = value.toString();
} else if (field == "state") {
acc.state = value.toInt();
emit dataChanged(createIndex(i, 2), createIndex(i, 2));
}
}
Account* acc = static_cast<Account*>(sender());
if (column < columnCount(QModelIndex())) {
int row = acc->row();
emit dataChanged(createIndex(row, column, this), createIndex(row, column, this));
}
}
Models::Account * Models::Accounts::getAccount(int index)
{
return accs[index];
}

View file

@ -3,6 +3,7 @@
#include <qabstractitemmodel.h>
#include <deque>
#include "account.h"
namespace Models
{
@ -14,19 +15,22 @@ public:
Accounts(QObject* parent = 0);
~Accounts();
void addAccount(const QMap<QString, QVariant>& map);
void updateAccount(const QString& account, const QString& field, const QVariant& value);
void addAccount(Account* account);
QVariant data ( const QModelIndex& index, int role ) const override;
int columnCount ( const QModelIndex& parent ) const override;
int rowCount ( const QModelIndex& parent ) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
Account* getAccount(int index);
private:
std::deque<Account*> accs;
static std::deque<QString> columns;
private slots:
void onAccountChanged(int column);
};
}

View file

@ -3,6 +3,7 @@
using namespace Models;
Models::Item::Item(Type p_type, const QMap<QString, QVariant> &p_data, Item *p_parent):
QObject(),
type(p_type),
name(p_data.value("name").toString()),
childItems(),
@ -21,7 +22,10 @@ Models::Item::~Item()
void Models::Item::setName(const QString& p_name)
{
name = p_name;
if (name != p_name) {
name = p_name;
emit changed(0);
}
}
void Models::Item::appendChild(Models::Item* child)

View file

@ -9,7 +9,8 @@
namespace Models {
class Item {
class Item : public QObject{
Q_OBJECT
public:
enum Type {
account,
@ -22,6 +23,10 @@ class Item {
explicit Item(Type p_type, const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Item();
signals:
void changed(int col);
public:
void appendChild(Item *child);
QString getName() const;
void setName(const QString& name);

View file

@ -5,13 +5,20 @@ using namespace Models;
Models::Roster::Roster(QObject* parent):
QAbstractItemModel(parent),
root(0)
accountsModel(new Accounts()),
root(new Item(Item::root, {{"name", "root"}})),
accounts(),
elements()
{
root = new Item(Item::root, {{"name", "root"}});
connect(accountsModel,
SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)),
this,
SLOT(onAccountDataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)));
}
Models::Roster::~Roster()
{
delete accountsModel;
delete root;
}
@ -21,6 +28,7 @@ void Models::Roster::addAccount(const QMap<QString, QVariant>& data)
beginInsertRows(QModelIndex(), root->childCount(), root->childCount());
root->appendChild(acc);
accounts.insert(std::make_pair(acc->getName(), acc));
accountsModel->addAccount(acc);
endInsertRows();
}
@ -42,7 +50,8 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
case Qt::DecorationRole:
switch (item->type) {
case Item::account:{
int state = item->data(1).toInt();
Account* acc = static_cast<Account*>(item);
int state = acc->getState();
switch (state) {
case Shared::disconnected:
result = QIcon::fromTheme("im-user-offline");
@ -81,17 +90,7 @@ void Models::Roster::updateAccount(const QString& account, const QString& field,
std::map<QString, Account*>::iterator itr = accounts.find(account);
if (itr != accounts.end()) {
Account* acc = itr->second;
if (field == "name") {
acc->setName(value.toString());
accounts.erase(itr);
accounts.insert(std::make_pair(acc->name(), acc));
int row = acc->row();
emit dataChanged(createIndex(row, 0, acc), createIndex(row, 0, acc));
} else if (field == "state") {
acc->setState(value.toInt());
int row = acc->row();
emit dataChanged(createIndex(row, 0, acc), createIndex(row, 0, acc));
}
acc->update(field, value);
}
}
@ -169,15 +168,10 @@ QModelIndex Models::Roster::index (int row, int column, const QModelIndex& paren
}
}
Models::Roster::ElId::ElId(const QString& p_account, const QString& p_name):
account(p_account),
name(p_name)
{
}
{}
bool Models::Roster::ElId::operator <(const Models::Roster::ElId& other) const
{
@ -188,4 +182,15 @@ bool Models::Roster::ElId::operator <(const Models::Roster::ElId& other) const
}
}
void Models::Roster::onAccountDataChanged(const QModelIndex& tl, const QModelIndex& br, const QVector<int>& roles)
{
if (tl.column() == 0) {
emit dataChanged(tl, br, roles);
} else if (tl.column() == 2) {
int row = tl.row();
Account* acc = accountsModel->getAccount(row);
emit dataChanged(createIndex(row, 0, acc), createIndex(br.row(), 0, acc), roles);
}
}

View file

@ -4,6 +4,7 @@
#include <qabstractitemmodel.h>
#include <deque>
#include <map>
#include <QVector>
#include "../../global.h"
#include "accounts.h"
#include "item.h"
@ -38,6 +39,9 @@ private:
std::map<QString, Account*> accounts;
std::map<ElId, Item*> elements;
private slots:
void onAccountDataChanged(const QModelIndex& tl, const QModelIndex& br, const QVector<int>& roles);
private:
class ElId {
public: