Enable auto-scrolling while selecting text in ChatViews
[quassel.git] / src / qtui / chatview.cpp
index 77a0976..d360d9d 100644 (file)
@@ -21,7 +21,6 @@
 #include <QGraphicsTextItem>
 #include <QScrollBar>
 
-#include "buffer.h"
 #include "chatlinemodelitem.h"
 #include "chatscene.h"
 #include "chatview.h"
 
 ChatView::ChatView(BufferId bufferId, QWidget *parent)
   : QGraphicsView(parent),
-    AbstractChatView()
+    AbstractChatView(),
+    _currentScaleFactor(1)
 {
   QList<BufferId> filterList;
   filterList.append(bufferId);
   MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
   init(filter);
-
 }
 
 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
   : QGraphicsView(parent),
-    AbstractChatView()
+    AbstractChatView(),
+    _currentScaleFactor(1)
 {
   init(filter);
 }
@@ -51,56 +51,85 @@ void ChatView::init(MessageFilter *filter) {
   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   setAlignment(Qt::AlignBottom);
   setInteractive(true);
-
-  _scene = new ChatScene(filter, filter->idString(), this);
-  connect(_scene, SIGNAL(heightChanged(qreal)), this, SLOT(sceneHeightChanged(qreal)));
+  //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
+  // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
+  setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
+  // setTransformationAnchor(QGraphicsView::NoAnchor);
+  setTransformationAnchor(QGraphicsView::AnchorViewCenter);
+
+  _scrollTimer.setInterval(100);
+  _scrollTimer.setSingleShot(true);
+  connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
+
+  _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 2, this); // see below: resizeEvent()
+  connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
+  connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
+  connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));
   setScene(_scene);
+  // installEventFilter(_scene);
 
-  connect(verticalScrollBar(), SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
-  connect(verticalScrollBar(), SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
 }
 
 void ChatView::resizeEvent(QResizeEvent *event) {
-  scene()->setWidth(event->size().width() - 2);  // FIXME figure out why we have to hardcode the -2 here
+  QGraphicsView::resizeEvent(event);
+
+  // we can reduce viewport updates if we scroll to the bottom allready at the beginning
   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
-}
 
-void ChatView::sceneHeightChanged(qreal h) {
-  Q_UNUSED(h)
-  bool scrollable = qAbs(verticalScrollBar()->value() - verticalScrollBar()->maximum()) <= 2; // be a bit tolerant here, also FIXME (why we need this?)
-  setSceneRect(scene()->sceneRect());
-  if(scrollable) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
+  // FIXME: without the hardcoded -4 Qt reserves space for a horizontal scrollbar even though it's disabled permanently.
+  // this does only occur on QtX11 (at least not on Qt for Mac OS). Seems like a Qt Bug.
+  scene()->updateForViewport(viewport()->width() - 4, viewport()->height());
+
+  _lastScrollbarPos = verticalScrollBar()->maximum();
+  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
 }
 
-void ChatView::setBufferForBacklogFetching(BufferId id) {
-  scene()->setBufferForBacklogFetching(id);
+void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
+  int y = (int)mapFromScene(scenePos).y();
+  _scrollOffset = 0;
+  if(y < 0)
+    _scrollOffset = y;
+  else if(y > height())
+    _scrollOffset = y - height();
+
+  if(_scrollOffset && !_scrollTimer.isActive())
+    _scrollTimer.start();
 }
 
-void ChatView::sliderPressed() {
-  verticalScrollbarChanged(verticalScrollBar()->value());
+void ChatView::scrollTimerTimeout() {
+  // scroll view
+  QAbstractSlider *vbar = verticalScrollBar();
+  if(_scrollOffset < 0 && vbar->value() > 0)
+    vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
+  else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
+    vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
 }
 
-void ChatView::sliderReleased() {
-  if(scene()->isFetchingBacklog()) scene()->setIsFetchingBacklog(false);
+void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
+  Q_UNUSED(chatLine)
+  QAbstractSlider *vbar = verticalScrollBar();
+  Q_ASSERT(vbar);
+  if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
+    vbar->setValue(vbar->maximum());
+  }
 }
 
 void ChatView::verticalScrollbarChanged(int newPos) {
-  Q_UNUSED(newPos);
-  if(!scene()->isBacklogFetchingEnabled()) return;
-
   QAbstractSlider *vbar = verticalScrollBar();
-  if(!vbar)
-    return;
-  if(vbar->isSliderDown()) {
-    /*
+  Q_ASSERT(vbar);
+
+  // check for backlog request
+  if(newPos < _lastScrollbarPos) {
     int relativePos = 100;
     if(vbar->maximum() - vbar->minimum() != 0)
       relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
-    scene()->setIsFetchingBacklog(relativePos < 20);
-    */
-    scene()->setIsFetchingBacklog(vbar->value() == vbar->minimum());
+
+    if(relativePos < 20) {
+      scene()->requestBacklog();
+    }
   }
+  _lastScrollbarPos = newPos;
 }
 
 MsgId ChatView::lastMsgId() const {
@@ -111,6 +140,23 @@ MsgId ChatView::lastMsgId() const {
   if(!model || model->rowCount() == 0)
     return MsgId();
 
-  
   return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
 }
+
+void ChatView::zoomIn() {
+    _currentScaleFactor *= 1.2;
+    scale(1.2, 1.2);
+    scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
+}
+
+void ChatView::zoomOut() {
+    _currentScaleFactor /= 1.2;
+    scale(1 / 1.2, 1 / 1.2);
+    scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
+}
+
+void ChatView::zoomNormal() {
+    scale(1/_currentScaleFactor, 1/_currentScaleFactor);
+    _currentScaleFactor = 1;
+    scene()->setWidth(viewport()->width() - 2);
+}