diff --git a/ui/models/account.cpp b/ui/models/account.cpp new file mode 100644 index 0000000..0b22699 --- /dev/null +++ b/ui/models/account.cpp @@ -0,0 +1,77 @@ +#include "account.h" + +Models::Account::Account(const QMap& 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; +} diff --git a/ui/models/account.h b/ui/models/account.h new file mode 100644 index 0000000..afdb9d7 --- /dev/null +++ b/ui/models/account.h @@ -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 &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 diff --git a/ui/models/accounts.h b/ui/models/accounts.h index 892cec4..0ead813 100644 --- a/ui/models/accounts.h +++ b/ui/models/accounts.h @@ -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 accs; + std::deque accs; static std::deque columns; }; - -struct Account { - QString name; - QString server; - QString login; - QString password; - int state; -}; - } #endif // MODELS_ACCOUNT_H diff --git a/ui/models/item.cpp b/ui/models/item.cpp new file mode 100644 index 0000000..8b9b83d --- /dev/null +++ b/ui/models/item.cpp @@ -0,0 +1,80 @@ +#include "item.h" + +using namespace Models; + +Models::Item::Item(Type p_type, const QMap &p_data, Item *p_parent): + type(p_type), + name(p_data.value("name").toString()), + childItems(), + parent(p_parent) +{} + +Models::Item::~Item() +{ + std::deque::const_iterator itr = childItems.begin(); + std::deque::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::const_iterator itr = parent->childItems.begin(); + std::deque::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; +} diff --git a/ui/models/item.h b/ui/models/item.h new file mode 100644 index 0000000..1d556d3 --- /dev/null +++ b/ui/models/item.h @@ -0,0 +1,46 @@ +#ifndef MODELS_ITEM_H +#define MODELS_ITEM_H + +#include +#include +#include + +#include + +namespace Models { + +class Item { + public: + enum Type { + account, + group, + contect, + conversation, + root + }; + + explicit Item(Type p_type, const QMap &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 childItems; + Item* parent; + }; + +} + +#endif // MODELS_ITEM_H diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index d07db94..20886d0 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -20,7 +20,7 @@ void Models::Roster::addAccount(const QMap& 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 &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::const_iterator itr = childItems.begin(); - std::deque::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::const_iterator itr = parent->childItems.begin(); - std::deque::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& 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; -} - diff --git a/ui/models/roster.h b/ui/models/roster.h index 2db1c7d..769778e 100644 --- a/ui/models/roster.h +++ b/ui/models/roster.h @@ -5,15 +5,16 @@ #include #include #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 accounts; std::map elements; private: - class Item { - public: - enum Type { - account, - group, - contect, - conversation, - root - }; - - explicit Item(Type p_type, const QMap &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 childItems; - std::deque itemData; - Item* parent; - }; - - class Account : public Item { - public: - explicit Account(const QMap &data, Item *parentItem = 0); - ~Account(); - - void setState(int state); - }; - class ElId { public: ElId (const QString& p_account, const QString& p_name);