Correctly resize ChatView height on disconnect
[quassel.git] / src / qtui / chatscene.cpp
index aeac420..681996a 100644 (file)
@@ -23,7 +23,6 @@
 #include <QGraphicsSceneMouseEvent>
 #include <QPersistentModelIndex>
 
-#include "buffer.h"
 #include "chatitem.h"
 #include "chatline.h"
 #include "chatlinemodelitem.h"
@@ -31,6 +30,7 @@
 #include "client.h"
 #include "clientbacklogmanager.h"
 #include "columnhandleitem.h"
+#include "messagefilter.h"
 #include "qtui.h"
 #include "qtuisettings.h"
 
@@ -38,14 +38,20 @@ const qreal minContentsWidth = 200;
 
 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
   : QGraphicsScene(parent),
-  _idString(idString),
-  _model(model)
+    _idString(idString),
+    _width(0),
+    _height(0),
+    _model(model),
+    _singleBufferScene(false),
+    _selectingItem(0),
+    _selectionStart(-1),
+    _isSelecting(false),
+    _fetchingBacklog(false)
 {
-  _width = 0;
-  _selectingItem = 0;
-  _isSelecting = false;
-  _selectionStart = -1;
-  _fetchingBacklog = false;
+  MessageFilter *filter = qobject_cast<MessageFilter*>(model);
+  if(filter) {
+    _singleBufferScene = filter->isSingleBufferFilter();
+  }
 
   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
 
@@ -106,6 +112,15 @@ void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
     _lines[i]->setRow(i);
   }
 
+  // update selection
+  if(_selectionStart >= 0) {
+    int offset = end - start + 1;
+    if(_selectionStart >= start) _selectionStart += offset;
+    if(_selectionEnd >= start) _selectionEnd += offset;
+    if(_firstSelectionRow >= start) _firstSelectionRow += offset;
+    if(_lastSelectionRow >= start) _lastSelectionRow += offset;
+  }
+
   if(h > 0) {
     _height += h;
     for(int i = end+1; i < _lines.count(); i++) {
@@ -125,6 +140,7 @@ void ChatScene::modelReset() {
   }
   _lines.clear();
   setSceneRect(QRectF(0, 0, _width, 0));
+  emit heightChanged(0);
 }
 
 void ChatScene::setWidth(qreal w) {
@@ -172,7 +188,7 @@ void ChatScene::setSelectingItem(ChatItem *item) {
 }
 
 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
-  _selectionStart = _selectionEnd = item->row();
+  _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
   _selectionStartCol = _selectionMinCol = item->column();
   _isSelecting = true;
   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
@@ -198,35 +214,40 @@ void ChatScene::updateSelection(const QPointF &pos) {
       _lines[l]->setSelected(true, minColumn);
     }
   }
-
-  if(curRow > _selectionEnd && curRow > _selectionStart) {  // select further towards bottom
-    for(int l = _selectionEnd + 1; l <= curRow; l++) {
+  int newstart = qMin(curRow, _firstSelectionRow);
+  int newend = qMax(curRow, _firstSelectionRow);
+  if(newstart < _selectionStart) {
+    for(int l = newstart; l < _selectionStart; l++)
       _lines[l]->setSelected(true, minColumn);
-    }
-  } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
-    for(int l = _selectionEnd; l < curRow; l++) {
-      _lines[l]->setSelected(false);
-    }
-  } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
-    for(int l = _selectionEnd; l > curRow; l--) {
+  }
+  if(newstart > _selectionStart) {
+    for(int l = _selectionStart; l < newstart; l++)
       _lines[l]->setSelected(false);
-    }
-  } else if(curRow < _selectionEnd && curRow < _selectionStart) {
-    for(int l = _selectionEnd - 1; l >= curRow; l--) {
+  }
+  if(newend > _selectionEnd) {
+    for(int l = _selectionEnd+1; l <= newend; l++)
       _lines[l]->setSelected(true, minColumn);
-    }
   }
-  _selectionEnd = curRow;
+  if(newend < _selectionEnd) {
+    for(int l = newend+1; l <= _selectionEnd; l++)
+      _lines[l]->setSelected(false);
+  }
+
+  _selectionStart = newstart;
+  _selectionEnd = newend;
+  _lastSelectionRow = curRow;
 
-  if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
+  if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
     _lines[curRow]->setSelected(false);
     _isSelecting = false;
+    Q_ASSERT(_selectingItem); // this seems to not always be true, but I have no idea why
+                              // adding this assert to make sure the occasional segfault is caused by this
     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
   }
 }
 
 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
-  if(_isSelecting && event->buttons() & Qt::LeftButton) {
+  if(_isSelecting && event->buttons() == Qt::LeftButton) {
     updateSelection(event->scenePos());
     event->accept();
   } else {
@@ -235,7 +256,7 @@ void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
-  if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
+  if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
       _lines[l]->setSelected(false);
     }
@@ -247,13 +268,8 @@ void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
-  if(_isSelecting) {
-#   ifdef Q_WS_X11
-      QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
-#   endif
-//# else
-      QApplication::clipboard()->setText(selectionToString());
-//# endif
+  if(_isSelecting && !event->buttons() & Qt::LeftButton) {
+    putToClipboard(selectionToString());
     _isSelecting = false;
     event->accept();
   } else {
@@ -261,6 +277,16 @@ void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
   }
 }
 
+void ChatScene::putToClipboard(const QString &selection) {
+  // TODO Configure clipboards
+#   ifdef Q_WS_X11
+  QApplication::clipboard()->setText(selection, QClipboard::Selection);
+#   endif
+//# else
+  QApplication::clipboard()->setText(selection);
+//# endif
+}
+
 //!\brief Convert current selection to human-readable string.
 QString ChatScene::selectionToString() const {
   //TODO Make selection format configurable!