internal tweaks
[quassel.git] / src / client / treemodel.cpp
index 9a91980..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 {
@@ -399,9 +429,6 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const {
     return QVariant();
 
   AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
-  if(role == Qt::DisplayRole && !item->data(index.column(), role).isValid()) {
-    qDebug() << item->data(0, role) << item->columnCount();
-  }
   return item->data(index.column(), role);
 }