forked from blue/squawk
first prototype
This commit is contained in:
parent
494afcf2b5
commit
83a2e6af85
13 changed files with 365 additions and 244 deletions
|
@ -18,6 +18,8 @@
|
|||
|
||||
#include "item.h"
|
||||
#include "account.h"
|
||||
#include "reference.h"
|
||||
#include "contact.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
@ -46,11 +48,16 @@ Models::Item::Item(const Models::Item& other):
|
|||
|
||||
Models::Item::~Item()
|
||||
{
|
||||
std::deque<Item*>::const_iterator itr = childItems.begin();
|
||||
std::deque<Item*>::const_iterator end = childItems.end();
|
||||
for (Reference* ref : references) {
|
||||
Item* parent = ref->parentItem();
|
||||
if (parent != nullptr) {
|
||||
parent->removeChild(ref->row());
|
||||
}
|
||||
delete ref;
|
||||
}
|
||||
|
||||
for (;itr != end; ++itr) {
|
||||
delete (*itr);
|
||||
for (Item* child : childItems) {
|
||||
delete child;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,18 +74,26 @@ void Models::Item::appendChild(Models::Item* child)
|
|||
bool moving = false;
|
||||
int newRow = 0;
|
||||
std::deque<Item*>::const_iterator before = childItems.begin();
|
||||
Type ct = child->type;
|
||||
if (ct == reference) {
|
||||
ct = static_cast<Reference*>(child)->dereference()->type;
|
||||
}
|
||||
while (before != childItems.end()) {
|
||||
Item* bfr = *before;
|
||||
if (bfr->type > child->type) {
|
||||
Type bt = bfr->type;
|
||||
if (bt == reference) {
|
||||
bt = static_cast<Reference*>(bfr)->dereference()->type;
|
||||
}
|
||||
if (bt > ct) {
|
||||
break;
|
||||
} else if (bfr->type == child->type && bfr->getDisplayedName() > child->getDisplayedName()) {
|
||||
} else if (bt == ct && bfr->getDisplayedName() > child->getDisplayedName()) {
|
||||
break;
|
||||
}
|
||||
newRow++;
|
||||
before++;
|
||||
}
|
||||
|
||||
if (child->parent != 0) {
|
||||
if (child->parent != nullptr) {
|
||||
int oldRow = child->row();
|
||||
moving = true;
|
||||
emit childIsAboutToBeMoved(child->parent, oldRow, oldRow, this, newRow);
|
||||
|
@ -116,7 +131,7 @@ int Models::Item::childCount() const
|
|||
|
||||
int Models::Item::row() const
|
||||
{
|
||||
if (parent != 0) {
|
||||
if (parent != nullptr) {
|
||||
std::deque<Item*>::const_iterator itr = parent->childItems.begin();
|
||||
std::deque<Item*>::const_iterator end = parent->childItems.end();
|
||||
|
||||
|
@ -127,7 +142,7 @@ int Models::Item::row() const
|
|||
}
|
||||
}
|
||||
|
||||
return 0; //TODO not sure how it helps, i copy-pasted it from the example
|
||||
return -1; //TODO not sure how it helps
|
||||
}
|
||||
|
||||
Models::Item * Models::Item::parentItem()
|
||||
|
@ -178,13 +193,13 @@ void Models::Item::_removeChild(int index)
|
|||
QObject::disconnect(child, &Item::childMoved, this, &Item::childMoved);
|
||||
|
||||
childItems.erase(childItems.begin() + index);
|
||||
child->parent = 0;
|
||||
child->parent = nullptr;
|
||||
}
|
||||
|
||||
|
||||
void Models::Item::changed(int col)
|
||||
{
|
||||
if (parent != 0) {
|
||||
if (parent != nullptr) {
|
||||
emit childChanged(this, row(), col);
|
||||
}
|
||||
}
|
||||
|
@ -197,21 +212,21 @@ void Models::Item::toOfflineState()
|
|||
}
|
||||
}
|
||||
|
||||
const Models::Item * Models::Item::getParentAccount() const
|
||||
const Models::Account * Models::Item::getParentAccount() const
|
||||
{
|
||||
const Item* p = this;
|
||||
|
||||
while (p != 0 && p->type != Item::account) {
|
||||
while (p != nullptr && p->type != Item::account) {
|
||||
p = p->parentItemConst();
|
||||
}
|
||||
|
||||
return p;
|
||||
return static_cast<const Account*>(p);
|
||||
}
|
||||
|
||||
QString Models::Item::getAccountJid() const
|
||||
{
|
||||
const Account* acc = static_cast<const Account*>(getParentAccount());
|
||||
if (acc == 0) {
|
||||
const Account* acc = getParentAccount();
|
||||
if (acc == nullptr) {
|
||||
return "";
|
||||
}
|
||||
return acc->getLogin() + "@" + acc->getServer();
|
||||
|
@ -219,8 +234,8 @@ QString Models::Item::getAccountJid() const
|
|||
|
||||
QString Models::Item::getAccountResource() const
|
||||
{
|
||||
const Account* acc = static_cast<const Account*>(getParentAccount());
|
||||
if (acc == 0) {
|
||||
const Account* acc = getParentAccount();
|
||||
if (acc == nullptr) {
|
||||
return "";
|
||||
}
|
||||
return acc->getResource();
|
||||
|
@ -228,8 +243,8 @@ QString Models::Item::getAccountResource() const
|
|||
|
||||
QString Models::Item::getAccountName() const
|
||||
{
|
||||
const Account* acc = static_cast<const Account*>(getParentAccount());
|
||||
if (acc == 0) {
|
||||
const Account* acc = getParentAccount();
|
||||
if (acc == nullptr) {
|
||||
return "";
|
||||
}
|
||||
return acc->getName();
|
||||
|
@ -237,8 +252,8 @@ QString Models::Item::getAccountName() const
|
|||
|
||||
Shared::Availability Models::Item::getAccountAvailability() const
|
||||
{
|
||||
const Account* acc = static_cast<const Account*>(getParentAccount());
|
||||
if (acc == 0) {
|
||||
const Account* acc = getParentAccount();
|
||||
if (acc == nullptr) {
|
||||
return Shared::Availability::offline;
|
||||
}
|
||||
return acc->getAvailability();
|
||||
|
@ -246,8 +261,8 @@ Shared::Availability Models::Item::getAccountAvailability() const
|
|||
|
||||
Shared::ConnectionState Models::Item::getAccountConnectionState() const
|
||||
{
|
||||
const Account* acc = static_cast<const Account*>(getParentAccount());
|
||||
if (acc == 0) {
|
||||
const Account* acc = getParentAccount();
|
||||
if (acc == nullptr) {
|
||||
return Shared::ConnectionState::disconnected;
|
||||
}
|
||||
return acc->getState();
|
||||
|
@ -261,15 +276,24 @@ QString Models::Item::getDisplayedName() const
|
|||
void Models::Item::onChildChanged(Models::Item* item, int row, int col)
|
||||
{
|
||||
Item* parent = item->parentItem();
|
||||
if (parent != 0 && parent == this) {
|
||||
if (parent != nullptr && parent == this) {
|
||||
if (item->columnInvolvedInDisplay(col)) {
|
||||
int newRow = 0;
|
||||
std::deque<Item*>::const_iterator before = childItems.begin();
|
||||
|
||||
Type ct = item->type;
|
||||
if (ct == reference) {
|
||||
ct = static_cast<Reference*>(item)->dereference()->type;
|
||||
}
|
||||
while (before != childItems.end()) {
|
||||
Item* bfr = *before;
|
||||
if (bfr->type > item->type) {
|
||||
Type bt = bfr->type;
|
||||
if (bt == reference) {
|
||||
bt = static_cast<Reference*>(bfr)->dereference()->type;
|
||||
}
|
||||
if (bt > ct) {
|
||||
break;
|
||||
} else if (bfr->type == item->type && bfr->getDisplayedName() > item->getDisplayedName()) {
|
||||
} else if (bt == ct && bfr->getDisplayedName() > item->getDisplayedName()) {
|
||||
break;
|
||||
}
|
||||
newRow++;
|
||||
|
@ -293,3 +317,42 @@ bool Models::Item::columnInvolvedInDisplay(int col)
|
|||
{
|
||||
return col == 0;
|
||||
}
|
||||
|
||||
void Models::Item::addReference(Models::Reference* ref)
|
||||
{
|
||||
references.insert(ref);
|
||||
}
|
||||
|
||||
void Models::Item::removeReference(Models::Reference* ref)
|
||||
{
|
||||
std::set<Reference*>::const_iterator itr = references.find(ref);
|
||||
if (itr != references.end()) {
|
||||
references.erase(itr);
|
||||
}
|
||||
}
|
||||
|
||||
int Models::Item::getContact(const QString& jid) const
|
||||
{
|
||||
int index = -1;
|
||||
for (std::deque<Item*>::size_type i = 0; i < childItems.size(); ++i) {
|
||||
const Models::Item* item = childItems[i];
|
||||
const Contact* cnt = nullptr;
|
||||
if (item->type == Item::reference) {
|
||||
item = static_cast<const Reference*>(item)->dereferenceConst();
|
||||
}
|
||||
|
||||
if (item->type == Item::contact) {
|
||||
cnt = static_cast<const Contact*>(item);
|
||||
if (cnt->getJid() == jid) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
std::set<Models::Reference *>::size_type Models::Item::referencesCount() const
|
||||
{
|
||||
return references.size();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue