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);
}
+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()) {
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();
+ }
+}
+
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;
#include "nickmodel.h"
#include <QHeaderView>
+#include <QDebug>
NickView::NickView(QWidget *parent) : QTreeView(parent) {
setGeometry(0, 0, 30, 30);
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?
+}
+