fixed slow buffer switches
authorMarcus Eggenberger <egs@quassel-irc.org>
Thu, 6 Mar 2008 12:43:59 +0000 (12:43 +0000)
committerMarcus Eggenberger <egs@quassel-irc.org>
Thu, 6 Mar 2008 12:43:59 +0000 (12:43 +0000)
src/client/treemodel.cpp
src/qtui/bufferwidget.cpp
src/qtui/mainwin.cpp
src/qtui/nicklistwidget.cpp
src/qtui/nicklistwidget.h
src/uisupport/bufferview.cpp
src/uisupport/nickview.cpp
src/uisupport/nickview.h
src/uisupport/nickviewfilter.cpp
src/uisupport/nickviewfilter.h
version.inc

index d66cc3a..ace9d3d 100644 (file)
@@ -528,7 +528,7 @@ void TreeModel::clear() {
 }
 
 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
-  // qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
+  qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
 }
 
 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
@@ -566,5 +566,5 @@ void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end
 }
 
 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
-  // qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
+  qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
 }
index 8831a28..e408e71 100644 (file)
@@ -48,8 +48,11 @@ void BufferWidget::setModel(BufferModel *bufferModel) {
     disconnect(_bufferModel, 0, this, 0);
   }
   _bufferModel = bufferModel;
-  connect(bufferModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
-         this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
+
+  if(bufferModel) {
+    connect(bufferModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
+           this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
+  }
 }
 
 void BufferWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
@@ -57,8 +60,11 @@ void BufferWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
     disconnect(_selectionModel, 0, this, 0);
   }
   _selectionModel = selectionModel;
-  connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
-         this, SLOT(currentChanged(QModelIndex, QModelIndex)));
+
+  if(selectionModel) {
+    connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
+           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
+  }
 }
 
 void BufferWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
index d700b64..1f168b6 100644 (file)
@@ -111,7 +111,7 @@ void MainWin::init() {
   disconnectedFromCore();  // Disable menus and stuff
   showCoreConnectionDlg(true); // autoconnect if appropriate
 
-  // attach the BufferWidget to the PropertyMapper
+  // attach the BufferWidget to the BufferModel and the default selection
   ui.bufferWidget->setModel(Client::bufferModel());
   ui.bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
 
@@ -208,6 +208,10 @@ void MainWin::setupNickWidget() {
 
   ui.menuViews->addAction(nickDock->toggleViewAction());
 
+  // attach the NickListWidget to the BufferModel and the default selection
+  nickListWidget->setModel(Client::bufferModel());
+  nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
+
   Client::bufferModel()->mapProperty(0, NetworkModel::BufferIdRole, nickListWidget, "currentBuffer");
 }
 
index 8219172..cb94161 100644 (file)
 
 NickListWidget::NickListWidget(QWidget *parent)
   : QWidget(parent),
-    _currentBuffer(0)
+    _bufferModel(0),
+    _selectionModel(0)
 {
   ui.setupUi(this);
 }
 
+void NickListWidget::setModel(BufferModel *bufferModel) {
+  if(_bufferModel) {
+    disconnect(_bufferModel, 0, this, 0);
+  }
+  
+  _bufferModel = bufferModel;
+
+  if(bufferModel) {
+    connect(bufferModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
+           this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
+  }
+}
+
+void NickListWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
+  if(_selectionModel) {
+    disconnect(_selectionModel, 0, this, 0);
+  }
+
+  _selectionModel = selectionModel;
+
+  if(selectionModel) {
+    connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
+           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
+  }
+}
+
 
-BufferId NickListWidget::currentBuffer() const {
-  return _currentBuffer;
+void NickListWidget::reset() {
+  NickView *nickView;
+  QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
+  while(iter != nickViews.end()) {
+    nickView = *iter;
+    iter = nickViews.erase(iter);
+    ui.stackedWidget->removeWidget(nickView);
+    nickView->deleteLater();
+  }
+}
+
+
+void NickListWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
+  Q_UNUSED(previous);
+  QVariant variant;
+
+  variant = current.data(NetworkModel::BufferIdRole);
+  if(!variant.isValid())
+    return;
+  setCurrentBuffer(variant.value<BufferId>());
 }
 
+
 void NickListWidget::setCurrentBuffer(BufferId bufferId) {
   QModelIndex bufferIdx = Client::networkModel()->bufferIndex(bufferId);
   
@@ -50,21 +96,36 @@ void NickListWidget::setCurrentBuffer(BufferId bufferId) {
     ui.stackedWidget->setCurrentWidget(nickViews.value(bufferId));
   } else {
     NickView *view = new NickView(this);
-    NickViewFilter *filter = new NickViewFilter(Client::networkModel());
+    NickViewFilter *filter = new NickViewFilter(bufferId, Client::networkModel());
+    filter->setObjectName("Buffer " + QString::number(bufferId.toInt()));
     view->setModel(filter);
     view->setRootIndex(filter->mapFromSource(bufferIdx));
+    view->expandAll();
     nickViews[bufferId] = view;
     ui.stackedWidget->addWidget(view);
     ui.stackedWidget->setCurrentWidget(view);
   }
 }
 
-void NickListWidget::reset() {
-  foreach(NickView *view, nickViews.values()) {
-    ui.stackedWidget->removeWidget(view);
-    view->deleteLater();
+
+void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
+  Q_ASSERT(model());
+  if(!parent.isValid()) {
+    // ok this means that whole networks are about to be removed
+    // we can't determine which buffers are affect, so we hope that all nets are removed
+    // this is the most common case (for example disconnecting from the core or terminating the clint)
+    reset();
+  } else {
+    // check if there are explicitly buffers removed
+    for(int i = start; i <= end; i++) {
+      QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
+      if(!variant.isValid())
+       continue;
+
+      BufferId bufferId = qVariantValue<BufferId>(variant);
+      removeBuffer(bufferId);
+    }
   }
-  nickViews.clear();
 }
 
 void NickListWidget::removeBuffer(BufferId bufferId) {
index a004f2a..3db7e08 100644 (file)
 
 #include <QHash>
 
+#include <QPointer>
+#include <QItemSelectionModel>
+
+#include "buffermodel.h"
+
 class Buffer;
 class NickView;
 
 class NickListWidget : public QWidget {
   Q_OBJECT
 
-  Q_PROPERTY(BufferId currentBuffer READ currentBuffer WRITE setCurrentBuffer); // FIXME BufferId
-
 public:
   NickListWidget(QWidget *parent = 0);
 
+  inline BufferModel *model() { return _bufferModel; }
+  void setModel(BufferModel *bufferModel);
+
+  inline QItemSelectionModel *selectionModel() const { return _selectionModel; }
+  void setSelectionModel(QItemSelectionModel *selectionModel);
+
 public slots:
-  BufferId currentBuffer() const;
-  void setCurrentBuffer(BufferId bufferId);
   void reset();
 
 protected:
   virtual QSize sizeHint() const;
 
+protected slots:
+//   virtual void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);
+//   virtual void commitData(QWidget *editor);
+  virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous);
+//   virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+//   virtual void editorDestroyed(QObject *editor);
+  virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
+//   virtual void rowsInserted(const QModelIndex &parent, int start, int end);
+//   virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
+
 private slots:
   void removeBuffer(BufferId bufferId);
+  void setCurrentBuffer(BufferId bufferId);
   
 private:
   Ui::NickListWidget ui;
   QHash<BufferId, NickView *> nickViews;
-  BufferId _currentBuffer;
-  
+
+  QPointer<BufferModel> _bufferModel;
+  QPointer<QItemSelectionModel> _selectionModel;
 };
 
 #endif
index 8c26056..6bc01d8 100644 (file)
@@ -287,6 +287,8 @@ void BufferView::wheelEvent(QWheelEvent* event) {
 
 
 QSize BufferView::sizeHint() const {
+  return QSize(120, 50);
+  
   if(!model())
     return QTreeView::sizeHint();
 
index 7e30cb7..78cb709 100644 (file)
@@ -30,7 +30,8 @@
 
 
 NickView::NickView(QWidget *parent)
-  : QTreeView(parent)
+  : QTreeView(parent),
+    _sizeHint(QTreeView::sizeHint())
 {
   setIndentation(10);
   setAnimated(true);
@@ -41,7 +42,7 @@ NickView::NickView(QWidget *parent)
   setContextMenuPolicy(Qt::CustomContextMenu);
 
   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
-          this, SLOT(showContextMenu(const QPoint&)));
+         this, SLOT(showContextMenu(const QPoint&)));
   connect(this, SIGNAL(activated( const QModelIndex& )),
           this, SLOT(startQuery( const QModelIndex& )));
 }
@@ -57,6 +58,7 @@ void NickView::init() {
     setColumnHidden(i, true);
 
   expandAll();
+  updateSizeHint();
 }
 
 void NickView::setModel(QAbstractItemModel *model) {
@@ -64,9 +66,21 @@ void NickView::setModel(QAbstractItemModel *model) {
   init();
 }
 
-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?
+void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
+  QTreeView::rowsInserted(parent, start, end);
+  if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent))
+    expand(parent);
+  updateSizeHint();
+}
+
+void NickView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
+  QTreeView::rowsAboutToBeRemoved(parent, start, end);
+  updateSizeHint();
+}
+
+void NickView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
+  QTreeView::dataChanged(topLeft, bottomRight);
+  updateSizeHint();
 }
 
 QString NickView::nickFromModelIndex(const QModelIndex & index) {
@@ -143,9 +157,9 @@ void NickView::executeCommand(const BufferInfo & bufferInfo, const QString & com
   Client::instance()->userInput(bufferInfo, command);
 }
 
-QSize NickView::sizeHint() const {
+void NickView::updateSizeHint() {
   if(!model())
-    return QTreeView::sizeHint();
+    return;
 
   int columnSize = 0;
   for(int i = 0; i < model()->columnCount(); i++) {
@@ -153,5 +167,9 @@ QSize NickView::sizeHint() const {
       columnSize += sizeHintForColumn(i);
   }
 
-  return QSize(columnSize, 50);
+  _sizeHint = QSize(columnSize, 50);
+}
+
+QSize NickView::sizeHint() const {
+  return _sizeHint;
 }
index 18a2bd6..cb263c4 100644 (file)
@@ -35,25 +35,30 @@ class QSortFilterProxyModel;
 class NickView : public QTreeView {
   Q_OBJECT
 
-  public:
-    NickView(QWidget *parent = 0);
-    virtual ~NickView();
+public:
+  NickView(QWidget *parent = 0);
+  virtual ~NickView();
 
-  protected:
-    virtual void rowsInserted(const QModelIndex &, int, int);
-    virtual QSize sizeHint() const;
-
-  public slots:
-    void setModel(QAbstractItemModel *model);
-    void init();
-    void showContextMenu(const QPoint & pos);
-    void startQuery(const QModelIndex & modelIndex);
-
-  private:
-    BufferInfo bufferInfoFromModelIndex(const QModelIndex & index);
-    QString nickFromModelIndex(const QModelIndex & index);
-    void executeCommand(const BufferInfo & bufferInfo, const QString & command);
+protected:
+  virtual void rowsInserted(const QModelIndex &parent, int start, int end);
+  virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+  virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
+  virtual QSize sizeHint() const;
 
+public slots:
+  void setModel(QAbstractItemModel *model);
+  void init();
+  void showContextMenu(const QPoint & pos);
+  void startQuery(const QModelIndex & modelIndex);
+  
+private:
+  QSize _sizeHint;
+  
+  void updateSizeHint();
+  BufferInfo bufferInfoFromModelIndex(const QModelIndex & index);
+  QString nickFromModelIndex(const QModelIndex & index);
+  void executeCommand(const BufferInfo & bufferInfo, const QString & command);
+  
 };
 
 
index 993bac0..7a6e704 100644 (file)
@@ -29,8 +29,9 @@
 /******************************************************************************************
  * NickViewFilter
  ******************************************************************************************/
-NickViewFilter::NickViewFilter(NetworkModel *parent)
-  : QSortFilterProxyModel(parent)
+NickViewFilter::NickViewFilter(const BufferId &bufferId, NetworkModel *parent)
+  : QSortFilterProxyModel(parent),
+    _bufferId(bufferId)
 {
   setSourceModel(parent);
   setDynamicSortFilter(true);
@@ -42,6 +43,12 @@ QVariant NickViewFilter::data(const QModelIndex &index, int role) const {
     return foreground(index);
   else
     return QSortFilterProxyModel::data(index, role);
+//   else {
+//     QVariant d = 
+//     if(role == 0)
+//       qDebug() << index << role << d;
+//     return d;
+//   }
 }
 
 QVariant NickViewFilter::foreground(const QModelIndex &index) const {
@@ -57,3 +64,9 @@ QVariant NickViewFilter::foreground(const QModelIndex &index) const {
   // FIXME:: make colors configurable;
   // FIXME: use the style interface instead of qsettings
 }
+
+
+bool NickViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
+  QModelIndex source_child = source_parent.child(source_row, 0);
+  return (sourceModel()->data(source_child, NetworkModel::BufferIdRole).value<BufferId>() == _bufferId);
+}
index 47da294..38b7ca3 100644 (file)
@@ -22,6 +22,7 @@
 #define NICKVIEWFILTER_H
 
 #include <QSortFilterProxyModel>
+#include "types.h"
 
 class NetworkModel;
 
@@ -31,9 +32,15 @@ class NickViewFilter : public QSortFilterProxyModel {
   Q_OBJECT
 
 public:
-  NickViewFilter(NetworkModel *parent = 0);
+  NickViewFilter(const BufferId &bufferId, NetworkModel *parent = 0);
   QVariant data(const QModelIndex &index, int role) const;
   QVariant foreground(const QModelIndex &index) const;
+
+protected:
+  virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
+
+private:
+  BufferId _bufferId;
 };
 
 #endif
index f7051ff..ab351ae 100644 (file)
@@ -4,8 +4,8 @@
 { using namespace Global;
 
   quasselVersion = "0.2.0-alpha3-pre";
-  quasselDate = "2008-03-03";
-  quasselBuild = 615;
+  quasselDate = "2008-03-06";
+  quasselBuild = 616;
 
   //! Minimum client build number the core needs
   clientBuildNeeded = 613;