refactoring ui models, temp

This commit is contained in:
Blue 2019-04-03 18:09:29 +03:00
parent 4a4ba47968
commit 2bcee521c5
7 changed files with 246 additions and 143 deletions

77
ui/models/account.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "account.h"
Models::Account::Account(const QMap<QString, QVariant>& data, Models::Item* parentItem):
Item(account, data, parentItem),
login(data.value("login").toString()),
password(data.value("password").toString()),
server(data.value("server").toString()),
state(data.value("state").toInt())
{
}
Models::Account::~Account()
{
}
void Models::Account::setState(int p_state)
{
state = p_state;
}
QString Models::Account::getLogin() const
{
return login;
}
QString Models::Account::getPassword() const
{
return password;
}
QString Models::Account::getServer() const
{
return server;
}
int Models::Account::getState() const
{
return state;
}
void Models::Account::setLogin(const QString& p_login)
{
login = p_login;
}
void Models::Account::setPassword(const QString& p_password)
{
password = p_password;
}
void Models::Account::setServer(const QString& p_server)
{
server = p_server;
}
QVariant Models::Account::data(int column) const
{
switch (column) {
case 0:
return Item::data(column);
case 1:
return server;
case 2:
return state;
case 3:
return login;
case 4:
return password;
default:
return QVariant();
}
}
int Models::Account::columnCount() const
{
return 5;
}

36
ui/models/account.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef MODELS_ACCOUNT_H
#define MODELS_ACCOUNT_H
#include "item.h"
namespace Models {
class Account : public Item {
public:
explicit Account(const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Account();
void setState(int p_state);
int getState() const;
void setLogin(const QString& p_login);
QString getLogin() const;
void setServer(const QString& p_server);
QString getServer() const;
void setPassword(const QString& p_password);
QString getPassword() const;
QVariant data(int column) const override;
int columnCount() const override;
private:
QString login;
QString password;
QString server;
int state;
};
}
#endif // MODELS_ACCOUNT_H

View File

@ -6,7 +6,6 @@
namespace Models
{
struct Account;
class Accounts : public QAbstractTableModel
{
@ -24,20 +23,11 @@ public:
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
private:
std::deque<Account> accs;
std::deque<Account*> accs;
static std::deque<QString> columns;
};
struct Account {
QString name;
QString server;
QString login;
QString password;
int state;
};
}
#endif // MODELS_ACCOUNT_H

80
ui/models/item.cpp Normal file
View File

@ -0,0 +1,80 @@
#include "item.h"
using namespace Models;
Models::Item::Item(Type p_type, const QMap<QString, QVariant> &p_data, Item *p_parent):
type(p_type),
name(p_data.value("name").toString()),
childItems(),
parent(p_parent)
{}
Models::Item::~Item()
{
std::deque<Item*>::const_iterator itr = childItems.begin();
std::deque<Item*>::const_iterator end = childItems.end();
for (;itr != end; ++itr) {
delete (*itr);
}
}
void Models::Item::setName(const QString& p_name)
{
name = p_name;
}
void Models::Item::appendChild(Models::Item* child)
{
childItems.push_back(child);
}
Models::Item * Models::Item::child(int row)
{
return childItems[row];
}
int Models::Item::childCount() const
{
return childItems.size();
}
int Models::Item::row() const
{
if (parent != 0) {
std::deque<Item*>::const_iterator itr = parent->childItems.begin();
std::deque<Item*>::const_iterator end = parent->childItems.end();
for (int i = 0; itr != end; ++itr, ++i) {
if (*itr == this) {
return i;
}
}
}
return 0; //TODO not sure how it helps, i copy-pasted it from the example
}
Models::Item * Models::Item::parentItem()
{
return parent;
}
int Models::Item::columnCount() const
{
return 1;
}
QString Models::Item::getName() const
{
return name;
}
QVariant Models::Item::data(int column) const
{
if (column != 0) {
return QVariant();
}
return name;
}

46
ui/models/item.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef MODELS_ITEM_H
#define MODELS_ITEM_H
#include <QMap>
#include <QString>
#include <QVariant>
#include <deque>
namespace Models {
class Item {
public:
enum Type {
account,
group,
contect,
conversation,
root
};
explicit Item(Type p_type, const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Item();
void appendChild(Item *child);
QString getName() const;
void setName(const QString& name);
Item *child(int row);
int childCount() const;
virtual int columnCount() const;
virtual QVariant data(int column) const;
int row() const;
Item *parentItem();
const Type type;
protected:
QString name;
std::deque<Item*> childItems;
Item* parent;
};
}
#endif // MODELS_ITEM_H

View File

@ -20,7 +20,7 @@ void Models::Roster::addAccount(const QMap<QString, QVariant>& data)
Account* acc = new Account(data, root);
beginInsertRows(QModelIndex(), root->childCount(), root->childCount());
root->appendChild(acc);
accounts.insert(std::make_pair(acc->name(), acc));
accounts.insert(std::make_pair(acc->getName(), acc));
endInsertRows();
}
@ -67,11 +67,6 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
return result;
}
void Models::Roster::Item::setName(const QString& name)
{
itemData[0] = name;
}
int Models::Roster::columnCount (const QModelIndex& parent) const
{
if (parent.isValid()) {
@ -175,76 +170,7 @@ QModelIndex Models::Roster::index (int row, int column, const QModelIndex& paren
}
Models::Roster::Item::Item(Type p_type, const QMap<QString, QVariant> &p_data, Item *p_parent):
type(p_type),
childItems(),
itemData(),
parent(p_parent)
{
itemData.push_back(p_data.value("name"));
}
Models::Roster::Item::~Item()
{
std::deque<Item*>::const_iterator itr = childItems.begin();
std::deque<Item*>::const_iterator end = childItems.end();
for (;itr != end; ++itr) {
delete (*itr);
}
}
void Models::Roster::Item::appendChild(Models::Roster::Item* child)
{
childItems.push_back(child);
}
Models::Roster::Item * Models::Roster::Item::child(int row)
{
return childItems[row];
}
int Models::Roster::Item::childCount() const
{
return childItems.size();
}
int Models::Roster::Item::row() const
{
if (parent != 0) {
std::deque<Item*>::const_iterator itr = parent->childItems.begin();
std::deque<Item*>::const_iterator end = parent->childItems.end();
for (int i = 0; itr != end; ++itr, ++i) {
if (*itr == this) {
return i;
}
}
}
return 0; //TODO not sure how it helps, i copy-pasted it from the example
}
Models::Roster::Item * Models::Roster::Item::parentItem()
{
return parent;
}
int Models::Roster::Item::columnCount() const
{
return itemData.size();
}
QString Models::Roster::Item::name() const
{
return itemData[0].toString();
}
QVariant Models::Roster::Item::data(int column) const
{
return itemData[column];
}
Models::Roster::ElId::ElId(const QString& p_account, const QString& p_name):
account(p_account),
@ -262,19 +188,4 @@ bool Models::Roster::ElId::operator <(const Models::Roster::ElId& other) const
}
}
Models::Roster::Account::Account(const QMap<QString, QVariant>& data, Models::Roster::Item* parentItem):
Item(account, data, parentItem)
{
itemData.push_back(data.value("state"));
}
Models::Roster::Account::~Account()
{
}
void Models::Roster::Account::setState(int state)
{
itemData[1] = state;
}

View File

@ -5,15 +5,16 @@
#include <deque>
#include <map>
#include "../../global.h"
#include "accounts.h"
#include "item.h"
#include "account.h"
namespace Models
{
class Roster : public QAbstractItemModel
{
class Item;
class ElId;
class Account;
Q_OBJECT
public:
Roster(QObject* parent = 0);
@ -30,52 +31,14 @@ public:
QModelIndex parent ( const QModelIndex& child ) const override;
QModelIndex index ( int row, int column, const QModelIndex& parent ) const override;
Accounts* accountsModel;
private:
Item* root;
std::map<QString, Account*> accounts;
std::map<ElId, Item*> elements;
private:
class Item {
public:
enum Type {
account,
group,
contect,
conversation,
root
};
explicit Item(Type p_type, const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Item();
void appendChild(Item *child);
QString name() const;
void setName(const QString& name);
Item *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
Item *parentItem();
const Type type;
protected:
std::deque<Item*> childItems;
std::deque<QVariant> itemData;
Item* parent;
};
class Account : public Item {
public:
explicit Account(const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Account();
void setState(int state);
};
class ElId {
public:
ElId (const QString& p_account, const QString& p_name);