internal tweaks
authorMarcus Eggenberger <egs@quassel-irc.org>
Thu, 10 Jul 2008 11:50:22 +0000 (13:50 +0200)
committerMarcus Eggenberger <egs@quassel-irc.org>
Thu, 10 Jul 2008 14:06:56 +0000 (16:06 +0200)
src/client/networkmodel.cpp
src/client/treemodel.cpp
src/client/treemodel.h

index c797179..5e1bc91 100644 (file)
@@ -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<IrcUser *> &ircUsers) {
   Q_ASSERT(_ircChannel);
 
   QHash<UserCategoryItem *, QList<IrcUser *> > categories;
+
+  int categoryId = -1;
+  UserCategoryItem *categoryItem = 0;
+  
   foreach(IrcUser *ircUser, ircUsers) {
-    UserCategoryItem *categoryItem;
-    int categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser));
-    if(!(categoryItem = qobject_cast<UserCategoryItem *>(childById(qHash(categoryId))))) {
+    categoryId = UserCategoryItem::categoryFromModes(_ircChannel->userModes(ircUser));
+    categoryItem = qobject_cast<UserCategoryItem *>(childById(qHash(categoryId)));
+    if(!categoryItem) {
       categoryItem = new UserCategoryItem(categoryId, this);
       categories[categoryItem] = QList<IrcUser *>();
       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<UserCategoryItem *>(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<UserCategoryItem *>(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<UserCategoryItem *>(childById(qHash(categoryId)))) && categoryItem->childById(qHash(ircUser)))
+  UserCategoryItem *categoryItem = qobject_cast<UserCategoryItem *>(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<UserCategoryItem *>(child(i));
+    IrcUserItem *userItem = qobject_cast<IrcUserItem *>(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<IrcUser *> &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) {
index 6fc0c25..c61cf9e 100644 (file)
@@ -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<AbstractTreeItem *>(this));
+  }
+  
+  int row_ = parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
+  if(row_ == -1)
+    qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent();
+  return row_;
 }
 
 AbstractTreeItem *AbstractTreeItem::parent() const {
index 8caffdd..6dd6ed8 100644 (file)
@@ -49,6 +49,8 @@ public:
 
   virtual quint64 id() const;
 
+  bool reParent(AbstractTreeItem *newParent);
+    
   AbstractTreeItem *child(int row) const;
   AbstractTreeItem *childById(const quint64 &id) const;