- //}
- return height();
-}
-
-// 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() const {
- // For matching URLs
- static QString urlEnd("(?:>|[,.;:\"]*\\s|\\b|$)");
- static QString urlChars("(?:[,.;:]*[\\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("((?:(?:mailto:|\\w+://)|www\\.)%1+)%2").arg(urlChars, urlEnd), Qt::CaseInsensitive),
-
- // 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", Qt::CaseInsensitive)
-
- // 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] = regExp[i].indexIn(str, 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];
- QString match = str.mid(matches[type], matchEnd[type] - matches[type]);
- if(type == Clickable::Url && str.at(idx-1) == ')') { // special case: closing paren only matches if we had an open one
- if(!match.contains('(')) {
- matchEnd[type]--;
- match.chop(1);
+
+ if (w != width() || h != height())
+ setGeometry(w, h);
+
+ return h;
+}
+
+
+void ContentsChatItem::initLayout(QTextLayout *layout) const
+{
+ initLayoutHelper(layout, QTextOption::WrapAtWordBoundaryOrAnywhere);
+ doLayout(layout);
+}
+
+
+void ContentsChatItem::doLayout(QTextLayout *layout) const
+{
+ ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
+ if (!wrapList.count()) return; // empty chatitem
+
+ qreal h = 0;
+ qreal spacing = qMax(fontMetrics()->lineSpacing(), fontMetrics()->height()); // cope with negative leading()
+ WrapColumnFinder finder(this);
+ layout->beginLayout();
+ forever {
+ QTextLine line = layout->createLine();
+ if (!line.isValid())
+ break;
+
+ int col = finder.nextWrapColumn(width());
+ if (col < 0)
+ col = layout->text().length();
+ int num = col - line.textStart();
+
+ line.setNumColumns(num);
+
+ // Sometimes, setNumColumns will create a line that's too long (cf. Qt bug 238249)
+ // We verify this and try setting the width again, making it shorter each time until the lengths match.
+ // Dead fugly, but seems to work…
+ for (int i = line.textLength()-1; i >= 0 && line.textLength() > num; i--) {
+ line.setNumColumns(i);
+ }
+ if (num != line.textLength()) {
+ qWarning() << "WARNING: Layout engine couldn't workaround Qt bug 238249, please report!";
+ // qDebug() << num << line.textLength() << t.mid(line.textStart(), line.textLength()) << t.mid(line.textStart() + line.textLength());
+ }
+
+ line.setPosition(QPointF(0, h));
+ h += spacing;
+ }
+ layout->endLayout();
+}
+
+
+Clickable ContentsChatItem::clickableAt(const QPointF &pos) const
+{
+ return privateData()->clickables.atCursorPos(posToCursor(pos));
+}
+
+
+UiStyle::FormatList ContentsChatItem::formatList() const
+{
+ UiStyle::FormatList fmtList = ChatItem::formatList();
+ for (int i = 0; i < privateData()->clickables.count(); i++) {
+ Clickable click = privateData()->clickables.at(i);
+ if (click.type() == Clickable::Url) {
+ overlayFormat(fmtList, click.start(), click.start() + click.length(), UiStyle::Url);
+ }
+ }
+ return fmtList;
+}
+
+
+QVector<QTextLayout::FormatRange> ContentsChatItem::additionalFormats() const
+{
+ QVector<QTextLayout::FormatRange> fmt = ChatItem::additionalFormats();
+ // mark a clickable if hovered upon
+ 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 (privateData()) {
+ if (privateData()->currentClickable.isValid()) {
+ chatLine()->unsetCursor();
+ privateData()->currentClickable = Clickable();
+ }
+ clearWebPreview();
+ chatLine()->update();
+ }
+}
+
+
+void ContentsChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clickMode)
+{
+ if (clickMode == ChatScene::SingleClick) {
+ qint16 idx = posToCursor(pos);
+ Clickable foo = privateData()->clickables.atCursorPos(idx);
+ if (foo.isValid()) {
+ NetworkId networkId = Client::networkModel()->networkId(data(MessageModel::BufferIdRole).value<BufferId>());
+ QString text = data(ChatLineModel::DisplayRole).toString();
+ foo.activate(networkId, text);
+ }
+ }
+ else if (clickMode == ChatScene::DoubleClick) {
+ chatScene()->setSelectingItem(this);
+ setSelectionMode(PartialSelection);
+ Clickable click = clickableAt(pos);
+ if (click.isValid()) {
+ setSelectionStart(click.start());
+ setSelectionEnd(click.start() + click.length());
+ }
+ else {
+ // find word boundary
+ QString str = data(ChatLineModel::DisplayRole).toString();
+ qint16 cursor = posToCursor(pos);
+ qint16 start = str.lastIndexOf(QRegExp("\\W"), cursor) + 1;
+ qint16 end = qMin(str.indexOf(QRegExp("\\W"), cursor), str.length());
+ if (end < 0) end = str.length();
+ setSelectionStart(start);
+ setSelectionEnd(end);
+ }
+ chatLine()->update();
+ }
+ else if (clickMode == ChatScene::TripleClick) {
+ setSelection(PartialSelection, 0, data(ChatLineModel::DisplayRole).toString().length());
+ }
+ ChatItem::handleClick(pos, clickMode);
+}
+
+
+void ContentsChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+ // mouse move events always mean we're not hovering anymore...
+ endHoverMode();
+ ChatItem::mouseMoveEvent(event);
+}
+
+
+void ContentsChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
+{
+ endHoverMode();
+ event->accept();
+}
+
+
+void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
+{
+ bool onClickable = false;
+ Clickable click = clickableAt(event->pos());
+ if (click.isValid()) {
+ if (click.type() == Clickable::Url) {
+ onClickable = true;
+ showWebPreview(click);
+ }
+ else if (click.type() == Clickable::Channel) {
+ QString name = data(ChatLineModel::DisplayRole).toString().mid(click.start(), click.length());
+ // don't make clickable if it's our own name
+ BufferId myId = data(MessageModel::BufferIdRole).value<BufferId>();
+ if (Client::networkModel()->bufferName(myId) != name)
+ onClickable = true;
+ }
+ if (onClickable) {
+ chatLine()->setCursor(Qt::PointingHandCursor);
+ privateData()->currentClickable = click;
+ chatLine()->update();
+ return;