From 31fecb082033f6614986aeb649e571f059176d09 Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 21 Jan 2015 15:50:54 +0100 Subject: [PATCH] TreeModel: Add more item functions canFetchMore/fetchMore, and insertChild (at given position). Make prepend/appendChild use the latter. Change-Id: I4162fe6e64f37d26de209aa81894c9730957694b Reviewed-by: Christian Stenger Reviewed-by: Eike Ziller --- src/libs/utils/treemodel.cpp | 50 +++++++++++++++++++++++------------- src/libs/utils/treemodel.h | 7 +++++ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/libs/utils/treemodel.cpp b/src/libs/utils/treemodel.cpp index 4542e91bb27..6e6b1910c25 100644 --- a/src/libs/utils/treemodel.cpp +++ b/src/libs/utils/treemodel.cpp @@ -671,39 +671,36 @@ Qt::ItemFlags TreeItem::flags(int column) const return m_flags; } +bool TreeItem::canFetchMore() const +{ + return false; +} + void TreeItem::prependChild(TreeItem *item) { - QTC_CHECK(!item->parent()); - - if (m_model && !m_lazy) { - QModelIndex idx = index(); - item->propagateModel(m_model); - m_model->beginInsertRows(idx, 0, 0); - item->m_parent = this; - item->m_model = m_model; - m_children.prepend(item); - m_model->endInsertRows(); - } else { - m_children.prepend(item); - } + insertChild(0, item); } void TreeItem::appendChild(TreeItem *item) +{ + insertChild(m_children.size(), item); +} + +void TreeItem::insertChild(int pos, TreeItem *item) { QTC_CHECK(!item->parent()); + QTC_ASSERT(0 <= pos && pos <= m_children.size(), return); // '<= size' is intentional. if (m_model && !m_lazy) { - const int n = rowCount(); QModelIndex idx = index(); - item->propagateModel(m_model); - m_model->beginInsertRows(idx, n, n); + m_model->beginInsertRows(idx, pos, pos); item->m_parent = this; item->m_model = m_model; - m_children.append(item); + m_children.insert(m_children.begin() + pos, item); m_model->endInsertRows(); } else { item->m_parent = this; - m_children.append(item); + m_children.insert(m_children.begin() + pos, item); } } @@ -910,6 +907,23 @@ Qt::ItemFlags TreeModel::flags(const QModelIndex &idx) const : (Qt::ItemIsEnabled|Qt::ItemIsSelectable); } +bool TreeModel::canFetchMore(const QModelIndex &idx) const +{ + if (!idx.isValid()) + return false; + TreeItem *item = itemFromIndex(idx); + return item ? item->canFetchMore() : false; +} + +void TreeModel::fetchMore(const QModelIndex &idx) +{ + if (!idx.isValid()) + return; + TreeItem *item = itemFromIndex(idx); + if (item) + item->fetchMore(); +} + TreeItem *TreeModel::rootItem() const { return m_root; diff --git a/src/libs/utils/treemodel.h b/src/libs/utils/treemodel.h index 2d076d2fa4a..0fb3cf4c67a 100644 --- a/src/libs/utils/treemodel.h +++ b/src/libs/utils/treemodel.h @@ -62,8 +62,12 @@ public: virtual bool setData(int column, const QVariant &data, int role); virtual Qt::ItemFlags flags(int column) const; + virtual bool canFetchMore() const; + virtual void fetchMore() {} + void prependChild(TreeItem *item); void appendChild(TreeItem *item); + void insertChild(int pos, TreeItem *item); void removeChildren(); void update(); void expand(); @@ -242,6 +246,9 @@ public: Qt::ItemFlags flags(const QModelIndex &idx) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; + bool canFetchMore(const QModelIndex &idx) const; + void fetchMore(const QModelIndex &idx); + TreeItem *rootItem() const; TreeItem *itemFromIndex(const QModelIndex &) const; QModelIndex indexFromItem(const TreeItem *needle) const;