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