1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "chatscene.h"
25 #include <QApplication>
27 #include <QDesktopServices>
29 #include <QGraphicsSceneMouseEvent>
33 #include <QPersistentModelIndex>
37 # include <QWebEngineView>
38 #elif defined HAVE_WEBKIT
44 #include "chatlinemodelitem.h"
46 #include "chatviewsettings.h"
48 #include "clientbacklogmanager.h"
49 #include "columnhandleitem.h"
50 #include "contextmenuactionprovider.h"
53 #include "markerlineitem.h"
54 #include "messagefilter.h"
56 #include "qtuistyle.h"
57 #include "webpreviewitem.h"
59 const qreal minContentsWidth = 200;
61 ChatScene::ChatScene(QAbstractItemModel *model, QString idString, qreal width, ChatView *parent)
62 : QGraphicsScene(0, 0, width, 0, (QObject *)parent),
64 _idString(std::move(idString)),
66 _singleBufferId(BufferId()),
67 _sceneRect(0, 0, width, 0),
70 _markerLine(new MarkerLineItem(width)),
71 _markerLineVisible(false),
72 _markerLineValid(false),
73 _markerLineJumpPending(false),
74 _cutoffMode(CutoffRight),
75 _alwaysBracketSender(false),
76 _selectingItem(nullptr),
81 _leftButtonPressed(false)
83 MessageFilter *filter = qobject_cast<MessageFilter *>(model);
84 if (filter && filter->isSingleBufferFilter()) {
85 _singleBufferId = filter->singleBufferId();
89 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _markerLine, SLOT(sceneRectChanged(const QRectF &)));
91 ChatViewSettings defaultSettings;
92 _defaultFirstColHandlePos = defaultSettings.value("FirstColumnHandlePos", 80).toInt();
93 _defaultSecondColHandlePos = defaultSettings.value("SecondColumnHandlePos", 200).toInt();
95 ChatViewSettings viewSettings(this);
96 _firstColHandlePos = viewSettings.value("FirstColumnHandlePos", _defaultFirstColHandlePos).toInt();
97 _secondColHandlePos = viewSettings.value("SecondColumnHandlePos", _defaultSecondColHandlePos).toInt();
99 _firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator());
100 addItem(_firstColHandle);
101 _firstColHandle->setXPos(_firstColHandlePos);
102 connect(_firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(firstHandlePositionChanged(qreal)));
103 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _firstColHandle, SLOT(sceneRectChanged(const QRectF &)));
105 _secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator());
106 addItem(_secondColHandle);
107 _secondColHandle->setXPos(_secondColHandlePos);
108 connect(_secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(secondHandlePositionChanged(qreal)));
110 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _secondColHandle, SLOT(sceneRectChanged(const QRectF &)));
114 if (model->rowCount() > 0)
115 rowsInserted(QModelIndex(), 0, model->rowCount() - 1);
117 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
118 this, SLOT(rowsInserted(const QModelIndex &, int, int)));
119 connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
120 this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
121 connect(model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
122 this, SLOT(rowsRemoved()));
123 connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), SLOT(dataChanged(QModelIndex, QModelIndex)));
125 #if defined HAVE_WEBKIT || defined HAVE_WEBENGINE
126 webPreview.timer.setSingleShot(true);
127 connect(&webPreview.timer, SIGNAL(timeout()), this, SLOT(webPreviewNextStep()));
129 _showWebPreview = defaultSettings.showWebPreview();
130 defaultSettings.notify("ShowWebPreview", this, SLOT(showWebPreviewChanged()));
132 _showSenderBrackets = defaultSettings.showSenderBrackets();
133 defaultSettings.notify("ShowSenderBrackets", this, SLOT(showSenderBracketsChanged()));
135 _useCustomTimestampFormat = defaultSettings.useCustomTimestampFormat();
136 defaultSettings.notify("UseCustomTimestampFormat", this, SLOT(useCustomTimestampFormatChanged()));
138 _timestampFormatString = defaultSettings.timestampFormatString();
139 defaultSettings.notify("TimestampFormat", this, SLOT(timestampFormatStringChanged()));
140 updateTimestampHasBrackets();
142 _clickTimer.setInterval(QApplication::doubleClickInterval());
143 _clickTimer.setSingleShot(true);
144 connect(&_clickTimer, SIGNAL(timeout()), SLOT(clickTimeout()));
146 setItemIndexMethod(QGraphicsScene::NoIndex);
150 ChatScene::~ChatScene()
155 ChatView *ChatScene::chatView() const
161 ColumnHandleItem *ChatScene::firstColumnHandle() const
163 return _firstColHandle;
167 ColumnHandleItem *ChatScene::secondColumnHandle() const
169 return _secondColHandle;
172 void ChatScene::resetColumnWidths()
174 //make sure first column is at least 80 px wide, second 120 px
175 int firstColHandlePos = qMax(_defaultFirstColHandlePos,
177 int secondColHandlePos = qMax(_defaultSecondColHandlePos,
178 firstColHandlePos + 120);
180 _firstColHandle->setXPos(firstColHandlePos);
181 _secondColHandle->setXPos(secondColHandlePos);
184 ChatLine *ChatScene::chatLine(MsgId msgId, bool matchExact, bool ignoreDayChange) const
189 QList<ChatLine *>::ConstIterator start = _lines.begin();
190 QList<ChatLine *>::ConstIterator end = _lines.end();
191 QList<ChatLine *>::ConstIterator middle;
193 int n = int(end - start);
198 middle = start + half;
199 if ((*middle)->msgId() < msgId) {
208 if (start != end && (*start)->msgId() == msgId && (ignoreDayChange ? (*start)->msgType() != Message::DayChange : true))
214 if (start == _lines.begin()) // not (yet?) in our scene
217 // if we didn't find the exact msgId, take the next-lower one (this makes sense for lastSeen)
219 if (start == end) { // higher than last element
220 if (!ignoreDayChange)
221 return _lines.last();
223 for (int i = _lines.count() -1; i >= 0; i--) {
224 if (_lines.at(i)->msgType() != Message::DayChange)
230 // return the next-lower line
231 if (!ignoreDayChange)
235 if ((*(--start))->msgType() != Message::DayChange)
238 while (start != _lines.begin());
243 ChatItem *ChatScene::chatItemAt(const QPointF &scenePos) const
245 foreach(QGraphicsItem *item, items(scenePos, Qt::IntersectsItemBoundingRect, Qt::AscendingOrder)) {
246 ChatLine *line = qgraphicsitem_cast<ChatLine *>(item);
248 return line->itemAt(line->mapFromScene(scenePos));
254 bool ChatScene::containsBuffer(const BufferId &id) const
256 MessageFilter *filter = qobject_cast<MessageFilter *>(model());
258 return filter->containsBuffer(id);
264 void ChatScene::setMarkerLineVisible(bool visible)
266 _markerLineVisible = visible;
267 if (visible && _markerLineValid)
268 markerLine()->setVisible(true);
270 markerLine()->setVisible(false);
274 void ChatScene::setMarkerLine(MsgId msgId)
276 if (!isSingleBufferScene())
279 if (!msgId.isValid())
280 msgId = Client::markerLine(singleBufferId());
282 if (msgId.isValid()) {
283 ChatLine *line = chatLine(msgId, false, true);
285 markerLine()->setChatLine(line);
286 // if this was the last line, we won't see it because it's outside the sceneRect
287 // .. which is exactly what we want :)
288 markerLine()->setPos(line->pos() + QPointF(0, line->height()));
290 // DayChange messages might have been hidden outside the scene rect, don't make the markerline visible then!
291 if (markerLine()->pos().y() >= sceneRect().y()) {
292 _markerLineValid = true;
293 if (_markerLineVisible)
294 markerLine()->setVisible(true);
295 if (_markerLineJumpPending) {
296 _markerLineJumpPending = false;
297 if (markerLine()->isVisible()) {
298 markerLine()->ensureVisible(QRectF(), 50, 50);
305 _markerLineValid = false;
306 markerLine()->setVisible(false);
310 void ChatScene::jumpToMarkerLine(bool requestBacklog)
312 if (!isSingleBufferScene())
315 if (markerLine()->isVisible()) {
316 markerLine()->ensureVisible(QRectF(), 50, 50);
319 if (!_markerLineValid && requestBacklog) {
320 MsgId msgId = Client::markerLine(singleBufferId());
321 if (msgId.isValid()) {
322 _markerLineJumpPending = true;
323 Client::backlogManager()->requestBacklog(singleBufferId(), msgId, -1, -1, 0);
325 // If we filtered out the lastSeenMsg (by changing filters after setting it), we'd never jump because the above request
326 // won't fetch any prior lines. Thus, trigger a dynamic backlog request just in case, so repeated
327 // jump tries will eventually cause enough backlog to be fetched.
328 // This is a bit hackish, but not wasteful, as jumping to the top of the ChatView would trigger a dynamic fetch anyway.
329 this->requestBacklog();
335 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end)
339 // QModelIndex sidx = model()->index(start, 2);
340 // QModelIndex eidx = model()->index(end, 2);
341 // qDebug() << "rowsInserted:";
343 // QModelIndex ssidx = model()->index(start - 1, 2);
344 // qDebug() << "Start--:" << start - 1 << ssidx.data(MessageModel::MsgIdRole).value<MsgId>()
345 // << ssidx.data(Qt::DisplayRole).toString();
347 // qDebug() << "Start:" << start << sidx.data(MessageModel::MsgIdRole).value<MsgId>()
348 // << sidx.data(Qt::DisplayRole).toString();
349 // qDebug() << "End:" << end << eidx.data(MessageModel::MsgIdRole).value<MsgId>()
350 // << eidx.data(Qt::DisplayRole).toString();
351 // if(end + 1 < model()->rowCount()) {
352 // QModelIndex eeidx = model()->index(end + 1, 2);
353 // qDebug() << "End++:" << end + 1 << eeidx.data(MessageModel::MsgIdRole).value<MsgId>()
354 // << eeidx.data(Qt::DisplayRole).toString();
359 qreal width = _sceneRect.width();
360 bool atBottom = (start == _lines.count());
361 bool atTop = !atBottom && (start == 0);
363 if (start < _lines.count()) {
364 y = _lines.value(start)->y();
366 else if (atBottom && !_lines.isEmpty()) {
367 y = _lines.last()->y() + _lines.last()->height();
370 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
371 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
372 qreal timestampWidth = firstColumnHandle()->sceneLeft();
373 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
374 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
377 for (int i = end; i >= start; i--) {
378 ChatLine *line = new ChatLine(i, model(),
380 timestampWidth, senderWidth, contentsWidth,
381 senderPos, contentsPos);
383 line->setPos(0, y-h);
384 _lines.insert(start, line);
389 for (int i = start; i <= end; i++) {
390 ChatLine *line = new ChatLine(i, model(),
392 timestampWidth, senderWidth, contentsWidth,
393 senderPos, contentsPos);
394 line->setPos(0, y+h);
396 _lines.insert(i, line);
401 // update existing items
402 for (int i = end+1; i < _lines.count(); i++) {
403 _lines[i]->setRow(i);
407 if (_selectionStart >= 0) {
408 int offset = end - start + 1;
409 int oldStart = _selectionStart;
410 if (_selectionStart >= start)
411 _selectionStart += offset;
412 if (_selectionEnd >= start) {
413 _selectionEnd += offset;
414 if (_selectionStart == oldStart)
415 for (int i = start; i < start + offset; i++)
416 _lines[i]->setSelected(true);
418 if (_firstSelectionRow >= start)
419 _firstSelectionRow += offset;
422 // neither pre- or append means we have to do dirty work: move items...
423 if (!(atTop || atBottom)) {
424 ChatLine *line = nullptr;
425 for (int i = 0; i <= end; i++) {
427 line->setPos(0, line->pos().y() - h);
428 if (line == markerLine()->chatLine())
429 markerLine()->setPos(line->pos() + QPointF(0, line->height()));
433 // check if all went right
434 Q_ASSERT(start == 0 || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
436 // if(_lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() != _lines.at(start)->pos().y()) {
437 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
438 // qDebug() << "line[start - 1]:" << _lines.at(start - 1)->pos().y() << "+" << _lines.at(start - 1)->height() << "=" << _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height();
439 // qDebug() << "line[start]" << _lines.at(start)->pos().y();
440 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
444 Q_ASSERT(end + 1 == _lines.count() || _lines.at(end)->pos().y() + _lines.at(end)->height() == _lines.at(end + 1)->pos().y());
445 // if(end + 1 < _lines.count()) {
446 // if(_lines.at(end)->pos().y() + _lines.at(end)->height() != _lines.at(end + 1)->pos().y()) {
447 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
448 // qDebug() << "line[end]:" << _lines.at(end)->pos().y() << "+" << _lines.at(end)->height() << "=" << _lines.at(end)->pos().y() + _lines.at(end)->height();
449 // qDebug() << "line[end+1]" << _lines.at(end + 1)->pos().y();
450 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
456 if (start < _firstLineRow) {
457 int prevFirstLineRow = _firstLineRow + (end - start + 1);
458 for (int i = end + 1; i < prevFirstLineRow; i++) {
459 _lines.at(i)->show();
462 // force new search for first proper line
467 emit lastLineChanged(_lines.last(), h);
470 // now move the marker line if necessary. we don't need to do anything if we appended lines though...
471 if (!_markerLineValid)
476 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
480 qreal h = 0; // total height of removed items;
482 bool atTop = (start == 0);
483 bool atBottom = (end == _lines.count() - 1);
486 if (_selectingItem) {
487 int row = _selectingItem->row();
488 if (row >= start && row <= end)
489 setSelectingItem(nullptr);
492 // remove items from scene
493 QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
494 int lineCount = start;
495 while (lineIter != _lines.end() && lineCount <= end) {
496 if ((*lineIter) == markerLine()->chatLine())
497 markerLine()->setChatLine(nullptr);
498 h += (*lineIter)->height();
500 lineIter = _lines.erase(lineIter);
504 // update rows of remaining chatlines
505 for (int i = start; i < _lines.count(); i++) {
506 _lines.at(i)->setRow(i);
510 if (_selectionStart >= 0) {
511 int offset = end - start + 1;
512 if (_selectionStart >= start)
513 _selectionStart = qMax(_selectionStart - offset, start);
514 if (_selectionEnd >= start)
515 _selectionEnd -= offset;
516 if (_firstSelectionRow >= start)
517 _firstSelectionRow -= offset;
519 if (_selectionEnd < _selectionStart) {
520 _isSelecting = false;
521 _selectionStart = -1;
525 // neither removing at bottom or top means we have to move items...
526 if (!(atTop || atBottom)) {
529 int moveEnd = _lines.count() - 1;
530 if (start < _lines.count() - start) {
539 ChatLine *line = nullptr;
540 for (int i = moveStart; i <= moveEnd; i++) {
542 line->setPos(0, line->pos().y() + offset);
546 Q_ASSERT(start == 0 || start >= _lines.count() || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
549 // when searching for the first non-date-line we have to take into account that our
550 // model still contains the just removed lines so we cannot simply call updateSceneRect()
551 int numRows = model()->rowCount();
552 QModelIndex firstLineIdx;
554 bool needOffset = false;
557 if (_firstLineRow >= start && _firstLineRow <= end) {
558 _firstLineRow = end + 1;
561 firstLineIdx = model()->index(_firstLineRow, 0);
563 while ((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) == Message::DayChange && _firstLineRow < numRows);
566 _firstLineRow -= end - start + 1;
571 void ChatScene::rowsRemoved()
573 // move the marker line if necessary
578 void ChatScene::dataChanged(const QModelIndex &tl, const QModelIndex &br)
580 layout(tl.row(), br.row(), _sceneRect.width());
584 void ChatScene::updateForViewport(qreal width, qreal height)
586 _viewportHeight = height;
591 void ChatScene::setWidth(qreal width)
593 if (width == _sceneRect.width())
595 layout(0, _lines.count()-1, width);
599 void ChatScene::layout(int start, int end, qreal width)
601 // clock_t startT = clock();
603 // disabling the index while doing this complex updates is about
604 // 2 to 10 times faster!
605 //setItemIndexMethod(QGraphicsScene::NoIndex);
609 qreal linePos = _lines.at(row)->scenePos().y() + _lines.at(row)->height();
610 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
611 while (row >= start) {
612 _lines.at(row--)->setGeometryByWidth(width, contentsWidth, linePos);
616 // remaining items don't need geometry changes, but maybe repositioning?
617 ChatLine *line = _lines.at(row);
618 qreal offset = linePos - (line->scenePos().y() + line->height());
621 line = _lines.at(row--);
622 line->setPos(0, line->scenePos().y() + offset);
628 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
630 updateSceneRect(width);
633 emit layoutChanged();
635 // clock_t endT = clock();
636 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
640 void ChatScene::firstHandlePositionChanged(qreal xpos)
642 if (_firstColHandlePos == xpos)
645 _firstColHandlePos = xpos >= 0 ? xpos : 0;
646 ChatViewSettings viewSettings(this);
647 viewSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
648 ChatViewSettings defaultSettings;
649 defaultSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
651 // clock_t startT = clock();
653 // disabling the index while doing this complex updates is about
654 // 2 to 10 times faster!
655 //setItemIndexMethod(QGraphicsScene::NoIndex);
657 QList<ChatLine *>::iterator lineIter = _lines.end();
658 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
659 qreal timestampWidth = firstColumnHandle()->sceneLeft();
660 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
661 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
663 while (lineIter != lineIterBegin) {
665 (*lineIter)->setFirstColumn(timestampWidth, senderWidth, senderPos);
667 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
671 // clock_t endT = clock();
672 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
676 void ChatScene::secondHandlePositionChanged(qreal xpos)
678 if (_secondColHandlePos == xpos)
681 _secondColHandlePos = xpos;
682 ChatViewSettings viewSettings(this);
683 viewSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
684 ChatViewSettings defaultSettings;
685 defaultSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
687 // clock_t startT = clock();
689 // disabling the index while doing this complex updates is about
690 // 2 to 10 times faster!
691 //setItemIndexMethod(QGraphicsScene::NoIndex);
693 QList<ChatLine *>::iterator lineIter = _lines.end();
694 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
695 qreal linePos = _sceneRect.y() + _sceneRect.height();
696 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
697 qreal contentsWidth = _sceneRect.width() - secondColumnHandle()->sceneRight();
698 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
699 while (lineIter != lineIterBegin) {
701 (*lineIter)->setSecondColumn(senderWidth, contentsWidth, contentsPos, linePos);
703 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
707 emit layoutChanged();
709 // clock_t endT = clock();
710 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
714 void ChatScene::setHandleXLimits()
716 _firstColHandle->setXLimits(0, _secondColHandle->sceneLeft());
717 _secondColHandle->setXLimits(_firstColHandle->sceneRight(), width() - minContentsWidth);
722 void ChatScene::setSelectingItem(ChatItem *item)
724 if (_selectingItem) _selectingItem->clearSelection();
725 _selectingItem = item;
729 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos)
731 _selectionStart = _selectionEnd = _firstSelectionRow = item->row();
732 _selectionStartCol = _selectionMinCol = item->column();
734 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
735 updateSelection(item->mapToScene(itemPos));
739 void ChatScene::updateSelection(const QPointF &pos)
741 int curRow = rowByScenePos(pos);
742 if (curRow < 0) return;
743 int curColumn = (int)columnByScenePos(pos);
744 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
745 if (minColumn != _selectionMinCol) {
746 _selectionMinCol = minColumn;
747 for (int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
748 _lines[l]->setSelected(true, minColumn);
751 int newstart = qMin(curRow, _firstSelectionRow);
752 int newend = qMax(curRow, _firstSelectionRow);
753 if (newstart < _selectionStart) {
754 for (int l = newstart; l < _selectionStart; l++)
755 _lines[l]->setSelected(true, minColumn);
757 if (newstart > _selectionStart) {
758 for (int l = _selectionStart; l < newstart; l++)
759 _lines[l]->setSelected(false);
761 if (newend > _selectionEnd) {
762 for (int l = _selectionEnd+1; l <= newend; l++)
763 _lines[l]->setSelected(true, minColumn);
765 if (newend < _selectionEnd) {
766 for (int l = newend+1; l <= _selectionEnd; l++)
767 _lines[l]->setSelected(false);
770 _selectionStart = newstart;
771 _selectionEnd = newend;
773 if (newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
774 if (!_selectingItem) {
775 // _selectingItem has been removed already
778 _lines[curRow]->setSelected(false);
779 _isSelecting = false;
780 _selectionStart = -1;
781 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
786 bool ChatScene::isPosOverSelection(const QPointF &pos) const
788 ChatItem *chatItem = chatItemAt(pos);
791 if (hasGlobalSelection()) {
792 int row = chatItem->row();
793 if (row >= qMin(_selectionStart, _selectionEnd) && row <= qMax(_selectionStart, _selectionEnd))
794 return columnByScenePos(pos) >= _selectionMinCol;
797 return chatItem->isPosOverSelection(chatItem->mapFromScene(pos));
803 bool ChatScene::isScrollingAllowed() const
808 // TODO: Handle clicks and single-item selections too
814 /******** MOUSE HANDLING **************************************************************************/
816 void ChatScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
818 QPointF pos = event->scenePos();
821 // zoom actions and similar
822 chatView()->addActionsToMenu(&menu, pos);
825 // item-specific options (select link etc)
826 ChatItem *item = chatItemAt(pos);
828 item->addActionsToMenu(&menu, item->mapFromScene(pos));
830 // no item -> default scene actions
831 GraphicalUi::contextMenuActionProvider()->addActions(&menu, filter(), BufferId());
833 // If we have text selected, insert the Copy Selection as first item
834 if (isPosOverSelection(pos)) {
835 QAction *sep = menu.insertSeparator(menu.actions().first());
836 QAction *act = new Action(icon::get("edit-copy"), tr("Copy Selection"), &menu, this,
837 SLOT(selectionToClipboard()), QKeySequence::Copy);
838 menu.insertAction(sep, act);
840 QString searchSelectionText = selection();
841 if (searchSelectionText.length() > _webSearchSelectionTextMaxVisible)
842 searchSelectionText = searchSelectionText.left(_webSearchSelectionTextMaxVisible).append(QString::fromUtf8("…"));
843 searchSelectionText = tr("Search '%1'").arg(searchSelectionText);
845 QAction *webSearchAction = new Action(icon::get("edit-find"), searchSelectionText, &menu, this, SLOT(webSearchOnSelection()));
846 menu.insertAction(sep, webSearchAction);
849 if (QtUi::mainWindow()->menuBar()->isHidden())
850 menu.addAction(QtUi::actionCollection("General")->action("ToggleMenuBar"));
852 // show column reset action if columns have been resized in this session or there is at least one very narrow column
853 if ((_firstColHandlePos != _defaultFirstColHandlePos) || (_secondColHandlePos != _defaultSecondColHandlePos) ||
854 (_firstColHandlePos <= 10) || (_secondColHandlePos - _firstColHandlePos <= 10))
855 menu.addAction(new Action(tr("Reset Column Widths"), &menu, this, SLOT(resetColumnWidths()), 0));
857 menu.exec(event->screenPos());
861 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
863 if (event->buttons() == Qt::LeftButton) {
864 if (!_clickHandled && (event->scenePos() - _clickPos).toPoint().manhattanLength() >= QApplication::startDragDistance()) {
865 if (_clickTimer.isActive())
867 if (_clickMode == SingleClick && isPosOverSelection(_clickPos))
868 initiateDrag(event->widget());
870 _clickMode = DragStartClick;
871 handleClick(Qt::LeftButton, _clickPos);
873 _clickMode = NoClick;
876 updateSelection(event->scenePos());
877 emit mouseMoveWhileSelecting(event->scenePos());
880 else if (_clickHandled && _clickMode < DoubleClick)
881 QGraphicsScene::mouseMoveEvent(event);
884 QGraphicsScene::mouseMoveEvent(event);
888 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
890 if (event->buttons() == Qt::LeftButton) {
891 _leftButtonPressed = true;
892 _clickHandled = false;
893 if (!isPosOverSelection(event->scenePos())) {
894 // immediately clear selection if clicked outside; otherwise, wait for potential drag
897 if (_clickMode != NoClick && _clickTimer.isActive()) {
898 switch (_clickMode) {
900 _clickMode = SingleClick; break;
902 _clickMode = DoubleClick; break;
904 _clickMode = TripleClick; break;
906 _clickMode = DoubleClick; break;
910 handleClick(Qt::LeftButton, _clickPos);
913 _clickMode = SingleClick;
914 _clickPos = event->scenePos();
918 if (event->type() == QEvent::GraphicsSceneMouseDoubleClick)
919 QGraphicsScene::mouseDoubleClickEvent(event);
921 QGraphicsScene::mousePressEvent(event);
925 void ChatScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
927 // we check for doubleclick ourselves, so just call press handler
928 mousePressEvent(event);
932 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
934 if (event->button() == Qt::LeftButton && _leftButtonPressed) {
935 _leftButtonPressed = false;
936 if (_clickMode != NoClick) {
937 if (_clickMode == SingleClick)
940 if (!_clickTimer.isActive())
941 handleClick(Qt::LeftButton, _clickPos);
944 // no click -> drag or selection move
945 if (isGloballySelecting()) {
946 selectionToClipboard(QClipboard::Selection);
947 _isSelecting = false;
953 QGraphicsScene::mouseReleaseEvent(event);
957 void ChatScene::clickTimeout()
959 if (!_leftButtonPressed && _clickMode == SingleClick)
960 handleClick(Qt::LeftButton, _clickPos);
964 void ChatScene::handleClick(Qt::MouseButton button, const QPointF &scenePos)
966 if (button == Qt::LeftButton) {
969 // Now send click down to items
970 ChatItem *chatItem = chatItemAt(scenePos);
972 chatItem->handleClick(chatItem->mapFromScene(scenePos), _clickMode);
974 _clickHandled = true;
979 void ChatScene::initiateDrag(QWidget *source)
981 QDrag *drag = new QDrag(source);
982 QMimeData *mimeData = new QMimeData;
983 mimeData->setText(selection());
984 drag->setMimeData(mimeData);
986 drag->exec(Qt::CopyAction);
990 /******** SELECTIONS ******************************************************************************/
992 void ChatScene::selectionToClipboard(QClipboard::Mode mode)
997 stringToClipboard(selection(), mode);
1001 void ChatScene::stringToClipboard(const QString &str_, QClipboard::Mode mode)
1004 // remove trailing linefeeds
1005 if (str.endsWith('\n'))
1009 case QClipboard::Clipboard:
1010 QApplication::clipboard()->setText(str);
1012 case QClipboard::Selection:
1013 if (QApplication::clipboard()->supportsSelection())
1014 QApplication::clipboard()->setText(str, QClipboard::Selection);
1022 //!\brief Convert current selection to human-readable string.
1023 QString ChatScene::selection() const
1025 //TODO Make selection format configurable!
1026 if (hasGlobalSelection()) {
1027 int start = qMin(_selectionStart, _selectionEnd);
1028 int end = qMax(_selectionStart, _selectionEnd);
1029 if (start < 0 || end >= _lines.count()) {
1030 qDebug() << "Invalid selection range:" << start << end;
1035 for (int l = start; l <= end; l++) {
1036 if (_selectionMinCol == ChatLineModel::TimestampColumn) {
1037 ChatItem *item = _lines[l]->item(ChatLineModel::TimestampColumn);
1038 if (!_showSenderBrackets && !_timestampHasBrackets) {
1039 // Only re-add brackets if the current timestamp format does not include them
1040 // -and- sender brackets are disabled. Don't filter on Message::Plain as
1041 // timestamp brackets affect all types of messages.
1042 // Remove any spaces before and after, otherwise it may look weird.
1043 result += QString("[%1] ").arg(item->data(MessageModel::DisplayRole)
1044 .toString().trimmed());
1046 result += item->data(MessageModel::DisplayRole).toString() + " ";
1049 if (_selectionMinCol <= ChatLineModel::SenderColumn) {
1050 ChatItem *item = _lines[l]->item(ChatLineModel::SenderColumn);
1051 if (!_showSenderBrackets && (_alwaysBracketSender
1052 || item->chatLine()->msgType() == Message::Plain)) {
1053 // Copying to plain-text. Re-add the sender brackets if they're normally hidden
1056 // * All messages in the Chat Monitor
1058 // The Chat Monitor sets alwaysBracketSender() to true.
1059 result += QString("<%1> ").arg(item->data(MessageModel::DisplayRole)
1062 result += item->data(MessageModel::DisplayRole).toString() + " ";
1065 result += _lines[l]->item(ChatLineModel::ContentsColumn)
1066 ->data(MessageModel::DisplayRole).toString() + "\n";
1070 else if (selectingItem())
1071 return selectingItem()->selection();
1076 bool ChatScene::hasSelection() const
1078 return hasGlobalSelection() || (selectingItem() && selectingItem()->hasSelection());
1082 bool ChatScene::hasGlobalSelection() const
1084 return _selectionStart >= 0;
1088 bool ChatScene::isGloballySelecting() const
1090 return _isSelecting;
1094 void ChatScene::clearGlobalSelection()
1096 if (hasGlobalSelection()) {
1097 for (int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++)
1098 _lines[l]->setSelected(false);
1099 _isSelecting = false;
1100 _selectionStart = -1;
1105 void ChatScene::clearSelection()
1107 clearGlobalSelection();
1108 if (selectingItem())
1109 selectingItem()->clearSelection();
1113 /******** *************************************************************************************/
1115 void ChatScene::webSearchOnSelection()
1117 if (!hasSelection())
1120 ChatViewSettings settings;
1121 QString webSearchBaseUrl = settings.webSearchUrlFormatString();
1122 QString webSearchUrl = webSearchBaseUrl.replace(QString("%s"), selection());
1123 QUrl url = QUrl::fromUserInput(webSearchUrl);
1124 QDesktopServices::openUrl(url);
1128 /******** *************************************************************************************/
1130 void ChatScene::requestBacklog()
1132 MessageFilter *filter = qobject_cast<MessageFilter *>(model());
1134 return filter->requestBacklog();
1139 ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) const
1141 if (x < _firstColHandle->x())
1142 return ChatLineModel::TimestampColumn;
1143 if (x < _secondColHandle->x())
1144 return ChatLineModel::SenderColumn;
1146 return ChatLineModel::ContentsColumn;
1150 int ChatScene::rowByScenePos(qreal y) const
1152 QList<QGraphicsItem *> itemList = items(QPointF(0, y));
1154 // ChatLine should be at the bottom of the list
1155 for (int i = itemList.count()-1; i >= 0; i--) {
1156 ChatLine *line = qgraphicsitem_cast<ChatLine *>(itemList.at(i));
1164 void ChatScene::updateSceneRect(qreal width)
1166 if (_lines.isEmpty()) {
1167 updateSceneRect(QRectF(0, 0, width, 0));
1171 // we hide day change messages at the top by making the scene rect smaller
1172 // and by calling QGraphicsItem::hide() on all leading day change messages
1173 // the first one is needed to ensure proper scrollbar ranges
1174 // the second for cases where the viewport is larger then the set scenerect
1175 // (in this case the items are shown anyways)
1176 if (_firstLineRow == -1) {
1177 int numRows = model()->rowCount();
1179 QModelIndex firstLineIdx;
1180 while (_firstLineRow < numRows) {
1181 firstLineIdx = model()->index(_firstLineRow, 0);
1182 if ((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
1184 _lines.at(_firstLineRow)->hide();
1189 // the following call should be safe. If it crashes something went wrong during insert/remove
1190 if (_firstLineRow < _lines.count()) {
1191 ChatLine *firstLine = _lines.at(_firstLineRow);
1192 ChatLine *lastLine = _lines.last();
1193 updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
1197 updateSceneRect(QRectF(0, 0, width, 0));
1202 void ChatScene::updateSceneRect(const QRectF &rect)
1210 // ========================================
1211 // Webkit/WebEngine Only stuff
1212 // ========================================
1213 #if defined HAVE_WEBKIT || defined HAVE_WEBENGINE
1214 void ChatScene::loadWebPreview(ChatItem *parentItem, const QUrl &url, const QRectF &urlRect)
1216 if (!_showWebPreview)
1219 if (webPreview.urlRect != urlRect)
1220 webPreview.urlRect = urlRect;
1222 if (webPreview.parentItem != parentItem)
1223 webPreview.parentItem = parentItem;
1225 if (webPreview.url != url) {
1226 webPreview.url = url;
1227 // prepare to load a different URL
1228 if (webPreview.previewItem) {
1229 if (webPreview.previewItem->scene())
1230 removeItem(webPreview.previewItem);
1231 delete webPreview.previewItem;
1232 webPreview.previewItem = nullptr;
1234 webPreview.previewState = WebPreview::NoPreview;
1237 if (webPreview.url.isEmpty())
1240 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
1241 switch (webPreview.previewState) {
1242 case WebPreview::NoPreview:
1243 webPreview.previewState = WebPreview::NewPreview;
1244 webPreview.timer.start(500);
1246 case WebPreview::NewPreview:
1247 case WebPreview::DelayPreview:
1248 case WebPreview::ShowPreview:
1249 // we're already waiting for the next step or showing the preview
1251 case WebPreview::HidePreview:
1252 // we still have a valid preview
1253 webPreview.previewState = WebPreview::DelayPreview;
1254 webPreview.timer.start(1000);
1257 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
1261 void ChatScene::webPreviewNextStep()
1263 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
1264 switch (webPreview.previewState) {
1265 case WebPreview::NoPreview:
1267 case WebPreview::NewPreview:
1268 Q_ASSERT(!webPreview.previewItem);
1269 webPreview.previewItem = new WebPreviewItem(webPreview.url);
1270 webPreview.previewState = WebPreview::DelayPreview;
1271 webPreview.timer.start(1000);
1273 case WebPreview::DelayPreview:
1274 Q_ASSERT(webPreview.previewItem);
1275 // calc position and show
1277 qreal previewY = webPreview.urlRect.bottom();
1278 qreal previewX = webPreview.urlRect.x();
1279 if (previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
1280 previewY = webPreview.urlRect.y() - webPreview.previewItem->boundingRect().height();
1282 if (previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
1283 previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
1285 webPreview.previewItem->setPos(previewX, previewY);
1287 addItem(webPreview.previewItem);
1288 webPreview.previewState = WebPreview::ShowPreview;
1290 case WebPreview::ShowPreview:
1291 qWarning() << "ChatScene::webPreviewNextStep() called while in ShowPreview Step!";
1292 qWarning() << "removing preview";
1293 if (webPreview.previewItem && webPreview.previewItem->scene()) {
1294 removeItem(webPreview.previewItem);
1297 // Intentional fallthrough
1299 case WebPreview::HidePreview:
1300 if (webPreview.previewItem) {
1301 delete webPreview.previewItem;
1302 webPreview.previewItem = nullptr;
1304 webPreview.parentItem = nullptr;
1305 webPreview.url = QUrl();
1306 webPreview.urlRect = QRectF();
1307 webPreview.previewState = WebPreview::NoPreview;
1309 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
1313 void ChatScene::clearWebPreview(ChatItem *parentItem)
1315 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
1316 switch (webPreview.previewState) {
1317 case WebPreview::NewPreview:
1318 webPreview.previewState = WebPreview::NoPreview; // we haven't loaded anything yet
1320 case WebPreview::ShowPreview:
1321 if (parentItem == nullptr || webPreview.parentItem == parentItem) {
1322 if (webPreview.previewItem && webPreview.previewItem->scene())
1323 removeItem(webPreview.previewItem);
1326 // Intentional fallthrough
1328 case WebPreview::DelayPreview:
1329 // we're just loading, so haven't shown the preview yet.
1330 webPreview.previewState = WebPreview::HidePreview;
1331 webPreview.timer.start(5000);
1333 case WebPreview::NoPreview:
1334 case WebPreview::HidePreview:
1337 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
1343 // ========================================
1344 // end of webkit only
1345 // ========================================
1347 // Local configuration caching
1348 void ChatScene::showWebPreviewChanged()
1350 ChatViewSettings settings;
1351 _showWebPreview = settings.showWebPreview();
1354 void ChatScene::showSenderBracketsChanged()
1356 ChatViewSettings settings;
1357 _showSenderBrackets = settings.showSenderBrackets();
1360 void ChatScene::useCustomTimestampFormatChanged()
1362 ChatViewSettings settings;
1363 _useCustomTimestampFormat = settings.useCustomTimestampFormat();
1364 updateTimestampHasBrackets();
1367 void ChatScene::timestampFormatStringChanged()
1369 ChatViewSettings settings;
1370 _timestampFormatString = settings.timestampFormatString();
1371 updateTimestampHasBrackets();
1374 void ChatScene::updateTimestampHasBrackets()
1376 // Calculate these parameters only as needed, rather than on-demand
1378 if (!_useCustomTimestampFormat) {
1379 // The default timestamp format string does not have brackets, no need to check.
1380 // If UiStyle::updateSystemTimestampFormat() has brackets added, change this, too.
1381 _timestampHasBrackets = false;
1383 // Does the timestamp format contain brackets? For example:
1384 // Classic: "[hh:mm:ss]"
1385 // Modern: " hh:mm:ss"
1387 // Match groups of any opening or closing brackets - (), {}, [], <>, (>, {], etc:
1388 // ^\s*[({[<].+[)}\]>]\s*$
1389 // [...] is a character group containing ...
1390 // ^ matches start of string
1391 // \s* matches any amount of whitespace
1392 // [({[<] matches (, {, [, or <
1393 // .+ matches one or more characters
1394 // [)}\]>] matches ), }, ], or >, escaping the ]
1395 // $ matches end of string
1396 // Alternatively, if opening and closing brackets must be in pairs, use this:
1397 // (^\s*\(.+\)\s*$)|(^\s*\{.+\}\s*$)|(^\s*\[.+\]\s*$)|(^\s*<.+>\s*$)
1398 // Note that '\' must be escaped as '\\'
1399 // Helpful interactive website for debugging and explaining: https://regex101.com/
1400 const QRegExp regExpMatchBrackets("^\\s*[({[<].+[)}\\]>]\\s*$");
1401 _timestampHasBrackets = regExpMatchBrackets.exactMatch(_timestampFormatString);