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