just a minor update before I get my hands dirty with the TreeModel...
[quassel.git] / src / client / treemodel.cpp
index dd06446..949dba1 100644 (file)
@@ -57,7 +57,11 @@ bool AbstractTreeItem::newChild(int column, AbstractTreeItem *item) {
     _childItems[column] = QList<AbstractTreeItem *>();
   }
 
+  // check if a child with that ID is already known
+  Q_ASSERT(childById(item->id()) == 0);
+    
   int newRow = _childItems[column].count();
+  qDebug() << "# new Child:" << this << newRow << item << item->id() << item->data(0, Qt::DisplayRole);
   emit beginAppendChilds(column, newRow, newRow);
   _childItems[column].append(item);
   emit endAppendChilds();
@@ -73,6 +77,8 @@ bool AbstractTreeItem::removeChild(int column, int row) {
   if(!_childItems.contains(column) || row >= childCount(column))
     return false;
 
+  AbstractTreeItem *item = _childItems[column][row];  
+  qDebug() << "# Remove Child:" << this << row << item << item->id() << item->data(0, Qt::DisplayRole);
   emit beginRemoveChilds(column, row, row);
   AbstractTreeItem *treeitem = _childItems[column].takeAt(row);
   treeitem->deleteLater();
@@ -89,6 +95,7 @@ bool AbstractTreeItem::removeChildById(int column, const quint64 &id) {
   if(!_childItems.contains(column))
     return false;
 
+  qDebug() << "removeChildyById" << id;
   for(int i = 0; i < _childItems[column].count(); i++) {
     if(_childItems[column][i]->id() == id)
       return removeChild(column, i);
@@ -191,6 +198,25 @@ void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
   _flags = flags;
 }
 
+void AbstractTreeItem::dumpChildList() {
+  AbstractTreeItem *child;
+
+  qDebug() << "==== Childlist for Item:" << this << id() << "====";
+  QHash<int, QList<AbstractTreeItem *> >::iterator columnIter = _childItems.begin();
+  while(columnIter != _childItems.end()) {
+    if(columnIter->count() > 0) {
+      QList<AbstractTreeItem *>::const_iterator childIter = columnIter->constBegin();
+      while(childIter != columnIter->constEnd()) {
+       child = *childIter;
+       qDebug() << "Column:" << columnIter.key() << "Row:" << child->row() << child << child->id() << child->data(0, Qt::DisplayRole);
+       childIter++;
+      }
+    }
+    columnIter++;
+  }
+  qDebug() << "==== End Of Childlist ====";  
+}
+
 /*****************************************
  * SimpleTreeItem
  *****************************************/
@@ -247,10 +273,18 @@ PropertyMapItem::~PropertyMapItem() {
 }
   
 QVariant PropertyMapItem::data(int column, int role) const {
-  if(column >= columnCount() || role != Qt::DisplayRole)
+  if(column >= columnCount())
     return QVariant();
 
-  return property(_propertyOrder[column].toAscii());
+  switch(role) {
+  case Qt::ToolTipRole:
+    return toolTip(column);
+  case Qt::DisplayRole:
+    return property(_propertyOrder[column].toAscii());
+  default:
+    return QVariant();
+  }
+  
 }
 
 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
@@ -280,7 +314,17 @@ TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
     _aboutToRemoveOrInsert(false)
 {
   rootItem = new SimpleTreeItem(data, 0);
+  rootItem->setObjectName("rootItem");
   connectItem(rootItem);
+
+  connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
+         this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
+  connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
+         this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
+  connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
+         this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
+  connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
+         this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
 }
 
 TreeModel::~TreeModel() {
@@ -511,3 +555,65 @@ void TreeModel::endRemoveChilds() {
 void TreeModel::clear() {
   rootItem->removeAllChilds();
 }
+
+void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
+  //  qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
+}
+
+void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
+  AbstractTreeItem *parentItem;
+  parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
+  if(!parentItem)
+    parentItem = rootItem;
+  qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
+
+  if(!_childLists.contains(parentItem))
+    _childLists[parentItem] = QList<AbstractTreeItem *>();
+
+  QList<AbstractTreeItem *> &childList = _childLists[parentItem];
+  QModelIndex child;
+  AbstractTreeItem *childItem;
+  for(int i = end; i >= start; i--) {
+    if(childList.count() <= i) {
+      qDebug() << "#! not removing existing item! lastItem:" << childList.count() - 1 << "Delpos:" << i;
+      Q_ASSERT(false);
+    } else {
+      child = parent.child(i, 0);
+      childItem = parentItem->child(i);
+      Q_ASSERT(childItem);
+      childList.removeAt(i);
+      qDebug() << ">>>" << i << child << childItem->id() << child.data().toString();
+    }
+  }
+}
+
+void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
+  AbstractTreeItem *parentItem;
+  parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
+  if(!parentItem)
+    parentItem = rootItem;
+  qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
+
+  if(!_childLists.contains(parentItem))
+    _childLists[parentItem] = QList<AbstractTreeItem *>();
+
+  QList<AbstractTreeItem *> &childList = _childLists[parentItem];
+  QModelIndex child;
+  AbstractTreeItem *childItem;
+  for(int i = start; i <= end; i++) {
+    if(childList.count() != i) {
+      qDebug() << "#! not appending at the End! End:" << childList.count() << "Insertpos:" << i;
+      Q_ASSERT(false);
+    } else {
+      child = parent.child(i, 0);
+      childItem = parentItem->child(i);
+      Q_ASSERT(childItem);
+      childList.append(childItem);
+      qDebug() << "<<<" << i << child << childItem->id() << child.data().toString();
+    }
+  }
+}
+
+void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
+  // qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
+}