From 8a3c3f283e33a9ae87b4b3bed94e676f12dd5d2e Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Thu, 29 Nov 2007 17:09:15 +0000 Subject: [PATCH] Fixed annoying bug where the nicklist wouldn't be shown sometimes because of an inconsistent ProxyModel. I had to hack NickView to always call expandAll() when rows are inserted. This works, but is ugly and we should find a more elegant solution in due time. --- src/client/nickmodel.cpp | 30 ++++++++++++++++++++++++++++-- src/client/nickmodel.h | 7 +++++++ src/uisupport/nickview.cpp | 7 +++++++ src/uisupport/nickview.h | 3 +++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/client/nickmodel.cpp b/src/client/nickmodel.cpp index a1e8bcd3..f755c0ed 100644 --- a/src/client/nickmodel.cpp +++ b/src/client/nickmodel.cpp @@ -194,14 +194,14 @@ int NickModel::categoryFromIndex(const QModelIndex &index) const { return index.internalId(); } -void NickModel::addUser(IrcUser *user) { //qDebug() << "adding" << user->nick(); +void NickModel::addUser(IrcUser *user) { int cat = userCategory(user); beginInsertRows(createIndex(cat-1, 0, 0), 0, 0); users[cat-1].prepend(user); endInsertRows(); } -void NickModel::removeUser(IrcUser *user) { //qDebug() << "removing" << user->nick(); +void NickModel::removeUser(IrcUser *user) { // we don't know for sure which category this user was in, so we have to search QModelIndex index = indexOfUser(user); removeUser(index); @@ -241,6 +241,16 @@ FilteredNickModel::FilteredNickModel(QObject *parent) : QSortFilterProxyModel(pa } +FilteredNickModel::~FilteredNickModel() { + +} + +void FilteredNickModel::setSourceModel(QAbstractItemModel *model) { + QSortFilterProxyModel::setSourceModel(model); + connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(sourceRowsInserted(const QModelIndex &, int, int))); + connect(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SLOT(sourceRowsRemoved(const QModelIndex &, int, int))); +} + // Hide empty categories bool FilteredNickModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { if(!source_parent.isValid()) { @@ -250,3 +260,19 @@ bool FilteredNickModel::filterAcceptsRow(int source_row, const QModelIndex &sour return true; } +void FilteredNickModel::sourceRowsInserted(const QModelIndex &index, int start, int end) { + if(!index.isValid()) return; + if(sourceModel()->rowCount(index) <= end - start + 1) { + // category no longer empty + invalidateFilter(); + } +} + +void FilteredNickModel::sourceRowsRemoved(const QModelIndex &index, int, int) { + if(!index.isValid()) return; + if(sourceModel()->rowCount(index) == 0) { + // category is now empty! + invalidateFilter(); + } +} + diff --git a/src/client/nickmodel.h b/src/client/nickmodel.h index 8a65e9d7..938543dd 100644 --- a/src/client/nickmodel.h +++ b/src/client/nickmodel.h @@ -79,6 +79,13 @@ class FilteredNickModel : public QSortFilterProxyModel { public: FilteredNickModel(QObject *parent = 0); + virtual ~FilteredNickModel(); + + virtual void setSourceModel(QAbstractItemModel *model); + + private slots: + void sourceRowsInserted(const QModelIndex &, int, int); + void sourceRowsRemoved(const QModelIndex &, int, int); protected: virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; diff --git a/src/uisupport/nickview.cpp b/src/uisupport/nickview.cpp index 9894ba80..68b51cf1 100644 --- a/src/uisupport/nickview.cpp +++ b/src/uisupport/nickview.cpp @@ -22,6 +22,7 @@ #include "nickmodel.h" #include +#include NickView::NickView(QWidget *parent) : QTreeView(parent) { setGeometry(0, 0, 30, 30); @@ -47,3 +48,9 @@ void NickView::setModel(NickModel *model) { filteredModel->setSourceModel(model); expandAll(); } + +void NickView::rowsInserted(const QModelIndex &index, int start, int end) { + QTreeView::rowsInserted(index, start, end); + expandAll(); // FIXME We need to do this more intelligently. Maybe a pimped TreeView? +} + diff --git a/src/uisupport/nickview.h b/src/uisupport/nickview.h index 2dcf1801..a131c0a2 100644 --- a/src/uisupport/nickview.h +++ b/src/uisupport/nickview.h @@ -34,6 +34,9 @@ class NickView : public QTreeView { NickView(QWidget *parent = 0); virtual ~NickView(); + protected: + void rowsInserted(const QModelIndex &, int, int); + public slots: void setModel(NickModel *model); -- 2.20.1