1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include "chatwidget.h"
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);
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)));
44 void ChatWidget::init(QString netname, QString bufname) {
45 networkName = netname;
47 setBackgroundRole(QPalette::Base);
48 setFont(QFont("Fixed"));
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());
59 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
60 setMouseTracking(true);
62 selectionMode = NoSelection;
63 connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
66 ChatWidget::~ChatWidget() {
67 //qDebug() << "destroying chatwidget" << bufferName;
68 //foreach(ChatLine *l, lines) {
73 QSize ChatWidget::sizeHint() const {
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());
87 //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
88 //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
92 void ChatWidget::scrollBarValChanged(int val) {
94 if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
96 int bot = val + viewport()->height();
97 int line = yToLineIdx(bot);
102 void ChatWidget::scrollBarAction(int 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());
108 case QScrollBar::SliderSingleStepSub:
109 //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
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));
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]);
139 void ChatWidget::clear() {
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);
149 // Fix all variables containing line numbers
152 selectionStart ++; selectionEnd ++;
154 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
155 viewport()->update();
158 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
159 QList<qreal> tmpy; tmpy.append(0);
161 foreach(ChatLine *l, clist) {
162 h += l->layout(tsWidth, senderWidth, textWidth);
165 ycoords.removeFirst();
166 for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
167 ycoords = tmpy + ycoords;
168 lines = clist + lines;
170 // Fix all variables containing line numbers
171 int i = clist.count();
174 selectionStart += i; selectionEnd += i; //? selectionEnd += i;
175 //if(bottomLine >= 0) bottomLine += i;
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();
185 void ChatWidget::appendChatLine(ChatLine *line) {
186 qreal h = line->layout(tsWidth, senderWidth, textWidth);
187 ycoords.append(h + ycoords[ycoords.count() - 1]);
189 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
191 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
193 viewport()->update();
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]);
203 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
205 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
206 viewport()->update();
209 void ChatWidget::setContents(QList<ChatLine *> list) {
214 appendChatLines(list);
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;
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()) {
236 //qDebug() << viewport()->size() << viewport()->height();
237 //QAbstractScrollArea::resizeEvent(event);
238 //qDebug() << viewport()->size() << viewport()->geometry();
241 void ChatWidget::paintEvent(QPaintEvent *event) {
242 QPainter painter(viewport());
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);
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;
256 //!\brief Layout the widget.
257 void ChatWidget::layout() {
258 // TODO fix scrollbars
259 //int botLine = yToLineIdx(verticalScrollBar()->value() +
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];
265 height = ycoords[ycoords.count()-1];
267 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
268 viewport()->update();
271 int ChatWidget::yToLineIdx(qreal y) {
272 if(y >= ycoords[ycoords.count()-1]) ycoords.count()-1;
273 if(ycoords.count() <= 1) return 0;
275 int oidx = ycoords.count() - 1;
278 if(uidx == oidx - 1) return uidx;
279 idx = (uidx + oidx) / 2;
280 if(ycoords[idx] > y) oidx = idx;
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) {
290 dragStartMode = Normal;
293 if(mousePos == OverTsSep) {
294 dragStartMode = DragTsSep;
295 setCursor(Qt::ClosedHandCursor);
296 } else if(mousePos == OverTextSep) {
297 dragStartMode = DragTextSep;
298 setCursor(Qt::ClosedHandCursor);
300 dragStartLine = yToLineIdx(pos.y());
301 dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
309 void ChatWidget::mouseDoubleClickEvent(QMouseEvent *event) {
315 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
316 //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
318 if(event->button() == Qt::LeftButton) {
319 dragStartPos = QPoint();
320 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
321 else setCursor(Qt::ArrowCursor);
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());
339 selectionMode = LinesSelected;
340 selectionStart = qMin(dragStartLine, curLine);
341 selectionEnd = qMax(dragStartLine, curLine);
342 // TODO Make X11SelectionMode configurable!
343 QApplication::clipboard()->setText(selectionToString());
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.
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();
363 if(scrollTimer->isActive()) {
367 handleMouseMoveEvent(pos);
370 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
372 if(lines.count() <= 0) return;
373 // Set some basic properties of the current position
374 QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
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;
382 // Pass 1: Do whatever we can before switching mouse mode (if at all).
384 // No special mode. Set mouse cursor if appropriate.
387 //if(oldpos != mousePos) {
388 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
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);
395 setCursor(Qt::ArrowCursor);
400 // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
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;
415 } else if(selectionMode == LinesSelected) {
416 int l = yToLineIdx(y);
417 if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
419 // Ok, so just start drag & drop if appropriate.
421 QDrag *drag = new QDrag(this);
422 QMimeData *mimeData = new QMimeData;
423 mimeData->setText(selectionToString());
424 drag->setMimeData(mimeData);
427 // Otherwise, clear the selection and start text marking!
429 setCursor(Qt::ArrowCursor);
431 if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
432 else mouseMode = MarkText;
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;
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;
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) {
462 lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
463 viewport()->update();
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();
471 } else if(mouseMode == MarkLines) {
473 int l = yToLineIdx(y);
475 selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
477 Q_ASSERT(selectionStart == selectionEnd);
478 lines[l]->setSelection(ChatLine::Full);
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);
492 viewport()->update();
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);
506 selectionMode = NoSelection;
507 viewport()->update();
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) {
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());
522 // selectionMode == TextSelected
523 return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);
526 /************************************************************************************/
528 //!\brief Construct a ChatLine object from a message.
530 * \param m The message to be layouted and rendered
531 * \param net The network name
532 * \param buf The buffer name
534 ChatLine::ChatLine(Message m) : QObject() {
536 //networkName = m.buffer.network();
537 //bufferName = m.buffer.buffer();
539 selectionMode = None;
543 ChatLine::~ChatLine() {
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();
555 QString c = tr("%DT[%1]").arg(msg.timeStamp.toLocalTime().toString("hh:mm:ss"));
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;
563 s = tr("%De*"); t = tr("%De%1").arg(text); break;
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;
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);
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);
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);
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);
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);
593 case Message::Action:
595 t = tr("%Da%DN%DU%1%DU%DN %2").arg(nick).arg(msg.text);
598 s = tr("%De%1").arg(msg.sender);
599 t = tr("%De[%1]").arg(msg.text);
601 QTextOption tsOption, senderOption, textOption;
602 tsFormatted = Style::internalToFormatted(c);
603 senderFormatted = Style::internalToFormatted(s);
604 textFormatted = Style::internalToFormatted(t);
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);
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);
620 range.start = 0; range.length = 1; range.format= format;
623 if(format == range.format) range.length++;
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;
634 QFontMetrics metrics(range.format.font());
635 range.height = metrics.lineSpacing();
636 ranges.append(range);
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;
648 tsFormat = calcFormatRanges(tsFormatted);
649 senderFormat = calcFormatRanges(senderFormatted);
650 textFormat = calcFormatRanges(textFormatted);
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);
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);
679 uint ChatLine::msgId() {
680 return msg.buffer.uid();
683 BufferId ChatLine::bufferId() {
687 QDateTime ChatLine::timeStamp() {
688 return msg.timeStamp;
691 QString ChatLine::sender() {
692 return senderFormatted.text;
695 QString ChatLine::text() {
696 return textFormatted.text;
699 bool ChatLine::isUrl(int c) {
700 if(c < 0 || c >= charUrlIdx.count()) return false;;
701 return charUrlIdx[c] >= 0;
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;
711 //!\brief Return the cursor position for the given coordinate pos.
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
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!
733 void ChatLine::precomputeLine() {
734 tsFormat = calcFormatRanges(tsFormatted);
735 senderFormat = calcFormatRanges(senderFormatted);
736 textFormat = calcFormatRanges(textFormatted);
739 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
740 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
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;
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());
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
765 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
767 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
771 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
776 if(++i < textFormatted.text.length() && ++cnt >= textFormat[idx].length) {
778 Q_ASSERT(idx < textFormat.count());
779 metrics = QFontMetrics(textFormat[idx].format.font());
782 charPos[textFormatted.text.length()] = w;
783 if(wr.start >= 0) words.append(wr);
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;
791 int offset = 0; int numWords = 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;
804 // ok, we had some words before, so store the layout and start a new line
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];
814 // check if the word fits into the current line
815 if(lastpos - offset <= textw) {
816 line.length = words[i].length;
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;
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) {
827 line.height = qMax(line.height, charHeights[j]);
831 lineLayouts.append(line);
832 line.y += line.height;
834 line.height = charHeights[j];
837 border = (int)textw + offset;
845 lineLayouts.append(line);
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();
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) {
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);
869 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
870 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
871 p->setBackgroundMode(Qt::OpaqueMode);
873 QRectF rect(pos, QSizeF(tsWidth, minHeight));
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());
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());
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));
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;
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);
904 h += lineLayouts[l].height;
906 if(l < lineLayouts.count()) {
907 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
908 offset = charPos[lineLayouts[l].start];
911 } while(end < frend && l < lineLayouts.count());
915 /******************************************************************************************************************/