diff options
Diffstat (limited to 'smtreemodel.cpp')
-rw-r--r-- | smtreemodel.cpp | 74 |
1 files changed, 41 insertions, 33 deletions
diff --git a/smtreemodel.cpp b/smtreemodel.cpp index 7df30d3..98fa4c8 100644 --- a/smtreemodel.cpp +++ b/smtreemodel.cpp @@ -9,6 +9,8 @@ #include <QSettings> #include <QBrush> +#include <QDebug> + #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<SmTreeItem*>(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<SmTreeItem*>(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<SmTreeItem*>(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<SmTreeItem*>(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()){ |