Roster model, account model, connection commena, connection report
This commit is contained in:
parent
6823b41f24
commit
3d947a0748
12 changed files with 551 additions and 15 deletions
|
@ -14,6 +14,7 @@ set(squawkUI_SRC
|
|||
accounts.cpp
|
||||
account.cpp
|
||||
models/accounts.cpp
|
||||
models/roster.cpp
|
||||
)
|
||||
|
||||
# Tell CMake to create the helloworld executable
|
||||
|
|
212
ui/models/roster.cpp
Normal file
212
ui/models/roster.cpp
Normal file
|
@ -0,0 +1,212 @@
|
|||
#include "roster.h"
|
||||
|
||||
using namespace Models;
|
||||
|
||||
Models::Roster::Roster(QObject* parent):
|
||||
QAbstractItemModel(parent),
|
||||
root(0)
|
||||
{
|
||||
root = new Item(Item::root, {{"name", "root"}});
|
||||
}
|
||||
|
||||
Models::Roster::~Roster()
|
||||
{
|
||||
delete root;
|
||||
}
|
||||
|
||||
void Models::Roster::addAccount(const QMap<QString, QVariant>& data)
|
||||
{
|
||||
Item* acc = new Item(Item::account, data, root);
|
||||
beginInsertRows(QModelIndex(), root->childCount(), root->childCount());
|
||||
root->appendChild(acc);
|
||||
accounts.insert(std::make_pair(acc->name(), acc));
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
QVariant Models::Roster::data (const QModelIndex& index, int role) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QVariant result;
|
||||
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
{
|
||||
Item *item = static_cast<Item*>(index.internalPointer());
|
||||
result = item->data(index.column());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int Models::Roster::columnCount (const QModelIndex& parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return static_cast<Item*>(parent.internalPointer())->columnCount();
|
||||
} else {
|
||||
return root->columnCount();
|
||||
}
|
||||
}
|
||||
|
||||
Qt::ItemFlags Models::Roster::flags(const QModelIndex& index) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return QAbstractItemModel::flags(index);
|
||||
}
|
||||
|
||||
|
||||
int Models::Roster::rowCount (const QModelIndex& parent) const
|
||||
{
|
||||
Item *parentItem;
|
||||
if (parent.column() > 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!parent.isValid()) {
|
||||
parentItem = root;
|
||||
} else {
|
||||
parentItem = static_cast<Item*>(parent.internalPointer());
|
||||
}
|
||||
|
||||
return parentItem->childCount();
|
||||
}
|
||||
|
||||
QVariant Models::Roster::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QModelIndex Models::Roster::parent (const QModelIndex& child) const
|
||||
{
|
||||
if (!child.isValid()) {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
Item *childItem = static_cast<Item*>(child.internalPointer());
|
||||
Item *parentItem = childItem->parentItem();
|
||||
|
||||
if (parentItem == root) {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
return createIndex(parentItem->row(), 0, parentItem);
|
||||
}
|
||||
|
||||
QModelIndex Models::Roster::index (int row, int column, const QModelIndex& parent) const
|
||||
{
|
||||
if (!hasIndex(row, column, parent)) {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
Item *parentItem;
|
||||
|
||||
if (!parent.isValid()) {
|
||||
parentItem = root;
|
||||
} else {
|
||||
parentItem = static_cast<Item*>(parent.internalPointer());
|
||||
}
|
||||
|
||||
Item *childItem = parentItem->child(row);
|
||||
if (childItem) {
|
||||
return createIndex(row, column, childItem);
|
||||
} else {
|
||||
return QModelIndex();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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),
|
||||
name(p_name)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool Models::Roster::ElId::operator <(const Models::Roster::ElId& other) const
|
||||
{
|
||||
if (account == other.account) {
|
||||
return name < other.name;
|
||||
} else {
|
||||
return account < other.account;
|
||||
}
|
||||
}
|
80
ui/models/roster.h
Normal file
80
ui/models/roster.h
Normal file
|
@ -0,0 +1,80 @@
|
|||
#ifndef MODELS_ROSTER_H
|
||||
#define MODELS_ROSTER_H
|
||||
|
||||
#include <qabstractitemmodel.h>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
|
||||
namespace Models
|
||||
{
|
||||
|
||||
class Roster : public QAbstractItemModel
|
||||
{
|
||||
class Item;
|
||||
class ElId;
|
||||
Q_OBJECT
|
||||
public:
|
||||
Roster(QObject* parent = 0);
|
||||
~Roster();
|
||||
|
||||
void addAccount(const QMap<QString, QVariant> &data);
|
||||
|
||||
QVariant data ( const QModelIndex& index, int role ) const;
|
||||
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;
|
||||
|
||||
private:
|
||||
Item* root;
|
||||
std::map<QString, Item*> 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;
|
||||
|
||||
Item *child(int row);
|
||||
int childCount() const;
|
||||
int columnCount() const;
|
||||
QVariant data(int column) const;
|
||||
int row() const;
|
||||
Item *parentItem();
|
||||
|
||||
const Type type;
|
||||
|
||||
private:
|
||||
std::deque<Item*> childItems;
|
||||
std::deque<QVariant> itemData;
|
||||
Item* parent;
|
||||
};
|
||||
|
||||
class ElId {
|
||||
public:
|
||||
ElId (const QString& p_account, const QString& p_name);
|
||||
|
||||
const QString account;
|
||||
const QString name;
|
||||
|
||||
bool operator < (const ElId& other) const;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // MODELS_ROSTER_H
|
|
@ -5,11 +5,14 @@ Squawk::Squawk(QWidget *parent) :
|
|||
QMainWindow(parent),
|
||||
m_ui(new Ui::Squawk),
|
||||
accounts(0),
|
||||
accountsCache()
|
||||
accountsCache(),
|
||||
rosterModel()
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
m_ui->roster->setModel(&rosterModel);
|
||||
|
||||
connect(m_ui->actionAccounts, SIGNAL(triggered()), this, SLOT(onAccounts()));
|
||||
connect(m_ui->comboBox, SIGNAL(activated(int)), this, SLOT(onComboboxActivated(int)));
|
||||
//m_ui->mainToolBar->addWidget(m_ui->comboBox);
|
||||
}
|
||||
|
||||
Squawk::~Squawk() {
|
||||
|
@ -57,7 +60,42 @@ void Squawk::onAccountsClosed(QObject* parent)
|
|||
void Squawk::newAccount(const QMap<QString, QVariant>& account)
|
||||
{
|
||||
accountsCache.push_back(account);
|
||||
rosterModel.addAccount(account);
|
||||
if (accounts != 0) {
|
||||
accounts->addAccount(account);
|
||||
}
|
||||
}
|
||||
|
||||
void Squawk::onComboboxActivated(int index)
|
||||
{
|
||||
if (index == 0) {
|
||||
if (accountsCache.size() > 0) {
|
||||
AC::const_iterator itr = accountsCache.begin();
|
||||
AC::const_iterator end = accountsCache.end();
|
||||
|
||||
for (; itr != end; ++itr) {
|
||||
const QMap<QString, QVariant>& acc = *itr;
|
||||
if (acc.value("state").toInt() == Shared::disconnected) {
|
||||
emit connectAccount(acc.value("name").toString());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m_ui->comboBox->setCurrentIndex(1);
|
||||
}
|
||||
} else if (index == 1) {
|
||||
AC::const_iterator itr = accountsCache.begin();
|
||||
AC::const_iterator end = accountsCache.end();
|
||||
|
||||
for (; itr != end; ++itr) {
|
||||
const QMap<QString, QVariant>& acc = *itr;
|
||||
if (acc.value("state").toInt() != Shared::disconnected) {
|
||||
emit disconnectAccount(acc.value("name").toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Squawk::accountConnectionStateChanged(const QString& account, int state)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,9 @@
|
|||
#include <deque>
|
||||
|
||||
#include "accounts.h"
|
||||
#include "models/roster.h"
|
||||
|
||||
#include "../global.h"
|
||||
|
||||
namespace Ui {
|
||||
class Squawk;
|
||||
|
@ -22,9 +25,12 @@ public:
|
|||
|
||||
signals:
|
||||
void newAccountRequest(const QMap<QString, QVariant>&);
|
||||
void connectAccount(const QString&);
|
||||
void disconnectAccount(const QString&);
|
||||
|
||||
public slots:
|
||||
void newAccount(const QMap<QString, QVariant>& account);
|
||||
void accountConnectionStateChanged(const QString& account, int state);
|
||||
|
||||
private:
|
||||
typedef std::deque<QMap<QString, QVariant>> AC;
|
||||
|
@ -32,6 +38,7 @@ private:
|
|||
|
||||
Accounts* accounts;
|
||||
AC accountsCache;
|
||||
Models::Roster rosterModel;
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent * event) override;
|
||||
|
@ -39,6 +46,8 @@ protected:
|
|||
private slots:
|
||||
void onAccounts();
|
||||
void onAccountsClosed(QObject* parent = 0);
|
||||
void onComboboxActivated(int index);
|
||||
|
||||
};
|
||||
|
||||
#endif // SQUAWK_H
|
||||
|
|
80
ui/squawk.ui
80
ui/squawk.ui
|
@ -6,41 +6,105 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
<width>385</width>
|
||||
<height>508</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>squawk</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralWidget"/>
|
||||
<property name="toolButtonStyle">
|
||||
<enum>Qt::ToolButtonFollowStyle</enum>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralWidget">
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QComboBox" name="comboBox">
|
||||
<property name="editable">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="currentText">
|
||||
<string>Disconnected</string>
|
||||
</property>
|
||||
<property name="currentIndex">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Available</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset theme="im-user-online"/>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Disconnected</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset theme="im-user-offline"/>
|
||||
</property>
|
||||
</item>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QTreeView" name="roster">
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::NoFrame</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<attribute name="headerVisible">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menuBar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<width>385</width>
|
||||
<height>27</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QMenu" name="menu">
|
||||
<widget class="QMenu" name="menuSettings">
|
||||
<property name="title">
|
||||
<string>Settings</string>
|
||||
</property>
|
||||
<addaction name="actionAccounts"/>
|
||||
</widget>
|
||||
<addaction name="menu"/>
|
||||
<addaction name="menuSettings"/>
|
||||
</widget>
|
||||
<widget class="QToolBar" name="mainToolBar">
|
||||
<attribute name="toolBarArea">
|
||||
<enum>TopToolBarArea</enum>
|
||||
</attribute>
|
||||
<attribute name="toolBarBreak">
|
||||
<bool>true</bool>
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
<addaction name="actionAccounts"/>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
<action name="actionAccounts">
|
||||
<property name="icon">
|
||||
<iconset theme="system-users">
|
||||
<normaloff>.</normaloff>.</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Accounts</string>
|
||||
</property>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue