Now we have a sorted nicklist and hide unused categories \o/
authorManuel Nickschas <sputnick@quassel-irc.org>
Tue, 27 Nov 2007 01:09:34 +0000 (01:09 +0000)
committerManuel Nickschas <sputnick@quassel-irc.org>
Tue, 27 Nov 2007 01:09:34 +0000 (01:09 +0000)
src/client/nickmodel.cpp
src/client/nickmodel.h
src/qtui/mainwin.cpp
src/uisupport/nickview.cpp
src/uisupport/nickview.h

index d7e4248..a3e7692 100644 (file)
@@ -140,25 +140,32 @@ QVariant NickModel::data(const QModelIndex &index, int role) const {
   }
   int cat = index.internalId();
   if(!cat) { // top-level item (category)
   }
   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();
   } 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();
     }
       default:
         return QVariant();
     }
@@ -223,4 +230,23 @@ void NickModel::changeUserModes(IrcUser *user) {
   }
 }
 
   }
 }
 
+/******************************************************************************************
+ * FilteredNickModel
+ ******************************************************************************************/
+
+FilteredNickModel::FilteredNickModel(QObject *parent) : QSortFilterProxyModel(parent) {
+  setDynamicSortFilter(true);
+  setSortCaseSensitivity(Qt::CaseInsensitive);
+  setSortRole(NickModel::SortKeyRole);
+
+}
+
+// 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;
+}
 
 
index b49ebf1..828f21d 100644 (file)
@@ -22,6 +22,7 @@
 #define _NICKMODEL_H_
 
 #include <QAbstractItemModel>
 #define _NICKMODEL_H_
 
 #include <QAbstractItemModel>
+#include <QSortFilterProxyModel>
 #include <QVector>
 
 class IrcChannel;
 #include <QVector>
 
 class IrcChannel;
@@ -32,12 +33,14 @@ class IrcUser;
  *  levels, where the top-level items are the categories (such as Ops, Voiced etc), and the second-level items
  *  the actual nicks/users. Several roles are provided to access information about a nick.
  *
  *  levels, where the top-level items are the categories (such as Ops, Voiced etc), and the second-level items
  *  the actual nicks/users. Several roles are provided to access information about a nick.
  *
- *  Note that the nicks are not sorted in any way. Use a QSortFilterProxyModel to do that instead.
+ *  Note that the nicks are not sorted in any way. Use a FilteredNickModel instead.
  */
 class NickModel : public QAbstractItemModel {
   Q_OBJECT
 
   public:
  */
 class NickModel : public QAbstractItemModel {
   Q_OBJECT
 
   public:
+    enum NickModelRole { SortKeyRole = Qt::UserRole };
+
     NickModel(IrcChannel *channel = 0, QObject *parent = 0);
     virtual ~NickModel();
 
     NickModel(IrcChannel *channel = 0, QObject *parent = 0);
     virtual ~NickModel();
 
@@ -70,4 +73,16 @@ class NickModel : public QAbstractItemModel {
 
 };
 
 
 };
 
+//! This ProxyModel can be used on top of a NickModel in order to provide a sorted nicklist and to hide unused categories.
+class FilteredNickModel : public QSortFilterProxyModel {
+  Q_OBJECT
+
+  public:
+    FilteredNickModel(QObject *parent = 0);
+
+  protected:
+    virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
+
+};
+
 #endif
 #endif
index e8f515f..a815df9 100644 (file)
@@ -197,6 +197,7 @@ void MainWin::connectedToCore() {
   ui.actionDisconnectCore->setEnabled(true);
   ui.actionNetworkList->setEnabled(true);
   ui.bufferWidget->show();
   ui.actionDisconnectCore->setEnabled(true);
   ui.actionNetworkList->setEnabled(true);
   ui.bufferWidget->show();
+  statusBar()->showMessage(tr("Connected to core."));
 }
 
 void MainWin::disconnectedFromCore() {
 }
 
 void MainWin::disconnectedFromCore() {
@@ -206,7 +207,7 @@ void MainWin::disconnectedFromCore() {
   ui.actionNetworkList->setEnabled(false);
   ui.bufferWidget->hide();
   ui.actionConnectCore->setEnabled(true);
   ui.actionNetworkList->setEnabled(false);
   ui.bufferWidget->hide();
   ui.actionConnectCore->setEnabled(true);
-  //qDebug() << "mainwin disconnected";
+  statusBar()->showMessage(tr("Not connected to core."));
 }
 
 AbstractUiMsg *MainWin::layoutMsg(const Message &msg) {
 }
 
 AbstractUiMsg *MainWin::layoutMsg(const Message &msg) {
index 26a345d..d4eb827 100644 (file)
 #include "nickview.h"
 #include "nickmodel.h"
 
 #include "nickview.h"
 #include "nickmodel.h"
 
+#include <QHeaderView>
+#include <QSortFilterProxyModel>
 
 NickView::NickView(QWidget *parent) : QTreeView(parent) {
   setGeometry(0, 0, 30, 30);
   //setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
 
 
 NickView::NickView(QWidget *parent) : QTreeView(parent) {
   setGeometry(0, 0, 30, 30);
   //setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
 
+  setIndentation(10);
+  header()->hide();
+  header()->hideSection(1);
+  setAnimated(true);
+  setSortingEnabled(true);
+  sortByColumn(0, Qt::AscendingOrder);
 
 
+  filteredModel = new FilteredNickModel(this);
+  QTreeView::setModel(filteredModel);
 }
 
 NickView::~NickView() {
 }
 
 NickView::~NickView() {
@@ -35,5 +45,7 @@ NickView::~NickView() {
 }
 
 void NickView::setModel(NickModel *model) {
 }
 
 void NickView::setModel(NickModel *model) {
-  QTreeView::setModel(model);
+  filteredModel->setSourceModel(model);
+  expandAll();
+  
 }
 }
index 25a6b3e..2dcf180 100644 (file)
@@ -24,6 +24,8 @@
 #include <QTreeView>
 
 class NickModel;
 #include <QTreeView>
 
 class NickModel;
+class FilteredNickModel;
+class QSortFilterProxyModel;
 
 class NickView : public QTreeView {
   Q_OBJECT
 
 class NickView : public QTreeView {
   Q_OBJECT
@@ -35,7 +37,8 @@ class NickView : public QTreeView {
   public slots:
     void setModel(NickModel *model);
 
   public slots:
     void setModel(NickModel *model);
 
-
+  private:
+    QSortFilterProxyModel *filteredModel;
 
 };
 
 
 };