+qreal ContentsChatItem::setGeometryByWidth(qreal w) {
+ if(w != width()) {
+ setWidth(w);
+ // compute height
+ int lines = 1;
+ WrapColumnFinder finder(this);
+ while(finder.nextWrapColumn() > 0)
+ lines++;
+ setHeight(lines * fontMetrics()->lineSpacing());
+ }
+ return height();
+}
+
+void ContentsChatItem::updateLayout() {
+ if(!privateData()) {
+ ContentsChatItemPrivate *data = new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this);
+ setPrivateData(data);
+ }
+
+ // Now layout
+ ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
+ if(!wrapList.count()) return; // empty chatitem
+
+ qreal h = 0;
+ WrapColumnFinder finder(this);
+ layout()->beginLayout();
+ forever {
+ QTextLine line = layout()->createLine();
+ if(!line.isValid())
+ break;
+
+ int col = finder.nextWrapColumn();
+ line.setNumColumns(col >= 0 ? col - line.textStart() : layout()->text().length());
+ line.setPosition(QPointF(0, h));
+ h += fontMetrics()->lineSpacing();
+ }
+ layout()->endLayout();
+}
+
+// NOTE: This method is not threadsafe and not reentrant!
+// (RegExps are not constant while matching, and they are static here for efficiency)
+QList<ContentsChatItem::Clickable> ContentsChatItem::findClickables() {
+ // For matching URLs
+ static QString urlEnd("(?:>|[,.;:\"]*\\s|\\b|$)");
+ static QString urlChars("(?:[\\w\\-~@/?&=+$()!%#]|[,.;:]\\w)");
+
+ static QRegExp regExp[] = {
+ // URL
+ // QRegExp(QString("((?:https?://|s?ftp://|irc://|mailto:|www\\.)%1+|%1+\\.[a-z]{2,4}(?:?=/%1+|\\b))%2").arg(urlChars, urlEnd)),
+ QRegExp(QString("((?:(?:https?://|s?ftp://|irc://|mailto:)|www)%1+)%2").arg(urlChars, urlEnd)),
+
+ // Channel name
+ // We don't match for channel names starting with + or &, because that gives us a lot of false positives.
+ QRegExp("((?:#|![A-Z0-9]{5})[^,:\\s]+(?::[^,:\\s]+)?)\\b")
+
+ // TODO: Nicks, we'll need a filtering for only matching known nicknames further down if we do this
+ };
+
+ static const int regExpCount = 2; // number of regexps in the array above
+
+ qint16 matches[] = { 0, 0, 0 };
+ qint16 matchEnd[] = { 0, 0, 0 };
+
+ QString str = data(ChatLineModel::DisplayRole).toString();
+
+ QList<Clickable> result;
+ qint16 idx = 0;
+ qint16 minidx;
+ int type = -1;
+
+ do {
+ type = -1;
+ minidx = str.length();
+ for(int i = 0; i < regExpCount; i++) {
+ if(matches[i] < 0 || matchEnd[i] > str.length()) continue;
+ if(idx >= matchEnd[i]) {
+ matches[i] = str.indexOf(regExp[i], qMax(matchEnd[i], idx));
+ if(matches[i] >= 0) matchEnd[i] = matches[i] + regExp[i].cap(1).length();
+ }
+ if(matches[i] >= 0 && matches[i] < minidx) {
+ minidx = matches[i];
+ type = i;
+ }
+ }
+ if(type >= 0) {
+ idx = matchEnd[type];
+ if(type == Clickable::Url && str.at(idx-1) == ')') { // special case: closing paren only matches if we had an open one
+ if(!str.mid(matches[type], matchEnd[type]-matches[type]).contains('(')) matchEnd[type]--;
+ }
+ result.append(Clickable((Clickable::Type)type, matches[type], matchEnd[type] - matches[type]));
+ }
+ } while(type >= 0);
+
+ /* testing
+ if(!result.isEmpty()) qDebug() << str;
+ foreach(Clickable click, result) {
+ qDebug() << str.mid(click.start, click.length);
+ }
+ */
+ return result;
+}
+
+QVector<QTextLayout::FormatRange> ContentsChatItem::additionalFormats() const {
+ // mark a clickable if hovered upon
+ QVector<QTextLayout::FormatRange> fmt;
+ if(privateData()->currentClickable.isValid()) {
+ Clickable click = privateData()->currentClickable;
+ QTextLayout::FormatRange f;
+ f.start = click.start;
+ f.length = click.length;
+ f.format.setFontUnderline(true);
+ fmt.append(f);
+ }
+ return fmt;
+}
+
+void ContentsChatItem::endHoverMode() {
+ if(hasLayout()) {
+ if(privateData()->currentClickable.isValid()) {
+ setCursor(Qt::ArrowCursor);
+ privateData()->currentClickable = Clickable();
+ }
+#ifdef HAVE_WEBKIT
+ clearWebPreview();
+#endif
+ update();
+ }
+}
+
+void ContentsChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+ privateData()->hasDragged = false;
+ ChatItem::mousePressEvent(event);
+}
+
+void ContentsChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+ if(!event->buttons() && !privateData()->hasDragged) {
+ // got a click
+ Clickable click = privateData()->currentClickable;
+ if(click.isValid()) {
+ QString str = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length);
+ switch(click.type) {
+ case Clickable::Url:
+ if(!str.contains("://"))
+ str = "http://" + str;
+ QDesktopServices::openUrl(str);
+ break;
+ case Clickable::Channel:
+ // TODO join or whatever...
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ ChatItem::mouseReleaseEvent(event);
+}
+
+void ContentsChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+ // mouse move events always mean we're not hovering anymore...
+ endHoverMode();
+ // also, check if we have dragged the mouse
+ if(hasLayout() && !privateData()->hasDragged && event->buttons() & Qt::LeftButton
+ && (event->buttonDownScreenPos(Qt::LeftButton) - event->screenPos()).manhattanLength() >= QApplication::startDragDistance())
+ privateData()->hasDragged = true;
+ ChatItem::mouseMoveEvent(event);
+}
+
+void ContentsChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
+ endHoverMode();
+ event->accept();
+}
+
+void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
+ bool onClickable = false;
+ qint16 idx = posToCursor(event->pos());
+ for(int i = 0; i < privateData()->clickables.count(); i++) {
+ Clickable click = privateData()->clickables.at(i);
+ if(idx >= click.start && idx < click.start + click.length) {
+ if(click.type == Clickable::Url) {
+ onClickable = true;
+#ifdef HAVE_WEBKIT
+ showWebPreview(click);
+#endif
+ } else if(click.type == Clickable::Channel) {
+ // TODO: don't make clickable if it's our own name
+ //onClickable = true; //FIXME disabled for now
+ }
+ if(onClickable) {
+ setCursor(Qt::PointingHandCursor);
+ privateData()->currentClickable = click;
+ update();
+ break;
+ }
+ }
+ }
+ if(!onClickable) endHoverMode();
+ event->accept();
+}
+
+#ifdef HAVE_WEBKIT
+void ContentsChatItem::showWebPreview(const Clickable &click) {
+ if(!hasLayout())
+ updateLayout();
+
+ QTextLine line = layout()->lineForTextPosition(click.start);
+ qreal x = line.cursorToX(click.start);
+ qreal width = line.cursorToX(click.start + click.length) - x;
+ qreal height = line.height();
+ qreal y = height * line.lineNumber();
+ QRectF urlRect(x, y, width, height);
+ QString url = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length);
+ if(!url.contains("://"))
+ url = "http://" + url;
+ privateData()->loadWebPreview(url, urlRect);
+}
+
+void ContentsChatItem::clearWebPreview() {
+ if(hasLayout())
+ privateData()->clearWebPreview();
+}
+#endif
+
+// ****************************************
+// ContentsChatItemPrivate
+// ****************************************
+#ifdef HAVE_WEBKIT
+ContentsChatItemPrivate::~ContentsChatItemPrivate() {
+ clearWebPreview();
+}
+
+void ContentsChatItemPrivate::loadWebPreview(const QString &url, const QRectF &urlRect) {
+ if(url != previewUrl) {
+ previewUrl = url;
+ // load a new web view and delete the old one (if exists)
+ if(previewItem) {
+ contentsItem->scene()->removeItem(previewItem);
+ delete previewItem;
+ }
+ QWebView *view = new QWebView;
+ view->load(url);
+ previewItem = new ContentsChatItemPrivate::PreviewItem(view);
+ contentsItem->scene()->addItem(previewItem);
+ }
+ if(urlRect != previewUrlRect) {
+ previewUrlRect = urlRect;
+ QPointF sPos = contentsItem->scenePos();
+ qreal previewY = sPos.y() + urlRect.y() + urlRect.height(); // bottom of url;
+ qreal previewX = sPos.x() + urlRect.x();
+ if(previewY + previewItem->boundingRect().height() > contentsItem->scene()->sceneRect().bottom())
+ previewY = sPos.y() + urlRect.y() - previewItem->boundingRect().height();
+
+ if(previewX + previewItem->boundingRect().width() > contentsItem->scene()->sceneRect().width())
+ previewX = contentsItem->scene()->sceneRect().right() - previewItem->boundingRect().width();
+
+ previewItem->setPos(previewX, previewY);
+ }
+}
+
+void ContentsChatItemPrivate::clearWebPreview() {
+ if(previewItem) {
+ contentsItem->scene()->removeItem(previewItem);
+ delete previewItem;
+ previewItem = 0;
+ }
+ previewUrl = QString();
+ previewUrlRect = QRectF();
+}
+
+ContentsChatItemPrivate::PreviewItem::PreviewItem(QWebView *webView)
+ : QGraphicsItem(0), // needs to be a top level item as we otherwise cannot guarantee that it's on top of other chatlines
+ _boundingRect(0, 0, 400, 300)
+{
+ qreal frameWidth = 5;
+ webView->resize(1000, 750);
+ QGraphicsProxyWidget *proxyItem = new QGraphicsProxyWidget(this);
+ proxyItem->setWidget(webView);
+ proxyItem->setAcceptHoverEvents(false);
+
+ qreal xScale = (_boundingRect.width() - 2 * frameWidth) / webView->width();
+ qreal yScale = (_boundingRect.height() - 2 * frameWidth) / webView->height();
+ proxyItem->scale(xScale, yScale);
+ proxyItem->setPos(frameWidth, frameWidth);
+
+ setZValue(30);
+}
+
+void ContentsChatItemPrivate::PreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {