1 /***************************************************************************
2 * Copyright (C) 2005-2010 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>
31 #include "buffermodel.h"
32 #include "bufferview.h"
35 #include "chatlinemodel.h"
36 #include "contextmenuactionprovider.h"
37 #include "iconloader.h"
40 #include "qtuistyle.h"
42 ChatItem::ChatItem(const QRectF &boundingRect, ChatLine *parent)
44 _boundingRect(boundingRect),
45 _selectionMode(NoSelection),
51 QVariant ChatItem::data(int role) const {
52 QModelIndex index = model()->index(row(), column());
53 if(!index.isValid()) {
54 qWarning() << "ChatItem::data(): model index is invalid!" << index;
57 return model()->data(index, role);
60 qint16 ChatItem::posToCursor(const QPointF &posInLine) const {
61 QPointF pos = mapFromLine(posInLine);
62 if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
63 if(pos.y() < 0) return 0;
67 for(int l = layout.lineCount() - 1; l >= 0; l--) {
68 QTextLine line = layout.lineAt(l);
69 if(pos.y() >= line.y()) {
70 return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
76 void ChatItem::initLayoutHelper(QTextLayout *layout, QTextOption::WrapMode wrapMode, Qt::Alignment alignment) const {
79 layout->setText(data(MessageModel::DisplayRole).toString());
82 option.setWrapMode(wrapMode);
83 option.setAlignment(alignment);
84 layout->setTextOption(option);
86 QList<QTextLayout::FormatRange> formatRanges
87 = QtUi::style()->toTextLayoutList(formatList(), layout->text().length(), data(ChatLineModel::MsgLabelRole).toUInt());
88 layout->setAdditionalFormats(formatRanges);
91 UiStyle::FormatList ChatItem::formatList() const {
92 return data(MessageModel::FormatRole).value<UiStyle::FormatList>();
95 void ChatItem::doLayout(QTextLayout *layout) const {
96 layout->beginLayout();
97 QTextLine line = layout->createLine();
99 line.setLineWidth(width());
100 line.setPosition(QPointF(0,0));
105 void ChatItem::paintBackground(QPainter *painter) {
107 if(_selectionMode == FullSelection)
108 bgBrush = data(ChatLineModel::SelectedBackgroundRole);
110 bgBrush = data(ChatLineModel::BackgroundRole);
111 if(bgBrush.isValid())
112 painter->fillRect(boundingRect(), bgBrush.value<QBrush>());
115 // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
116 // This is a deliberate trade-off. (-> selectFmt creation, data() call)
117 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
118 Q_UNUSED(option); Q_UNUSED(widget);
120 painter->setClipRect(boundingRect());
121 paintBackground(painter);
125 layout.draw(painter, pos(), additionalFormats(), boundingRect());
127 // layout()->draw(painter, QPointF(0,0), formats, boundingRect());
130 // uncomment partially or all of the following stuff:
132 // 0) alternativ painter color for debug stuff
134 // painter->setPen(Qt::red);
136 // painter->setPen(Qt::blue);
137 // 1) draw wordwrap points in the first line
138 // if(column() == 2) {
139 // ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
140 // foreach(ChatLineModel::Word word, wrapList) {
141 // if(word.endX > width())
143 // painter->drawLine(word.endX, 0, word.endX, height());
146 // 2) draw MsgId over the time column
147 // if(column() == 0) {
148 // QString msgIdString = QString::number(data(MessageModel::MsgIdRole).value<MsgId>().toInt());
149 // QPointF bottomPoint = boundingRect().bottomLeft();
150 // bottomPoint.ry() -= 2;
151 // painter->drawText(bottomPoint, msgIdString);
153 // 3) draw bounding rect
154 // painter->drawRect(_boundingRect.adjusted(0, 0, -1, -1));
159 void ChatItem::overlayFormat(UiStyle::FormatList &fmtList, int start, int end, quint32 overlayFmt) const {
160 for(int i = 0; i < fmtList.count(); i++) {
161 int fmtStart = fmtList.at(i).first;
162 int fmtEnd = (i < fmtList.count()-1 ? fmtList.at(i+1).first : data(MessageModel::DisplayRole).toString().length());
169 // split the format if necessary
170 if(fmtStart < start) {
171 fmtList.insert(i, fmtList.at(i));
172 fmtList[++i].first = start;
175 fmtList.insert(i, fmtList.at(i));
176 fmtList[i+1].first = end;
179 fmtList[i].second |= overlayFmt;
183 QVector<QTextLayout::FormatRange> ChatItem::additionalFormats() const {
184 return selectionFormats();
187 QVector<QTextLayout::FormatRange> ChatItem::selectionFormats() const {
189 return QVector<QTextLayout::FormatRange>();
192 if(_selectionMode == FullSelection) {
194 end = data(MessageModel::DisplayRole).toString().length();
196 start = qMin(_selectionStart, _selectionEnd);
197 end = qMax(_selectionStart, _selectionEnd);
200 UiStyle::FormatList fmtList = formatList();
202 while(fmtList.count() > 1 && fmtList.at(1).first <= start)
203 fmtList.removeFirst();
205 fmtList.first().first = start;
207 while(fmtList.count() > 1 && fmtList.last().first >= end)
208 fmtList.removeLast();
210 return QtUi::style()->toTextLayoutList(fmtList, end, UiStyle::Selected|data(ChatLineModel::MsgLabelRole).toUInt()).toVector();
213 bool ChatItem::hasSelection() const {
214 if(_selectionMode == NoSelection)
216 if(_selectionMode == FullSelection)
219 return _selectionStart != _selectionEnd;
222 QString ChatItem::selection() const {
223 if(_selectionMode == FullSelection)
224 return data(MessageModel::DisplayRole).toString();
225 if(_selectionMode == PartialSelection)
226 return data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
230 void ChatItem::setSelection(SelectionMode mode, qint16 start, qint16 end) {
231 _selectionMode = mode;
232 _selectionStart = start;
234 chatLine()->update();
237 void ChatItem::setFullSelection() {
238 if(_selectionMode != FullSelection) {
239 _selectionMode = FullSelection;
240 chatLine()->update();
244 void ChatItem::clearSelection() {
245 if(_selectionMode != NoSelection) {
246 _selectionMode = NoSelection;
247 chatLine()->update();
251 void ChatItem::continueSelecting(const QPointF &pos) {
252 _selectionMode = PartialSelection;
253 _selectionEnd = posToCursor(pos);
254 chatLine()->update();
257 bool ChatItem::isPosOverSelection(const QPointF &pos) const {
258 if(_selectionMode == FullSelection)
260 if(_selectionMode == PartialSelection) {
261 int cursor = posToCursor(pos);
262 return cursor >= qMin(_selectionStart, _selectionEnd) && cursor <= qMax(_selectionStart, _selectionEnd);
267 QList<QRectF> ChatItem::findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive) {
268 QList<QRectF> resultList;
269 const QAbstractItemModel *model_ = model();
273 QString plainText = model_->data(model_->index(row(), column()), MessageModel::DisplayRole).toString();
274 QList<int> indexList;
275 int searchIdx = plainText.indexOf(searchWord, 0, caseSensitive);
276 while(searchIdx != -1) {
277 indexList << searchIdx;
278 searchIdx = plainText.indexOf(searchWord, searchIdx + 1, caseSensitive);
283 foreach(int idx, indexList) {
284 QTextLine line = layout.lineForTextPosition(idx);
285 qreal x = line.cursorToX(idx);
286 qreal width = line.cursorToX(idx + searchWord.count()) - x;
287 qreal height = line.height();
288 qreal y = height * line.lineNumber();
289 resultList << QRectF(x, y, width, height);
295 void ChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) {
296 // single clicks are already handled by the scene (for clearing the selection)
297 if(clickMode == ChatScene::DragStartClick) {
298 chatScene()->setSelectingItem(this);
299 _selectionStart = _selectionEnd = posToCursor(pos);
300 _selectionMode = NoSelection; // will be set to PartialSelection by mouseMoveEvent
301 chatLine()->update();
305 void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
306 if(event->buttons() == Qt::LeftButton) {
307 if(boundingRect().contains(event->pos())) {
308 qint16 end = posToCursor(event->pos());
309 if(end != _selectionEnd) {
311 _selectionMode = (_selectionStart != _selectionEnd ? PartialSelection : NoSelection);
312 chatLine()->update();
316 chatScene()->startGlobalSelection(this, event->pos());
324 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
325 if(event->buttons() == Qt::LeftButton)
331 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
332 if(_selectionMode != NoSelection && event->button() == Qt::LeftButton) {
333 chatScene()->selectionToClipboard(QClipboard::Selection);
339 void ChatItem::addActionsToMenu(QMenu *menu, const QPointF &pos) {
342 GraphicalUi::contextMenuActionProvider()->addActions(menu, chatScene()->filter(), data(MessageModel::BufferIdRole).value<BufferId>());
345 // ************************************************************
347 // ************************************************************
349 void SenderChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
350 Q_UNUSED(option); Q_UNUSED(widget);
352 painter->setClipRect(boundingRect());
353 paintBackground(painter);
357 qreal layoutWidth = layout.minimumWidth();
359 if(chatScene()->senderCutoffMode() == ChatScene::CutoffLeft)
360 offset = qMin(width() - layoutWidth, (qreal)0);
362 offset = qMax(layoutWidth - width(), (qreal)0);
364 if(layoutWidth > width()) {
365 // Draw a nice gradient for longer items
366 // Qt's text drawing with a gradient brush sucks, so we use an alpha-channeled pixmap instead
367 QPixmap pixmap(layout.boundingRect().toRect().size());
368 pixmap.fill(Qt::transparent);
369 QPainter pixPainter(&pixmap);
370 layout.draw(&pixPainter, QPointF(qMax(offset, (qreal)0), 0), additionalFormats());
373 // Create alpha channel mask
374 QPixmap mask(pixmap.size());
375 QPainter maskPainter(&mask);
376 QLinearGradient gradient;
378 gradient.setStart(0, 0);
379 gradient.setFinalStop(12, 0);
380 gradient.setColorAt(0, Qt::black);
381 gradient.setColorAt(1, Qt::white);
383 gradient.setStart(width()-10, 0);
384 gradient.setFinalStop(width(), 0);
385 gradient.setColorAt(0, Qt::white);
386 gradient.setColorAt(1, Qt::black);
388 maskPainter.fillRect(0, 0, pixmap.width(), pixmap.height(), gradient);
389 pixmap.setAlphaChannel(mask);
390 painter->drawPixmap(pos(), pixmap);
392 layout.draw(painter, pos(), additionalFormats(), boundingRect());
397 void SenderChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) {
398 if(clickMode == ChatScene::DoubleClick) {
399 BufferInfo curBufInfo = Client::networkModel()->bufferInfo(data(MessageModel::BufferIdRole).value<BufferId>());
400 QString nick = data(MessageModel::EditRole).toString();
401 // check if the nick is a valid ircUser
402 if(!nick.isEmpty() && Client::network(curBufInfo.networkId())->ircUser(nick))
403 Client::bufferModel()->switchToOrStartQuery(curBufInfo.networkId(), nick);
406 ChatItem::handleClick(pos, clickMode);
409 // ************************************************************
411 // ************************************************************
413 ContentsChatItem::ActionProxy ContentsChatItem::_actionProxy;
415 ContentsChatItem::ContentsChatItem(const QPointF &pos, const qreal &width, ChatLine *parent)
416 : ChatItem(QRectF(pos, QSizeF(width, 0)), parent),
420 setGeometryByWidth(width);
423 QFontMetricsF *ContentsChatItem::fontMetrics() const {
424 return QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second, 0);
427 ContentsChatItem::~ContentsChatItem() {
431 ContentsChatItemPrivate *ContentsChatItem::privateData() const {
433 ContentsChatItem *that = const_cast<ContentsChatItem *>(this);
434 that->_data = new ContentsChatItemPrivate(ClickableList::fromString(data(ChatLineModel::DisplayRole).toString()), that);
439 qreal ContentsChatItem::setGeometryByWidth(qreal w) {
440 // We use this for reloading layout info as well, so we can't bail out if the width doesn't change
444 WrapColumnFinder finder(this);
445 while(finder.nextWrapColumn(w) > 0)
447 qreal spacing = qMax(fontMetrics()->lineSpacing(), fontMetrics()->height()); // cope with negative leading()
448 qreal h = lines * spacing;
452 if(w != width() || h != height())
458 void ContentsChatItem::doLayout(QTextLayout *layout) const {
459 // QString t = data(Qt::DisplayRole).toString(); bool d = t.contains("protien");
460 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
461 if(!wrapList.count()) return; // empty chatitem
464 qreal spacing = qMax(fontMetrics()->lineSpacing(), fontMetrics()->height()); // cope with negative leading()
465 WrapColumnFinder finder(this);
466 layout->beginLayout();
468 QTextLine line = layout->createLine();
472 int col = finder.nextWrapColumn(width());
474 col = layout->text().length();
475 int num = col - line.textStart();
477 line.setNumColumns(num);
479 // Sometimes, setNumColumns will create a line that's too long (cf. Qt bug 238249)
480 // We verify this and try setting the width again, making it shorter each time until the lengths match.
481 // Dead fugly, but seems to work…
482 for(int i = line.textLength()-1; i >= 0 && line.textLength() > num; i--) {
483 line.setNumColumns(i);
485 if(num != line.textLength()) {
486 qWarning() << "WARNING: Layout engine couldn't workaround Qt bug 238249, please report!";
487 // qDebug() << num << line.textLength() << t.mid(line.textStart(), line.textLength()) << t.mid(line.textStart() + line.textLength());
490 line.setPosition(QPointF(0, h));
496 Clickable ContentsChatItem::clickableAt(const QPointF &pos) const {
497 return privateData()->clickables.atCursorPos(posToCursor(pos));
500 UiStyle::FormatList ContentsChatItem::formatList() const {
501 UiStyle::FormatList fmtList = ChatItem::formatList();
502 for(int i = 0; i < privateData()->clickables.count(); i++) {
503 Clickable click = privateData()->clickables.at(i);
504 if(click.type() == Clickable::Url) {
505 overlayFormat(fmtList, click.start(), click.start() + click.length(), UiStyle::Url);
511 QVector<QTextLayout::FormatRange> ContentsChatItem::additionalFormats() const {
512 QVector<QTextLayout::FormatRange> fmt = ChatItem::additionalFormats();
513 // mark a clickable if hovered upon
514 if(privateData()->currentClickable.isValid()) {
515 Clickable click = privateData()->currentClickable;
516 QTextLayout::FormatRange f;
517 f.start = click.start();
518 f.length = click.length();
519 f.format.setFontUnderline(true);
525 void ContentsChatItem::endHoverMode() {
527 if(privateData()->currentClickable.isValid()) {
528 chatLine()->setCursor(Qt::ArrowCursor);
529 privateData()->currentClickable = Clickable();
532 chatLine()->update();
536 void ContentsChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) {
537 if(clickMode == ChatScene::SingleClick) {
538 qint16 idx = posToCursor(pos);
539 Clickable foo = privateData()->clickables.atCursorPos(idx);
541 NetworkId networkId = Client::networkModel()->networkId(data(MessageModel::BufferIdRole).value<BufferId>());
542 QString text = data(ChatLineModel::DisplayRole).toString();
543 foo.activate(networkId, text);
545 } else if(clickMode == ChatScene::DoubleClick) {
546 chatScene()->setSelectingItem(this);
547 setSelectionMode(PartialSelection);
548 Clickable click = clickableAt(pos);
549 if(click.isValid()) {
550 setSelectionStart(click.start());
551 setSelectionEnd(click.start() + click.length());
553 // find word boundary
554 QString str = data(ChatLineModel::DisplayRole).toString();
555 qint16 cursor = posToCursor(pos);
556 qint16 start = str.lastIndexOf(QRegExp("\\W"), cursor) + 1;
557 qint16 end = qMin(str.indexOf(QRegExp("\\W"), cursor), str.length());
558 if(end < 0) end = str.length();
559 setSelectionStart(start);
560 setSelectionEnd(end);
562 chatLine()->update();
563 } else if(clickMode == ChatScene::TripleClick) {
564 setSelection(PartialSelection, 0, data(ChatLineModel::DisplayRole).toString().length());
566 ChatItem::handleClick(pos, clickMode);
569 void ContentsChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
570 // mouse move events always mean we're not hovering anymore...
572 ChatItem::mouseMoveEvent(event);
575 void ContentsChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
580 void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
581 bool onClickable = false;
582 Clickable click = clickableAt(event->pos());
583 if(click.isValid()) {
584 if(click.type() == Clickable::Url) {
586 showWebPreview(click);
587 } else if(click.type() == Clickable::Channel) {
588 QString name = data(ChatLineModel::DisplayRole).toString().mid(click.start(), click.length());
589 // don't make clickable if it's our own name
590 BufferId myId = data(MessageModel::BufferIdRole).value<BufferId>();
591 if(Client::networkModel()->bufferName(myId) != name)
595 chatLine()->setCursor(Qt::PointingHandCursor);
596 privateData()->currentClickable = click;
597 chatLine()->update();
601 if(!onClickable) endHoverMode();
605 void ContentsChatItem::addActionsToMenu(QMenu *menu, const QPointF &pos) {
606 if(privateData()->currentClickable.isValid()) {
607 Clickable click = privateData()->currentClickable;
608 switch(click.type()) {
610 privateData()->activeClickable = click;
611 menu->addAction(SmallIcon("edit-copy"), tr("Copy Link Address"),
612 &_actionProxy, SLOT(copyLinkToClipboard()))->setData(QVariant::fromValue<void *>(this));
614 case Clickable::Channel: {
615 // Hide existing menu actions, they confuse us when right-clicking on a clickable
616 foreach(QAction *action, menu->actions())
617 action->setVisible(false);
618 QString name = data(ChatLineModel::DisplayRole).toString().mid(click.start(), click.length());
619 GraphicalUi::contextMenuActionProvider()->addActions(menu, chatScene()->filter(), data(MessageModel::BufferIdRole).value<BufferId>(), name);
626 // Buffer-specific actions
627 ChatItem::addActionsToMenu(menu, pos);
631 void ContentsChatItem::copyLinkToClipboard() {
632 Clickable click = privateData()->activeClickable;
633 if(click.isValid() && click.type() == Clickable::Url) {
634 QString url = data(ChatLineModel::DisplayRole).toString().mid(click.start(), click.length());
635 if(!url.contains("://"))
636 url = "http://" + url;
637 chatScene()->stringToClipboard(url);
641 /******** WEB PREVIEW *****************************************************************************/
643 void ContentsChatItem::showWebPreview(const Clickable &click) {
649 QTextLine line = layout.lineForTextPosition(click.start());
650 qreal x = line.cursorToX(click.start());
651 qreal width = line.cursorToX(click.start() + click.length()) - x;
652 qreal height = line.height();
653 qreal y = height * line.lineNumber();
655 QPointF topLeft = mapToScene(pos()) + QPointF(x, y);
656 QRectF urlRect = QRectF(topLeft.x(), topLeft.y(), width, height);
658 QString urlstr = data(ChatLineModel::DisplayRole).toString().mid(click.start(), click.length());
659 if(!urlstr.contains("://"))
660 urlstr= "http://" + urlstr;
661 QUrl url = QUrl::fromEncoded(urlstr.toUtf8(), QUrl::TolerantMode);
662 chatScene()->loadWebPreview(this, url, urlRect);
666 void ContentsChatItem::clearWebPreview() {
668 chatScene()->clearWebPreview(this);
672 /*************************************************************************************************/
674 ContentsChatItem::WrapColumnFinder::WrapColumnFinder(const ChatItem *_item)
676 wrapList(item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>()),
683 ContentsChatItem::WrapColumnFinder::~WrapColumnFinder() {
686 qint16 ContentsChatItem::WrapColumnFinder::nextWrapColumn(qreal width) {
687 if(wordidx >= wrapList.count())
691 qreal targetWidth = lineCount * width + choppedTrailing;
693 qint16 start = wordidx;
694 qint16 end = wrapList.count() - 1;
696 // check if the whole line fits
697 if(wrapList.at(end).endX <= targetWidth) // || start == end)
700 // check if we have a very long word that needs inter word wrap
701 if(wrapList.at(start).endX > targetWidth) {
702 if(!line.isValid()) {
703 item->initLayoutHelper(&layout, QTextOption::NoWrap);
704 layout.beginLayout();
705 line = layout.createLine();
708 return line.xToCursor(targetWidth, QTextLine::CursorOnCharacter);
712 if(start + 1 == end) {
714 const ChatLineModel::Word &lastWord = wrapList.at(start); // the last word we were able to squeeze in
716 // both cases should be cought preliminary
717 Q_ASSERT(lastWord.endX <= targetWidth); // ensure that "start" really fits in
718 Q_ASSERT(end < wrapList.count()); // ensure that start isn't the last word
720 choppedTrailing += lastWord.trailing - (targetWidth - lastWord.endX);
721 return wrapList.at(wordidx).start;
724 qint16 pivot = (end + start) / 2;
725 if(wrapList.at(pivot).endX > targetWidth) {
735 /*************************************************************************************************/