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>
24 #include <QGraphicsSceneMouseEvent>
27 #include <QPersistentModelIndex>
41 #include "chatlinemodelitem.h"
42 #include "chatscene.h"
45 #include "clientbacklogmanager.h"
46 #include "columnhandleitem.h"
47 #include "contextmenuactionprovider.h"
48 #include "iconloader.h"
50 #include "markerlineitem.h"
51 #include "messagefilter.h"
53 #include "qtuistyle.h"
54 #include "chatviewsettings.h"
55 #include "webpreviewitem.h"
57 const qreal minContentsWidth = 200;
59 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, ChatView *parent)
60 : QGraphicsScene(0, 0, width, 0, (QObject *)parent),
64 _singleBufferId(BufferId()),
65 _sceneRect(0, 0, width, 0),
68 _markerLine(new MarkerLineItem(width)),
69 _markerLineVisible(false),
70 _markerLineValid(false),
71 _markerLineJumpPending(false),
72 _cutoffMode(CutoffRight),
78 _leftButtonPressed(false)
80 MessageFilter *filter = qobject_cast<MessageFilter*>(model);
81 if(filter && filter->isSingleBufferFilter()) {
82 _singleBufferId = filter->singleBufferId();
86 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _markerLine, SLOT(sceneRectChanged(const QRectF &)));
88 ChatViewSettings defaultSettings;
89 int defaultFirstColHandlePos = defaultSettings.value("FirstColumnHandlePos", 80).toInt();
90 int defaultSecondColHandlePos = defaultSettings.value("SecondColumnHandlePos", 200).toInt();
92 ChatViewSettings viewSettings(this);
93 _firstColHandlePos = viewSettings.value("FirstColumnHandlePos", defaultFirstColHandlePos).toInt();
94 _secondColHandlePos = viewSettings.value("SecondColumnHandlePos", defaultSecondColHandlePos).toInt();
96 _firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator());
97 addItem(_firstColHandle);
98 _firstColHandle->setXPos(_firstColHandlePos);
99 connect(_firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(firstHandlePositionChanged(qreal)));
100 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _firstColHandle, SLOT(sceneRectChanged(const QRectF &)));
102 _secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator());
103 addItem(_secondColHandle);
104 _secondColHandle->setXPos(_secondColHandlePos);
105 connect(_secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(secondHandlePositionChanged(qreal)));
107 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _secondColHandle, SLOT(sceneRectChanged(const QRectF &)));
111 if(model->rowCount() > 0)
112 rowsInserted(QModelIndex(), 0, model->rowCount() - 1);
114 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
115 this, SLOT(rowsInserted(const QModelIndex &, int, int)));
116 connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
117 this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
118 connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
119 this, SLOT(rowsRemoved()));
120 connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), SLOT(dataChanged(QModelIndex, QModelIndex)));
123 webPreview.timer.setSingleShot(true);
124 connect(&webPreview.timer, SIGNAL(timeout()), this, SLOT(webPreviewNextStep()));
126 _showWebPreview = defaultSettings.showWebPreview();
127 defaultSettings.notify("ShowWebPreview", this, SLOT(showWebPreviewChanged()));
129 _clickTimer.setInterval(QApplication::doubleClickInterval());
130 _clickTimer.setSingleShot(true);
131 connect(&_clickTimer, SIGNAL(timeout()), SLOT(clickTimeout()));
133 setItemIndexMethod(QGraphicsScene::NoIndex);
136 ChatScene::~ChatScene() {
139 ChatView *ChatScene::chatView() const {
143 ColumnHandleItem *ChatScene::firstColumnHandle() const {
144 return _firstColHandle;
147 ColumnHandleItem *ChatScene::secondColumnHandle() const {
148 return _secondColHandle;
151 ChatLine *ChatScene::chatLine(MsgId msgId, bool matchExact, bool ignoreDayChange) const {
155 QList<ChatLine*>::ConstIterator start = _lines.begin();
156 QList<ChatLine*>::ConstIterator end = _lines.end();
157 QList<ChatLine*>::ConstIterator middle;
159 int n = int(end - start);
164 middle = start + half;
165 if((*middle)->msgId() < msgId) {
173 if(start != end && (*start)->msgId() == msgId && (ignoreDayChange? (*start)->msgType() != Message::DayChange : true))
179 if(start == _lines.begin()) // not (yet?) in our scene
182 // if we didn't find the exact msgId, take the next-lower one (this makes sense for lastSeen)
184 if(start == end) { // higher than last element
186 return _lines.last();
188 for(int i = _lines.count() -1; i >= 0; i--) {
189 if(_lines.at(i)->msgType() != Message::DayChange)
195 // return the next-lower line
200 if((*(--start))->msgType() != Message::DayChange)
202 } while(start != _lines.begin());
206 ChatItem *ChatScene::chatItemAt(const QPointF &scenePos) const {
207 foreach(QGraphicsItem *item, items(scenePos, Qt::IntersectsItemBoundingRect, Qt::AscendingOrder)) {
208 ChatLine *line = qgraphicsitem_cast<ChatLine*>(item);
210 return line->itemAt(line->mapFromScene(scenePos));
215 bool ChatScene::containsBuffer(const BufferId &id) const {
216 MessageFilter *filter = qobject_cast<MessageFilter*>(model());
218 return filter->containsBuffer(id);
223 void ChatScene::setMarkerLineVisible(bool visible) {
224 _markerLineVisible = visible;
225 if(visible && _markerLineValid)
226 markerLine()->setVisible(true);
228 markerLine()->setVisible(false);
231 void ChatScene::setMarkerLine(MsgId msgId) {
232 if(!isSingleBufferScene())
236 msgId = Client::markerLine(singleBufferId());
238 if(msgId.isValid()) {
239 ChatLine *line = chatLine(msgId, false, true);
241 markerLine()->setChatLine(line);
242 // if this was the last line, we won't see it because it's outside the sceneRect
243 // .. which is exactly what we want :)
244 markerLine()->setPos(line->pos() + QPointF(0, line->height()));
246 // DayChange messages might have been hidden outside the scene rect, don't make the markerline visible then!
247 if(markerLine()->pos().y() >= sceneRect().y()) {
248 _markerLineValid = true;
249 if(_markerLineVisible)
250 markerLine()->setVisible(true);
251 if(_markerLineJumpPending) {
252 _markerLineJumpPending = false;
253 if(markerLine()->isVisible()) {
254 markerLine()->ensureVisible(QRectF(), 50, 50);
261 _markerLineValid = false;
262 markerLine()->setVisible(false);
265 void ChatScene::jumpToMarkerLine(bool requestBacklog) {
266 if(!isSingleBufferScene())
269 if(markerLine()->isVisible()) {
270 markerLine()->ensureVisible(QRectF(), 50, 50);
273 if(!_markerLineValid && requestBacklog) {
274 MsgId msgId = Client::markerLine(singleBufferId());
275 if(msgId.isValid()) {
276 _markerLineJumpPending = true;
277 Client::backlogManager()->requestBacklog(singleBufferId(), msgId, -1, -1, 0);
279 // If we filtered out the lastSeenMsg (by changing filters after setting it), we'd never jump because the above request
280 // won't fetch any prior lines. Thus, trigger a dynamic backlog request just in case, so repeated
281 // jump tries will eventually cause enough backlog to be fetched.
282 // This is a bit hackish, but not wasteful, as jumping to the top of the ChatView would trigger a dynamic fetch anyway.
283 this->requestBacklog();
288 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
291 // QModelIndex sidx = model()->index(start, 2);
292 // QModelIndex eidx = model()->index(end, 2);
293 // qDebug() << "rowsInserted:";
295 // QModelIndex ssidx = model()->index(start - 1, 2);
296 // qDebug() << "Start--:" << start - 1 << ssidx.data(MessageModel::MsgIdRole).value<MsgId>()
297 // << ssidx.data(Qt::DisplayRole).toString();
299 // qDebug() << "Start:" << start << sidx.data(MessageModel::MsgIdRole).value<MsgId>()
300 // << sidx.data(Qt::DisplayRole).toString();
301 // qDebug() << "End:" << end << eidx.data(MessageModel::MsgIdRole).value<MsgId>()
302 // << eidx.data(Qt::DisplayRole).toString();
303 // if(end + 1 < model()->rowCount()) {
304 // QModelIndex eeidx = model()->index(end + 1, 2);
305 // qDebug() << "End++:" << end + 1 << eeidx.data(MessageModel::MsgIdRole).value<MsgId>()
306 // << eeidx.data(Qt::DisplayRole).toString();
311 qreal width = _sceneRect.width();
312 bool atBottom = (start == _lines.count());
313 bool atTop = !atBottom && (start == 0);
315 if(start < _lines.count()) {
316 y = _lines.value(start)->y();
317 } else if(atBottom && !_lines.isEmpty()) {
318 y = _lines.last()->y() + _lines.last()->height();
321 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
322 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
323 qreal timestampWidth = firstColumnHandle()->sceneLeft();
324 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
325 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
328 for(int i = end; i >= start; i--) {
329 ChatLine *line = new ChatLine(i, model(),
331 timestampWidth, senderWidth, contentsWidth,
332 senderPos, contentsPos);
334 line->setPos(0, y-h);
335 _lines.insert(start, line);
339 for(int i = start; i <= end; i++) {
340 ChatLine *line = new ChatLine(i, model(),
342 timestampWidth, senderWidth, contentsWidth,
343 senderPos, contentsPos);
344 line->setPos(0, y+h);
346 _lines.insert(i, line);
351 // update existing items
352 for(int i = end+1; i < _lines.count(); i++) {
353 _lines[i]->setRow(i);
357 if(_selectionStart >= 0) {
358 int offset = end - start + 1;
359 int oldStart = _selectionStart;
360 if(_selectionStart >= start)
361 _selectionStart += offset;
362 if(_selectionEnd >= start) {
363 _selectionEnd += offset;
364 if(_selectionStart == oldStart)
365 for(int i = start; i < start + offset; i++)
366 _lines[i]->setSelected(true);
368 if(_firstSelectionRow >= start)
369 _firstSelectionRow += offset;
372 // neither pre- or append means we have to do dirty work: move items...
373 if(!(atTop || atBottom)) {
375 for(int i = 0; i <= end; i++) {
377 line->setPos(0, line->pos().y() - h);
378 if(line == markerLine()->chatLine())
379 markerLine()->setPos(line->pos() + QPointF(0, line->height()));
383 // check if all went right
384 Q_ASSERT(start == 0 || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
386 // if(_lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() != _lines.at(start)->pos().y()) {
387 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
388 // 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();
389 // qDebug() << "line[start]" << _lines.at(start)->pos().y();
390 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
394 Q_ASSERT(end + 1 == _lines.count() || _lines.at(end)->pos().y() + _lines.at(end)->height() == _lines.at(end + 1)->pos().y());
395 // if(end + 1 < _lines.count()) {
396 // if(_lines.at(end)->pos().y() + _lines.at(end)->height() != _lines.at(end + 1)->pos().y()) {
397 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
398 // qDebug() << "line[end]:" << _lines.at(end)->pos().y() << "+" << _lines.at(end)->height() << "=" << _lines.at(end)->pos().y() + _lines.at(end)->height();
399 // qDebug() << "line[end+1]" << _lines.at(end + 1)->pos().y();
400 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
406 if(start < _firstLineRow) {
407 int prevFirstLineRow = _firstLineRow + (end - start + 1);
408 for(int i = end + 1; i < prevFirstLineRow; i++) {
409 _lines.at(i)->show();
412 // force new search for first proper line
417 emit lastLineChanged(_lines.last(), h);
420 // now move the marker line if necessary. we don't need to do anything if we appended lines though...
421 if(!_markerLineValid)
425 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
428 qreal h = 0; // total height of removed items;
430 bool atTop = (start == 0);
431 bool atBottom = (end == _lines.count() - 1);
435 int row = _selectingItem->row();
436 if(row >= start && row <= end)
440 // remove items from scene
441 QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
442 int lineCount = start;
443 while(lineIter != _lines.end() && lineCount <= end) {
444 if((*lineIter) == markerLine()->chatLine())
445 markerLine()->setChatLine(0);
446 h += (*lineIter)->height();
448 lineIter = _lines.erase(lineIter);
452 // update rows of remaining chatlines
453 for(int i = start; i < _lines.count(); i++) {
454 _lines.at(i)->setRow(i);
458 if(_selectionStart >= 0) {
459 int offset = end - start + 1;
460 if(_selectionStart >= start)
461 _selectionStart = qMax(_selectionStart - offset, start);
462 if(_selectionEnd >= start)
463 _selectionEnd -= offset;
464 if(_firstSelectionRow >= start)
465 _firstSelectionRow -= offset;
467 if(_selectionEnd < _selectionStart) {
468 _isSelecting = false;
469 _selectionStart = -1;
473 // neither removing at bottom or top means we have to move items...
474 if(!(atTop || atBottom)) {
477 int moveEnd = _lines.count() - 1;
478 if(start < _lines.count() - start) {
487 for(int i = moveStart; i <= moveEnd; i++) {
489 line->setPos(0, line->pos().y() + offset);
493 Q_ASSERT(start == 0 || start >= _lines.count() || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
496 // when searching for the first non-date-line we have to take into account that our
497 // model still contains the just removed lines so we cannot simply call updateSceneRect()
498 int numRows = model()->rowCount();
499 QModelIndex firstLineIdx;
501 bool needOffset = false;
504 if(_firstLineRow >= start && _firstLineRow <= end) {
505 _firstLineRow = end + 1;
508 firstLineIdx = model()->index(_firstLineRow, 0);
509 } while((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) == Message::DayChange && _firstLineRow < numRows);
512 _firstLineRow -= end - start + 1;
516 void ChatScene::rowsRemoved() {
517 // move the marker line if necessary
521 void ChatScene::dataChanged(const QModelIndex &tl, const QModelIndex &br) {
522 layout(tl.row(), br.row(), _sceneRect.width());
525 void ChatScene::updateForViewport(qreal width, qreal height) {
526 _viewportHeight = height;
530 void ChatScene::setWidth(qreal width) {
531 if(width == _sceneRect.width())
533 layout(0, _lines.count()-1, width);
536 void ChatScene::layout(int start, int end, qreal width) {
537 // clock_t startT = clock();
539 // disabling the index while doing this complex updates is about
540 // 2 to 10 times faster!
541 //setItemIndexMethod(QGraphicsScene::NoIndex);
545 qreal linePos = _lines.at(row)->scenePos().y() + _lines.at(row)->height();
546 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
547 while(row >= start) {
548 _lines.at(row--)->setGeometryByWidth(width, contentsWidth, linePos);
552 // remaining items don't need geometry changes, but maybe repositioning?
553 ChatLine *line = _lines.at(row);
554 qreal offset = linePos - (line->scenePos().y() + line->height());
557 line = _lines.at(row--);
558 line->setPos(0, line->scenePos().y() + offset);
564 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
566 updateSceneRect(width);
569 emit layoutChanged();
571 // clock_t endT = clock();
572 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
575 void ChatScene::firstHandlePositionChanged(qreal xpos) {
576 if(_firstColHandlePos == xpos)
579 _firstColHandlePos = xpos >= 0 ? xpos : 0;
580 ChatViewSettings viewSettings(this);
581 viewSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
582 ChatViewSettings defaultSettings;
583 defaultSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
585 // clock_t startT = clock();
587 // disabling the index while doing this complex updates is about
588 // 2 to 10 times faster!
589 //setItemIndexMethod(QGraphicsScene::NoIndex);
591 QList<ChatLine *>::iterator lineIter = _lines.end();
592 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
593 qreal timestampWidth = firstColumnHandle()->sceneLeft();
594 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
595 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
597 while(lineIter != lineIterBegin) {
599 (*lineIter)->setFirstColumn(timestampWidth, senderWidth, senderPos);
601 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
605 // clock_t endT = clock();
606 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
609 void ChatScene::secondHandlePositionChanged(qreal xpos) {
610 if(_secondColHandlePos == xpos)
613 _secondColHandlePos = xpos;
614 ChatViewSettings viewSettings(this);
615 viewSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
616 ChatViewSettings defaultSettings;
617 defaultSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
619 // clock_t startT = clock();
621 // disabling the index while doing this complex updates is about
622 // 2 to 10 times faster!
623 //setItemIndexMethod(QGraphicsScene::NoIndex);
625 QList<ChatLine *>::iterator lineIter = _lines.end();
626 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
627 qreal linePos = _sceneRect.y() + _sceneRect.height();
628 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
629 qreal contentsWidth = _sceneRect.width() - secondColumnHandle()->sceneRight();
630 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
631 while(lineIter != lineIterBegin) {
633 (*lineIter)->setSecondColumn(senderWidth, contentsWidth, contentsPos, linePos);
635 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
639 emit layoutChanged();
641 // clock_t endT = clock();
642 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
645 void ChatScene::setHandleXLimits() {
646 _firstColHandle->setXLimits(0, _secondColHandle->sceneLeft());
647 _secondColHandle->setXLimits(_firstColHandle->sceneRight(), width() - minContentsWidth);
651 void ChatScene::setSelectingItem(ChatItem *item) {
652 if(_selectingItem) _selectingItem->clearSelection();
653 _selectingItem = item;
656 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
657 _selectionStart = _selectionEnd = _firstSelectionRow = item->row();
658 _selectionStartCol = _selectionMinCol = item->column();
660 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
661 updateSelection(item->mapToScene(itemPos));
664 void ChatScene::updateSelection(const QPointF &pos) {
665 int curRow = rowByScenePos(pos);
666 if(curRow < 0) return;
667 int curColumn = (int)columnByScenePos(pos);
668 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
669 if(minColumn != _selectionMinCol) {
670 _selectionMinCol = minColumn;
671 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
672 _lines[l]->setSelected(true, minColumn);
675 int newstart = qMin(curRow, _firstSelectionRow);
676 int newend = qMax(curRow, _firstSelectionRow);
677 if(newstart < _selectionStart) {
678 for(int l = newstart; l < _selectionStart; l++)
679 _lines[l]->setSelected(true, minColumn);
681 if(newstart > _selectionStart) {
682 for(int l = _selectionStart; l < newstart; l++)
683 _lines[l]->setSelected(false);
685 if(newend > _selectionEnd) {
686 for(int l = _selectionEnd+1; l <= newend; l++)
687 _lines[l]->setSelected(true, minColumn);
689 if(newend < _selectionEnd) {
690 for(int l = newend+1; l <= _selectionEnd; l++)
691 _lines[l]->setSelected(false);
694 _selectionStart = newstart;
695 _selectionEnd = newend;
697 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
698 if(!_selectingItem) {
699 // _selectingItem has been removed already
702 _lines[curRow]->setSelected(false);
703 _isSelecting = false;
704 _selectionStart = -1;
705 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
709 bool ChatScene::isPosOverSelection(const QPointF &pos) const {
710 ChatItem *chatItem = chatItemAt(pos);
713 if(hasGlobalSelection()) {
714 int row = chatItem->row();
715 if(row >= qMin(_selectionStart, _selectionEnd) && row <= qMax(_selectionStart, _selectionEnd))
716 return columnByScenePos(pos) >= _selectionMinCol;
718 return chatItem->isPosOverSelection(chatItem->mapFromScene(pos));
723 bool ChatScene::isScrollingAllowed() const {
727 // TODO: Handle clicks and single-item selections too
732 /******** MOUSE HANDLING **************************************************************************/
734 void ChatScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
735 QPointF pos = event->scenePos();
738 // zoom actions and similar
739 chatView()->addActionsToMenu(&menu, pos);
742 if(isPosOverSelection(pos))
743 menu.addAction(SmallIcon("edit-copy"), tr("Copy Selection"),
744 this, SLOT(selectionToClipboard()),
747 // item-specific options (select link etc)
748 ChatItem *item = chatItemAt(pos);
750 item->addActionsToMenu(&menu, item->mapFromScene(pos));
752 // no item -> default scene actions
753 GraphicalUi::contextMenuActionProvider()->addActions(&menu, filter(), BufferId());
755 if (QtUi::mainWindow()->menuBar()->isHidden())
756 menu.addAction(QtUi::actionCollection("General")->action("ToggleMenuBar"));
758 menu.exec(event->screenPos());
762 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
763 if(event->buttons() == Qt::LeftButton) {
764 if(!_clickHandled && (event->scenePos() - _clickPos).toPoint().manhattanLength() >= QApplication::startDragDistance()) {
765 if(_clickTimer.isActive())
767 if(_clickMode == SingleClick && isPosOverSelection(_clickPos))
768 initiateDrag(event->widget());
770 _clickMode = DragStartClick;
771 handleClick(Qt::LeftButton, _clickPos);
773 _clickMode = NoClick;
776 updateSelection(event->scenePos());
777 emit mouseMoveWhileSelecting(event->scenePos());
779 } else if(_clickHandled && _clickMode < DoubleClick)
780 QGraphicsScene::mouseMoveEvent(event);
782 QGraphicsScene::mouseMoveEvent(event);
785 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
786 if(event->buttons() == Qt::LeftButton) {
787 _leftButtonPressed = true;
788 _clickHandled = false;
789 if(!isPosOverSelection(event->scenePos())) {
790 // immediately clear selection if clicked outside; otherwise, wait for potential drag
793 if(_clickMode != NoClick && _clickTimer.isActive()) {
795 case NoClick: _clickMode = SingleClick; break;
796 case SingleClick: _clickMode = DoubleClick; break;
797 case DoubleClick: _clickMode = TripleClick; break;
798 case TripleClick: _clickMode = DoubleClick; break;
799 case DragStartClick: break;
801 handleClick(Qt::LeftButton, _clickPos);
803 _clickMode = SingleClick;
804 _clickPos = event->scenePos();
808 if(event->type() == QEvent::GraphicsSceneMouseDoubleClick)
809 QGraphicsScene::mouseDoubleClickEvent(event);
811 QGraphicsScene::mousePressEvent(event);
814 void ChatScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
815 // we check for doubleclick ourselves, so just call press handler
816 mousePressEvent(event);
819 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
820 if(event->button() == Qt::LeftButton && _leftButtonPressed) {
821 _leftButtonPressed = false;
822 if(_clickMode != NoClick) {
823 if(_clickMode == SingleClick)
826 if(!_clickTimer.isActive())
827 handleClick(Qt::LeftButton, _clickPos);
829 // no click -> drag or selection move
830 if(isGloballySelecting()) {
831 selectionToClipboard(QClipboard::Selection);
832 _isSelecting = false;
838 QGraphicsScene::mouseReleaseEvent(event);
841 void ChatScene::clickTimeout() {
842 if(!_leftButtonPressed && _clickMode == SingleClick)
843 handleClick(Qt::LeftButton, _clickPos);
846 void ChatScene::handleClick(Qt::MouseButton button, const QPointF &scenePos) {
847 if(button == Qt::LeftButton) {
850 // Now send click down to items
851 ChatItem *chatItem = chatItemAt(scenePos);
853 chatItem->handleClick(chatItem->mapFromScene(scenePos), _clickMode);
855 _clickHandled = true;
859 void ChatScene::initiateDrag(QWidget *source) {
860 QDrag *drag = new QDrag(source);
861 QMimeData *mimeData = new QMimeData;
862 mimeData->setText(selection());
863 drag->setMimeData(mimeData);
865 drag->exec(Qt::CopyAction);
868 /******** SELECTIONS ******************************************************************************/
870 void ChatScene::selectionToClipboard(QClipboard::Mode mode) {
874 stringToClipboard(selection(), mode);
877 void ChatScene::stringToClipboard(const QString &str_, QClipboard::Mode mode) {
879 // remove trailing linefeeds
880 if(str.endsWith('\n'))
884 case QClipboard::Clipboard:
885 QApplication::clipboard()->setText(str);
887 case QClipboard::Selection:
888 if(QApplication::clipboard()->supportsSelection())
889 QApplication::clipboard()->setText(str, QClipboard::Selection);
896 //!\brief Convert current selection to human-readable string.
897 QString ChatScene::selection() const {
898 //TODO Make selection format configurable!
899 if(hasGlobalSelection()) {
900 int start = qMin(_selectionStart, _selectionEnd);
901 int end = qMax(_selectionStart, _selectionEnd);
902 if(start < 0 || end >= _lines.count()) {
903 qDebug() << "Invalid selection range:" << start << end;
907 for(int l = start; l <= end; l++) {
908 if(_selectionMinCol == ChatLineModel::TimestampColumn)
909 result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
910 if(_selectionMinCol <= ChatLineModel::SenderColumn)
911 result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " ";
912 result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n";
915 } else if(selectingItem())
916 return selectingItem()->selection();
920 bool ChatScene::hasSelection() const {
921 return hasGlobalSelection() || (selectingItem() && selectingItem()->hasSelection());
924 bool ChatScene::hasGlobalSelection() const {
925 return _selectionStart >= 0;
928 bool ChatScene::isGloballySelecting() const {
932 void ChatScene::clearGlobalSelection() {
933 if(hasGlobalSelection()) {
934 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++)
935 _lines[l]->setSelected(false);
936 _isSelecting = false;
937 _selectionStart = -1;
941 void ChatScene::clearSelection() {
942 clearGlobalSelection();
944 selectingItem()->clearSelection();
947 /******** *************************************************************************************/
949 void ChatScene::requestBacklog() {
950 MessageFilter *filter = qobject_cast<MessageFilter*>(model());
952 return filter->requestBacklog();
956 ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) const {
957 if(x < _firstColHandle->x())
958 return ChatLineModel::TimestampColumn;
959 if(x < _secondColHandle->x())
960 return ChatLineModel::SenderColumn;
962 return ChatLineModel::ContentsColumn;
965 int ChatScene::rowByScenePos(qreal y) const {
966 QList<QGraphicsItem*> itemList = items(QPointF(0, y));
968 // ChatLine should be at the bottom of the list
969 for(int i = itemList.count()-1; i >= 0; i--) {
970 ChatLine *line = qgraphicsitem_cast<ChatLine *>(itemList.at(i));
977 void ChatScene::updateSceneRect(qreal width) {
978 if(_lines.isEmpty()) {
979 updateSceneRect(QRectF(0, 0, width, 0));
983 // we hide day change messages at the top by making the scene rect smaller
984 // and by calling QGraphicsItem::hide() on all leading day change messages
985 // the first one is needed to ensure proper scrollbar ranges
986 // the second for cases where the viewport is larger then the set scenerect
987 // (in this case the items are shown anyways)
988 if(_firstLineRow == -1) {
989 int numRows = model()->rowCount();
991 QModelIndex firstLineIdx;
992 while(_firstLineRow < numRows) {
993 firstLineIdx = model()->index(_firstLineRow, 0);
994 if((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
996 _lines.at(_firstLineRow)->hide();
1001 // the following call should be safe. If it crashes something went wrong during insert/remove
1002 if(_firstLineRow < _lines.count()) {
1003 ChatLine *firstLine = _lines.at(_firstLineRow);
1004 ChatLine *lastLine = _lines.last();
1005 updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
1008 updateSceneRect(QRectF(0, 0, width, 0));
1012 void ChatScene::updateSceneRect(const QRectF &rect) {
1018 // ========================================
1019 // Webkit Only stuff
1020 // ========================================
1022 void ChatScene::loadWebPreview(ChatItem *parentItem, const QUrl &url, const QRectF &urlRect) {
1023 if(!_showWebPreview)
1026 if(webPreview.urlRect != urlRect)
1027 webPreview.urlRect = urlRect;
1029 if(webPreview.parentItem != parentItem)
1030 webPreview.parentItem = parentItem;
1032 if(webPreview.url != url) {
1033 webPreview.url = url;
1034 // prepare to load a different URL
1035 if(webPreview.previewItem) {
1036 if(webPreview.previewItem->scene())
1037 removeItem(webPreview.previewItem);
1038 delete webPreview.previewItem;
1039 webPreview.previewItem = 0;
1041 webPreview.previewState = WebPreview::NoPreview;
1044 if(webPreview.url.isEmpty())
1047 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
1048 switch(webPreview.previewState) {
1049 case WebPreview::NoPreview:
1050 webPreview.previewState = WebPreview::NewPreview;
1051 webPreview.timer.start(500);
1053 case WebPreview::NewPreview:
1054 case WebPreview::DelayPreview:
1055 case WebPreview::ShowPreview:
1056 // we're already waiting for the next step or showing the preview
1058 case WebPreview::HidePreview:
1059 // we still have a valid preview
1060 webPreview.previewState = WebPreview::DelayPreview;
1061 webPreview.timer.start(1000);
1064 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
1067 void ChatScene::webPreviewNextStep() {
1068 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
1069 switch(webPreview.previewState) {
1070 case WebPreview::NoPreview:
1072 case WebPreview::NewPreview:
1073 Q_ASSERT(!webPreview.previewItem);
1074 webPreview.previewItem = new WebPreviewItem(webPreview.url);
1075 webPreview.previewState = WebPreview::DelayPreview;
1076 webPreview.timer.start(1000);
1078 case WebPreview::DelayPreview:
1079 Q_ASSERT(webPreview.previewItem);
1080 // calc position and show
1082 qreal previewY = webPreview.urlRect.bottom();
1083 qreal previewX = webPreview.urlRect.x();
1084 if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
1085 previewY = webPreview.urlRect.y() - webPreview.previewItem->boundingRect().height();
1087 if(previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
1088 previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
1090 webPreview.previewItem->setPos(previewX, previewY);
1092 addItem(webPreview.previewItem);
1093 webPreview.previewState = WebPreview::ShowPreview;
1095 case WebPreview::ShowPreview:
1096 qWarning() << "ChatScene::webPreviewNextStep() called while in ShowPreview Step!";
1097 qWarning() << "removing preview";
1098 if(webPreview.previewItem && webPreview.previewItem->scene())
1099 removeItem(webPreview.previewItem);
1100 // Fall through to deletion!
1101 case WebPreview::HidePreview:
1102 if(webPreview.previewItem) {
1103 delete webPreview.previewItem;
1104 webPreview.previewItem = 0;
1106 webPreview.parentItem = 0;
1107 webPreview.url = QUrl();
1108 webPreview.urlRect = QRectF();
1109 webPreview.previewState = WebPreview::NoPreview;
1111 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
1114 void ChatScene::clearWebPreview(ChatItem *parentItem) {
1115 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
1116 switch(webPreview.previewState) {
1117 case WebPreview::NewPreview:
1118 webPreview.previewState = WebPreview::NoPreview; // we haven't loaded anything yet
1120 case WebPreview::ShowPreview:
1121 if(parentItem == 0 || webPreview.parentItem == parentItem) {
1122 if(webPreview.previewItem && webPreview.previewItem->scene())
1123 removeItem(webPreview.previewItem);
1125 // fall through into to set hidden state
1126 case WebPreview::DelayPreview:
1127 // we're just loading, so haven't shown the preview yet.
1128 webPreview.previewState = WebPreview::HidePreview;
1129 webPreview.timer.start(5000);
1131 case WebPreview::NoPreview:
1132 case WebPreview::HidePreview:
1135 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
1139 // ========================================
1140 // end of webkit only
1141 // ========================================
1143 void ChatScene::showWebPreviewChanged() {
1144 ChatViewSettings settings;
1145 _showWebPreview = settings.showWebPreview();