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 setItemIndexMethod(QGraphicsScene::NoIndex);
113 ChatScene::~ChatScene() {
116 ChatView *ChatScene::chatView() const {
120 ColumnHandleItem *ChatScene::firstColumnHandle() const {
121 return _firstColHandle;
124 ColumnHandleItem *ChatScene::secondColumnHandle() const {
125 return _secondColHandle;
128 ChatItem *ChatScene::chatItemAt(const QPointF &scenePos) const {
129 QGraphicsItem *item = itemAt(scenePos);
130 return dynamic_cast<ChatItem *>(item);
133 bool ChatScene::containsBuffer(const BufferId &id) const {
134 MessageFilter *filter = qobject_cast<MessageFilter*>(model());
136 return filter->containsBuffer(id);
141 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
145 // QModelIndex sidx = model()->index(start, 2);
146 // QModelIndex eidx = model()->index(end, 2);
147 // qDebug() << "rowsInserted:";
149 // QModelIndex ssidx = model()->index(start - 1, 2);
150 // qDebug() << "Start--:" << start - 1 << ssidx.data(MessageModel::MsgIdRole).value<MsgId>()
151 // << ssidx.data(Qt::DisplayRole).toString();
153 // qDebug() << "Start:" << start << sidx.data(MessageModel::MsgIdRole).value<MsgId>()
154 // << sidx.data(Qt::DisplayRole).toString();
155 // qDebug() << "End:" << end << eidx.data(MessageModel::MsgIdRole).value<MsgId>()
156 // << eidx.data(Qt::DisplayRole).toString();
157 // if(end + 1 < model()->rowCount()) {
158 // QModelIndex eeidx = model()->index(end + 1, 2);
159 // qDebug() << "End++:" << end + 1 << eeidx.data(MessageModel::MsgIdRole).value<MsgId>()
160 // << eeidx.data(Qt::DisplayRole).toString();
165 qreal width = _sceneRect.width();
166 bool atBottom = (start == _lines.count());
167 bool atTop = !atBottom && (start == 0);
168 bool moveTop = false;
170 if(start < _lines.count()) {
171 y = _lines.value(start)->y();
172 } else if(atBottom && !_lines.isEmpty()) {
173 y = _lines.last()->y() + _lines.last()->height();
176 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
177 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
178 qreal timestampWidth = firstColumnHandle()->sceneLeft();
179 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
180 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
183 for(int i = end; i >= start; i--) {
184 ChatLine *line = new ChatLine(i, model(),
186 timestampWidth, senderWidth, contentsWidth,
187 senderPos, contentsPos);
189 line->setPos(0, y-h);
190 _lines.insert(start, line);
194 for(int i = start; i <= end; i++) {
195 ChatLine *line = new ChatLine(i, model(),
197 timestampWidth, senderWidth, contentsWidth,
198 senderPos, contentsPos);
199 line->setPos(0, y+h);
201 _lines.insert(i, line);
206 // update existing items
207 for(int i = end+1; i < _lines.count(); i++) {
208 _lines[i]->setRow(i);
212 if(_selectionStart >= 0) {
213 int offset = end - start + 1;
214 int oldStart = _selectionStart;
215 if(_selectionStart >= start)
216 _selectionStart += offset;
217 if(_selectionEnd >= start) {
218 _selectionEnd += offset;
219 if(_selectionStart == oldStart)
220 for(int i = start; i < start + offset; i++)
221 _lines[i]->setSelected(true);
223 if(_firstSelectionRow >= start)
224 _firstSelectionRow += offset;
227 // neither pre- or append means we have to do dirty work: move items...
229 int moveEnd = _lines.count() - 1;
231 if(!(atTop || atBottom)) {
232 // move top means: moving 0 to end (aka: end + 1)
233 // move top means: moving end + 1 to _lines.count() - 1 (aka: _lines.count() - (end + 1)
234 if(end + 1 < _lines.count() - end - 1) {
244 for(int i = moveStart; i <= moveEnd; i++) {
246 line->setPos(0, line->pos().y() + offset);
250 // check if all went right
251 Q_ASSERT(start == 0 || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
253 // if(_lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() != _lines.at(start)->pos().y()) {
254 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
255 // 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();
256 // qDebug() << "line[start]" << _lines.at(start)->pos().y();
257 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
261 Q_ASSERT(end + 1 == _lines.count() || _lines.at(end)->pos().y() + _lines.at(end)->height() == _lines.at(end + 1)->pos().y());
262 // if(end + 1 < _lines.count()) {
263 // if(_lines.at(end)->pos().y() + _lines.at(end)->height() != _lines.at(end + 1)->pos().y()) {
264 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
265 // qDebug() << "line[end]:" << _lines.at(end)->pos().y() << "+" << _lines.at(end)->height() << "=" << _lines.at(end)->pos().y() + _lines.at(end)->height();
266 // qDebug() << "line[end+1]" << _lines.at(end + 1)->pos().y();
267 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
273 if(start < _firstLineRow) {
274 int prevFirstLineRow = _firstLineRow + (end - start + 1);
275 for(int i = end + 1; i < prevFirstLineRow; i++) {
276 _lines.at(i)->show();
279 // force new search for first proper line
283 if(atBottom || (!atTop && !moveTop)) {
284 emit lastLineChanged(_lines.last(), h);
288 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
291 qreal h = 0; // total height of removed items;
293 bool atTop = (start == 0);
294 bool atBottom = (end == _lines.count() - 1);
295 bool moveTop = false;
299 int row = _selectingItem->row();
300 if(row >= start && row <= end)
304 // remove items from scene
305 QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
306 int lineCount = start;
307 while(lineIter != _lines.end() && lineCount <= end) {
308 h += (*lineIter)->height();
310 lineIter = _lines.erase(lineIter);
314 // update rows of remaining chatlines
315 for(int i = start; i < _lines.count(); i++) {
316 _lines.at(i)->setRow(i);
320 if(_selectionStart >= 0) {
321 int offset = end - start + 1;
322 if(_selectionStart >= start)
323 _selectionStart = qMax(_selectionStart -= offset, start);
324 if(_selectionEnd >= start)
325 _selectionEnd -= offset;
326 if(_firstSelectionRow >= start)
327 _firstSelectionRow -= offset;
329 if(_selectionEnd < _selectionStart) {
330 _isSelecting = false;
331 _selectionStart = -1;
335 // neither removing at bottom or top means we have to move items...
336 if(!(atTop || atBottom)) {
339 int moveEnd = _lines.count() - 1;
340 if(start < _lines.count() - start) {
350 for(int i = moveStart; i <= moveEnd; i++) {
352 line->setPos(0, line->pos().y() + offset);
356 Q_ASSERT(start == 0 || start >= _lines.count() || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
359 // when searching for the first non-date-line we have to take into account that our
360 // model still contains the just removed lines so we cannot simply call updateSceneRect()
361 int numRows = model()->rowCount();
362 QModelIndex firstLineIdx;
364 bool needOffset = false;
367 if(_firstLineRow >= start && _firstLineRow <= end) {
368 _firstLineRow = end + 1;
371 firstLineIdx = model()->index(_firstLineRow, 0);
372 } while((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) == Message::DayChange && _firstLineRow < numRows);
375 _firstLineRow -= end - start + 1;
379 void ChatScene::updateForViewport(qreal width, qreal height) {
380 _viewportHeight = height;
384 void ChatScene::setWidth(qreal width) {
385 if(width == _sceneRect.width())
388 // clock_t startT = clock();
390 // disabling the index while doing this complex updates is about
391 // 2 to 10 times faster!
392 //setItemIndexMethod(QGraphicsScene::NoIndex);
394 QList<ChatLine *>::iterator lineIter = _lines.end();
395 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
396 qreal linePos = _sceneRect.y() + _sceneRect.height();
397 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
398 while(lineIter != lineIterBegin) {
400 (*lineIter)->setGeometryByWidth(width, contentsWidth, linePos);
402 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
404 updateSceneRect(width);
406 emit layoutChanged();
408 // clock_t endT = clock();
409 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
412 void ChatScene::firstHandlePositionChanged(qreal xpos) {
413 if(_firstColHandlePos == xpos)
416 _firstColHandlePos = xpos;
417 ChatViewSettings viewSettings(this);
418 viewSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
419 ChatViewSettings defaultSettings;
420 defaultSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
422 // clock_t startT = clock();
424 // disabling the index while doing this complex updates is about
425 // 2 to 10 times faster!
426 //setItemIndexMethod(QGraphicsScene::NoIndex);
428 QList<ChatLine *>::iterator lineIter = _lines.end();
429 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
430 qreal timestampWidth = firstColumnHandle()->sceneLeft();
431 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
432 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
434 while(lineIter != lineIterBegin) {
436 (*lineIter)->setFirstColumn(timestampWidth, senderWidth, senderPos);
438 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
442 // clock_t endT = clock();
443 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
446 void ChatScene::secondHandlePositionChanged(qreal xpos) {
447 if(_secondColHandlePos == xpos)
450 _secondColHandlePos = xpos;
451 ChatViewSettings viewSettings(this);
452 viewSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
453 ChatViewSettings defaultSettings;
454 defaultSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
456 // clock_t startT = clock();
458 // disabling the index while doing this complex updates is about
459 // 2 to 10 times faster!
460 //setItemIndexMethod(QGraphicsScene::NoIndex);
462 QList<ChatLine *>::iterator lineIter = _lines.end();
463 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
464 qreal linePos = _sceneRect.y() + _sceneRect.height();
465 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
466 qreal contentsWidth = _sceneRect.width() - secondColumnHandle()->sceneRight();
467 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
468 while(lineIter != lineIterBegin) {
470 (*lineIter)->setSecondColumn(senderWidth, contentsWidth, contentsPos, linePos);
472 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
476 emit layoutChanged();
478 // clock_t endT = clock();
479 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
482 void ChatScene::setHandleXLimits() {
483 _firstColHandle->setXLimits(0, _secondColHandle->sceneLeft());
484 _secondColHandle->setXLimits(_firstColHandle->sceneRight(), width() - minContentsWidth);
487 void ChatScene::setSelectingItem(ChatItem *item) {
488 if(_selectingItem) _selectingItem->clearSelection();
489 _selectingItem = item;
492 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
493 _selectionStart = _selectionEnd = _firstSelectionRow = item->row();
494 _selectionStartCol = _selectionMinCol = item->column();
496 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
497 updateSelection(item->mapToScene(itemPos));
500 void ChatScene::updateSelection(const QPointF &pos) {
501 int curRow = rowByScenePos(pos);
502 if(curRow < 0) return;
503 int curColumn = (int)columnByScenePos(pos);
504 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
505 if(minColumn != _selectionMinCol) {
506 _selectionMinCol = minColumn;
507 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
508 _lines[l]->setSelected(true, minColumn);
511 int newstart = qMin(curRow, _firstSelectionRow);
512 int newend = qMax(curRow, _firstSelectionRow);
513 if(newstart < _selectionStart) {
514 for(int l = newstart; l < _selectionStart; l++)
515 _lines[l]->setSelected(true, minColumn);
517 if(newstart > _selectionStart) {
518 for(int l = _selectionStart; l < newstart; l++)
519 _lines[l]->setSelected(false);
521 if(newend > _selectionEnd) {
522 for(int l = _selectionEnd+1; l <= newend; l++)
523 _lines[l]->setSelected(true, minColumn);
525 if(newend < _selectionEnd) {
526 for(int l = newend+1; l <= _selectionEnd; l++)
527 _lines[l]->setSelected(false);
530 _selectionStart = newstart;
531 _selectionEnd = newend;
533 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
534 if(!_selectingItem) {
535 // _selectingItem has been removed already
538 _lines[curRow]->setSelected(false);
539 _isSelecting = false;
540 _selectionStart = -1;
541 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
545 bool ChatScene::isPosOverSelection(const QPointF &pos) const {
546 ChatItem *chatItem = chatItemAt(pos);
549 if(hasGlobalSelection()) {
550 int row = chatItem->row();
551 if(row >= qMin(_selectionStart, _selectionEnd) && row <= qMax(_selectionStart, _selectionEnd))
554 return chatItem->isPosOverSelection(chatItem->mapFromScene(pos));
559 bool ChatScene::isScrollingAllowed() const {
563 // TODO: Handle clicks and single-item selections too
568 /******** MOUSE HANDLING **************************************************************************/
570 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
571 if(event->buttons() == Qt::LeftButton) {
572 if(!_clickHandled && (event->scenePos() - _clickPos).toPoint().manhattanLength() >= QApplication::startDragDistance()) {
573 if(_clickTimer.isActive()) _clickTimer.stop();
574 if(_clickMode == SingleClick && isPosOverSelection(_clickPos))
575 initiateDrag(event->widget());
577 _clickMode = DragStartClick;
578 handleClick(Qt::LeftButton, _clickPos);
580 _clickMode = NoClick;
583 updateSelection(event->scenePos());
584 emit mouseMoveWhileSelecting(event->scenePos());
586 } else if(_clickHandled)
587 QGraphicsScene::mouseMoveEvent(event);
589 QGraphicsScene::mouseMoveEvent(event);
592 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
593 if(event->buttons() == Qt::LeftButton) {
594 _leftButtonPressed = true;
595 _clickHandled = false;
596 if(!isPosOverSelection(event->scenePos())) {
597 // immediately clear selection if clicked outside; otherwise, wait for potential drag
600 if(_clickMode != NoClick && _clickTimer.isActive()) {
601 _clickMode = (ClickMode)(_clickMode == TripleClick ? DoubleClick : _clickMode + 1);
602 handleClick(Qt::LeftButton, event->scenePos());
604 _clickMode = SingleClick;
605 _clickPos = event->scenePos();
608 } else if(event->buttons() == Qt::RightButton) {
609 handleClick(Qt::RightButton, event->scenePos());
611 if(event->type() == QEvent::GraphicsSceneMouseDoubleClick)
612 QGraphicsScene::mouseDoubleClickEvent(event);
614 QGraphicsScene::mousePressEvent(event);
617 void ChatScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
618 // we check for doubleclick ourselves, so just call press handler
619 mousePressEvent(event);
622 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
623 if(!event->buttons() & Qt::LeftButton) {
624 _leftButtonPressed = false;
625 if(_clickMode != NoClick) {
626 if(_clickMode == SingleClick)
629 if(!_clickTimer.isActive())
630 handleClick(Qt::LeftButton, _clickPos);
632 // no click -> drag or selection move
633 if(isGloballySelecting()) {
634 putToClipboard(selection());
635 _isSelecting = false;
641 QGraphicsScene::mouseReleaseEvent(event);
644 void ChatScene::clickTimeout() {
645 if(!_leftButtonPressed && _clickMode == SingleClick)
646 handleClick(Qt::LeftButton, _clickPos);
649 void ChatScene::handleClick(Qt::MouseButton button, const QPointF &scenePos) {
650 if(button == Qt::LeftButton) {
653 // Now send click down to items
654 ChatItem *chatItem = chatItemAt(scenePos);
656 chatItem->handleClick(chatItem->mapFromScene(scenePos), _clickMode);
658 _clickHandled = true;
659 } else if(button == Qt::RightButton) {
660 // TODO: context menu
665 void ChatScene::initiateDrag(QWidget *source) {
666 QDrag *drag = new QDrag(source);
667 QMimeData *mimeData = new QMimeData;
668 mimeData->setText(selection());
669 drag->setMimeData(mimeData);
671 drag->exec(Qt::CopyAction);
674 /******** SELECTIONS ******************************************************************************/
676 void ChatScene::putToClipboard(const QString &selection) {
677 // TODO Configure clipboards
679 QApplication::clipboard()->setText(selection, QClipboard::Selection);
682 QApplication::clipboard()->setText(selection);
686 //!\brief Convert current selection to human-readable string.
687 QString ChatScene::selection() const {
688 //TODO Make selection format configurable!
689 if(hasGlobalSelection()) {
690 int start = qMin(_selectionStart, _selectionEnd);
691 int end = qMax(_selectionStart, _selectionEnd);
692 if(start < 0 || end >= _lines.count()) {
693 qDebug() << "Invalid selection range:" << start << end;
697 for(int l = start; l <= end; l++) {
698 if(_selectionMinCol == ChatLineModel::TimestampColumn)
699 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
700 if(_selectionMinCol <= ChatLineModel::SenderColumn)
701 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
702 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
705 } else if(selectingItem())
706 return selectingItem()->selection();
710 void ChatScene::clearGlobalSelection() {
711 if(hasGlobalSelection()) {
712 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++)
713 _lines[l]->setSelected(false);
714 _isSelecting = false;
715 _selectionStart = -1;
719 void ChatScene::clearSelection() {
720 clearGlobalSelection();
722 selectingItem()->clearSelection();
725 /******** *************************************************************************************/
727 void ChatScene::requestBacklog() {
728 MessageFilter *filter = qobject_cast<MessageFilter*>(model());
730 return filter->requestBacklog();
734 ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) {
735 if(x < _firstColHandle->x())
736 return ChatLineModel::TimestampColumn;
737 if(x < _secondColHandle->x())
738 return ChatLineModel::SenderColumn;
740 return ChatLineModel::ContentsColumn;
743 int ChatScene::rowByScenePos(qreal y) {
744 // This is somewhat hacky... we look at the contents item that is at the given y position, since
745 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
746 // ChatItems cover their ChatLine, so we won't get to the latter directly.
747 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(_secondColHandle->sceneRight() + 1, y)));
748 if(!contentItem) return -1;
749 return contentItem->row();
752 void ChatScene::updateSceneRect(qreal width) {
753 if(_lines.isEmpty()) {
754 updateSceneRect(QRectF(0, 0, width, 0));
758 // we hide day change messages at the top by making the scene rect smaller
759 // and by calling QGraphicsItem::hide() on all leading day change messages
760 // the first one is needed to ensure proper scrollbar ranges
761 // the second for cases where the viewport is larger then the set scenerect
762 // (in this case the items are shown anyways)
763 if(_firstLineRow == -1) {
764 int numRows = model()->rowCount();
766 QModelIndex firstLineIdx;
767 while(_firstLineRow < numRows) {
768 firstLineIdx = model()->index(_firstLineRow, 0);
769 if((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
771 _lines.at(_firstLineRow)->hide();
776 // the following call should be safe. If it crashes something went wrong during insert/remove
777 if(_firstLineRow < _lines.count()) {
778 ChatLine *firstLine = _lines.at(_firstLineRow);
779 ChatLine *lastLine = _lines.last();
780 updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
783 updateSceneRect(QRectF(0, 0, width, 0));
787 void ChatScene::updateSceneRect(const QRectF &rect) {
793 bool ChatScene::event(QEvent *e) {
794 if(e->type() == QEvent::ApplicationPaletteChange) {
795 _firstColHandle->setColor(QApplication::palette().windowText().color());
796 _secondColHandle->setColor(QApplication::palette().windowText().color());
798 return QGraphicsScene::event(e);
801 /******** WEB PREVIEW *****************************************************************************/
803 void ChatScene::loadWebPreview(ChatItem *parentItem, const QString &url, const QRectF &urlRect) {
812 if(webPreview.parentItem != parentItem)
813 webPreview.parentItem = parentItem;
815 if(webPreview.url != url) {
816 webPreview.url = url;
817 // load a new web view and delete the old one (if exists)
818 if(webPreview.previewItem && webPreview.previewItem->scene()) {
819 removeItem(webPreview.previewItem);
820 delete webPreview.previewItem;
822 webPreview.previewItem = new WebPreviewItem(url);
823 webPreview.delayTimer.start(2000);
824 webPreview.deleteTimer.stop();
825 } else if(webPreview.previewItem && !webPreview.previewItem->scene()) {
826 // we just have to readd the item to the scene
827 webPreview.delayTimer.start(2000);
828 webPreview.deleteTimer.stop();
830 if(webPreview.urlRect != urlRect) {
831 webPreview.urlRect = urlRect;
832 qreal previewY = urlRect.bottom();
833 qreal previewX = urlRect.x();
834 if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
835 previewY = urlRect.y() - webPreview.previewItem->boundingRect().height();
837 if(previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
838 previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
840 webPreview.previewItem->setPos(previewX, previewY);
845 void ChatScene::showWebPreviewEvent() {
847 if(webPreview.previewItem)
848 addItem(webPreview.previewItem);
852 void ChatScene::clearWebPreview(ChatItem *parentItem) {
856 if(parentItem == 0 || webPreview.parentItem == parentItem) {
857 if(webPreview.previewItem && webPreview.previewItem->scene()) {
858 removeItem(webPreview.previewItem);
859 webPreview.deleteTimer.start();
861 webPreview.delayTimer.stop();
866 void ChatScene::deleteWebPreviewEvent() {
868 if(webPreview.previewItem) {
869 delete webPreview.previewItem;
870 webPreview.previewItem = 0;
872 webPreview.parentItem = 0;
873 webPreview.url = QString();
874 webPreview.urlRect = QRectF();
878 void ChatScene::showWebPreviewChanged() {
879 ChatViewSettings settings;
880 _showWebPreview = settings.showWebPreview();