1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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 ***************************************************************************/
21 #include <QApplication>
23 #include <QDesktopServices>
24 #include <QFontMetrics>
25 #include <QGraphicsSceneMouseEvent>
28 #include <QTextLayout>
30 #include <QGraphicsProxyWidget>
33 #include "chatlinemodel.h"
35 #include "qtuistyle.h"
37 ChatItem::ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent)
38 : QGraphicsItem(parent),
40 _boundingRect(0, 0, width, height),
41 _selectionMode(NoSelection),
44 setAcceptHoverEvents(true);
49 ChatItem::~ChatItem() {
53 QVariant ChatItem::data(int role) const {
54 QModelIndex index = model()->index(row(), column());
55 if(!index.isValid()) {
56 qWarning() << "ChatItem::data(): model index is invalid!" << index;
59 return model()->data(index, role);
62 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) {
63 QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
66 option.setWrapMode(wrapMode);
67 option.setAlignment(alignment);
68 layout->setTextOption(option);
70 QList<QTextLayout::FormatRange> formatRanges
71 = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
72 layout->setAdditionalFormats(formatRanges);
76 void ChatItem::updateLayout() {
78 setPrivateData(new ChatItemPrivate(createLayout()));
80 QTextLayout *layout_ = layout();
81 layout_->beginLayout();
82 QTextLine line = layout_->createLine();
84 line.setLineWidth(width());
85 line.setPosition(QPointF(0,0));
90 void ChatItem::clearLayout() {
95 // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
96 // This is a deliberate trade-off. (-> selectFmt creation, data() call)
97 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
98 Q_UNUSED(option); Q_UNUSED(widget);
101 painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
102 //if(_selectionMode == FullSelection) {
104 //painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight));
105 //painter->restore();
107 QVector<QTextLayout::FormatRange> formats = additionalFormats();
108 if(_selectionMode != NoSelection) {
109 QTextLayout::FormatRange selectFmt;
110 selectFmt.format.setForeground(QApplication::palette().brush(QPalette::HighlightedText));
111 selectFmt.format.setBackground(QApplication::palette().brush(QPalette::Highlight));
112 if(_selectionMode == PartialSelection) {
113 selectFmt.start = qMin(_selectionStart, _selectionEnd);
114 selectFmt.length = qAbs(_selectionStart - _selectionEnd);
115 } else { // FullSelection
117 selectFmt.length = data(MessageModel::DisplayRole).toString().length();
119 formats.append(selectFmt);
121 layout()->draw(painter, QPointF(0,0), formats, boundingRect());
124 // uncomment the following lines to draw the bounding rect and the row number in alternating colors
126 // painter->setPen(Qt::red);
128 // painter->setPen(Qt::blue);
129 // QString rowString = QString::number(row());
130 // QRect rowRect = painter->fontMetrics().boundingRect(rowString);
131 // QPointF topPoint = _boundingRect.topLeft();
132 // topPoint.ry() += rowRect.height();
133 // painter->drawText(topPoint, rowString);
134 // QPointF bottomPoint = _boundingRect.bottomRight();
135 // bottomPoint.rx() -= rowRect.width();
136 // painter->drawText(bottomPoint, rowString);
137 // painter->drawRect(_boundingRect.adjusted(0, 0, -1, -1));
140 qint16 ChatItem::posToCursor(const QPointF &pos) {
141 if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
142 if(pos.y() < 0) return 0;
145 for(int l = layout()->lineCount() - 1; l >= 0; l--) {
146 QTextLine line = layout()->lineAt(l);
147 if(pos.y() >= line.y()) {
148 return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
154 void ChatItem::setFullSelection() {
155 if(_selectionMode != FullSelection) {
156 _selectionMode = FullSelection;
161 void ChatItem::clearSelection() {
162 _selectionMode = NoSelection;
166 void ChatItem::continueSelecting(const QPointF &pos) {
167 _selectionMode = PartialSelection;
168 _selectionEnd = posToCursor(pos);
172 QList<QRectF> ChatItem::findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive) {
173 QList<QRectF> resultList;
174 const QAbstractItemModel *model_ = model();
178 QString plainText = model_->data(model_->index(row(), column()), MessageModel::DisplayRole).toString();
179 QList<int> indexList;
180 int searchIdx = plainText.indexOf(searchWord, 0, caseSensitive);
181 while(searchIdx != -1) {
182 indexList << searchIdx;
183 searchIdx = plainText.indexOf(searchWord, searchIdx + 1, caseSensitive);
186 bool hadLayout = hasLayout();
190 foreach(int idx, indexList) {
191 QTextLine line = layout()->lineForTextPosition(idx);
192 qreal x = line.cursorToX(idx);
193 qreal width = line.cursorToX(idx + searchWord.count()) - x;
194 qreal height = line.height();
195 qreal y = height * line.lineNumber();
196 resultList << QRectF(x, y, width, height);
204 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
205 if(event->buttons() == Qt::LeftButton) {
206 chatScene()->setSelectingItem(this);
207 _selectionStart = _selectionEnd = posToCursor(event->pos());
208 _selectionMode = NoSelection; // will be set to PartialSelection by mouseMoveEvent
216 void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
217 if(event->buttons() == Qt::LeftButton) {
218 if(contains(event->pos())) {
219 qint16 end = posToCursor(event->pos());
220 if(end != _selectionEnd) {
222 _selectionMode = (_selectionStart != _selectionEnd ? PartialSelection : NoSelection);
227 chatScene()->startGlobalSelection(this, event->pos());
235 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
236 if(_selectionMode != NoSelection && !event->buttons() & Qt::LeftButton) {
237 _selectionEnd = posToCursor(event->pos());
239 = data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
240 chatScene()->putToClipboard(selection);
247 // ************************************************************
249 // ************************************************************
251 // ************************************************************
253 // ************************************************************
254 ContentsChatItem::ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent)
255 : ChatItem(0, 0, pos, parent)
257 const QAbstractItemModel *model_ = model();
258 QModelIndex index = model_->index(row(), column());
259 _fontMetrics = QtUi::style()->fontMetrics(model_->data(index, ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
261 setGeometryByWidth(width);
264 qreal ContentsChatItem::setGeometryByWidth(qreal w) {
269 WrapColumnFinder finder(this);
270 while(finder.nextWrapColumn() > 0)
272 setHeight(lines * fontMetrics()->lineSpacing());
277 void ContentsChatItem::updateLayout() {
279 ContentsChatItemPrivate *data = new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this);
280 setPrivateData(data);
284 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
285 if(!wrapList.count()) return; // empty chatitem
288 WrapColumnFinder finder(this);
289 layout()->beginLayout();
291 QTextLine line = layout()->createLine();
295 int col = finder.nextWrapColumn();
296 line.setNumColumns(col >= 0 ? col - line.textStart() : layout()->text().length());
297 line.setPosition(QPointF(0, h));
298 h += fontMetrics()->lineSpacing();
300 layout()->endLayout();
303 // NOTE: This method is not threadsafe and not reentrant!
304 // (RegExps are not constant while matching, and they are static here for efficiency)
305 QList<ContentsChatItem::Clickable> ContentsChatItem::findClickables() {
307 static QString urlEnd("(?:>|[,.;:\"]*\\s|\\b|$)");
308 static QString urlChars("(?:[\\w\\-~@/?&=+$()!%#]|[,.;:]\\w)");
310 static QRegExp regExp[] = {
312 // QRegExp(QString("((?:https?://|s?ftp://|irc://|mailto:|www\\.)%1+|%1+\\.[a-z]{2,4}(?:?=/%1+|\\b))%2").arg(urlChars, urlEnd)),
313 QRegExp(QString("((?:(?:https?://|s?ftp://|irc://|mailto:)|www)%1+)%2").arg(urlChars, urlEnd)),
316 // We don't match for channel names starting with + or &, because that gives us a lot of false positives.
317 QRegExp("((?:#|![A-Z0-9]{5})[^,:\\s]+(?::[^,:\\s]+)?)\\b")
319 // TODO: Nicks, we'll need a filtering for only matching known nicknames further down if we do this
322 static const int regExpCount = 2; // number of regexps in the array above
324 qint16 matches[] = { 0, 0, 0 };
325 qint16 matchEnd[] = { 0, 0, 0 };
327 QString str = data(ChatLineModel::DisplayRole).toString();
329 QList<Clickable> result;
336 minidx = str.length();
337 for(int i = 0; i < regExpCount; i++) {
338 if(matches[i] < 0 || matchEnd[i] > str.length()) continue;
339 if(idx >= matchEnd[i]) {
340 matches[i] = str.indexOf(regExp[i], qMax(matchEnd[i], idx));
341 if(matches[i] >= 0) matchEnd[i] = matches[i] + regExp[i].cap(1).length();
343 if(matches[i] >= 0 && matches[i] < minidx) {
349 idx = matchEnd[type];
350 if(type == Clickable::Url && str.at(idx-1) == ')') { // special case: closing paren only matches if we had an open one
351 if(!str.mid(matches[type], matchEnd[type]-matches[type]).contains('(')) matchEnd[type]--;
353 result.append(Clickable((Clickable::Type)type, matches[type], matchEnd[type] - matches[type]));
358 if(!result.isEmpty()) qDebug() << str;
359 foreach(Clickable click, result) {
360 qDebug() << str.mid(click.start, click.length);
366 QVector<QTextLayout::FormatRange> ContentsChatItem::additionalFormats() const {
367 // mark a clickable if hovered upon
368 QVector<QTextLayout::FormatRange> fmt;
369 if(privateData()->currentClickable.isValid()) {
370 Clickable click = privateData()->currentClickable;
371 QTextLayout::FormatRange f;
372 f.start = click.start;
373 f.length = click.length;
374 f.format.setFontUnderline(true);
380 void ContentsChatItem::endHoverMode() {
381 if(hasLayout() && privateData()->currentClickable.isValid()) {
382 setCursor(Qt::ArrowCursor);
383 privateData()->currentClickable = Clickable();
384 privateData()->clearPreview();
389 void ContentsChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
390 privateData()->hasDragged = false;
391 ChatItem::mousePressEvent(event);
394 void ContentsChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
395 if(!event->buttons() && !privateData()->hasDragged) {
397 Clickable click = privateData()->currentClickable;
398 if(click.isValid()) {
399 QString str = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length);
402 if(!str.contains("://"))
403 str = "http://" + str;
404 QDesktopServices::openUrl(str);
406 case Clickable::Channel:
407 // TODO join or whatever...
414 ChatItem::mouseReleaseEvent(event);
417 void ContentsChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
418 // mouse move events always mean we're not hovering anymore...
420 // also, check if we have dragged the mouse
421 if(hasLayout() && !privateData()->hasDragged && event->buttons() & Qt::LeftButton
422 && (event->buttonDownScreenPos(Qt::LeftButton) - event->screenPos()).manhattanLength() >= QApplication::startDragDistance())
423 privateData()->hasDragged = true;
424 ChatItem::mouseMoveEvent(event);
427 void ContentsChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
432 void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
433 bool onClickable = false;
434 qint16 idx = posToCursor(event->pos());
435 for(int i = 0; i < privateData()->clickables.count(); i++) {
436 Clickable click = privateData()->clickables.at(i);
437 if(idx >= click.start && idx < click.start + click.length) {
438 if(click.type == Clickable::Url) {
444 QTextLine line = layout()->lineForTextPosition(click.start);
445 qreal x = line.cursorToX(click.start);
446 qreal width = line.cursorToX(click.start + click.length) - x;
447 qreal height = line.height();
448 qreal y = height * line.lineNumber();
449 QRectF urlRect(x, y, width, height);
450 QString url = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length);
451 if(!url.contains("://"))
452 url = "http://" + url;
453 privateData()->loadWebPreview(url, urlRect);
454 } else if(click.type == Clickable::Channel) {
455 // TODO: don't make clickable if it's our own name
456 //onClickable = true; //FIXME disabled for now
459 setCursor(Qt::PointingHandCursor);
460 privateData()->currentClickable = click;
466 if(!onClickable) endHoverMode();
470 // ****************************************
471 // ContentsChatItemPrivate
472 // ****************************************
473 ContentsChatItemPrivate::~ContentsChatItemPrivate() {
477 void ContentsChatItemPrivate::loadWebPreview(const QString &url, const QRectF &urlRect) {
479 controller = new PreviewController(contentsItem);
480 controller->loadPage(url, urlRect);
483 void ContentsChatItemPrivate::clearPreview() {
488 ContentsChatItemPrivate::PreviewController::~PreviewController() {
490 contentsItem->scene()->removeItem(previewItem);
495 void ContentsChatItemPrivate::PreviewController::loadPage(const QString &newUrl, const QRectF &urlRect) {
496 if(newUrl.isEmpty() || newUrl == url)
500 QWebView *view = new QWebView;
501 connect(view, SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded(bool)));
503 previewItem = new ContentsChatItemPrivate::PreviewItem(view);
505 QPointF sPos = contentsItem->scenePos();
506 qreal previewY = sPos.y() + urlRect.y() + urlRect.height(); // bottom of url;
507 qreal previewX = sPos.x() + urlRect.x();
508 if(previewY + previewItem->boundingRect().height() > contentsItem->scene()->sceneRect().bottom())
509 previewY = sPos.y() + urlRect.y() - previewItem->boundingRect().height();
511 if(previewX + previewItem->boundingRect().width() > contentsItem->scene()->sceneRect().width())
512 previewX = contentsItem->scene()->sceneRect().right() - previewItem->boundingRect().width();
514 previewItem->setPos(previewX, previewY);
515 contentsItem->scene()->addItem(previewItem);
518 void ContentsChatItemPrivate::PreviewController::pageLoaded(bool success) {
522 ContentsChatItemPrivate::PreviewItem::PreviewItem(QWebView *webView)
523 : QGraphicsItem(0), // needs to be a top level item as we otherwise cannot guarantee that it's on top of other chatlines
524 _boundingRect(0, 0, 400, 300)
526 qreal frameWidth = 5;
527 webView->resize(1000, 750);
528 QGraphicsProxyWidget *proxyItem = new QGraphicsProxyWidget(this);
529 proxyItem->setWidget(webView);
530 proxyItem->setAcceptHoverEvents(false);
532 qreal xScale = (_boundingRect.width() - 2 * frameWidth) / webView->width();
533 qreal yScale = (_boundingRect.height() - 2 * frameWidth) / webView->height();
534 proxyItem->scale(xScale, yScale);
535 proxyItem->setPos(frameWidth, frameWidth);
540 void ContentsChatItemPrivate::PreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
541 Q_UNUSED(option); Q_UNUSED(widget);
542 painter->setClipRect(boundingRect());
543 painter->setPen(QPen(Qt::black, 5));
544 painter->setBrush(Qt::black);
545 painter->setRenderHints(QPainter::Antialiasing);
546 painter->drawRoundedRect(boundingRect(), 10, 10);
548 painter->setPen(QPen(Qt::green));
549 QString text = QString::number(zValue());
550 painter->drawText(_boundingRect.center(), text);
553 /*************************************************************************************************/
555 ContentsChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item)
558 wrapList(item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>()),
565 ContentsChatItem::WrapColumnFinder::~WrapColumnFinder() {
569 qint16 ContentsChatItem::WrapColumnFinder::nextWrapColumn() {
570 if(wordidx >= wrapList.count())
574 qreal targetWidth = lineCount * item->width() + choppedTrailing;
576 qint16 start = wordidx;
577 qint16 end = wrapList.count() - 1;
579 // check if the whole line fits
580 if(wrapList.at(end).endX <= targetWidth) // || start == end)
583 // check if we have a very long word that needs inter word wrap
584 if(wrapList.at(start).endX > targetWidth) {
585 if(!line.isValid()) {
586 layout = item->createLayout(QTextOption::NoWrap);
587 layout->beginLayout();
588 line = layout->createLine();
591 return line.xToCursor(targetWidth, QTextLine::CursorOnCharacter);
595 if(start + 1 == end) {
597 const ChatLineModel::Word &lastWord = wrapList.at(start); // the last word we were able to squeeze in
599 // both cases should be cought preliminary
600 Q_ASSERT(lastWord.endX <= targetWidth); // ensure that "start" really fits in
601 Q_ASSERT(end < wrapList.count()); // ensure that start isn't the last word
603 choppedTrailing += lastWord.trailing - (targetWidth - lastWord.endX);
604 return wrapList.at(wordidx).start;
607 qint16 pivot = (end + start) / 2;
608 if(wrapList.at(pivot).endX > targetWidth) {