+ return h;
+}
+
+void ContentsChatItem::doLayout(QTextLayout *layout) const {
+ // QString t = data(Qt::DisplayRole).toString(); bool d = t.contains("protien");
+ 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()->setCursor(Qt::ArrowCursor);
+ 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;
+ }
+ }
+ if(!onClickable) endHoverMode();
+ event->accept();
+}
+
+void ContentsChatItem::addActionsToMenu(QMenu *menu, const QPointF &pos) {
+ if(privateData()->currentClickable.isValid()) {
+ Clickable click = privateData()->currentClickable;
+ switch(click.type()) {
+ case Clickable::Url:
+ privateData()->activeClickable = click;
+ menu->addAction(SmallIcon("edit-copy"), tr("Copy Link Address"),
+ &_actionProxy, SLOT(copyLinkToClipboard()))->setData(QVariant::fromValue<void *>(this));
+ break;
+ case Clickable::Channel: {
+ // Hide existing menu actions, they confuse us when right-clicking on a clickable
+ foreach(QAction *action, menu->actions())
+ action->setVisible(false);
+ QString name = data(ChatLineModel::DisplayRole).toString().mid(click.start(), click.length());
+ GraphicalUi::contextMenuActionProvider()->addActions(menu, chatScene()->filter(), data(MessageModel::BufferIdRole).value<BufferId>(), name);
+ break;