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 <robert.loehning@theqtcompany.com>
Reviewed-by: hjk <hjk@theqtcompany.com>
This commit is contained in:
Eike Ziller
2015-03-11 16:24:01 +01:00
committed by hjk
parent deae570c5d
commit 7450a09af3
4 changed files with 25 additions and 5 deletions

View File

@@ -41,8 +41,9 @@
#include <QDir> #include <QDir>
#include <QGridLayout> #include <QGridLayout>
#include <QHeaderView> #include <QHeaderView>
#include <QSet>
#include <QItemSelectionModel> #include <QItemSelectionModel>
#include <QSet>
#include <QSortFilterProxyModel>
/*! /*!
\class ExtensionSystem::PluginView \class ExtensionSystem::PluginView
@@ -153,8 +154,8 @@ public:
{ {
if (column == LoadedColumn && role == Qt::CheckStateRole) { if (column == LoadedColumn && role == Qt::CheckStateRole) {
m_spec->setEnabled(data.toBool()); m_spec->setEnabled(data.toBool());
update(); updateColumn(column);
parent()->update(); parent()->updateColumn(column);
emit m_view->pluginSettingsChanged(m_spec); emit m_view->pluginSettingsChanged(m_spec);
return true; return true;
} }
@@ -280,13 +281,17 @@ PluginView::PluginView(QWidget *parent)
m_categoryView->setColumnWidth(LoadedColumn, 40); m_categoryView->setColumnWidth(LoadedColumn, 40);
m_categoryView->header()->setDefaultSectionSize(120); m_categoryView->header()->setDefaultSectionSize(120);
m_categoryView->header()->setMinimumSectionSize(35); m_categoryView->header()->setMinimumSectionSize(35);
m_categoryView->header()->setSortIndicator(0, Qt::AscendingOrder);
m_categoryView->setActivationMode(DoubleClickActivation); m_categoryView->setActivationMode(DoubleClickActivation);
m_categoryView->setSelectionMode(QAbstractItemView::SingleSelection); m_categoryView->setSelectionMode(QAbstractItemView::SingleSelection);
m_categoryView->setSelectionBehavior(QAbstractItemView::SelectRows); m_categoryView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_model = new TreeModel(this); m_model = new TreeModel(this);
m_model->setHeader(QStringList() << tr("Name") << tr("Load") << tr("Version") << tr("Vendor")); 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); QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->setContentsMargins(2, 2, 2, 2); gridLayout->setContentsMargins(2, 2, 2, 2);
@@ -325,7 +330,8 @@ PluginSpec *PluginView::currentPlugin() const
PluginSpec *PluginView::pluginForIndex(const QModelIndex &index) const PluginSpec *PluginView::pluginForIndex(const QModelIndex &index) const
{ {
auto item = dynamic_cast<PluginItem *>(m_model->itemFromIndex(index)); const QModelIndex &sourceIndex = m_sortModel->mapToSource(index);
auto item = dynamic_cast<PluginItem *>(m_model->itemFromIndex(sourceIndex));
return item ? item->m_spec: 0; return item ? item->m_spec: 0;
} }

View File

@@ -37,6 +37,10 @@
#include <QSet> #include <QSet>
#include <QHash> #include <QHash>
QT_BEGIN_NAMESPACE
class QSortFilterProxyModel;
QT_END_NAMESPACE
namespace Utils { namespace Utils {
class TreeItem; class TreeItem;
class TreeModel; class TreeModel;
@@ -71,6 +75,7 @@ private:
Utils::TreeView *m_categoryView; Utils::TreeView *m_categoryView;
Utils::TreeModel *m_model; Utils::TreeModel *m_model;
QSortFilterProxyModel *m_sortModel;
friend class CollectionItem; friend class CollectionItem;
friend class PluginItem; friend class PluginItem;

View File

@@ -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 TreeItem *TreeItem::firstChild() const
{ {
return m_children.isEmpty() ? 0 : m_children.first(); return m_children.isEmpty() ? 0 : m_children.first();

View File

@@ -89,6 +89,7 @@ public:
void insertChild(int pos, TreeItem *item); void insertChild(int pos, TreeItem *item);
void removeChildren(); void removeChildren();
void update(); void update();
void updateColumn(int column);
void expand(); void expand();
TreeItem *firstChild() const; TreeItem *firstChild() const;
TreeItem *lastChild() const; TreeItem *lastChild() const;