Started to reorganize the Buffer{Model|View|Filter}. Mostly cleanup at the moment.
[quassel.git] / src / qtui / chatwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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 "style.h"
23 #include "chatwidget.h"
24 #include "chatline.h"
25
26
27 ChatWidget::ChatWidget(QWidget *parent) : QAbstractScrollArea(parent) {
28   //setAutoFillBackground(false);
29   //QPalette palette;
30   //palette.setColor(backgroundRole(), QColor(0, 0, 0, 50));
31   //setPalette(palette);
32   scrollTimer = new QTimer(this);
33   scrollTimer->setSingleShot(false);
34   scrollTimer->setInterval(100);
35   setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
36   setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
37   setMinimumSize(QSize(400,400));
38   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
39   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
40   bottomLine = -1;
41   height = 0;
42   ycoords.append(0);
43   pointerPosition = QPoint(0,0);
44   connect(verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(scrollBarAction(int)));
45   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollBarValChanged(int)));
46 }
47
48 void ChatWidget::init(QString netname, QString bufname) {
49   networkName = netname;
50   bufferName = bufname;
51   setBackgroundRole(QPalette::Base);
52   setFont(QFont("Fixed"));
53   tsWidth = 90;
54   senderWidth = 100;
55   computePositions();
56   adjustScrollBar();
57   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
58   //verticalScrollBar()->setPageStep(viewport()->height());
59   //verticalScrollBar()->setSingleStep(20);
60   //verticalScrollBar()->setMinimum(0);
61   //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
62
63   setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
64   setMouseTracking(true);
65   mouseMode = Normal;
66   selectionMode = NoSelection;
67   connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
68 }
69
70 ChatWidget::~ChatWidget() {
71   //qDebug() << "destroying chatwidget" << bufferName;
72   //foreach(ChatLine *l, lines) {
73   //  delete l;
74   //}
75 }
76
77 QSize ChatWidget::sizeHint() const {
78   //qDebug() << size();
79   return size();
80 }
81
82 void ChatWidget::adjustScrollBar() {
83   verticalScrollBar()->setPageStep(viewport()->height());
84   verticalScrollBar()->setSingleStep(20);
85   verticalScrollBar()->setMinimum(0);
86   verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
87   //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
88   //if(bottomLine < 0) {
89   //  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
90   //} else {
91     //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
92     //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
93   //}
94 }
95
96 void ChatWidget::scrollBarValChanged(int /*val*/) {
97   /*
98   if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
99   else {
100     int bot = val + viewport()->height();
101     int line = yToLineIdx(bot);
102     //bottomLine = line;
103   }
104   */
105 }
106
107 void ChatWidget::scrollBarAction(int action) {
108   switch(action) {
109     case QScrollBar::SliderSingleStepAdd:
110       // More elaborate. But what with loooong lines?
111       // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
112       break;
113     case QScrollBar::SliderSingleStepSub:
114       //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
115       break;
116
117   }
118
119 }
120
121 void ChatWidget::handleScrollTimer() {
122   if(mouseMode == MarkText || mouseMode == MarkLines) {
123     if(pointerPosition.y() > viewport()->height()) {
124       verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
125       handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
126     } else if(pointerPosition.y() < 0) {
127       verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
128       handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
129     }
130   }
131 }
132
133 void ChatWidget::ensureVisible(int line) {
134   int top = verticalScrollBar()->value();
135   int bot = top + viewport()->height();
136   if(ycoords[line+1] > bot) {
137     verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
138   } else if(ycoords[line] < top) {
139     verticalScrollBar()->setValue((int)ycoords[line]);
140   }
141
142 }
143
144 void ChatWidget::clear() {
145   //contents->clear();
146 }
147
148 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
149   ChatLine *line = dynamic_cast<ChatLine*>(msg);
150   Q_ASSERT(line);
151   prependChatLine(line);
152 }
153
154 void ChatWidget::prependChatLine(ChatLine *line) {
155   qreal h = line->layout(tsWidth, senderWidth, textWidth);
156   for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
157   ycoords.insert(1, h);
158   lines.prepend(line);
159   height += h;
160   // Fix all variables containing line numbers
161   dragStartLine ++;
162   curLine ++;
163   selectionStart ++; selectionEnd ++;
164   adjustScrollBar();
165   verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
166   viewport()->update();
167 }
168
169 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
170   QList<qreal> tmpy; tmpy.append(0);
171   qreal h = 0;
172   foreach(ChatLine *l, clist) {
173     h += l->layout(tsWidth, senderWidth, textWidth);
174     tmpy.append(h);
175   }
176   ycoords.removeFirst();
177   for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
178   ycoords = tmpy + ycoords;
179   lines = clist + lines;
180   height += h;
181   // Fix all variables containing line numbers
182   int i = clist.count();
183   dragStartLine += i;
184   curLine += i;
185   selectionStart += i; selectionEnd += i; //? selectionEnd += i;
186   //if(bottomLine >= 0) bottomLine += i;
187   adjustScrollBar();
188   //verticalScrollBar()->setPageStep(viewport()->height());
189   //verticalScrollBar()->setSingleStep(20);
190   //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
191   verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
192   viewport()->update();
193 }
194
195 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
196   ChatLine *line = dynamic_cast<ChatLine*>(msg);
197   Q_ASSERT(line);
198   appendChatLine(line);
199 }
200
201 void ChatWidget::appendChatLine(ChatLine *line) {
202   qreal h = line->layout(tsWidth, senderWidth, textWidth);
203   ycoords.append(h + ycoords[ycoords.count() - 1]);
204   height += h;
205   bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
206   adjustScrollBar();
207   if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
208   lines.append(line);
209   viewport()->update();
210 }
211
212
213 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
214   foreach(ChatLine *line, list) {
215     qreal h = line->layout(tsWidth, senderWidth, textWidth);
216     ycoords.append(h + ycoords[ycoords.count() - 1]);
217     height += h;
218     lines.append(line);
219   }
220   bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
221   adjustScrollBar();
222   if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
223   viewport()->update();
224 }
225
226 void ChatWidget::setContents(QList<ChatLine *> list) {
227   ycoords.clear();
228   ycoords.append(0);
229   height = 0;
230   lines.clear();
231   appendChatLines(list);
232 }
233
234 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
235 void ChatWidget::computePositions() {
236   senderX = tsWidth + Style::sepTsSender();
237   textX = senderX + senderWidth + Style::sepSenderText();
238   tsGrabPos = tsWidth + (int)Style::sepTsSender()/2;
239   senderGrabPos = senderX + senderWidth + (int)Style::sepSenderText()/2;
240   textWidth = viewport()->size().width() - textX;
241 }
242
243 void ChatWidget::resizeEvent(QResizeEvent *event) {
244   //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
245   /*if(event->oldSize().isValid())*/
246   //contents->setWidth(event->size().width());
247   //setAlignment(Qt::AlignBottom);
248   if(event->size().width() != event->oldSize().width()) {
249     computePositions();
250     layout();
251   }
252   //adjustScrollBar();
253   //qDebug() << viewport()->size() << viewport()->height();
254   //QAbstractScrollArea::resizeEvent(event);
255   //qDebug() << viewport()->size() << viewport()->geometry();
256 }
257
258 void ChatWidget::paintEvent(QPaintEvent *event) {
259   QPainter painter(viewport());
260
261   //qDebug() <<  verticalScrollBar()->value();
262   painter.translate(0, -verticalScrollBar()->value());
263   int top = event->rect().top() + verticalScrollBar()->value();
264   int bot = top + event->rect().height();
265   int idx = yToLineIdx(top);
266   if(idx < 0) return;
267   for(int i = idx; i < lines.count() ; i++) {
268     lines[i]->draw(&painter, QPointF(0, ycoords[i]));
269     if(ycoords[i+1] > bot) return;
270   }
271 }
272
273 //!\brief Layout the widget.
274 void ChatWidget::layout() {
275   // TODO fix scrollbars
276   //int botLine = yToLineIdx(verticalScrollBar()->value() + 
277   for(int i = 0; i < lines.count(); i++) {
278     qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
279     ycoords[i+1] = h + ycoords[i];
280   }
281   height = ycoords[ycoords.count()-1];
282   adjustScrollBar();
283   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
284   viewport()->update();
285 }
286
287 int ChatWidget::yToLineIdx(qreal y) {
288   if(y >= ycoords[ycoords.count()-1]) return ycoords.count()-2;
289   if(ycoords.count() <= 1) return 0;
290   int uidx = 0;
291   int oidx = ycoords.count() - 1;
292   int idx;
293   while(1) {
294     if(uidx == oidx - 1) return uidx;
295     idx = (uidx + oidx) / 2;
296     if(ycoords[idx] > y) oidx = idx;
297     else uidx = idx;
298   }
299 }
300
301 void ChatWidget::mousePressEvent(QMouseEvent *event) {
302   if(lines.isEmpty()) return;
303   QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
304   if(event->button() == Qt::LeftButton) {
305     dragStartPos = pos;
306     dragStartMode = Normal;
307     switch(mouseMode) {
308       case Normal:
309         if(mousePos == OverTsSep) {
310           dragStartMode = DragTsSep;
311           setCursor(Qt::ClosedHandCursor);
312         } else if(mousePos == OverTextSep) {
313           dragStartMode = DragTextSep;
314           setCursor(Qt::ClosedHandCursor);
315         } else {
316           dragStartLine = yToLineIdx(pos.y());
317           dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
318         }
319         mouseMode = Pressed;
320         break;
321       default:
322         break;
323     }
324   }
325 }
326
327 void ChatWidget::mouseDoubleClickEvent(QMouseEvent * /*event*/) {
328
329
330
331 }
332
333 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
334   //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
335
336   if(event->button() == Qt::LeftButton) {
337     dragStartPos = QPoint();
338     if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
339     else setCursor(Qt::ArrowCursor);
340
341     switch(mouseMode) {
342       case Pressed:
343         mouseMode = Normal;
344         clearSelection();
345         break;
346       case MarkText:
347         mouseMode = Normal;
348         selectionMode = TextSelected;
349         selectionLine = dragStartLine;
350         selectionStart = qMin(dragStartCursor, curCursor);
351         selectionEnd = qMax(dragStartCursor, curCursor);
352         // TODO Make X11SelectionMode configurable!
353         QApplication::clipboard()->setText(selectionToString());
354         break;
355       case MarkLines:
356         mouseMode = Normal;
357         selectionMode = LinesSelected;
358         selectionStart = qMin(dragStartLine, curLine);
359         selectionEnd = qMax(dragStartLine, curLine);
360         // TODO Make X11SelectionMode configurable!
361         QApplication::clipboard()->setText(selectionToString());
362         break;
363       default:
364         mouseMode = Normal;
365     }
366   }
367 }
368
369 //!\brief React to mouse movements over the ChatWidget.
370 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
371  * such as changing column widths, marking text or initiating drag & drop.
372  */
373 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
374   QPoint pos = event->pos(); pointerPosition = pos;
375   // Scroll if mouse pointer leaves widget while dragging
376   if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
377     if(!scrollTimer->isActive()) {
378       scrollTimer->start();
379     }
380   } else {
381     if(scrollTimer->isActive()) {
382       scrollTimer->stop();
383     }
384   }
385   handleMouseMoveEvent(pos);
386 }
387
388 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
389   // FIXME
390   if(lines.count() <= 0) return;
391   // Set some basic properties of the current position
392   QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
393   int x = pos.x();
394   int y = pos.y();
395   //MousePos oldpos = mousePos;
396   if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
397   else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
398   else mousePos = None;
399
400   // Pass 1: Do whatever we can before switching mouse mode (if at all).
401   switch(mouseMode) {
402     // No special mode. Set mouse cursor if appropriate.
403     case Normal:
404     {
405       //if(oldpos != mousePos) {
406       if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
407       else {
408         int l = yToLineIdx(y);
409         int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
410         if(c >= 0 && lines[l]->isUrl(c)) {
411           setCursor(Qt::PointingHandCursor);
412         } else {
413           setCursor(Qt::ArrowCursor);
414         }
415       }
416     }
417       break;
418     // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
419     case Pressed:
420       if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
421         // Moving a column separator?
422         if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
423         else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
424         // Nope. Check if we are over a selection to start drag & drop.
425         else if(dragStartMode == Normal) {
426           bool dragdrop = false;
427           if(selectionMode == TextSelected) {
428             int l = yToLineIdx(y);
429             if(selectionLine == l) {
430               int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
431               if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
432             }
433           } else if(selectionMode == LinesSelected) {
434             int l = yToLineIdx(y);
435             if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
436           }
437           // Ok, so just start drag & drop if appropriate.
438           if(dragdrop) {
439             QDrag *drag = new QDrag(this);
440             QMimeData *mimeData = new QMimeData;
441             mimeData->setText(selectionToString());
442             drag->setMimeData(mimeData);
443             drag->start();
444             mouseMode = Normal;
445           // Otherwise, clear the selection and start text marking!
446           } else {
447             setCursor(Qt::ArrowCursor);
448             clearSelection();
449             if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
450             else mouseMode = MarkText;
451           }
452         }
453       }
454       break;
455     case DragTsSep:
456       break;
457     case DragTextSep:
458       break;
459     default:
460       break;
461   }
462   // Pass 2: Some mouse modes need work after being set...
463   if(mouseMode == DragTsSep && x < size().width() - Style::sepSenderText() - senderWidth - 10) {
464     // Drag first column separator
465     int foo = Style::sepTsSender()/2;
466     tsWidth = qMax(x, foo) - foo;
467     computePositions();
468     layout();
469   } else if(mouseMode == DragTextSep && x < size().width() - 10) {
470     // Drag second column separator
471     int foo = tsWidth + Style::sepTsSender() + Style::sepSenderText()/2;
472     senderWidth = qMax(x, foo) - foo;
473     computePositions();
474     layout();
475   } else if(mouseMode == MarkText) {
476     // Change currently marked text
477     curLine = yToLineIdx(y);
478     int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
479     if(curLine == dragStartLine && c >= 0) {
480       if(c != curCursor) {
481         curCursor = c;
482         lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
483         viewport()->update();
484       }
485     } else {
486       mouseMode = MarkLines;
487       selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
488       for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
489       viewport()->update();
490     }
491   } else if(mouseMode == MarkLines) {
492     // Line marking
493     int l = yToLineIdx(y);
494     if(l != curLine) {
495       selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
496       if(curLine < 0) {
497         Q_ASSERT(selectionStart == selectionEnd);
498         lines[l]->setSelection(ChatLine::Full);
499       } else {
500         if(curLine < selectionStart) {
501           for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
502         } else if(curLine > selectionEnd) {
503           for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
504         } else if(selectionStart < curLine && l < curLine) {
505           for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
506         } else if(curLine < selectionEnd && l > curLine) {
507           for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
508         }
509       }
510       curLine = l;
511       //ensureVisible(l);
512       viewport()->update();
513     }
514   }
515 }
516
517 //!\brief Clear current text selection.
518 void ChatWidget::clearSelection() {
519   if(selectionMode == TextSelected) {
520     lines[selectionLine]->setSelection(ChatLine::None);
521   } else if(selectionMode == LinesSelected) {
522     for(int i = selectionStart; i <= selectionEnd; i++) {
523       lines[i]->setSelection(ChatLine::None);
524     }
525   }
526   selectionMode = NoSelection;
527   viewport()->update();
528 }
529
530 //!\brief Convert current selection to human-readable string.
531 QString ChatWidget::selectionToString() {
532   //TODO Make selection format configurable!
533   if(selectionMode == NoSelection) return "";
534   if(selectionMode == LinesSelected) {
535     QString result;
536     for(int l = selectionStart; l <= selectionEnd; l++) {
537       result += QString("[%1] %2 %3\n").arg(lines[l]->timeStamp().toLocalTime().toString("hh:mm:ss"))
538           .arg(lines[l]->sender()).arg(lines[l]->text());
539     }
540     return result;
541   }
542   // selectionMode == TextSelected
543   return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);
544 }
545