1 /***************************************************************************
2 * Copyright (C) 2005-08 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>
25 #include <QPersistentModelIndex>
30 #include "chatlinemodelitem.h"
31 #include "chatscene.h"
33 #include "clientbacklogmanager.h"
34 #include "columnhandleitem.h"
35 #include "messagefilter.h"
37 #include "qtuistyle.h"
38 #include "chatviewsettings.h"
39 #include "webpreviewitem.h"
41 const qreal minContentsWidth = 200;
43 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, ChatView *parent)
44 : QGraphicsScene(0, 0, width, 0, (QObject *)parent),
48 _singleBufferScene(false),
49 _sceneRect(0, 0, width, 0),
52 _cutoffMode(CutoffRight),
58 _leftButtonPressed(false)
60 MessageFilter *filter = qobject_cast<MessageFilter*>(model);
62 _singleBufferScene = filter->isSingleBufferFilter();
65 ChatViewSettings defaultSettings;
66 int defaultFirstColHandlePos = defaultSettings.value("FirstColumnHandlePos", 80).toInt();
67 int defaultSecondColHandlePos = defaultSettings.value("SecondColumnHandlePos", 200).toInt();
69 ChatViewSettings viewSettings(this);
70 _firstColHandlePos = viewSettings.value("FirstColumnHandlePos", defaultFirstColHandlePos).toInt();
71 _secondColHandlePos = viewSettings.value("SecondColumnHandlePos", defaultSecondColHandlePos).toInt();
73 _firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator());
74 addItem(_firstColHandle);
75 _firstColHandle->setXPos(_firstColHandlePos);
76 connect(_firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(firstHandlePositionChanged(qreal)));
77 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _firstColHandle, SLOT(sceneRectChanged(const QRectF &)));
79 _secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator());
80 addItem(_secondColHandle);
81 _secondColHandle->setXPos(_secondColHandlePos);
82 connect(_secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(secondHandlePositionChanged(qreal)));
84 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _secondColHandle, SLOT(sceneRectChanged(const QRectF &)));
88 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
89 this, SLOT(rowsInserted(const QModelIndex &, int, int)));
90 connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
91 this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
93 if(model->rowCount() > 0)
94 rowsInserted(QModelIndex(), 0, model->rowCount() - 1);
97 webPreview.delayTimer.setSingleShot(true);
98 connect(&webPreview.delayTimer, SIGNAL(timeout()), this, SLOT(showWebPreviewEvent()));
99 //webPreview.deleteTimer.setInterval(600000);
100 webPreview.deleteTimer.setInterval(10000);
101 connect(&webPreview.deleteTimer, SIGNAL(timeout()), this, SLOT(deleteWebPreviewEvent()));
103 _showWebPreview = defaultSettings.showWebPreview();
104 defaultSettings.notify("ShowWebPreview", this, SLOT(showWebPreviewChanged()));
106 _clickTimer.setInterval(QApplication::doubleClickInterval());
107 _clickTimer.setSingleShot(true);
108 connect(&_clickTimer, SIGNAL(timeout()), SLOT(clickTimeout()));
110 _clickTimer.setInterval(QApplication::doubleClickInterval());
111 _clickTimer.setSingleShot(true);
112 connect(&_clickTimer, SIGNAL(timeout()), SLOT(clickTimeout()));
114 setItemIndexMethod(QGraphicsScene::NoIndex);
117 ChatScene::~ChatScene() {
120 ChatView *ChatScene::chatView() const {
124 ColumnHandleItem *ChatScene::firstColumnHandle() const {
125 return _firstColHandle;
128 ColumnHandleItem *ChatScene::secondColumnHandle() const {
129 return _secondColHandle;
132 ChatItem *ChatScene::chatItemAt(const QPointF &scenePos) const {
133 QGraphicsItem *item = itemAt(scenePos);
134 return dynamic_cast<ChatItem *>(item);
137 bool ChatScene::containsBuffer(const BufferId &id) const {
138 MessageFilter *filter = qobject_cast<MessageFilter*>(model());
140 return filter->containsBuffer(id);
145 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
149 // QModelIndex sidx = model()->index(start, 2);
150 // QModelIndex eidx = model()->index(end, 2);
151 // qDebug() << "rowsInserted:";
153 // QModelIndex ssidx = model()->index(start - 1, 2);
154 // qDebug() << "Start--:" << start - 1 << ssidx.data(MessageModel::MsgIdRole).value<MsgId>()
155 // << ssidx.data(Qt::DisplayRole).toString();
157 // qDebug() << "Start:" << start << sidx.data(MessageModel::MsgIdRole).value<MsgId>()
158 // << sidx.data(Qt::DisplayRole).toString();
159 // qDebug() << "End:" << end << eidx.data(MessageModel::MsgIdRole).value<MsgId>()
160 // << eidx.data(Qt::DisplayRole).toString();
161 // if(end + 1 < model()->rowCount()) {
162 // QModelIndex eeidx = model()->index(end + 1, 2);
163 // qDebug() << "End++:" << end + 1 << eeidx.data(MessageModel::MsgIdRole).value<MsgId>()
164 // << eeidx.data(Qt::DisplayRole).toString();
169 qreal width = _sceneRect.width();
170 bool atBottom = (start == _lines.count());
171 bool atTop = !atBottom && (start == 0);
172 bool moveTop = false;
174 if(start < _lines.count()) {
175 y = _lines.value(start)->y();
176 } else if(atBottom && !_lines.isEmpty()) {
177 y = _lines.last()->y() + _lines.last()->height();
180 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
181 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
182 qreal timestampWidth = firstColumnHandle()->sceneLeft();
183 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
184 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
187 for(int i = end; i >= start; i--) {
188 ChatLine *line = new ChatLine(i, model(),
190 timestampWidth, senderWidth, contentsWidth,
191 senderPos, contentsPos);
193 line->setPos(0, y-h);
194 _lines.insert(start, line);
198 for(int i = start; i <= end; i++) {
199 ChatLine *line = new ChatLine(i, model(),
201 timestampWidth, senderWidth, contentsWidth,
202 senderPos, contentsPos);
203 line->setPos(0, y+h);
205 _lines.insert(i, line);
210 // update existing items
211 for(int i = end+1; i < _lines.count(); i++) {
212 _lines[i]->setRow(i);
216 if(_selectionStart >= 0) {
217 int offset = end - start + 1;
218 int oldStart = _selectionStart;
219 if(_selectionStart >= start)
220 _selectionStart += offset;
221 if(_selectionEnd >= start) {
222 _selectionEnd += offset;
223 if(_selectionStart == oldStart)
224 for(int i = start; i < start + offset; i++)
225 _lines[i]->setSelected(true);
227 if(_firstSelectionRow >= start)
228 _firstSelectionRow += offset;
231 // neither pre- or append means we have to do dirty work: move items...
233 int moveEnd = _lines.count() - 1;
235 if(!(atTop || atBottom)) {
236 // move top means: moving 0 to end (aka: end + 1)
237 // move top means: moving end + 1 to _lines.count() - 1 (aka: _lines.count() - (end + 1)
238 if(end + 1 < _lines.count() - end - 1) {
248 for(int i = moveStart; i <= moveEnd; i++) {
250 line->setPos(0, line->pos().y() + offset);
254 // check if all went right
255 Q_ASSERT(start == 0 || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
257 // if(_lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() != _lines.at(start)->pos().y()) {
258 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
259 // 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();
260 // qDebug() << "line[start]" << _lines.at(start)->pos().y();
261 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
265 Q_ASSERT(end + 1 == _lines.count() || _lines.at(end)->pos().y() + _lines.at(end)->height() == _lines.at(end + 1)->pos().y());
266 // if(end + 1 < _lines.count()) {
267 // if(_lines.at(end)->pos().y() + _lines.at(end)->height() != _lines.at(end + 1)->pos().y()) {
268 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
269 // qDebug() << "line[end]:" << _lines.at(end)->pos().y() << "+" << _lines.at(end)->height() << "=" << _lines.at(end)->pos().y() + _lines.at(end)->height();
270 // qDebug() << "line[end+1]" << _lines.at(end + 1)->pos().y();
271 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
277 if(start < _firstLineRow) {
278 int prevFirstLineRow = _firstLineRow + (end - start + 1);
279 for(int i = end + 1; i < prevFirstLineRow; i++) {
280 _lines.at(i)->show();
283 // force new search for first proper line
287 if(atBottom || (!atTop && !moveTop)) {
288 emit lastLineChanged(_lines.last(), h);
292 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
295 qreal h = 0; // total height of removed items;
297 bool atTop = (start == 0);
298 bool atBottom = (end == _lines.count() - 1);
299 bool moveTop = false;
303 int row = _selectingItem->row();
304 if(row >= start && row <= end)
308 // remove items from scene
309 QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
310 int lineCount = start;
311 while(lineIter != _lines.end() && lineCount <= end) {
312 h += (*lineIter)->height();
314 lineIter = _lines.erase(lineIter);
318 // update rows of remaining chatlines
319 for(int i = start; i < _lines.count(); i++) {
320 _lines.at(i)->setRow(i);
324 if(_selectionStart >= 0) {
325 int offset = end - start + 1;
326 if(_selectionStart >= start)
327 _selectionStart = qMax(_selectionStart -= offset, start);
328 if(_selectionEnd >= start)
329 _selectionEnd -= offset;
330 if(_firstSelectionRow >= start)
331 _firstSelectionRow -= offset;
333 if(_selectionEnd < _selectionStart) {
334 _isSelecting = false;
335 _selectionStart = -1;
339 // neither removing at bottom or top means we have to move items...
340 if(!(atTop || atBottom)) {
343 int moveEnd = _lines.count() - 1;
344 if(start < _lines.count() - start) {
354 for(int i = moveStart; i <= moveEnd; i++) {
356 line->setPos(0, line->pos().y() + offset);
360 Q_ASSERT(start == 0 || start >= _lines.count() || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
363 // when searching for the first non-date-line we have to take into account that our
364 // model still contains the just removed lines so we cannot simply call updateSceneRect()
365 int numRows = model()->rowCount();
366 QModelIndex firstLineIdx;
368 bool needOffset = false;
371 if(_firstLineRow >= start && _firstLineRow <= end) {
372 _firstLineRow = end + 1;
375 firstLineIdx = model()->index(_firstLineRow, 0);
376 } while((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) == Message::DayChange && _firstLineRow < numRows);
379 _firstLineRow -= end - start + 1;
383 void ChatScene::updateForViewport(qreal width, qreal height) {
384 _viewportHeight = height;
388 void ChatScene::setWidth(qreal width) {
389 if(width == _sceneRect.width())
392 // clock_t startT = clock();
394 // disabling the index while doing this complex updates is about
395 // 2 to 10 times faster!
396 //setItemIndexMethod(QGraphicsScene::NoIndex);
398 QList<ChatLine *>::iterator lineIter = _lines.end();
399 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
400 qreal linePos = _sceneRect.y() + _sceneRect.height();
401 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
402 while(lineIter != lineIterBegin) {
404 (*lineIter)->setGeometryByWidth(width, contentsWidth, linePos);
406 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
408 updateSceneRect(width);
410 emit layoutChanged();
412 // clock_t endT = clock();
413 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
416 void ChatScene::firstHandlePositionChanged(qreal xpos) {
417 if(_firstColHandlePos == xpos)
420 _firstColHandlePos = xpos;
421 ChatViewSettings viewSettings(this);
422 viewSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
423 ChatViewSettings defaultSettings;
424 defaultSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
426 // clock_t startT = clock();
428 // disabling the index while doing this complex updates is about
429 // 2 to 10 times faster!
430 //setItemIndexMethod(QGraphicsScene::NoIndex);
432 QList<ChatLine *>::iterator lineIter = _lines.end();
433 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
434 qreal timestampWidth = firstColumnHandle()->sceneLeft();
435 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
436 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
438 while(lineIter != lineIterBegin) {
440 (*lineIter)->setFirstColumn(timestampWidth, senderWidth, senderPos);
442 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
446 // clock_t endT = clock();
447 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
450 void ChatScene::secondHandlePositionChanged(qreal xpos) {
451 if(_secondColHandlePos == xpos)
454 _secondColHandlePos = xpos;
455 ChatViewSettings viewSettings(this);
456 viewSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
457 ChatViewSettings defaultSettings;
458 defaultSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
460 // clock_t startT = clock();
462 // disabling the index while doing this complex updates is about
463 // 2 to 10 times faster!
464 //setItemIndexMethod(QGraphicsScene::NoIndex);
466 QList<ChatLine *>::iterator lineIter = _lines.end();
467 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
468 qreal linePos = _sceneRect.y() + _sceneRect.height();
469 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
470 qreal contentsWidth = _sceneRect.width() - secondColumnHandle()->sceneRight();
471 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
472 while(lineIter != lineIterBegin) {
474 (*lineIter)->setSecondColumn(senderWidth, contentsWidth, contentsPos, linePos);
476 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
480 emit layoutChanged();
482 // clock_t endT = clock();
483 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
486 void ChatScene::setHandleXLimits() {
487 _firstColHandle->setXLimits(0, _secondColHandle->sceneLeft());
488 _secondColHandle->setXLimits(_firstColHandle->sceneRight(), width() - minContentsWidth);
491 void ChatScene::setSelectingItem(ChatItem *item) {
492 if(_selectingItem) _selectingItem->clearSelection();
493 _selectingItem = item;
496 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
497 _selectionStart = _selectionEnd = _firstSelectionRow = item->row();
498 _selectionStartCol = _selectionMinCol = item->column();
500 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
501 updateSelection(item->mapToScene(itemPos));
504 void ChatScene::updateSelection(const QPointF &pos) {
505 int curRow = rowByScenePos(pos);
506 if(curRow < 0) return;
507 int curColumn = (int)columnByScenePos(pos);
508 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
509 if(minColumn != _selectionMinCol) {
510 _selectionMinCol = minColumn;
511 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
512 _lines[l]->setSelected(true, minColumn);
515 int newstart = qMin(curRow, _firstSelectionRow);
516 int newend = qMax(curRow, _firstSelectionRow);
517 if(newstart < _selectionStart) {
518 for(int l = newstart; l < _selectionStart; l++)
519 _lines[l]->setSelected(true, minColumn);
521 if(newstart > _selectionStart) {
522 for(int l = _selectionStart; l < newstart; l++)
523 _lines[l]->setSelected(false);
525 if(newend > _selectionEnd) {
526 for(int l = _selectionEnd+1; l <= newend; l++)
527 _lines[l]->setSelected(true, minColumn);
529 if(newend < _selectionEnd) {
530 for(int l = newend+1; l <= _selectionEnd; l++)
531 _lines[l]->setSelected(false);
534 _selectionStart = newstart;
535 _selectionEnd = newend;
537 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
538 if(!_selectingItem) {
539 // _selectingItem has been removed already
542 _lines[curRow]->setSelected(false);
543 _isSelecting = false;
544 _selectionStart = -1;
545 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
549 bool ChatScene::isPosOverSelection(const QPointF &pos) const {
550 ChatItem *chatItem = chatItemAt(pos);
553 if(hasGlobalSelection()) {
554 int row = chatItem->row();
555 if(row >= qMin(_selectionStart, _selectionEnd) && row <= qMax(_selectionStart, _selectionEnd))
558 return chatItem->isPosOverSelection(chatItem->mapFromScene(pos));
563 bool ChatScene::isScrollingAllowed() const {
567 // TODO: Handle clicks and single-item selections too
572 /******** MOUSE HANDLING **************************************************************************/
574 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
575 if(event->buttons() == Qt::LeftButton) {
576 if(!_clickHandled && (event->scenePos() - _clickPos).toPoint().manhattanLength() >= QApplication::startDragDistance()) {
577 if(_clickTimer.isActive()) _clickTimer.stop();
578 if(_clickMode == SingleClick && isPosOverSelection(_clickPos))
579 initiateDrag(event->widget());
581 _clickMode = DragStartClick;
582 handleClick(Qt::LeftButton, _clickPos);
584 _clickMode = NoClick;
587 updateSelection(event->scenePos());
588 emit mouseMoveWhileSelecting(event->scenePos());
590 } else if(_clickHandled)
591 QGraphicsScene::mouseMoveEvent(event);
593 QGraphicsScene::mouseMoveEvent(event);
596 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
597 if(event->buttons() == Qt::LeftButton) {
598 _leftButtonPressed = true;
599 _clickHandled = false;
600 if(!isPosOverSelection(event->scenePos())) {
601 // immediately clear selection if clicked outside; otherwise, wait for potential drag
604 if(_clickMode != NoClick && _clickTimer.isActive()) {
605 _clickMode = (ClickMode)(_clickMode == TripleClick ? DoubleClick : _clickMode + 1);
606 handleClick(Qt::LeftButton, event->scenePos());
608 _clickMode = SingleClick;
609 _clickPos = event->scenePos();
612 } else if(event->buttons() == Qt::RightButton) {
613 handleClick(Qt::RightButton, event->scenePos());
615 if(event->type() == QEvent::GraphicsSceneMouseDoubleClick)
616 QGraphicsScene::mouseDoubleClickEvent(event);
618 QGraphicsScene::mousePressEvent(event);
621 void ChatScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
622 // we check for doubleclick ourselves, so just call press handler
623 mousePressEvent(event);
626 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
627 if(!event->buttons() & Qt::LeftButton) {
628 _leftButtonPressed = false;
629 if(_clickMode != NoClick) {
630 if(_clickMode == SingleClick)
633 if(!_clickTimer.isActive())
634 handleClick(Qt::LeftButton, _clickPos);
636 // no click -> drag or selection move
637 if(isGloballySelecting()) {
638 putToClipboard(selection());
639 _isSelecting = false;
645 QGraphicsScene::mouseReleaseEvent(event);
648 void ChatScene::clickTimeout() {
649 if(!_leftButtonPressed && _clickMode == SingleClick)
650 handleClick(Qt::LeftButton, _clickPos);
653 void ChatScene::handleClick(Qt::MouseButton button, const QPointF &scenePos) {
654 if(button == Qt::LeftButton) {
657 // Now send click down to items
658 ChatItem *chatItem = chatItemAt(scenePos);
660 chatItem->handleClick(chatItem->mapFromScene(scenePos), _clickMode);
662 _clickHandled = true;
663 } else if(button == Qt::RightButton) {
664 // TODO: context menu
669 void ChatScene::initiateDrag(QWidget *source) {
670 QDrag *drag = new QDrag(source);
671 QMimeData *mimeData = new QMimeData;
672 mimeData->setText(selection());
673 drag->setMimeData(mimeData);
675 drag->exec(Qt::CopyAction);
678 /******** SELECTIONS ******************************************************************************/
680 void ChatScene::putToClipboard(const QString &selection) {
681 // TODO Configure clipboards
683 QApplication::clipboard()->setText(selection, QClipboard::Selection);
686 QApplication::clipboard()->setText(selection);
690 //!\brief Convert current selection to human-readable string.
691 QString ChatScene::selection() const {
692 //TODO Make selection format configurable!
693 if(hasGlobalSelection()) {
694 int start = qMin(_selectionStart, _selectionEnd);
695 int end = qMax(_selectionStart, _selectionEnd);
696 if(start < 0 || end >= _lines.count()) {
697 qDebug() << "Invalid selection range:" << start << end;
701 for(int l = start; l <= end; l++) {
702 if(_selectionMinCol == ChatLineModel::TimestampColumn)
703 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
704 if(_selectionMinCol <= ChatLineModel::SenderColumn)
705 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
706 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
709 } else if(selectingItem())
710 return selectingItem()->selection();
714 void ChatScene::clearGlobalSelection() {
715 if(hasGlobalSelection()) {
716 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++)
717 _lines[l]->setSelected(false);
718 _isSelecting = false;
719 _selectionStart = -1;
723 void ChatScene::clearSelection() {
724 clearGlobalSelection();
726 selectingItem()->clearSelection();
729 /******** *************************************************************************************/
731 void ChatScene::requestBacklog() {
732 MessageFilter *filter = qobject_cast<MessageFilter*>(model());
734 return filter->requestBacklog();
738 ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) {
739 if(x < _firstColHandle->x())
740 return ChatLineModel::TimestampColumn;
741 if(x < _secondColHandle->x())
742 return ChatLineModel::SenderColumn;
744 return ChatLineModel::ContentsColumn;
747 int ChatScene::rowByScenePos(qreal y) {
748 // This is somewhat hacky... we look at the contents item that is at the given y position, since
749 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
750 // ChatItems cover their ChatLine, so we won't get to the latter directly.
751 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(_secondColHandle->sceneRight() + 1, y)));
752 if(!contentItem) return -1;
753 return contentItem->row();
756 void ChatScene::updateSceneRect(qreal width) {
757 if(_lines.isEmpty()) {
758 updateSceneRect(QRectF(0, 0, width, 0));
762 // we hide day change messages at the top by making the scene rect smaller
763 // and by calling QGraphicsItem::hide() on all leading day change messages
764 // the first one is needed to ensure proper scrollbar ranges
765 // the second for cases where the viewport is larger then the set scenerect
766 // (in this case the items are shown anyways)
767 if(_firstLineRow == -1) {
768 int numRows = model()->rowCount();
770 QModelIndex firstLineIdx;
771 while(_firstLineRow < numRows) {
772 firstLineIdx = model()->index(_firstLineRow, 0);
773 if((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
775 _lines.at(_firstLineRow)->hide();
780 // the following call should be safe. If it crashes something went wrong during insert/remove
781 if(_firstLineRow < _lines.count()) {
782 ChatLine *firstLine = _lines.at(_firstLineRow);
783 ChatLine *lastLine = _lines.last();
784 updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
787 updateSceneRect(QRectF(0, 0, width, 0));
791 void ChatScene::updateSceneRect(const QRectF &rect) {
797 bool ChatScene::event(QEvent *e) {
798 if(e->type() == QEvent::ApplicationPaletteChange) {
799 _firstColHandle->setColor(QApplication::palette().windowText().color());
800 _secondColHandle->setColor(QApplication::palette().windowText().color());
802 return QGraphicsScene::event(e);
805 /******** WEB PREVIEW *****************************************************************************/
807 void ChatScene::loadWebPreview(ChatItem *parentItem, const QString &url, const QRectF &urlRect) {
816 if(webPreview.parentItem != parentItem)
817 webPreview.parentItem = parentItem;
819 if(webPreview.url != url) {
820 webPreview.url = url;
821 // load a new web view and delete the old one (if exists)
822 if(webPreview.previewItem && webPreview.previewItem->scene()) {
823 removeItem(webPreview.previewItem);
824 delete webPreview.previewItem;
826 webPreview.previewItem = new WebPreviewItem(url);
827 webPreview.delayTimer.start(2000);
828 webPreview.deleteTimer.stop();
829 } else if(webPreview.previewItem && !webPreview.previewItem->scene()) {
830 // we just have to readd the item to the scene
831 webPreview.delayTimer.start(2000);
832 webPreview.deleteTimer.stop();
834 if(webPreview.urlRect != urlRect) {
835 webPreview.urlRect = urlRect;
836 qreal previewY = urlRect.bottom();
837 qreal previewX = urlRect.x();
838 if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
839 previewY = urlRect.y() - webPreview.previewItem->boundingRect().height();
841 if(previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
842 previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
844 webPreview.previewItem->setPos(previewX, previewY);
849 void ChatScene::showWebPreviewEvent() {
851 if(webPreview.previewItem)
852 addItem(webPreview.previewItem);
856 void ChatScene::clearWebPreview(ChatItem *parentItem) {
860 if(parentItem == 0 || webPreview.parentItem == parentItem) {
861 if(webPreview.previewItem && webPreview.previewItem->scene()) {
862 removeItem(webPreview.previewItem);
863 webPreview.deleteTimer.start();
865 webPreview.delayTimer.stop();
870 void ChatScene::deleteWebPreviewEvent() {
872 if(webPreview.previewItem) {
873 delete webPreview.previewItem;
874 webPreview.previewItem = 0;
876 webPreview.parentItem = 0;
877 webPreview.url = QString();
878 webPreview.urlRect = QRectF();
882 void ChatScene::showWebPreviewChanged() {
883 ChatViewSettings settings;
884 _showWebPreview = settings.showWebPreview();