diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/controls/Private/qquicktreemodeladaptor.cpp | 52 | ||||
| -rw-r--r-- | src/controls/Private/qquicktreemodeladaptor_p.h | 6 | ||||
| -rw-r--r-- | src/controls/TreeView.qml | 1 | ||||
| -rw-r--r-- | src/controls/doc/src/qtquickcontrols-treeview.qdoc | 13 | ||||
| -rw-r--r-- | src/controls/plugin.cpp | 3 |
5 files changed, 60 insertions, 15 deletions
diff --git a/src/controls/Private/qquicktreemodeladaptor.cpp b/src/controls/Private/qquicktreemodeladaptor.cpp index dddcdd011..0fda57045 100644 --- a/src/controls/Private/qquicktreemodeladaptor.cpp +++ b/src/controls/Private/qquicktreemodeladaptor.cpp @@ -120,6 +120,29 @@ void QQuickTreeModelAdaptor::clearModelData() endResetModel(); } +const QModelIndex &QQuickTreeModelAdaptor::rootIndex() const +{ + return m_rootIndex; +} + +void QQuickTreeModelAdaptor::setRootIndex(const QModelIndex &idx) +{ + if (m_rootIndex == idx) + return; + + if (m_model) + clearModelData(); + m_rootIndex = idx; + if (m_model) + showModelTopLevelItems(); + emit rootIndexChanged(); +} + +void QQuickTreeModelAdaptor::resetRootIndex() +{ + setRootIndex(QModelIndex()); +} + QHash<int, QByteArray> QQuickTreeModelAdaptor::roleNames() const { if (!m_model) @@ -180,7 +203,7 @@ bool QQuickTreeModelAdaptor::setData(const QModelIndex &index, const QVariant &v int QQuickTreeModelAdaptor::itemIndex(const QModelIndex &index) const { // This is basically a plagiarism of QTreeViewPrivate::viewIndex() - if (!index.isValid() || m_items.isEmpty()) + if (!index.isValid() || index == m_rootIndex || m_items.isEmpty()) return -1; const int totalCount = m_items.count(); @@ -226,7 +249,7 @@ bool QQuickTreeModelAdaptor::isVisible(const QModelIndex &index) bool QQuickTreeModelAdaptor::childrenVisible(const QModelIndex &index) { - return (!index.isValid() && !m_items.isEmpty()) + return (index == m_rootIndex && !m_items.isEmpty()) || (m_expandedItems.contains(index) && isVisible(index)); } @@ -302,21 +325,21 @@ void QQuickTreeModelAdaptor::showModelTopLevelItems(bool doInsertRows) if (!m_model) return; - if (m_model->hasChildren(QModelIndex()) && m_model->canFetchMore(QModelIndex())) - m_model->fetchMore(QModelIndex()); - const long topLevelRowCount = m_model->rowCount(); + if (m_model->hasChildren(m_rootIndex) && m_model->canFetchMore(m_rootIndex)) + m_model->fetchMore(m_rootIndex); + const long topLevelRowCount = m_model->rowCount(m_rootIndex); if (topLevelRowCount == 0) return; - showModelChildItems(TreeItem(), 0, topLevelRowCount - 1, doInsertRows); + showModelChildItems(TreeItem(m_rootIndex), 0, topLevelRowCount - 1, doInsertRows); } void QQuickTreeModelAdaptor::showModelChildItems(const TreeItem &parentItem, int start, int end, bool doInsertRows, bool doExpandPendingRows) { const QModelIndex &parentIndex = parentItem.index; - int rowIdx = parentIndex.isValid() ? itemIndex(parentIndex) + 1 : 0; + int rowIdx = parentIndex.isValid() && parentIndex != m_rootIndex ? itemIndex(parentIndex) + 1 : 0; Q_ASSERT(rowIdx == 0 || parentItem.expanded); - if (parentIndex.isValid() && (rowIdx == 0 || !parentItem.expanded)) + if (parentIndex.isValid() && parentIndex != m_rootIndex && (rowIdx == 0 || !parentItem.expanded)) return; if (m_model->rowCount(parentIndex) == 0) { @@ -603,8 +626,11 @@ void QQuickTreeModelAdaptor::modelRowsInserted(const QModelIndex & parent, int s ASSERT_CONSISTENCY(); return; } - } else if (parent.isValid()) { + } else if (parent == m_rootIndex) { item = TreeItem(parent); + } else { + ASSERT_CONSISTENCY(); + return; } showModelChildItems(item, start, end); ASSERT_CONSISTENCY(); @@ -612,10 +638,8 @@ void QQuickTreeModelAdaptor::modelRowsInserted(const QModelIndex & parent, int s void QQuickTreeModelAdaptor::modelRowsAboutToBeRemoved(const QModelIndex & parent, int start, int end) { - Q_UNUSED(start); - Q_UNUSED(end); ASSERT_CONSISTENCY(); - if (!parent.isValid() || childrenVisible(parent)) { + if (parent == m_rootIndex || childrenVisible(parent)) { const QModelIndex &smi = m_model->index(start, 0, parent); int startIndex = itemIndex(smi); const QModelIndex &emi = m_model->index(end, 0, parent); @@ -756,9 +780,9 @@ bool QQuickTreeModelAdaptor::testConsistency(bool dumpOnFail) const } return true; } - QModelIndex parent; + QModelIndex parent = m_rootIndex; QStack<QModelIndex> ancestors; - QModelIndex idx = m_model->index(0, 0); + QModelIndex idx = m_model->index(0, 0, parent); for (int i = 0; i < m_items.count(); i++) { bool isConsistent = true; const TreeItem &item = m_items.at(i); diff --git a/src/controls/Private/qquicktreemodeladaptor_p.h b/src/controls/Private/qquicktreemodeladaptor_p.h index 2297c3657..3eefbe776 100644 --- a/src/controls/Private/qquicktreemodeladaptor_p.h +++ b/src/controls/Private/qquicktreemodeladaptor_p.h @@ -61,6 +61,7 @@ class QQuickTreeModelAdaptor : public QAbstractListModel { Q_OBJECT Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged) + Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex RESET resetRootIndex NOTIFY rootIndexChanged) struct TreeItem; @@ -68,6 +69,9 @@ public: explicit QQuickTreeModelAdaptor(QObject *parent = 0); QAbstractItemModel *model() const; + const QModelIndex &rootIndex() const; + void setRootIndex(const QModelIndex &idx); + void resetRootIndex(); enum { DepthRole = Qt::UserRole - 4, @@ -110,6 +114,7 @@ public: signals: void modelChanged(QAbstractItemModel *model); + void rootIndexChanged(); void expanded(const QModelIndex &index); void collapsed(const QModelIndex &index); @@ -149,6 +154,7 @@ private: }; QPointer<QAbstractItemModel> m_model; + QPersistentModelIndex m_rootIndex; QList<TreeItem> m_items; QSet<QPersistentModelIndex> m_expandedItems; QList<TreeItem *> m_itemsToExpand; diff --git a/src/controls/TreeView.qml b/src/controls/TreeView.qml index c97930f38..637c46c3c 100644 --- a/src/controls/TreeView.qml +++ b/src/controls/TreeView.qml @@ -44,6 +44,7 @@ BasicTableView { id: root property var model: null + property alias rootIndex: modelAdaptor.rootIndex readonly property var currentIndex: modelAdaptor.mapRowToModelIndex(__currentRow) property ItemSelectionModel selection: null diff --git a/src/controls/doc/src/qtquickcontrols-treeview.qdoc b/src/controls/doc/src/qtquickcontrols-treeview.qdoc index fb1860599..a9d41b706 100644 --- a/src/controls/doc/src/qtquickcontrols-treeview.qdoc +++ b/src/controls/doc/src/qtquickcontrols-treeview.qdoc @@ -142,6 +142,19 @@ */ /*! + \qmlproperty QModelIndex TreeView::rootIndex + The model index of the root item in the tree view. The root item is the + parent item to the view's top-level items. Only items descending from the + root item will be visible in the view. + + Its default value is an invalid QModelIndex, which means the whole + model data is shown by the tree view (assigning \c undefined to this + proprety resets it to its default value.) + + \since QtQuick.Controls 1.5 +*/ + +/*! \qmlproperty QModelIndex TreeView::currentIndex The model index of the current row in the tree view. */ diff --git a/src/controls/plugin.cpp b/src/controls/plugin.cpp index f20cada15..802dacb0a 100644 --- a/src/controls/plugin.cpp +++ b/src/controls/plugin.cpp @@ -113,7 +113,8 @@ static const struct { { "TreeView", 1, 4 }, - { "TextArea", 1, 5 } + { "TextArea", 1, 5 }, + { "TreeView", 1, 5 } }; void QtQuickControlsPlugin::registerTypes(const char *uri) |
