Accounts saving, tree status, sigint catching
This commit is contained in:
parent
d4afdd7a5f
commit
4a4ba47968
11 changed files with 251 additions and 15 deletions
|
@ -1,4 +1,5 @@
|
|||
#include "roster.h"
|
||||
#include <QIcon>
|
||||
|
||||
using namespace Models;
|
||||
|
||||
|
@ -16,7 +17,7 @@ Models::Roster::~Roster()
|
|||
|
||||
void Models::Roster::addAccount(const QMap<QString, QVariant>& data)
|
||||
{
|
||||
Item* acc = new Item(Item::account, data, root);
|
||||
Account* acc = new Account(data, root);
|
||||
beginInsertRows(QModelIndex(), root->childCount(), root->childCount());
|
||||
root->appendChild(acc);
|
||||
accounts.insert(std::make_pair(acc->name(), acc));
|
||||
|
@ -31,13 +32,34 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
|
|||
|
||||
QVariant result;
|
||||
|
||||
Item *item = static_cast<Item*>(index.internalPointer());
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
{
|
||||
Item *item = static_cast<Item*>(index.internalPointer());
|
||||
result = item->data(index.column());
|
||||
}
|
||||
break;
|
||||
case Qt::DecorationRole:
|
||||
switch (item->type) {
|
||||
case Item::account:{
|
||||
int state = item->data(1).toInt();
|
||||
switch (state) {
|
||||
case Shared::disconnected:
|
||||
result = QIcon::fromTheme("im-user-offline");
|
||||
break;
|
||||
case Shared::connecting:
|
||||
result = QIcon::fromTheme(Shared::ConnectionStateThemeIcons[state]);
|
||||
break;
|
||||
case Shared::connected:
|
||||
result = QIcon::fromTheme("im-user-online");
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -45,6 +67,11 @@ 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()) {
|
||||
|
@ -54,6 +81,26 @@ int Models::Roster::columnCount (const QModelIndex& parent) const
|
|||
}
|
||||
}
|
||||
|
||||
void Models::Roster::updateAccount(const QString& account, const QString& field, const QVariant& value)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Qt::ItemFlags Models::Roster::flags(const QModelIndex& index) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
|
@ -92,6 +139,10 @@ QModelIndex Models::Roster::parent (const QModelIndex& child) const
|
|||
}
|
||||
|
||||
Item *childItem = static_cast<Item*>(child.internalPointer());
|
||||
if (childItem == root) {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
Item *parentItem = childItem->parentItem();
|
||||
|
||||
if (parentItem == root) {
|
||||
|
@ -210,3 +261,20 @@ bool Models::Roster::ElId::operator <(const Models::Roster::ElId& other) const
|
|||
return account < other.account;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <qabstractitemmodel.h>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include "../../global.h"
|
||||
|
||||
namespace Models
|
||||
{
|
||||
|
@ -12,24 +13,26 @@ class Roster : public QAbstractItemModel
|
|||
{
|
||||
class Item;
|
||||
class ElId;
|
||||
class Account;
|
||||
Q_OBJECT
|
||||
public:
|
||||
Roster(QObject* parent = 0);
|
||||
~Roster();
|
||||
|
||||
void addAccount(const QMap<QString, QVariant> &data);
|
||||
void updateAccount(const QString& account, const QString& field, const QVariant& value);
|
||||
|
||||
QVariant data ( const QModelIndex& index, int role ) const;
|
||||
QVariant data ( const QModelIndex& index, int role ) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
int columnCount ( const QModelIndex& parent ) const;
|
||||
int rowCount ( const QModelIndex& parent ) const;
|
||||
QModelIndex parent ( const QModelIndex& child ) const;
|
||||
QModelIndex index ( int row, int column, const QModelIndex& parent ) const;
|
||||
int columnCount ( const QModelIndex& parent ) const override;
|
||||
int rowCount ( const QModelIndex& parent ) const override;
|
||||
QModelIndex parent ( const QModelIndex& child ) const override;
|
||||
QModelIndex index ( int row, int column, const QModelIndex& parent ) const override;
|
||||
|
||||
private:
|
||||
Item* root;
|
||||
std::map<QString, Item*> accounts;
|
||||
std::map<QString, Account*> accounts;
|
||||
std::map<ElId, Item*> elements;
|
||||
|
||||
private:
|
||||
|
@ -48,6 +51,7 @@ private:
|
|||
|
||||
void appendChild(Item *child);
|
||||
QString name() const;
|
||||
void setName(const QString& name);
|
||||
|
||||
Item *child(int row);
|
||||
int childCount() const;
|
||||
|
@ -58,12 +62,20 @@ private:
|
|||
|
||||
const Type type;
|
||||
|
||||
private:
|
||||
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);
|
||||
|
|
|
@ -107,6 +107,7 @@ void Squawk::accountConnectionStateChanged(const QString& account, int state)
|
|||
QMap<QString, QVariant>* acc = itr->second;
|
||||
acc->insert("state", state);
|
||||
|
||||
rosterModel.updateAccount(account, "state", state);
|
||||
if (accounts != 0) {
|
||||
accounts->updateAccount(account, "state", state);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue