From 887c2cd696c54af0cd6fdd54950d006626afeacc Mon Sep 17 00:00:00 2001 From: Arno Date: Sun, 30 Sep 2012 04:34:22 +0200 Subject: Foremost a fix for SmTreeModel Not working again, but I eventually have to commit the changes. Fixes to SmTreeModel: * Fix SmTreeModel::index(). The previous comment was quite valid. I'm surprised that it worked at all. I have no clue why to return an invalid QModelIndex if the column isn't 0. Now an index with any valid column number can be created. * Fix SmTreeModel::parent(). Again, why shouldn't we create a parent index with a column other than 0? No idea... * Fix SmTreeModel::headerData(). Add some sanity checks. * Fix SmTreeModel::findRecursive(). Well, what is there to say. It never worked for models with a depth > 1, but obviously it didn't really matter until now. To make it work I had to change SmTreeItem as well. SmTreeItem::next() returns the next valid parent/sibling, or 0 if there isn't one. There may be some fallout from these changes, but they're yet to be seen. Changes to PictureView: * fix selecting an item according to the new datasbase layout * same goes for editing items. If an update actually works has to be checked. Overall, it's an intermediate commit that should have been a sane series of commits. Can't be changed now... --- smtreemodel.cpp | 74 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 33 deletions(-) (limited to 'smtreemodel.cpp') diff --git a/smtreemodel.cpp b/smtreemodel.cpp index 7df30d3..98fa4c8 100644 --- a/smtreemodel.cpp +++ b/smtreemodel.cpp @@ -9,6 +9,8 @@ #include #include +#include + #include "smtreemodel.h" #include "smtreeitem.h" #include "smglobals.h" @@ -40,16 +42,20 @@ int SmTreeModel::columnCount(const QModelIndex &parent) const{ } QModelIndex SmTreeModel::index(int row, int column, const QModelIndex &parent) const{ - //this is totally bogus! Why? Tentatively leaving it in for now... - if(parent.isValid() && (parent.column() != 0)){ - return QModelIndex(); + // return child from root if parent is invalid + // this was difficult to figure this out! + if(!parent.isValid()){ + return createIndex(row, column, mRootItem->child(row)); } - SmTreeItem *parentItem = itemAt(parent); - SmTreeItem *childItem = parentItem->child(row); - if(childItem){ - return createIndex(row, column, childItem); - } - return QModelIndex(); + SmTreeItem *parentItem = static_cast(parent.internalPointer()); + if( (parentItem == 0) || + (row < 0) || + (column < 0) || + (row >= parentItem->childCount()) || + (column > parentItem->columnCount())){ + return QModelIndex(); + } + return createIndex(row, column, parentItem->child(row)); } QModelIndex SmTreeModel::parent(const QModelIndex &child) const{ @@ -57,13 +63,13 @@ QModelIndex SmTreeModel::parent(const QModelIndex &child) const{ return QModelIndex(); } - SmTreeItem *childItem = itemAt(child); + SmTreeItem *childItem = static_cast(child.internalPointer()); SmTreeItem *parentItem = childItem->parent(); - if(parentItem == mRootItem){ + if(parentItem == 0){ return QModelIndex(); } - return createIndex(parentItem->row(), 0, parentItem); + return createIndex(parentItem->row(), child.column(), parentItem); } Qt::ItemFlags SmTreeModel::flags(const QModelIndex &index) const{ @@ -74,7 +80,7 @@ Qt::ItemFlags SmTreeModel::flags(const QModelIndex &index) const{ } QVariant SmTreeModel::headerData(int section, Qt::Orientation orientation, int role) const{ - if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)){ + if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole) && (section > -1) && (section < mHeaders.count())){ return mHeaders.at(section); } return QVariant(); @@ -142,30 +148,28 @@ QModelIndex SmTreeModel::find(const QVariant &value, int column, const QModelInd return QModelIndex(); } -QModelIndex SmTreeModel::findRecursive(const QVariant &value, int column, const QModelIndex &parent) const{ - SmTreeItem *parentItem = 0; - if(!parent.isValid()){ - parentItem = mRootItem; - }else{ - parentItem = static_cast(parent.internalPointer()); - } - if(parentItem->data(column) == value){ - return parent; +QModelIndex SmTreeModel::findRecursive(const QVariant &value, int column, const QModelIndex &start) const{ + if(!start.isValid()){ + return QModelIndex(); } - for(int i = 0; i < parentItem->childCount(); ++i){ - SmTreeItem *child = parentItem->child(i); + SmTreeItem *startItem = static_cast(start.internalPointer()); + if(startItem->data(column) == value){ + return start; + } + for(int i = 0; i < startItem->childCount(); ++i){ + SmTreeItem *child = startItem->child(i); if(child->data(column) == value){ return createIndex(i, column, child); } - if(child->childCount()){ - return findRecursive(value, column, createIndex(i, column, child)); - } - } - QModelIndex next = index(parent.row() + 1, column, parent.parent()); - if(!next.isValid()){ - return QModelIndex(); - } - return findRecursive(value, column, next); + if(child->childCount()){ + return findRecursive(value, column, createIndex(i, column, child)); + } + } + SmTreeItem *next = startItem->next(); + if(next){ + return findRecursive(value, column, createIndex(next->row(), column, next)); + } + return QModelIndex(); } bool SmTreeModel::setRoot(SmTreeItem *rootItem){ @@ -179,6 +183,10 @@ bool SmTreeModel::setRoot(SmTreeItem *rootItem){ return false; } +QModelIndex SmTreeModel::rootIndex() const { + return createIndex(0, 0, mRootItem); +} + SmTreeItem *SmTreeModel::parentItem(const QModelIndex &child) const{ QModelIndex parent = child.parent(); if(parent == QModelIndex()){ -- cgit v1.2.3-70-g09d2