566de0d8c2aab235c81da54495f3e231a01a5e05
[quassel.git] / src / qtui / chatwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "util.h"
22 #include "chatwidget.h"
23 #include "chatline-old.h"
24 #include "qtui.h"
25 #include "uisettings.h"
26 #include "client.h"
27 #include "buffer.h"
28 #include "clientbacklogmanager.h"
29
30 ChatWidget::ChatWidget(QWidget *parent)
31   : QAbstractScrollArea(parent),
32     lastBacklogOffset(0),
33     lastBacklogSize(0)
34 {
35   //setAutoFillBackground(false);
36   //QPalette palette;
37   //palette.setColor(backgroundRole(), QColor(0, 0, 0, 50));
38   //setPalette(palette);
39   scrollTimer = new QTimer(this);
40   scrollTimer->setSingleShot(false);
41   scrollTimer->setInterval(100);
42   // setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
43   setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
44   setMinimumSize(QSize(20,20));
45   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
47   bottomLine = -1;
48   height = 0;
49   ycoords.append(0);
50   pointerPosition = QPoint(0,0);
51   connect(verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(scrollBarAction(int)));
52   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollBarValChanged(int)));
53 }
54
55 void ChatWidget::init(BufferId id) {
56   bufferId = id;
57   setBackgroundRole(QPalette::Base);
58   setFont(QFont("Fixed"));
59   UiSettings s;
60   QVariant tsDef = s.value("DefaultTimestampColumnWidth", 90);
61   QVariant senderDef = s.value("DefaultSenderColumnWidth", 100);
62   tsWidth = s.value(QString("%1/TimestampColumnWidth").arg(bufferId.toInt()), tsDef).toInt();
63   senderWidth = s.value(QString("%1/SenderColumnWidth").arg(bufferId.toInt()), senderDef).toInt();
64   computePositions();
65   adjustScrollBar();
66   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
67   //verticalScrollBar()->setPageStep(viewport()->height());
68   //verticalScrollBar()->setSingleStep(20);
69   //verticalScrollBar()->setMinimum(0);
70   //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
71
72   // setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
73   setMouseTracking(true);
74   mouseMode = Normal;
75   selectionMode = NoSelection;
76   connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
77
78   if(bufferId.isValid())
79     connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(viewportChanged(int)));
80 }
81
82 ChatWidget::~ChatWidget() {
83   //qDebug() << "destroying chatwidget" << bufferName;
84   //foreach(ChatLineOld *l, lines) {
85   //  delete l;
86   //}
87   UiSettings s;
88   s.setValue("DefaultTimestampColumnWidth", tsWidth);  // FIXME stupid dirty quicky
89   s.setValue("DefaultSenderColumnWidth", senderWidth);
90   s.setValue(QString("%1/TimestampColumnWidth").arg(bufferId.toInt()), tsWidth);
91   s.setValue(QString("%1/SenderColumnWidth").arg(bufferId.toInt()), senderWidth);
92 }
93
94 QSize ChatWidget::minimumSizeHint() const {
95   return QSize(20, 20);
96 }
97
98 QSize ChatWidget::sizeHint() const {
99   return QSize(400, 100);
100 }
101
102 // QSize ChatWidget::sizeHint() const {
103 //   //qDebug() << size();
104 //   return size();
105 // }
106
107 void ChatWidget::adjustScrollBar() {
108   verticalScrollBar()->setPageStep(viewport()->height());
109   verticalScrollBar()->setSingleStep(20);
110   verticalScrollBar()->setMinimum(0);
111   verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
112   //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
113   //if(bottomLine < 0) {
114   //  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
115   //} else {
116     //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
117     //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
118   //}
119 }
120
121 void ChatWidget::scrollBarValChanged(int /*val*/) {
122   /*
123   if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
124   else {
125     int bot = val + viewport()->height();
126     int line = yToLineIdx(bot);
127     //bottomLine = line;
128   }
129   */
130 }
131
132 void ChatWidget::scrollBarAction(int action) {
133   switch(action) {
134     case QScrollBar::SliderSingleStepAdd:
135       // More elaborate. But what with loooong lines?
136       // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
137       break;
138     case QScrollBar::SliderSingleStepSub:
139       //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
140       break;
141
142   }
143
144 }
145
146 void ChatWidget::handleScrollTimer() {
147   if(mouseMode == MarkText || mouseMode == MarkLines) {
148     if(pointerPosition.y() > viewport()->height()) {
149       verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
150       handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
151     } else if(pointerPosition.y() < 0) {
152       verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
153       handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
154     }
155   }
156 }
157
158 void ChatWidget::ensureVisible(int line) {
159   int top = verticalScrollBar()->value();
160   int bot = top + viewport()->height();
161   if(ycoords[line+1] > bot) {
162     verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
163   } else if(ycoords[line] < top) {
164     verticalScrollBar()->setValue((int)ycoords[line]);
165   }
166
167 }
168
169 void ChatWidget::clear() {
170   //contents->clear();
171 }
172
173 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
174   ChatLineOld *line = dynamic_cast<ChatLineOld*>(msg);
175   Q_ASSERT(line);
176   prependChatLine(line);
177 }
178
179 void ChatWidget::prependChatLine(ChatLineOld *line) {
180   qreal h = line->layout(tsWidth, senderWidth, textWidth);
181   for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
182   ycoords.insert(1, h);
183   lines.prepend(line);
184   height += h;
185   // Fix all variables containing line numbers
186   dragStartLine ++;
187   curLine ++;
188   selectionStart ++; selectionEnd ++;
189   adjustScrollBar();
190   verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
191   viewport()->update();
192 }
193
194 void ChatWidget::prependChatLines(QList<ChatLineOld *> clist) {
195   QList<qreal> tmpy; tmpy.append(0);
196   qreal h = 0;
197   foreach(ChatLineOld *l, clist) {
198     h += l->layout(tsWidth, senderWidth, textWidth);
199     tmpy.append(h);
200   }
201   ycoords.removeFirst();
202   for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
203   ycoords = tmpy + ycoords;
204   lines = clist + lines;
205   height += h;
206   // Fix all variables containing line numbers
207   int i = clist.count();
208   dragStartLine += i;
209   curLine += i;
210   selectionStart += i; selectionEnd += i; //? selectionEnd += i;
211   //if(bottomLine >= 0) bottomLine += i;
212   adjustScrollBar();
213   //verticalScrollBar()->setPageStep(viewport()->height());
214   //verticalScrollBar()->setSingleStep(20);
215   //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
216   verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
217   viewport()->update();
218 }
219
220 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
221   ChatLineOld *line = dynamic_cast<ChatLineOld*>(msg);
222   Q_ASSERT(line);
223   appendChatLine(line);
224 }
225
226 void ChatWidget::appendChatLine(ChatLineOld *line) {
227   qreal h = line->layout(tsWidth, senderWidth, textWidth);
228   ycoords.append(h + ycoords[ycoords.count() - 1]);
229   height += h;
230   bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
231   adjustScrollBar();
232   if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
233   lines.append(line);
234   viewport()->update();
235 }
236
237
238 void ChatWidget::appendChatLines(QList<ChatLineOld *> list) {
239   foreach(ChatLineOld *line, list) {
240     qreal h = line->layout(tsWidth, senderWidth, textWidth);
241     ycoords.append(h + ycoords[ycoords.count() - 1]);
242     height += h;
243     lines.append(line);
244   }
245   bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
246   adjustScrollBar();
247   if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
248   viewport()->update();
249 }
250
251 void ChatWidget::setContents(QList<ChatLineOld *> list) {
252   ycoords.clear();
253   ycoords.append(0);
254   height = 0;
255   lines.clear();
256   appendChatLines(list);
257 }
258
259 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
260 void ChatWidget::computePositions() {
261   senderX = tsWidth + QtUi::style()->sepTsSender();
262   textX = senderX + senderWidth + QtUi::style()->sepSenderText();
263   tsGrabPos = tsWidth + (int)QtUi::style()->sepTsSender()/2;
264   senderGrabPos = senderX + senderWidth + (int)QtUi::style()->sepSenderText()/2;
265   textWidth = viewport()->size().width() - textX;
266 }
267
268 void ChatWidget::resizeEvent(QResizeEvent *event) {
269   //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
270   /*if(event->oldSize().isValid())*/
271   //contents->setWidth(event->size().width());
272   //setAlignment(Qt::AlignBottom);
273   if(event->size() != event->oldSize()) {
274     computePositions();
275     layout();
276   }
277   //adjustScrollBar();
278   //qDebug() << viewport()->size() << viewport()->height();
279   //QAbstractScrollArea::resizeEvent(event);
280   //qDebug() << viewport()->size() << viewport()->geometry();
281 }
282
283 void ChatWidget::paintEvent(QPaintEvent *event) {
284   QPainter painter(viewport());
285
286   //qDebug() <<  verticalScrollBar()->value();
287   painter.translate(0, -verticalScrollBar()->value());
288   int top = event->rect().top() + verticalScrollBar()->value();
289   int bot = top + event->rect().height();
290   int idx = yToLineIdx(top);
291   if(idx < 0) return;
292   for(int i = idx; i < lines.count() ; i++) {
293     lines[i]->draw(&painter, QPointF(0, ycoords[i]));
294     if(ycoords[i+1] > bot) return;
295   }
296 }
297
298 //!\brief Layout the widget.
299 void ChatWidget::layout() {
300   // TODO fix scrollbars
301   //int botLine = yToLineIdx(verticalScrollBar()->value() + 
302   for(int i = 0; i < lines.count(); i++) {
303     qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
304     ycoords[i+1] = h + ycoords[i];
305   }
306   height = ycoords[ycoords.count()-1];
307   adjustScrollBar();
308   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
309   viewport()->update();
310 }
311
312 int ChatWidget::yToLineIdx(qreal y) {
313   if(y >= ycoords[ycoords.count()-1]) return ycoords.count()-2;
314   if(ycoords.count() <= 1) return 0;
315   int uidx = 0;
316   int oidx = ycoords.count() - 1;
317   int idx;
318   while(1) {
319     if(uidx == oidx - 1) return uidx;
320     idx = (uidx + oidx) / 2;
321     if(ycoords[idx] > y) oidx = idx;
322     else uidx = idx;
323   }
324 }
325
326 void ChatWidget::mousePressEvent(QMouseEvent *event) {
327   if(lines.isEmpty()) return;
328   QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
329   if(event->button() == Qt::LeftButton) {
330     dragStartPos = pos;
331     dragStartMode = Normal;
332     switch(mouseMode) {
333       case Normal:
334         if(mousePos == OverTsSep) {
335           dragStartMode = DragTsSep;
336           setCursor(Qt::ClosedHandCursor);
337         } else if(mousePos == OverTextSep) {
338           dragStartMode = DragTextSep;
339           setCursor(Qt::ClosedHandCursor);
340         } else {
341           dragStartLine = yToLineIdx(pos.y());
342           dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
343         }
344         mouseMode = Pressed;
345         break;
346       default:
347         break;
348     }
349   }
350 }
351
352 void ChatWidget::mouseDoubleClickEvent(QMouseEvent *event) {
353   // dirty and fast hack to make http:// urls klickable
354   if(lines.isEmpty())
355     return;
356
357   QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
358   int x = pos.x();
359   int y = pos.y();
360   int l = yToLineIdx(y);
361   if(lines.count() <= l)
362     return;
363
364   ChatLineOld *line = lines[l];
365   QString text = line->text();
366   int cursorAt = qMax(0, line->posToCursor(QPointF(x, y - ycoords[l])) - 1);
367
368   int start = 0;
369   if(cursorAt > 0) {
370     for(int i = cursorAt; i > 0; i--) {
371       if(text[i] == ' ') {
372         start = i + 1;
373         break;
374       }
375     }
376   }
377
378   int end = text.indexOf(" ", start);
379   int len = -1;
380   if(end != -1) {
381     len = end - start;
382   }
383   QString word = text.mid(start, len);
384   if(word.startsWith("http://")) {
385     QDesktopServices::openUrl(QUrl(word));
386   }
387   
388 }
389
390 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
391   //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
392
393   if(event->button() == Qt::LeftButton) {
394     dragStartPos = QPoint();
395     if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
396     else setCursor(Qt::ArrowCursor);
397
398     switch(mouseMode) {
399       case Pressed:
400         mouseMode = Normal;
401         clearSelection();
402         break;
403       case MarkText:
404         mouseMode = Normal;
405         selectionMode = TextSelected;
406         selectionLine = dragStartLine;
407         selectionStart = qMin(dragStartCursor, curCursor);
408         selectionEnd = qMax(dragStartCursor, curCursor);
409         // TODO Make X11SelectionMode configurable!
410         QApplication::clipboard()->setText(selectionToString());
411         break;
412       case MarkLines:
413         mouseMode = Normal;
414         selectionMode = LinesSelected;
415         selectionStart = qMin(dragStartLine, curLine);
416         selectionEnd = qMax(dragStartLine, curLine);
417         // TODO Make X11SelectionMode configurable!
418         QApplication::clipboard()->setText(selectionToString());
419         break;
420       default:
421         mouseMode = Normal;
422     }
423   }
424 }
425
426 //!\brief React to mouse movements over the ChatWidget.
427 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
428  * such as changing column widths, marking text or initiating drag & drop.
429  */
430 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
431   QPoint pos = event->pos(); pointerPosition = pos;
432   // Scroll if mouse pointer leaves widget while dragging
433   if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
434     if(!scrollTimer->isActive()) {
435       scrollTimer->start();
436     }
437   } else {
438     if(scrollTimer->isActive()) {
439       scrollTimer->stop();
440     }
441   }
442   handleMouseMoveEvent(pos);
443 }
444
445 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
446   // FIXME
447   if(lines.count() <= 0) return;
448   // Set some basic properties of the current position
449   QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
450   int x = pos.x();
451   int y = pos.y();
452   //MousePos oldpos = mousePos;
453   if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
454   else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
455   else mousePos = None;
456
457   // Pass 1: Do whatever we can before switching mouse mode (if at all).
458   switch(mouseMode) {
459     // No special mode. Set mouse cursor if appropriate.
460     case Normal:
461     {
462       //if(oldpos != mousePos) {
463       if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
464       else {
465         int l = yToLineIdx(y);
466         int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
467         if(c >= 0 && lines[l]->isUrl(c)) {
468           setCursor(Qt::PointingHandCursor);
469         } else {
470           setCursor(Qt::ArrowCursor);
471         }
472       }
473     }
474       break;
475     // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
476     case Pressed:
477       if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
478         // Moving a column separator?
479         if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
480         else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
481         // Nope. Check if we are over a selection to start drag & drop.
482         else if(dragStartMode == Normal) {
483           bool dragdrop = false;
484           if(selectionMode == TextSelected) {
485             int l = yToLineIdx(y);
486             if(selectionLine == l) {
487               int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
488               if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
489             }
490           } else if(selectionMode == LinesSelected) {
491             int l = yToLineIdx(y);
492             if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
493           }
494           // Ok, so just start drag & drop if appropriate.
495           if(dragdrop) {
496             QDrag *drag = new QDrag(this);
497             QMimeData *mimeData = new QMimeData;
498             mimeData->setText(selectionToString());
499             drag->setMimeData(mimeData);
500             drag->start();
501             mouseMode = Normal;
502           // Otherwise, clear the selection and start text marking!
503           } else {
504             setCursor(Qt::ArrowCursor);
505             clearSelection();
506             if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
507             else mouseMode = MarkText;
508           }
509         }
510       }
511       break;
512     case DragTsSep:
513       break;
514     case DragTextSep:
515       break;
516     default:
517       break;
518   }
519   // Pass 2: Some mouse modes need work after being set...
520   if(mouseMode == DragTsSep && x < size().width() - QtUi::style()->sepSenderText() - senderWidth - 10) {
521     // Drag first column separator
522     int foo = QtUi::style()->sepTsSender()/2;
523     tsWidth = qMax(x, foo) - foo;
524     computePositions();
525     layout();
526   } else if(mouseMode == DragTextSep && x < size().width() - 10) {
527     // Drag second column separator
528     int foo = tsWidth + QtUi::style()->sepTsSender() + QtUi::style()->sepSenderText()/2;
529     senderWidth = qMax(x, foo) - foo;
530     computePositions();
531     layout();
532   } else if(mouseMode == MarkText) {
533     // Change currently marked text
534     curLine = yToLineIdx(y);
535     int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
536     if(curLine == dragStartLine && c >= 0) {
537       if(c != curCursor) {
538         curCursor = c;
539         lines[curLine]->setSelection(ChatLineOld::Partial, dragStartCursor, c);
540         viewport()->update();
541       }
542     } else {
543       mouseMode = MarkLines;
544       selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
545       for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLineOld::Full);
546       viewport()->update();
547     }
548   } else if(mouseMode == MarkLines) {
549     // Line marking
550     int l = yToLineIdx(y);
551     if(l != curLine) {
552       selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
553       if(curLine < 0) {
554         Q_ASSERT(selectionStart == selectionEnd);
555         lines[l]->setSelection(ChatLineOld::Full);
556       } else {
557         if(curLine < selectionStart) {
558           for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLineOld::None);
559         } else if(curLine > selectionEnd) {
560           for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLineOld::None);
561         } else if(selectionStart < curLine && l < curLine) {
562           for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLineOld::Full);
563         } else if(curLine < selectionEnd && l > curLine) {
564           for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLineOld::Full);
565         }
566       }
567       curLine = l;
568       //ensureVisible(l);
569       viewport()->update();
570     }
571   }
572 }
573
574 //!\brief Clear current text selection.
575 void ChatWidget::clearSelection() {
576   if(selectionMode == TextSelected) {
577     lines[selectionLine]->setSelection(ChatLineOld::None);
578   } else if(selectionMode == LinesSelected) {
579     for(int i = selectionStart; i <= selectionEnd; i++) {
580       lines[i]->setSelection(ChatLineOld::None);
581     }
582   }
583   selectionMode = NoSelection;
584   viewport()->update();
585 }
586
587 //!\brief Convert current selection to human-readable string.
588 QString ChatWidget::selectionToString() {
589   //TODO Make selection format configurable!
590   if(selectionMode == NoSelection) return "";
591   if(selectionMode == LinesSelected) {
592     QString result;
593     for(int l = selectionStart; l <= selectionEnd; l++) {
594       result += QString("[%1] %2 %3\n").arg(lines[l]->timestamp().toLocalTime().toString("hh:mm:ss"))
595   .        arg(lines[l]->sender()).arg(lines[l]->text());
596     }
597     return result;
598   }
599   // selectionMode == TextSelected
600   return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);
601 }
602
603 void ChatWidget::viewportChanged(int newPos) {
604   const int REQUEST_COUNT = 50;
605   QAbstractSlider *vbar = verticalScrollBar();
606   if(!vbar)
607     return;
608
609   int relativePos = 100;
610   if(vbar->maximum() - vbar->minimum() != 0)
611     relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
612
613   if(relativePos < 20) {
614     Buffer *buffer = Client::buffer(bufferId);
615     Q_CHECK_PTR(buffer);
616     if(buffer->contents().isEmpty())
617       return;
618     MsgId msgId = buffer->contents().first()->msgId();
619     if(!lastBacklogOffset.isValid() || msgId < lastBacklogOffset && lastBacklogSize + REQUEST_COUNT <= buffer->contents().count()) {
620       Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
621       lastBacklogOffset = msgId;
622       lastBacklogSize = buffer->contents().size();
623     }
624   }
625 }