From e8a5c49548759045b49c208c250c6f61c7fdfcd5 Mon Sep 17 00:00:00 2001 From: Marcus Eggenberger Date: Thu, 10 Jul 2008 13:50:22 +0200 Subject: [PATCH] internal tweaks --- src/client/networkmodel.cpp | 58 ++++++++++++++++++++++--------------- src/client/treemodel.cpp | 36 +++++++++++++++++++++-- src/client/treemodel.h | 2 ++ 3 files changed, 70 insertions(+), 26 deletions(-) diff --git a/src/client/networkmodel.cpp b/src/client/networkmodel.cpp index c7971791..5e1bc911 100644 --- a/src/client/networkmodel.cpp +++ b/src/client/networkmodel.cpp @@ -104,6 +104,7 @@ QVariant BufferItem::data(int column, int role) const { } bool BufferItem::setData(int column, const QVariant &value, int role) { + qDebug() << "BufferItem::setData(int column, const QVariant &value, int role):" << this << column << value << role; switch(role) { case NetworkModel::BufferActivityRole: setActivityLevel((Buffer::ActivityLevel)value.toInt()); @@ -213,10 +214,14 @@ void BufferItem::addUsersToCategory(const QList &ircUsers) { Q_ASSERT(_ircChannel); QHash > categories; + + int categoryId = -1; + UserCategoryItem *categoryItem = 0; + foreach(IrcUser *ircUser, ircUsers) { - UserCategoryItem *categoryItem; - int categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser)); - if(!(categoryItem = qobject_cast(childById(qHash(categoryId))))) { + categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser)); + categoryItem = qobject_cast(childById(qHash(categoryId))); + if(!categoryItem) { categoryItem = new UserCategoryItem(categoryId, this); categories[categoryItem] = QList(); newChild(categoryItem); @@ -250,39 +255,42 @@ void BufferItem::removeUserFromCategory(IrcUser *ircUser) { return; } - bool success = false; UserCategoryItem *categoryItem = 0; for(int i = 0; i < childCount(); i++) { categoryItem = qobject_cast(child(i)); - if((success = categoryItem->removeUser(ircUser))) { + if(categoryItem->removeUser(ircUser)) { if(categoryItem->childCount() == 0) removeChild(i); break; } } - -// if(!success) { -// qDebug() << "didn't find User:" << ircUser << qHash(ircUser); -// qDebug() << "==== Childlist for Item:" << this << id() << bufferName() << "===="; -// for(int i = 0; i < childCount(); i++) { -// categoryItem = qobject_cast(child(i)); -// categoryItem->dumpChildList(); -// } -// qDebug() << "==== End Of Childlist for Item:" << this << id() << bufferName() << "===="; -// } -// Q_ASSERT(success); } void BufferItem::userModeChanged(IrcUser *ircUser) { Q_ASSERT(_ircChannel); - UserCategoryItem *categoryItem; int categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser)); - if((categoryItem = qobject_cast(childById(qHash(categoryId)))) && categoryItem->childById(qHash(ircUser))) + UserCategoryItem *categoryItem = qobject_cast(childById(qHash(categoryId))); + + if(categoryItem && categoryItem->childById(qHash(ircUser))) return; // already in the right category; - - removeUserFromCategory(ircUser); - addUserToCategory(ircUser); + + // find the item that needs reparenting + IrcUserItem *ircUserItem = 0; + for(int i = 0; i < childCount(); i++) { + UserCategoryItem *categoryItem = qobject_cast(child(i)); + IrcUserItem *userItem = qobject_cast(categoryItem->childById(qHash(ircUser))); + if(userItem) { + ircUserItem = userItem; + break; + } + } + + if(!ircUserItem) { + qWarning() << "BufferItem::userModeChanged(IrcUser *): unable to determine old category of" << ircUser; + return; + } + ircUserItem->reParent(categoryItem); } QString BufferItem::toolTip(int column) const { @@ -354,6 +362,7 @@ QDateTime BufferItem::lastSeen() { return _lastSeen; } */ + /***************************************** * Network Items *****************************************/ @@ -479,7 +488,6 @@ UserCategoryItem::UserCategoryItem(int category, AbstractTreeItem *parent) : PropertyMapItem(QStringList() << "categoryName", parent), _category(category) { - } // caching this makes no sense, since we display the user number dynamically @@ -504,10 +512,14 @@ void UserCategoryItem::addUsers(const QList &ircUsers) { foreach(IrcUser *ircUser, ircUsers) userItems << new IrcUserItem(ircUser, this); newChilds(userItems); + emit dataChanged(0); } bool UserCategoryItem::removeUser(IrcUser *ircUser) { - return removeChildById(qHash(ircUser)); + bool success = removeChildById(qHash(ircUser)); + if(success) + emit dataChanged(0); + return success; } int UserCategoryItem::categoryFromModes(const QString &modes) { diff --git a/src/client/treemodel.cpp b/src/client/treemodel.cpp index 6fc0c25b..c61cf9e8 100644 --- a/src/client/treemodel.cpp +++ b/src/client/treemodel.cpp @@ -131,6 +131,31 @@ void AbstractTreeItem::removeAllChilds() { emit endRemoveChilds(); } +bool AbstractTreeItem::reParent(AbstractTreeItem *newParent) { + // currently we support only re parenting if the child that's about to be + // adopted does not have any children itself. + if(childCount() != 0) { + qDebug() << "AbstractTreeItem::reParent(): cannot reparent" << this << "with children."; + return false; + } + + int oldRow = row(); + if(oldRow == -1) + return false; + + emit parent()->beginRemoveChilds(oldRow, oldRow); + parent()->_childItems.removeAt(oldRow); + emit parent()->endRemoveChilds(); + + setParent(newParent); + + bool success = newParent->newChild(this); + if(!success) + qWarning() << "AbstractTreeItem::reParent(): failed to attach to new parent after removing from old parent! this:" << this << "new parent:" << newParent; + + return success; +} + AbstractTreeItem *AbstractTreeItem::child(int row) const { if(childCount() <= row) return 0; @@ -155,10 +180,15 @@ int AbstractTreeItem::childCount(int column) const { } int AbstractTreeItem::row() const { - if(!parent()) + if(!parent()) { + qWarning() << "AbstractTreeItem::row():" << this << "has no parent AbstractTreeItem as it's parent! parent is" << QObject::parent(); return -1; - else - return parent()->_childItems.indexOf(const_cast(this)); + } + + int row_ = parent()->_childItems.indexOf(const_cast(this)); + if(row_ == -1) + qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent(); + return row_; } AbstractTreeItem *AbstractTreeItem::parent() const { diff --git a/src/client/treemodel.h b/src/client/treemodel.h index 8caffdd5..6dd6ed8b 100644 --- a/src/client/treemodel.h +++ b/src/client/treemodel.h @@ -49,6 +49,8 @@ public: virtual quint64 id() const; + bool reParent(AbstractTreeItem *newParent); + AbstractTreeItem *child(int row) const; AbstractTreeItem *childById(const quint64 &id) const; -- 2.20.1