Commit 6ebdc18f authored by hjk's avatar hjk

TreeModel: Simplify indexFromItem()

Change-Id: Ief7f120ef27eb43746826da7c771b5f9a3f7ce8f
Reviewed-by: default avatarChristian Stenger <christian.stenger@theqtcompany.com>
parent c6a983d2
......@@ -840,8 +840,13 @@ TreeItem *TreeModel::itemFromIndex(const QModelIndex &idx) const
QModelIndex TreeModel::indexFromItem(const TreeItem *item) const
{
// CHECK(checkItem(item));
return item ? indexFromItemHelper(item, m_root, QModelIndex()) : QModelIndex();
QTC_ASSERT(item, return QModelIndex());
if (item == m_root)
return QModelIndex();
TreeItem *mitem = const_cast<TreeItem *>(item);
int row = item->parent()->m_children.indexOf(mitem);
return createIndex(row, 0, mitem);
}
void TreeModel::appendItem(TreeItem *parent, TreeItem *item)
......@@ -904,23 +909,6 @@ void TreeModel::removeAllSubItems(TreeItem *item)
endRemoveRows();
}
QModelIndex TreeModel::indexFromItemHelper(const TreeItem *needle,
TreeItem *parentItem, const QModelIndex &parentIndex) const
{
CHECK_INDEX(parentIndex);
if (needle == parentItem)
return parentIndex;
for (int i = parentItem->rowCount(); --i >= 0; ) {
TreeItem *childItem = parentItem->child(i);
QModelIndex childIndex = index(i, 0, parentIndex);
QModelIndex idx = indexFromItemHelper(needle, childItem, childIndex);
CHECK_INDEX(idx);
if (idx.isValid())
return idx;
}
return QModelIndex();
}
//
// TreeLevelItems
//
......
......@@ -202,9 +202,6 @@ public:
}
private:
QModelIndex indexFromItemHelper(const TreeItem *needle,
TreeItem *parentItem, const QModelIndex &parentIndex) const;
TreeItem *m_root; // Owned.
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment