Make selections in bufferviews (mostly) synchronous again.
[quassel.git] / src / qtgui / 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 <QtGui>
25 #include <QtCore>
26
27 ChatWidget::ChatWidget(QWidget *parent) : QAbstractScrollArea(parent) {
28   scrollTimer = new QTimer(this);
29   scrollTimer->setSingleShot(false);
30   scrollTimer->setInterval(100);
31   setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
32   setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
33   setMinimumSize(QSize(400,400));
34   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
35   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
36   bottomLine = -1;
37   height = 0;
38   ycoords.append(0);
39   pointerPosition = QPoint(0,0);
40   connect(verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(scrollBarAction(int)));
41   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollBarValChanged(int)));
42 }
43
44 void ChatWidget::init(QString netname, QString bufname) {
45   networkName = netname;
46   bufferName = bufname;
47   setBackgroundRole(QPalette::Base);
48   setFont(QFont("Fixed"));
49   tsWidth = 90;
50   senderWidth = 100;
51   computePositions();
52   adjustScrollBar();
53   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
54   //verticalScrollBar()->setPageStep(viewport()->height());
55   //verticalScrollBar()->setSingleStep(20);
56   //verticalScrollBar()->setMinimum(0);
57   //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
58
59   setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
60   setMouseTracking(true);
61   mouseMode = Normal;
62   selectionMode = NoSelection;
63   connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
64 }
65
66 ChatWidget::~ChatWidget() {
67   //qDebug() << "destroying chatwidget" << bufferName;
68   //foreach(ChatLine *l, lines) {
69   //  delete l;
70   //}
71 }
72
73 QSize ChatWidget::sizeHint() const {
74   //qDebug() << size();
75   return size();
76 }
77
78 void ChatWidget::adjustScrollBar() {
79   verticalScrollBar()->setPageStep(viewport()->height());
80   verticalScrollBar()->setSingleStep(20);
81   verticalScrollBar()->setMinimum(0);
82   verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
83   //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
84   //if(bottomLine < 0) {
85   //  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
86   //} else {
87     //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
88     //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
89   //}
90 }
91
92 void ChatWidget::scrollBarValChanged(int val) {
93   return;
94   if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
95   else {
96     int bot = val + viewport()->height();
97     int line = yToLineIdx(bot);
98     //bottomLine = line;
99   }
100 }
101
102 void ChatWidget::scrollBarAction(int action) {
103   switch(action) {
104     case QScrollBar::SliderSingleStepAdd:
105       // More elaborate. But what with loooong lines?
106       // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
107       break;
108     case QScrollBar::SliderSingleStepSub:
109       //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
110       break;
111
112   }
113
114 }
115
116 void ChatWidget::handleScrollTimer() {
117   if(mouseMode == MarkText || mouseMode == MarkLines) {
118     if(pointerPosition.y() > viewport()->height()) {
119       verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
120       handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
121     } else if(pointerPosition.y() < 0) {
122       verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
123       handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
124     }
125   }
126 }
127
128 void ChatWidget::ensureVisible(int line) {
129   int top = verticalScrollBar()->value();
130   int bot = top + viewport()->height();
131   if(ycoords[line+1] > bot) {
132     verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
133   } else if(ycoords[line] < top) {
134     verticalScrollBar()->setValue((int)ycoords[line]);
135   }
136
137 }
138
139 void ChatWidget::clear() {
140   //contents->clear();
141 }
142
143 void ChatWidget::prependChatLine(ChatLine *line) {
144   qreal h = line->layout(tsWidth, senderWidth, textWidth);
145   for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
146   ycoords.insert(1, h);
147   lines.prepend(line);
148   height += h;
149   // Fix all variables containing line numbers
150   dragStartLine ++;
151   curLine ++;
152   selectionStart ++; selectionEnd ++;
153   adjustScrollBar();
154   verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
155   viewport()->update();
156 }
157
158 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
159   QList<qreal> tmpy; tmpy.append(0);
160   qreal h = 0;
161   foreach(ChatLine *l, clist) {
162     h += l->layout(tsWidth, senderWidth, textWidth);
163     tmpy.append(h);
164   }
165   ycoords.removeFirst();
166   for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
167   ycoords = tmpy + ycoords;
168   lines = clist + lines;
169   height += h;
170   // Fix all variables containing line numbers
171   int i = clist.count();
172   dragStartLine += i;
173   curLine += i;
174   selectionStart += i; selectionEnd += i; //? selectionEnd += i;
175   //if(bottomLine >= 0) bottomLine += i;
176   adjustScrollBar();
177   //verticalScrollBar()->setPageStep(viewport()->height());
178   //verticalScrollBar()->setSingleStep(20);
179   //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
180   verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
181   viewport()->update();
182 }
183
184
185 void ChatWidget::appendChatLine(ChatLine *line) {
186   qreal h = line->layout(tsWidth, senderWidth, textWidth);
187   ycoords.append(h + ycoords[ycoords.count() - 1]);
188   height += h;
189   bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
190   adjustScrollBar();
191   if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
192   lines.append(line);
193   viewport()->update();
194 }
195
196 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
197   foreach(ChatLine *line, list) {
198     qreal h = line->layout(tsWidth, senderWidth, textWidth);
199     ycoords.append(h + ycoords[ycoords.count() - 1]);
200     height += h;
201     lines.append(line);
202   }
203   bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
204   adjustScrollBar();
205   if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
206   viewport()->update();
207 }
208
209 void ChatWidget::setContents(QList<ChatLine *> list) {
210   ycoords.clear();
211   ycoords.append(0);
212   height = 0;
213   lines.clear();
214   appendChatLines(list);
215 }
216
217 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
218 void ChatWidget::computePositions() {
219   senderX = tsWidth + Style::sepTsSender();
220   textX = senderX + senderWidth + Style::sepSenderText();
221   tsGrabPos = tsWidth + (int)Style::sepTsSender()/2;
222   senderGrabPos = senderX + senderWidth + (int)Style::sepSenderText()/2;
223   textWidth = viewport()->size().width() - textX;
224 }
225
226 void ChatWidget::resizeEvent(QResizeEvent *event) {
227   //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
228   /*if(event->oldSize().isValid())*/
229   //contents->setWidth(event->size().width());
230   //setAlignment(Qt::AlignBottom);
231   if(event->size().width() != event->oldSize().width()) {
232     computePositions();
233     layout();
234   }
235   //adjustScrollBar();
236   //qDebug() << viewport()->size() << viewport()->height();
237   //QAbstractScrollArea::resizeEvent(event);
238   //qDebug() << viewport()->size() << viewport()->geometry();
239 }
240
241 void ChatWidget::paintEvent(QPaintEvent *event) {
242   QPainter painter(viewport());
243
244   //qDebug() <<  verticalScrollBar()->value();
245   painter.translate(0, -verticalScrollBar()->value());
246   int top = event->rect().top() + verticalScrollBar()->value();
247   int bot = top + event->rect().height();
248   int idx = yToLineIdx(top);
249   if(idx < 0) return;
250   for(int i = idx; i < lines.count() ; i++) {
251     lines[i]->draw(&painter, QPointF(0, ycoords[i]));
252     if(ycoords[i+1] > bot) return;
253   }
254 }
255
256 //!\brief Layout the widget.
257 void ChatWidget::layout() {
258   // TODO fix scrollbars
259   //int botLine = yToLineIdx(verticalScrollBar()->value() + 
260   qreal y = 0;
261   for(int i = 0; i < lines.count(); i++) {
262     qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
263     ycoords[i+1] = h + ycoords[i];
264   }
265   height = ycoords[ycoords.count()-1];
266   adjustScrollBar();
267   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
268   viewport()->update();
269 }
270
271 int ChatWidget::yToLineIdx(qreal y) {
272   if(y >= ycoords[ycoords.count()-1]) ycoords.count()-1;
273   if(ycoords.count() <= 1) return 0;
274   int uidx = 0;
275   int oidx = ycoords.count() - 1;
276   int idx;
277   while(1) {
278     if(uidx == oidx - 1) return uidx;
279     idx = (uidx + oidx) / 2;
280     if(ycoords[idx] > y) oidx = idx;
281     else uidx = idx;
282   }
283 }
284
285 void ChatWidget::mousePressEvent(QMouseEvent *event) {
286   if(lines.isEmpty()) return;
287   QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
288   if(event->button() == Qt::LeftButton) {
289     dragStartPos = pos;
290     dragStartMode = Normal;
291     switch(mouseMode) {
292       case Normal:
293         if(mousePos == OverTsSep) {
294           dragStartMode = DragTsSep;
295           setCursor(Qt::ClosedHandCursor);
296         } else if(mousePos == OverTextSep) {
297           dragStartMode = DragTextSep;
298           setCursor(Qt::ClosedHandCursor);
299         } else {
300           dragStartLine = yToLineIdx(pos.y());
301           dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
302         }
303         mouseMode = Pressed;
304         break;
305     }
306   }
307 }
308
309 void ChatWidget::mouseDoubleClickEvent(QMouseEvent *event) {
310
311
312
313 }
314
315 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
316   //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
317
318   if(event->button() == Qt::LeftButton) {
319     dragStartPos = QPoint();
320     if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
321     else setCursor(Qt::ArrowCursor);
322
323     switch(mouseMode) {
324       case Pressed:
325         mouseMode = Normal;
326         clearSelection();
327         break;
328       case MarkText:
329         mouseMode = Normal;
330         selectionMode = TextSelected;
331         selectionLine = dragStartLine;
332         selectionStart = qMin(dragStartCursor, curCursor);
333         selectionEnd = qMax(dragStartCursor, curCursor);
334         // TODO Make X11SelectionMode configurable!
335         QApplication::clipboard()->setText(selectionToString());
336         break;
337       case MarkLines:
338         mouseMode = Normal;
339         selectionMode = LinesSelected;
340         selectionStart = qMin(dragStartLine, curLine);
341         selectionEnd = qMax(dragStartLine, curLine);
342         // TODO Make X11SelectionMode configurable!
343         QApplication::clipboard()->setText(selectionToString());
344         break;
345       default:
346         mouseMode = Normal;
347     }
348   }
349 }
350
351 //!\brief React to mouse movements over the ChatWidget.
352 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
353  * such as changing column widths, marking text or initiating drag & drop.
354  */
355 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
356   QPoint pos = event->pos(); pointerPosition = pos;
357   // Scroll if mouse pointer leaves widget while dragging
358   if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
359     if(!scrollTimer->isActive()) {
360       scrollTimer->start();
361     }
362   } else {
363     if(scrollTimer->isActive()) {
364       scrollTimer->stop();
365     }
366   }
367   handleMouseMoveEvent(pos);
368 }
369
370 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
371   // FIXME
372   if(lines.count() <= 0) return;
373   // Set some basic properties of the current position
374   QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
375   int x = pos.x();
376   int y = pos.y();
377   MousePos oldpos = mousePos;
378   if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
379   else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
380   else mousePos = None;
381
382   // Pass 1: Do whatever we can before switching mouse mode (if at all).
383   switch(mouseMode) {
384     // No special mode. Set mouse cursor if appropriate.
385     case Normal:
386     {
387       //if(oldpos != mousePos) {
388       if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
389       else {
390         int l = yToLineIdx(y);
391         int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
392         if(c >= 0 && lines[l]->isUrl(c)) {
393           setCursor(Qt::PointingHandCursor);
394         } else {
395           setCursor(Qt::ArrowCursor);
396         }
397       }
398     }
399       break;
400     // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
401     case Pressed:
402       if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
403         // Moving a column separator?
404         if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
405         else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
406         // Nope. Check if we are over a selection to start drag & drop.
407         else if(dragStartMode == Normal) {
408           bool dragdrop = false;
409           if(selectionMode == TextSelected) {
410             int l = yToLineIdx(y);
411             if(selectionLine == l) {
412               int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
413               if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
414             }
415           } else if(selectionMode == LinesSelected) {
416             int l = yToLineIdx(y);
417             if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
418           }
419           // Ok, so just start drag & drop if appropriate.
420           if(dragdrop) {
421             QDrag *drag = new QDrag(this);
422             QMimeData *mimeData = new QMimeData;
423             mimeData->setText(selectionToString());
424             drag->setMimeData(mimeData);
425             drag->start();
426             mouseMode = Normal;
427           // Otherwise, clear the selection and start text marking!
428           } else {
429             setCursor(Qt::ArrowCursor);
430             clearSelection();
431             if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
432             else mouseMode = MarkText;
433           }
434         }
435       }
436       break;
437     case DragTsSep:
438       break;
439     case DragTextSep:
440       break;
441   }
442   // Pass 2: Some mouse modes need work after being set...
443   if(mouseMode == DragTsSep && x < size().width() - Style::sepSenderText() - senderWidth - 10) {
444     // Drag first column separator
445     int foo = Style::sepTsSender()/2;
446     tsWidth = qMax(x, foo) - foo;
447     computePositions();
448     layout();
449   } else if(mouseMode == DragTextSep && x < size().width() - 10) {
450     // Drag second column separator
451     int foo = tsWidth + Style::sepTsSender() + Style::sepSenderText()/2;
452     senderWidth = qMax(x, foo) - foo;
453     computePositions();
454     layout();
455   } else if(mouseMode == MarkText) {
456     // Change currently marked text
457     curLine = yToLineIdx(y);
458     int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
459     if(curLine == dragStartLine && c >= 0) {
460       if(c != curCursor) {
461         curCursor = c;
462         lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
463         viewport()->update();
464       }
465     } else {
466       mouseMode = MarkLines;
467       selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
468       for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
469       viewport()->update();
470     }
471   } else if(mouseMode == MarkLines) {
472     // Line marking
473     int l = yToLineIdx(y);
474     if(l != curLine) {
475       selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
476       if(curLine < 0) {
477         Q_ASSERT(selectionStart == selectionEnd);
478         lines[l]->setSelection(ChatLine::Full);
479       } else {
480         if(curLine < selectionStart) {
481           for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
482         } else if(curLine > selectionEnd) {
483           for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
484         } else if(selectionStart < curLine && l < curLine) {
485           for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
486         } else if(curLine < selectionEnd && l > curLine) {
487           for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
488         }
489       }
490       curLine = l;
491       //ensureVisible(l);
492       viewport()->update();
493     }
494   }
495 }
496
497 //!\brief Clear current text selection.
498 void ChatWidget::clearSelection() {
499   if(selectionMode == TextSelected) {
500     lines[selectionLine]->setSelection(ChatLine::None);
501   } else if(selectionMode == LinesSelected) {
502     for(int i = selectionStart; i <= selectionEnd; i++) {
503       lines[i]->setSelection(ChatLine::None);
504     }
505   }
506   selectionMode = NoSelection;
507   viewport()->update();
508 }
509
510 //!\brief Convert current selection to human-readable string.
511 QString ChatWidget::selectionToString() {
512   //TODO Make selection format configurable!
513   if(selectionMode == NoSelection) return "";
514   if(selectionMode == LinesSelected) {
515     QString result;
516     for(int l = selectionStart; l <= selectionEnd; l++) {
517       result += QString("[%1] %2 %3\n").arg(lines[l]->timeStamp().toLocalTime().toString("hh:mm:ss"))
518           .arg(lines[l]->sender()).arg(lines[l]->text());
519     }
520     return result;
521   }
522   // selectionMode == TextSelected
523   return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);
524 }
525
526 /************************************************************************************/
527
528 //!\brief Construct a ChatLine object from a message.
529 /**
530  * \param m   The message to be layouted and rendered
531  * \param net The network name
532  * \param buf The buffer name
533  */ 
534 ChatLine::ChatLine(Message m) : QObject() {
535   hght = 0;
536   //networkName = m.buffer.network();
537   //bufferName = m.buffer.buffer();
538   msg = m;
539   selectionMode = None;
540   formatMsg(msg);
541 }
542
543 ChatLine::~ChatLine() {
544
545 }
546
547 void ChatLine::formatMsg(Message msg) {
548   QString user = userFromMask(msg.sender);
549   QString host = hostFromMask(msg.sender);
550   QString nick = nickFromMask(msg.sender);
551   QString text = Style::mircToInternal(msg.text);
552   QString networkName = msg.buffer.network();
553   QString bufferName = msg.buffer.buffer();
554
555   QString c = tr("%DT[%1]").arg(msg.timeStamp.toLocalTime().toString("hh:mm:ss"));
556   QString s, t;
557   switch(msg.type) {
558     case Message::Plain:
559       s = tr("%DS<%1>").arg(nick); t = tr("%D0%1").arg(text); break;
560     case Message::Server:
561       s = tr("%Ds*"); t = tr("%Ds%1").arg(text); break;
562     case Message::Error:
563       s = tr("%De*"); t = tr("%De%1").arg(text); break;
564     case Message::Join:
565       s = tr("%Dj-->"); t = tr("%Dj%DN%DU%1%DU%DN %DH(%2@%3)%DH has joined %DC%DU%4%DU%DC").arg(nick, user, host, bufferName); break;
566     case Message::Part:
567       s = tr("%Dp<--"); t = tr("%Dp%DN%DU%1%DU%DN %DH(%2@%3)%DH has left %DC%DU%4%DU%DC").arg(nick, user, host, bufferName);
568       if(!text.isEmpty()) t = QString("%1 (%2)").arg(t).arg(text);
569       break;
570     case Message::Quit:
571       s = tr("%Dq<--"); t = tr("%Dq%DN%DU%1%DU%DN %DH(%2@%3)%DH has quit").arg(nick, user, host);
572       if(!text.isEmpty()) t = QString("%1 (%2)").arg(t).arg(text);
573       break;
574     case Message::Kick:
575       { s = tr("%Dk<-*");
576         QString victim = text.section(" ", 0, 0);
577         //if(victim == ui.ownNick->currentText()) victim = tr("you");
578         QString kickmsg = text.section(" ", 1);
579         t = tr("%Dk%DN%DU%1%DU%DN has kicked %DN%DU%2%DU%DN from %DC%DU%3%DU%DC").arg(nick).arg(victim).arg(bufferName);
580         if(!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg);
581       }
582       break;
583     case Message::Nick:
584       s = tr("%Dr<->");
585       if(nick == msg.text) t = tr("%DrYou are now known as %DN%1%DN").arg(msg.text);
586       else t = tr("%Dr%DN%1%DN is now known as %DN%DU%2%DU%DN").arg(nick, msg.text);
587       break;
588     case Message::Mode:
589       s = tr("%Dm***");
590       if(nick.isEmpty()) t = tr("%DmUser mode: %DM%1%DM").arg(msg.text);
591       else t = tr("%DmMode %DM%1%DM by %DN%DU%2%DU%DN").arg(msg.text, nick);
592       break;
593     case Message::Action:
594       s = tr("%Da-*-");
595       t = tr("%Da%DN%DU%1%DU%DN %2").arg(nick).arg(msg.text);
596       break;
597     default:
598       s = tr("%De%1").arg(msg.sender);
599       t = tr("%De[%1]").arg(msg.text);
600   }
601   QTextOption tsOption, senderOption, textOption;
602   tsFormatted = Style::internalToFormatted(c);
603   senderFormatted = Style::internalToFormatted(s);
604   textFormatted = Style::internalToFormatted(t);
605   precomputeLine();
606 }
607
608 QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const Style::FormattedString &fs, QTextLayout::FormatRange additional) {
609   QList<FormatRange> ranges;
610   QList<QTextLayout::FormatRange> formats = fs.formats;
611   formats.append(additional);
612   int cur = -1;
613   FormatRange range, lastrange;
614   for(int i = 0; i < fs.text.length(); i++) {
615     QTextCharFormat format;
616     foreach(QTextLayout::FormatRange f, formats) {
617       if(i >= f.start && i < f.start + f.length) format.merge(f.format);
618     }
619     if(cur < 0) {
620       range.start = 0; range.length = 1; range.format= format;
621       cur = 0;
622     } else {
623       if(format == range.format) range.length++;
624       else {
625         QFontMetrics metrics(range.format.font());
626         range.height = metrics.lineSpacing();
627         ranges.append(range);
628         range.start = i; range.length = 1; range.format = format;
629         cur++;
630       }
631     }
632   }
633   if(cur >= 0) {
634     QFontMetrics metrics(range.format.font());
635     range.height = metrics.lineSpacing();
636     ranges.append(range);
637   }
638   return ranges;
639 }
640
641 void ChatLine::setSelection(SelectionMode mode, int start, int end) {
642   selectionMode = mode;
643   //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
644   QPalette pal = QApplication::palette();
645   QTextLayout::FormatRange tsSel, senderSel, textSel;
646   switch (mode) {
647     case None:
648       tsFormat = calcFormatRanges(tsFormatted);
649       senderFormat = calcFormatRanges(senderFormatted);
650       textFormat = calcFormatRanges(textFormatted);
651       break;
652     case Partial:
653       selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
654       textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
655       textSel.format.setBackground(pal.brush(QPalette::Highlight));
656       textSel.start = selectionStart;
657       textSel.length = selectionEnd - selectionStart;
658       //textFormat.append(textSel);
659       textFormat = calcFormatRanges(textFormatted, textSel);
660       foreach(FormatRange fr, textFormat);
661       break;
662     case Full:
663       tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
664       tsSel.format.setBackground(pal.brush(QPalette::Highlight));
665       tsSel.start = 0; tsSel.length = tsFormatted.text.length();
666       tsFormat = calcFormatRanges(tsFormatted, tsSel);
667       senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
668       senderSel.format.setBackground(pal.brush(QPalette::Highlight));
669       senderSel.start = 0; senderSel.length = senderFormatted.text.length();
670       senderFormat = calcFormatRanges(senderFormatted, senderSel);
671       textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
672       textSel.format.setBackground(pal.brush(QPalette::Highlight));
673       textSel.start = 0; textSel.length = textFormatted.text.length();
674       textFormat = calcFormatRanges(textFormatted, textSel);
675       break;
676   }
677 }
678
679 uint ChatLine::msgId() {
680   return msg.buffer.uid();
681 }
682
683 BufferId ChatLine::bufferId() {
684   return msg.buffer;
685 }
686
687 QDateTime ChatLine::timeStamp() {
688   return msg.timeStamp;
689 }
690
691 QString ChatLine::sender() {
692   return senderFormatted.text;
693 }
694
695 QString ChatLine::text() {
696   return textFormatted.text;
697 }
698
699 bool ChatLine::isUrl(int c) {
700   if(c < 0 || c >= charUrlIdx.count()) return false;;
701   return charUrlIdx[c] >= 0;
702 }
703
704 QUrl ChatLine::getUrl(int c) {
705   if(c < 0 || c >= charUrlIdx.count()) return QUrl();
706   int i = charUrlIdx[c];
707   if(i >= 0) return textFormatted.urls[i].url;
708   else return QUrl();
709 }
710
711 //!\brief Return the cursor position for the given coordinate pos.
712 /**
713  * \param pos The position relative to the ChatLine
714  * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
715  */
716 int ChatLine::posToCursor(QPointF pos) {
717   if(pos.x() < tsWidth + (int)Style::sepTsSender()/2) return -2;
718   qreal textStart = tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText();
719   if(pos.x() < textStart) return -1;
720   int x = (int)(pos.x() - textStart);
721   for(int l = lineLayouts.count() - 1; l >=0; l--) {
722     LineLayout line = lineLayouts[l];
723     if(pos.y() >= line.y) {
724       int offset = charPos[line.start]; x += offset;
725       for(int i = line.start + line.length - 1; i >= line.start; i--) {
726         if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
727       }
728       return line.start;
729     }
730   }
731 }
732
733 void ChatLine::precomputeLine() {
734   tsFormat = calcFormatRanges(tsFormatted);
735   senderFormat = calcFormatRanges(senderFormatted);
736   textFormat = calcFormatRanges(textFormatted);
737
738   minHeight = 0;
739   foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
740   foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
741
742   words.clear();
743   charPos.resize(textFormatted.text.length() + 1);
744   charHeights.resize(textFormatted.text.length());
745   charUrlIdx.fill(-1, textFormatted.text.length());
746   for(int i = 0; i < textFormatted.urls.count(); i++) {
747     Style::UrlInfo url = textFormatted.urls[i];
748     for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
749   }
750   if(!textFormat.count()) return;
751   int idx = 0; int cnt = 0; int w = 0; int h = 0;
752   QFontMetrics metrics(textFormat[0].format.font());
753   Word wr;
754   wr.start = -1; wr.trailing = -1;
755   for(int i = 0; i < textFormatted.text.length(); ) {
756     charPos[i] = w; charHeights[i] = textFormat[idx].height;
757     w += metrics.charWidth(textFormatted.text, i);
758     if(!textFormatted.text[i].isSpace()) {
759       if(wr.trailing >= 0) {
760         // new word after space
761         words.append(wr);
762         wr.start = -1;
763       }
764       if(wr.start < 0) {
765         wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
766       } else {
767         wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
768       }
769     } else {
770       if(wr.start < 0) {
771         wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
772       } else {
773         wr.trailing++;
774       }
775     }
776     if(++i < textFormatted.text.length() && ++cnt >= textFormat[idx].length) {
777       cnt = 0; idx++;
778       Q_ASSERT(idx < textFormat.count());
779       metrics = QFontMetrics(textFormat[idx].format.font());
780     }
781   }
782   charPos[textFormatted.text.length()] = w;
783   if(wr.start >= 0) words.append(wr);
784 }
785
786 qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
787   tsWidth = tsw; senderWidth = senderw; textWidth = textw;
788   if(textw <= 0) return minHeight;
789   lineLayouts.clear(); LineLayout line;
790   int h = 0;
791   int offset = 0; int numWords = 0;
792   line.y = 0;
793   line.start = 0;
794   line.height = minHeight;  // first line needs room for ts and sender
795   for(int i = 0; i < words.count(); i++) {
796     int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
797     if(lastpos - offset <= textw) {
798       line.height = qMax(line.height, words[i].height);
799       line.length = words[i].start + words[i].length - line.start;
800       numWords++;
801     } else {
802       // we need to wrap!
803       if(numWords > 0) {
804         // ok, we had some words before, so store the layout and start a new line
805         h += line.height;
806         line.length = words[i-1].start + words[i-1].length - line.start;
807         lineLayouts.append(line);
808         line.y += line.height;
809         line.start = words[i].start;
810         line.height = words[i].height;
811         offset = charPos[words[i].start];
812       }
813       numWords = 1;
814       // check if the word fits into the current line
815       if(lastpos - offset <= textw) {
816         line.length = words[i].length;
817       } else {
818         // we need to break a word in the middle
819         int border = (int)textw + offset; // save some additions
820         line.start = words[i].start;
821         line.length = 1;
822         line.height = charHeights[line.start];
823         int j = line.start + 1;
824         for(int l = 1; l < words[i].length; j++, l++) {
825           if(charPos[j+1] < border) {
826             line.length++;
827             line.height = qMax(line.height, charHeights[j]);
828             continue;
829           } else {
830             h += line.height;
831             lineLayouts.append(line);
832             line.y += line.height;
833             line.start = j;
834             line.height = charHeights[j];
835             line.length = 1;
836             offset = charPos[j];
837             border = (int)textw + offset;
838           }
839         }
840       }
841     }
842   }
843   h += line.height;
844   if(numWords > 0) {
845     lineLayouts.append(line);
846   }
847   hght = h;
848   return hght;
849 }
850
851 //!\brief Draw ChatLine on the given QPainter at the given position.
852 void ChatLine::draw(QPainter *p, const QPointF &pos) {
853   QPalette pal = QApplication::palette();
854
855   if(selectionMode == Full) {
856     p->setPen(Qt::NoPen);
857     p->setBrush(pal.brush(QPalette::Highlight));
858     p->drawRect(QRectF(pos, QSizeF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText() + textWidth, height())));
859   } else if(selectionMode == Partial) {
860
861   } /*
862   p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
863   tsLayout.draw(p, pos, tsFormat);
864   p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
865   senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
866   p->setClipping(false);
867   textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
868   */
869   //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
870   //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
871   p->setBackgroundMode(Qt::OpaqueMode);
872   QPointF tp = pos;
873   QRectF rect(pos, QSizeF(tsWidth, minHeight));
874   QRectF brect;
875   foreach(FormatRange fr, tsFormat) {
876     p->setFont(fr.format.font());
877     p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
878     p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, tsFormatted.text.mid(fr.start, fr.length), &brect);
879     rect.setLeft(brect.right());
880   }
881   rect = QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, minHeight));
882   for(int i = senderFormat.count() - 1; i >= 0; i--) {
883     FormatRange fr = senderFormat[i];
884     p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
885     p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, senderFormatted.text.mid(fr.start, fr.length), &brect);
886     rect.setRight(brect.left());
887   }
888   QPointF tpos = pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0);
889   qreal h = 0; int l = 0;
890   rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
891   int offset = 0;
892   foreach(FormatRange fr, textFormat) {
893     if(l >= lineLayouts.count()) break;
894     p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
895     int start, end, frend, llend;
896     do {
897       frend = fr.start + fr.length;
898       if(frend <= lineLayouts[l].start) break;
899       llend = lineLayouts[l].start + lineLayouts[l].length;
900       start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
901       rect.setLeft(tpos.x() + charPos[start] - offset);
902       p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, textFormatted.text.mid(start, end - start), &brect);
903       if(llend <= end) {
904         h += lineLayouts[l].height;
905         l++;
906         if(l < lineLayouts.count()) {
907           rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
908           offset = charPos[lineLayouts[l].start];
909         }
910       }
911     } while(end < frend && l < lineLayouts.count());
912   }
913 }
914
915 /******************************************************************************************************************/
916