From 7450a09af3dfd5a6687a805be359dd7d54569cd9 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 11 Mar 2015 16:24:01 +0100 Subject: [PATCH] Plugin view: Fix sorting Broke after moving to Utils::TreeModel. Use QSortFilterProxyModel on top of TreeModel (can later be used for filtering as well). Additionally we need to make sure to report changes only for the changed column now, because otherwise QSortFilterProxyModel thinks that the change could make re-sorting necessary, and does that in a non-stable way. Change-Id: I9fd12c55a45aba4c05f8e318ae8ea9a4ab9f3310 Task-number: QTCREATORBUG-14107 Reviewed-by: Robert Loehning Reviewed-by: hjk --- src/libs/extensionsystem/pluginview.cpp | 16 +++++++++++----- src/libs/extensionsystem/pluginview.h | 5 +++++ src/libs/utils/treemodel.cpp | 8 ++++++++ src/libs/utils/treemodel.h | 1 + 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/libs/extensionsystem/pluginview.cpp b/src/libs/extensionsystem/pluginview.cpp index 21646f1d6ee..97b87f84db9 100644 --- a/src/libs/extensionsystem/pluginview.cpp +++ b/src/libs/extensionsystem/pluginview.cpp @@ -41,8 +41,9 @@ #include #include #include -#include #include +#include +#include /*! \class ExtensionSystem::PluginView @@ -153,8 +154,8 @@ public: { if (column == LoadedColumn && role == Qt::CheckStateRole) { m_spec->setEnabled(data.toBool()); - update(); - parent()->update(); + updateColumn(column); + parent()->updateColumn(column); emit m_view->pluginSettingsChanged(m_spec); return true; } @@ -280,13 +281,17 @@ PluginView::PluginView(QWidget *parent) m_categoryView->setColumnWidth(LoadedColumn, 40); m_categoryView->header()->setDefaultSectionSize(120); m_categoryView->header()->setMinimumSectionSize(35); + m_categoryView->header()->setSortIndicator(0, Qt::AscendingOrder); m_categoryView->setActivationMode(DoubleClickActivation); m_categoryView->setSelectionMode(QAbstractItemView::SingleSelection); m_categoryView->setSelectionBehavior(QAbstractItemView::SelectRows); m_model = new TreeModel(this); m_model->setHeader(QStringList() << tr("Name") << tr("Load") << tr("Version") << tr("Vendor")); - m_categoryView->setModel(m_model); + + m_sortModel = new QSortFilterProxyModel(this); + m_sortModel->setSourceModel(m_model); + m_categoryView->setModel(m_sortModel); QGridLayout *gridLayout = new QGridLayout(this); gridLayout->setContentsMargins(2, 2, 2, 2); @@ -325,7 +330,8 @@ PluginSpec *PluginView::currentPlugin() const PluginSpec *PluginView::pluginForIndex(const QModelIndex &index) const { - auto item = dynamic_cast(m_model->itemFromIndex(index)); + const QModelIndex &sourceIndex = m_sortModel->mapToSource(index); + auto item = dynamic_cast(m_model->itemFromIndex(sourceIndex)); return item ? item->m_spec: 0; } diff --git a/src/libs/extensionsystem/pluginview.h b/src/libs/extensionsystem/pluginview.h index e3ffc30897d..4cf31c9176c 100644 --- a/src/libs/extensionsystem/pluginview.h +++ b/src/libs/extensionsystem/pluginview.h @@ -37,6 +37,10 @@ #include #include +QT_BEGIN_NAMESPACE +class QSortFilterProxyModel; +QT_END_NAMESPACE + namespace Utils { class TreeItem; class TreeModel; @@ -71,6 +75,7 @@ private: Utils::TreeView *m_categoryView; Utils::TreeModel *m_model; + QSortFilterProxyModel *m_sortModel; friend class CollectionItem; friend class PluginItem; diff --git a/src/libs/utils/treemodel.cpp b/src/libs/utils/treemodel.cpp index 3013fac07d3..249c5e875e2 100644 --- a/src/libs/utils/treemodel.cpp +++ b/src/libs/utils/treemodel.cpp @@ -733,6 +733,14 @@ void TreeItem::update() } } +void TreeItem::updateColumn(int column) +{ + if (m_model) { + QModelIndex idx = index(); + m_model->dataChanged(idx.sibling(idx.row(), column), idx.sibling(idx.row(), column)); + } +} + TreeItem *TreeItem::firstChild() const { return m_children.isEmpty() ? 0 : m_children.first(); diff --git a/src/libs/utils/treemodel.h b/src/libs/utils/treemodel.h index 6e654fdfaab..23d2f1bea06 100644 --- a/src/libs/utils/treemodel.h +++ b/src/libs/utils/treemodel.h @@ -89,6 +89,7 @@ public: void insertChild(int pos, TreeItem *item); void removeChildren(); void update(); + void updateColumn(int column); void expand(); TreeItem *firstChild() const; TreeItem *lastChild() const;