X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fclient%2Fnickmodel.cpp;h=801a5cc10d019eed987f4d7dcb95e2e70650240b;hb=c6df551bf5d6b2f33daa5235d320239fcca37688;hp=d7e4248c35869d0b7fb497c92c367c7a8ead9448;hpb=e671e9da1edaab37ec403f575979f9a92a766e9a;p=quassel.git diff --git a/src/client/nickmodel.cpp b/src/client/nickmodel.cpp index d7e4248c..801a5cc1 100644 --- a/src/client/nickmodel.cpp +++ b/src/client/nickmodel.cpp @@ -5,7 +5,7 @@ * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * (at your option) version 3. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -50,6 +50,7 @@ void NickModel::setIrcChannel(IrcChannel *channel) { _ircChannel = channel; reset(); if(_ircChannel) { + connect(channel, SIGNAL(destroyed()), this, SLOT(setIrcChannel())); connect(channel, SIGNAL(ircUserJoined(IrcUser *)), this, SLOT(addUser(IrcUser *))); connect(channel, SIGNAL(ircUserParted(IrcUser *)), this, SLOT(removeUser(IrcUser *))); connect(channel, SIGNAL(ircUserNickSet(IrcUser *, QString)), this, SLOT(renameUser(IrcUser *))); @@ -60,7 +61,6 @@ void NickModel::setIrcChannel(IrcChannel *channel) { addUser(ircuser); } } - } QVariant NickModel::headerData(int section, Qt::Orientation orientation, int role) const { @@ -140,25 +140,32 @@ QVariant NickModel::data(const QModelIndex &index, int role) const { } int cat = index.internalId(); if(!cat) { // top-level item (category) - if(role == Qt::DisplayRole) { - QString title; - switch(index.row()) { - case 0: title = tr("%n Owner(s)", "", users[index.row()].count()); break; - case 1: title = tr("%n Admin(s)", "", users[index.row()].count()); break; - case 2: title = tr("%n Operator(s)", "", users[index.row()].count()); break; - case 3: title = tr("%n Half-Op(s)", "", users[index.row()].count()); break; - case 4: title = tr("%n Voiced", "", users[index.row()].count()); break; - case 5: title = tr("%n User(s)", "", users[index.row()].count()); break; - default: - qDebug() << "invalid model index"; return QVariant(); + switch(role) { + case Qt::DisplayRole: { + QString title; + switch(index.row()) { + case 0: title = tr("%n Owner(s)", "", users[index.row()].count()); break; + case 1: title = tr("%n Admin(s)", "", users[index.row()].count()); break; + case 2: title = tr("%n Operator(s)", "", users[index.row()].count()); break; + case 3: title = tr("%n Half-Op(s)", "", users[index.row()].count()); break; + case 4: title = tr("%n Voiced", "", users[index.row()].count()); break; + case 5: title = tr("%n User(s)", "", users[index.row()].count()); break; + default: qDebug() << "invalid model index"; return QVariant(); + } + return title; } - return title; - } else return QVariant(); + case SortKeyRole: return index.row(); + default: return QVariant(); + } } else { IrcUser *user = users[cat-1][index.row()]; switch(role) { case Qt::DisplayRole: return user->nick(); + case Qt::ToolTipRole: + return user->hostmask(); + case SortKeyRole: + return user->nick(); default: return QVariant(); } @@ -207,7 +214,7 @@ void NickModel::removeUser(const QModelIndex &index) { endRemoveRows(); } -void NickModel::renameUser(IrcUser *user) { +void NickModel::renameUser(IrcUser *user) { //qDebug() << "renaming" << user->nick(); QModelIndex index = indexOfUser(user); emit dataChanged(index, index); } @@ -223,4 +230,49 @@ void NickModel::changeUserModes(IrcUser *user) { } } +/****************************************************************************************** + * FilteredNickModel + ******************************************************************************************/ + +FilteredNickModel::FilteredNickModel(QObject *parent) : QSortFilterProxyModel(parent) { + setDynamicSortFilter(true); + setSortCaseSensitivity(Qt::CaseInsensitive); + setSortRole(NickModel::SortKeyRole); + +} + +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()) { + QModelIndex index = sourceModel()->index(source_row, 0); + return sourceModel()->rowCount(index); + } + 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(); + } +}