Finishing the renaming of the BufferTreeView, since SVN doesn't allow
[quassel.git] / src / client / nickmodel.cpp
index d7e4248..801a5cc 100644 (file)
@@ -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();
+  }
+}