1 /***************************************************************************
2 * Copyright (C) 2005-09 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>
26 #include <QPersistentModelIndex>
34 #include "chatlinemodelitem.h"
35 #include "chatscene.h"
38 #include "clientbacklogmanager.h"
39 #include "columnhandleitem.h"
40 #include "contextmenuactionprovider.h"
41 #include "iconloader.h"
42 #include "messagefilter.h"
44 #include "qtuistyle.h"
45 #include "chatviewsettings.h"
46 #include "webpreviewitem.h"
48 const qreal minContentsWidth = 200;
50 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, ChatView *parent)
51 : QGraphicsScene(0, 0, width, 0, (QObject *)parent),
55 _singleBufferId(BufferId()),
56 _sceneRect(0, 0, width, 0),
59 _cutoffMode(CutoffRight),
65 _leftButtonPressed(false)
67 MessageFilter *filter = qobject_cast<MessageFilter*>(model);
68 if(filter && filter->isSingleBufferFilter()) {
69 _singleBufferId = filter->singleBufferId();
72 ChatViewSettings defaultSettings;
73 int defaultFirstColHandlePos = defaultSettings.value("FirstColumnHandlePos", 80).toInt();
74 int defaultSecondColHandlePos = defaultSettings.value("SecondColumnHandlePos", 200).toInt();
76 ChatViewSettings viewSettings(this);
77 _firstColHandlePos = viewSettings.value("FirstColumnHandlePos", defaultFirstColHandlePos).toInt();
78 _secondColHandlePos = viewSettings.value("SecondColumnHandlePos", defaultSecondColHandlePos).toInt();
80 _firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator());
81 addItem(_firstColHandle);
82 _firstColHandle->setXPos(_firstColHandlePos);
83 connect(_firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(firstHandlePositionChanged(qreal)));
84 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _firstColHandle, SLOT(sceneRectChanged(const QRectF &)));
86 _secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator());
87 addItem(_secondColHandle);
88 _secondColHandle->setXPos(_secondColHandlePos);
89 connect(_secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(secondHandlePositionChanged(qreal)));
91 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _secondColHandle, SLOT(sceneRectChanged(const QRectF &)));
95 if(model->rowCount() > 0)
96 rowsInserted(QModelIndex(), 0, model->rowCount() - 1);
98 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
99 this, SLOT(rowsInserted(const QModelIndex &, int, int)));
100 connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
101 this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
102 connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), SLOT(dataChanged(QModelIndex, QModelIndex)));
105 webPreview.timer.setSingleShot(true);
106 connect(&webPreview.timer, SIGNAL(timeout()), this, SLOT(webPreviewNextStep()));
108 _showWebPreview = defaultSettings.showWebPreview();
109 defaultSettings.notify("ShowWebPreview", this, SLOT(showWebPreviewChanged()));
111 _clickTimer.setInterval(QApplication::doubleClickInterval());
112 _clickTimer.setSingleShot(true);
113 connect(&_clickTimer, SIGNAL(timeout()), SLOT(clickTimeout()));
115 setItemIndexMethod(QGraphicsScene::NoIndex);
118 ChatScene::~ChatScene() {
121 ChatView *ChatScene::chatView() const {
125 ColumnHandleItem *ChatScene::firstColumnHandle() const {
126 return _firstColHandle;
129 ColumnHandleItem *ChatScene::secondColumnHandle() const {
130 return _secondColHandle;
133 ChatItem *ChatScene::chatItemAt(const QPointF &scenePos) const {
134 QGraphicsItem *item = itemAt(scenePos);
135 return dynamic_cast<ChatItem *>(item);
138 bool ChatScene::containsBuffer(const BufferId &id) const {
139 MessageFilter *filter = qobject_cast<MessageFilter*>(model());
141 return filter->containsBuffer(id);
146 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
150 // QModelIndex sidx = model()->index(start, 2);
151 // QModelIndex eidx = model()->index(end, 2);
152 // qDebug() << "rowsInserted:";
154 // QModelIndex ssidx = model()->index(start - 1, 2);
155 // qDebug() << "Start--:" << start - 1 << ssidx.data(MessageModel::MsgIdRole).value<MsgId>()
156 // << ssidx.data(Qt::DisplayRole).toString();
158 // qDebug() << "Start:" << start << sidx.data(MessageModel::MsgIdRole).value<MsgId>()
159 // << sidx.data(Qt::DisplayRole).toString();
160 // qDebug() << "End:" << end << eidx.data(MessageModel::MsgIdRole).value<MsgId>()
161 // << eidx.data(Qt::DisplayRole).toString();
162 // if(end + 1 < model()->rowCount()) {
163 // QModelIndex eeidx = model()->index(end + 1, 2);
164 // qDebug() << "End++:" << end + 1 << eeidx.data(MessageModel::MsgIdRole).value<MsgId>()
165 // << eeidx.data(Qt::DisplayRole).toString();
170 qreal width = _sceneRect.width();
171 bool atBottom = (start == _lines.count());
172 bool atTop = !atBottom && (start == 0);
173 bool moveTop = false;
175 if(start < _lines.count()) {
176 y = _lines.value(start)->y();
177 } else if(atBottom && !_lines.isEmpty()) {
178 y = _lines.last()->y() + _lines.last()->height();
181 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
182 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
183 qreal timestampWidth = firstColumnHandle()->sceneLeft();
184 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
185 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
188 for(int i = end; i >= start; i--) {
189 ChatLine *line = new ChatLine(i, model(),
191 timestampWidth, senderWidth, contentsWidth,
192 senderPos, contentsPos);
194 line->setPos(0, y-h);
195 _lines.insert(start, line);
199 for(int i = start; i <= end; i++) {
200 ChatLine *line = new ChatLine(i, model(),
202 timestampWidth, senderWidth, contentsWidth,
203 senderPos, contentsPos);
204 line->setPos(0, y+h);
206 _lines.insert(i, line);
211 // update existing items
212 for(int i = end+1; i < _lines.count(); i++) {
213 _lines[i]->setRow(i);
217 if(_selectionStart >= 0) {
218 int offset = end - start + 1;
219 int oldStart = _selectionStart;
220 if(_selectionStart >= start)
221 _selectionStart += offset;
222 if(_selectionEnd >= start) {
223 _selectionEnd += offset;
224 if(_selectionStart == oldStart)
225 for(int i = start; i < start + offset; i++)
226 _lines[i]->setSelected(true);
228 if(_firstSelectionRow >= start)
229 _firstSelectionRow += offset;
232 // neither pre- or append means we have to do dirty work: move items...
233 if(!(atTop || atBottom)) {
235 for(int i = 0; i <= end; i++) {
237 line->setPos(0, line->pos().y() - h);
241 // check if all went right
242 Q_ASSERT(start == 0 || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
244 // if(_lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() != _lines.at(start)->pos().y()) {
245 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
246 // 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();
247 // qDebug() << "line[start]" << _lines.at(start)->pos().y();
248 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
252 Q_ASSERT(end + 1 == _lines.count() || _lines.at(end)->pos().y() + _lines.at(end)->height() == _lines.at(end + 1)->pos().y());
253 // if(end + 1 < _lines.count()) {
254 // if(_lines.at(end)->pos().y() + _lines.at(end)->height() != _lines.at(end + 1)->pos().y()) {
255 // qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
256 // qDebug() << "line[end]:" << _lines.at(end)->pos().y() << "+" << _lines.at(end)->height() << "=" << _lines.at(end)->pos().y() + _lines.at(end)->height();
257 // qDebug() << "line[end+1]" << _lines.at(end + 1)->pos().y();
258 // qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
264 if(start < _firstLineRow) {
265 int prevFirstLineRow = _firstLineRow + (end - start + 1);
266 for(int i = end + 1; i < prevFirstLineRow; i++) {
267 _lines.at(i)->show();
270 // force new search for first proper line
275 emit lastLineChanged(_lines.last(), h);
279 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
282 qreal h = 0; // total height of removed items;
284 bool atTop = (start == 0);
285 bool atBottom = (end == _lines.count() - 1);
286 bool moveTop = false;
290 int row = _selectingItem->row();
291 if(row >= start && row <= end)
295 // remove items from scene
296 QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
297 int lineCount = start;
298 while(lineIter != _lines.end() && lineCount <= end) {
299 h += (*lineIter)->height();
301 lineIter = _lines.erase(lineIter);
305 // update rows of remaining chatlines
306 for(int i = start; i < _lines.count(); i++) {
307 _lines.at(i)->setRow(i);
311 if(_selectionStart >= 0) {
312 int offset = end - start + 1;
313 if(_selectionStart >= start)
314 _selectionStart = qMax(_selectionStart -= offset, start);
315 if(_selectionEnd >= start)
316 _selectionEnd -= offset;
317 if(_firstSelectionRow >= start)
318 _firstSelectionRow -= offset;
320 if(_selectionEnd < _selectionStart) {
321 _isSelecting = false;
322 _selectionStart = -1;
326 // neither removing at bottom or top means we have to move items...
327 if(!(atTop || atBottom)) {
330 int moveEnd = _lines.count() - 1;
331 if(start < _lines.count() - start) {
341 for(int i = moveStart; i <= moveEnd; i++) {
343 line->setPos(0, line->pos().y() + offset);
347 Q_ASSERT(start == 0 || start >= _lines.count() || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
350 // when searching for the first non-date-line we have to take into account that our
351 // model still contains the just removed lines so we cannot simply call updateSceneRect()
352 int numRows = model()->rowCount();
353 QModelIndex firstLineIdx;
355 bool needOffset = false;
358 if(_firstLineRow >= start && _firstLineRow <= end) {
359 _firstLineRow = end + 1;
362 firstLineIdx = model()->index(_firstLineRow, 0);
363 } while((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) == Message::DayChange && _firstLineRow < numRows);
366 _firstLineRow -= end - start + 1;
370 void ChatScene::dataChanged(const QModelIndex &tl, const QModelIndex &br) {
371 layout(tl.row(), br.row(), _sceneRect.width());
374 void ChatScene::updateForViewport(qreal width, qreal height) {
375 _viewportHeight = height;
379 void ChatScene::setWidth(qreal width) {
380 if(width == _sceneRect.width())
382 layout(0, _lines.count()-1, width);
385 void ChatScene::layout(int start, int end, qreal width) {
386 // clock_t startT = clock();
388 // disabling the index while doing this complex updates is about
389 // 2 to 10 times faster!
390 //setItemIndexMethod(QGraphicsScene::NoIndex);
394 qreal linePos = _lines.at(row)->scenePos().y() + _lines.at(row)->height();
395 qreal contentsWidth = width - secondColumnHandle()->sceneRight();
396 while(row >= start) {
397 _lines.at(row--)->setGeometryByWidth(width, contentsWidth, linePos);
401 // remaining items don't need geometry changes, but maybe repositioning?
402 ChatLine *line = _lines.at(row);
403 qreal offset = linePos - (line->scenePos().y() + line->height());
406 line = _lines.at(row--);
407 line->setPos(0, line->scenePos().y() + offset);
413 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
415 updateSceneRect(width);
417 emit layoutChanged();
419 // clock_t endT = clock();
420 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
423 void ChatScene::firstHandlePositionChanged(qreal xpos) {
424 if(_firstColHandlePos == xpos)
427 _firstColHandlePos = xpos >= 0 ? xpos : 0;
428 ChatViewSettings viewSettings(this);
429 viewSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
430 ChatViewSettings defaultSettings;
431 defaultSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
433 // clock_t startT = clock();
435 // disabling the index while doing this complex updates is about
436 // 2 to 10 times faster!
437 //setItemIndexMethod(QGraphicsScene::NoIndex);
439 QList<ChatLine *>::iterator lineIter = _lines.end();
440 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
441 qreal timestampWidth = firstColumnHandle()->sceneLeft();
442 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
443 QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
445 while(lineIter != lineIterBegin) {
447 (*lineIter)->setFirstColumn(timestampWidth, senderWidth, senderPos);
449 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
453 // clock_t endT = clock();
454 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
457 void ChatScene::secondHandlePositionChanged(qreal xpos) {
458 if(_secondColHandlePos == xpos)
461 _secondColHandlePos = xpos;
462 ChatViewSettings viewSettings(this);
463 viewSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
464 ChatViewSettings defaultSettings;
465 defaultSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
467 // clock_t startT = clock();
469 // disabling the index while doing this complex updates is about
470 // 2 to 10 times faster!
471 //setItemIndexMethod(QGraphicsScene::NoIndex);
473 QList<ChatLine *>::iterator lineIter = _lines.end();
474 QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
475 qreal linePos = _sceneRect.y() + _sceneRect.height();
476 qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
477 qreal contentsWidth = _sceneRect.width() - secondColumnHandle()->sceneRight();
478 QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
479 while(lineIter != lineIterBegin) {
481 (*lineIter)->setSecondColumn(senderWidth, contentsWidth, contentsPos, linePos);
483 //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
487 emit layoutChanged();
489 // clock_t endT = clock();
490 // qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
493 void ChatScene::setHandleXLimits() {
494 _firstColHandle->setXLimits(0, _secondColHandle->sceneLeft());
495 _secondColHandle->setXLimits(_firstColHandle->sceneRight(), width() - minContentsWidth);
498 void ChatScene::setSelectingItem(ChatItem *item) {
499 if(_selectingItem) _selectingItem->clearSelection();
500 _selectingItem = item;
503 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
504 _selectionStart = _selectionEnd = _firstSelectionRow = item->row();
505 _selectionStartCol = _selectionMinCol = item->column();
507 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
508 updateSelection(item->mapToScene(itemPos));
511 void ChatScene::updateSelection(const QPointF &pos) {
512 int curRow = rowByScenePos(pos);
513 if(curRow < 0) return;
514 int curColumn = (int)columnByScenePos(pos);
515 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
516 if(minColumn != _selectionMinCol) {
517 _selectionMinCol = minColumn;
518 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
519 _lines[l]->setSelected(true, minColumn);
522 int newstart = qMin(curRow, _firstSelectionRow);
523 int newend = qMax(curRow, _firstSelectionRow);
524 if(newstart < _selectionStart) {
525 for(int l = newstart; l < _selectionStart; l++)
526 _lines[l]->setSelected(true, minColumn);
528 if(newstart > _selectionStart) {
529 for(int l = _selectionStart; l < newstart; l++)
530 _lines[l]->setSelected(false);
532 if(newend > _selectionEnd) {
533 for(int l = _selectionEnd+1; l <= newend; l++)
534 _lines[l]->setSelected(true, minColumn);
536 if(newend < _selectionEnd) {
537 for(int l = newend+1; l <= _selectionEnd; l++)
538 _lines[l]->setSelected(false);
541 _selectionStart = newstart;
542 _selectionEnd = newend;
544 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
545 if(!_selectingItem) {
546 // _selectingItem has been removed already
549 _lines[curRow]->setSelected(false);
550 _isSelecting = false;
551 _selectionStart = -1;
552 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
556 bool ChatScene::isPosOverSelection(const QPointF &pos) const {
557 ChatItem *chatItem = chatItemAt(pos);
560 if(hasGlobalSelection()) {
561 int row = chatItem->row();
562 if(row >= qMin(_selectionStart, _selectionEnd) && row <= qMax(_selectionStart, _selectionEnd))
563 return columnByScenePos(pos) >= _selectionMinCol;
565 return chatItem->isPosOverSelection(chatItem->mapFromScene(pos));
570 bool ChatScene::isScrollingAllowed() const {
574 // TODO: Handle clicks and single-item selections too
579 /******** MOUSE HANDLING **************************************************************************/
581 void ChatScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
582 QPointF pos = event->scenePos();
585 // zoom actions and similar
586 chatView()->addActionsToMenu(&menu, pos);
589 if(isPosOverSelection(pos))
590 menu.addAction(SmallIcon("edit-copy"), tr("Copy Selection"),
591 this, SLOT(selectionToClipboard()),
594 // item-specific options (select link etc)
595 ChatItem *item = chatItemAt(pos);
597 item->addActionsToMenu(&menu, item->mapFromScene(pos));
599 // no item -> default scene actions
600 GraphicalUi::contextMenuActionProvider()->addActions(&menu, filter(), BufferId());
602 menu.exec(event->screenPos());
606 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
607 if(event->buttons() == Qt::LeftButton) {
608 if(!_clickHandled && (event->scenePos() - _clickPos).toPoint().manhattanLength() >= QApplication::startDragDistance()) {
609 if(_clickTimer.isActive())
611 if(_clickMode == SingleClick && isPosOverSelection(_clickPos))
612 initiateDrag(event->widget());
614 _clickMode = DragStartClick;
615 handleClick(Qt::LeftButton, _clickPos);
617 _clickMode = NoClick;
620 updateSelection(event->scenePos());
621 emit mouseMoveWhileSelecting(event->scenePos());
623 } else if(_clickHandled && _clickMode < DoubleClick)
624 QGraphicsScene::mouseMoveEvent(event);
626 QGraphicsScene::mouseMoveEvent(event);
629 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
630 if(event->buttons() == Qt::LeftButton) {
631 _leftButtonPressed = true;
632 _clickHandled = false;
633 if(!isPosOverSelection(event->scenePos())) {
634 // immediately clear selection if clicked outside; otherwise, wait for potential drag
637 if(_clickMode != NoClick && _clickTimer.isActive()) {
639 case NoClick: _clickMode = SingleClick; break;
640 case SingleClick: _clickMode = DoubleClick; break;
641 case DoubleClick: _clickMode = TripleClick; break;
642 case TripleClick: _clickMode = DoubleClick; break;
643 case DragStartClick: break;
645 handleClick(Qt::LeftButton, _clickPos);
647 _clickMode = SingleClick;
648 _clickPos = event->scenePos();
652 if(event->type() == QEvent::GraphicsSceneMouseDoubleClick)
653 QGraphicsScene::mouseDoubleClickEvent(event);
655 QGraphicsScene::mousePressEvent(event);
658 void ChatScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
659 // we check for doubleclick ourselves, so just call press handler
660 mousePressEvent(event);
663 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
664 if(event->button() == Qt::LeftButton && _leftButtonPressed) {
665 _leftButtonPressed = false;
666 if(_clickMode != NoClick) {
667 if(_clickMode == SingleClick)
670 if(!_clickTimer.isActive())
671 handleClick(Qt::LeftButton, _clickPos);
673 // no click -> drag or selection move
674 if(isGloballySelecting()) {
675 selectionToClipboard(QClipboard::Selection);
676 _isSelecting = false;
682 QGraphicsScene::mouseReleaseEvent(event);
685 void ChatScene::clickTimeout() {
686 if(!_leftButtonPressed && _clickMode == SingleClick)
687 handleClick(Qt::LeftButton, _clickPos);
690 void ChatScene::handleClick(Qt::MouseButton button, const QPointF &scenePos) {
691 if(button == Qt::LeftButton) {
694 // Now send click down to items
695 ChatItem *chatItem = chatItemAt(scenePos);
697 chatItem->handleClick(chatItem->mapFromScene(scenePos), _clickMode);
699 _clickHandled = true;
703 void ChatScene::initiateDrag(QWidget *source) {
704 QDrag *drag = new QDrag(source);
705 QMimeData *mimeData = new QMimeData;
706 mimeData->setText(selection());
707 drag->setMimeData(mimeData);
709 drag->exec(Qt::CopyAction);
712 /******** SELECTIONS ******************************************************************************/
714 void ChatScene::selectionToClipboard(QClipboard::Mode mode) {
718 stringToClipboard(selection(), mode);
721 void ChatScene::stringToClipboard(const QString &str_, QClipboard::Mode mode) {
723 // remove trailing linefeeds
724 if(str.endsWith('\n'))
728 case QClipboard::Clipboard:
729 QApplication::clipboard()->setText(str);
731 case QClipboard::Selection:
732 if(QApplication::clipboard()->supportsSelection())
733 QApplication::clipboard()->setText(str, QClipboard::Selection);
740 //!\brief Convert current selection to human-readable string.
741 QString ChatScene::selection() const {
742 //TODO Make selection format configurable!
743 if(hasGlobalSelection()) {
744 int start = qMin(_selectionStart, _selectionEnd);
745 int end = qMax(_selectionStart, _selectionEnd);
746 if(start < 0 || end >= _lines.count()) {
747 qDebug() << "Invalid selection range:" << start << end;
751 for(int l = start; l <= end; l++) {
752 if(_selectionMinCol == ChatLineModel::TimestampColumn)
753 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
754 if(_selectionMinCol <= ChatLineModel::SenderColumn)
755 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
756 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
759 } else if(selectingItem())
760 return selectingItem()->selection();
764 bool ChatScene::hasSelection() const {
765 return hasGlobalSelection() || (selectingItem() && selectingItem()->hasSelection());
768 bool ChatScene::hasGlobalSelection() const {
769 return _selectionStart >= 0;
772 bool ChatScene::isGloballySelecting() const {
776 void ChatScene::clearGlobalSelection() {
777 if(hasGlobalSelection()) {
778 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++)
779 _lines[l]->setSelected(false);
780 _isSelecting = false;
781 _selectionStart = -1;
785 void ChatScene::clearSelection() {
786 clearGlobalSelection();
788 selectingItem()->clearSelection();
791 /******** *************************************************************************************/
793 void ChatScene::requestBacklog() {
794 MessageFilter *filter = qobject_cast<MessageFilter*>(model());
796 return filter->requestBacklog();
800 ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) const {
801 if(x < _firstColHandle->x())
802 return ChatLineModel::TimestampColumn;
803 if(x < _secondColHandle->x())
804 return ChatLineModel::SenderColumn;
806 return ChatLineModel::ContentsColumn;
809 int ChatScene::rowByScenePos(qreal y) const {
810 // This is somewhat hacky... we look at the contents item that is at the given y position, since
811 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
812 // ChatItems cover their ChatLine, so we won't get to the latter directly.
813 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(_secondColHandle->sceneRight() + 1, y)));
814 if(!contentItem) return -1;
815 return contentItem->row();
818 void ChatScene::updateSceneRect(qreal width) {
819 if(_lines.isEmpty()) {
820 updateSceneRect(QRectF(0, 0, width, 0));
824 // we hide day change messages at the top by making the scene rect smaller
825 // and by calling QGraphicsItem::hide() on all leading day change messages
826 // the first one is needed to ensure proper scrollbar ranges
827 // the second for cases where the viewport is larger then the set scenerect
828 // (in this case the items are shown anyways)
829 if(_firstLineRow == -1) {
830 int numRows = model()->rowCount();
832 QModelIndex firstLineIdx;
833 while(_firstLineRow < numRows) {
834 firstLineIdx = model()->index(_firstLineRow, 0);
835 if((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
837 _lines.at(_firstLineRow)->hide();
842 // the following call should be safe. If it crashes something went wrong during insert/remove
843 if(_firstLineRow < _lines.count()) {
844 ChatLine *firstLine = _lines.at(_firstLineRow);
845 ChatLine *lastLine = _lines.last();
846 updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
849 updateSceneRect(QRectF(0, 0, width, 0));
853 void ChatScene::updateSceneRect(const QRectF &rect) {
859 bool ChatScene::event(QEvent *e) {
860 if(e->type() == QEvent::ApplicationPaletteChange) {
861 _firstColHandle->setColor(QApplication::palette().windowText().color());
862 _secondColHandle->setColor(QApplication::palette().windowText().color());
864 return QGraphicsScene::event(e);
867 // ========================================
869 // ========================================
871 void ChatScene::loadWebPreview(ChatItem *parentItem, const QUrl &url, const QRectF &urlRect) {
875 if(webPreview.urlRect != urlRect)
876 webPreview.urlRect = urlRect;
878 if(webPreview.parentItem != parentItem)
879 webPreview.parentItem = parentItem;
881 if(webPreview.url != url) {
882 webPreview.url = url;
883 // prepare to load a different URL
884 if(webPreview.previewItem) {
885 if(webPreview.previewItem->scene())
886 removeItem(webPreview.previewItem);
887 delete webPreview.previewItem;
888 webPreview.previewItem = 0;
890 webPreview.previewState = WebPreview::NoPreview;
893 if(webPreview.url.isEmpty())
896 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
897 switch(webPreview.previewState) {
898 case WebPreview::NoPreview:
899 webPreview.previewState = WebPreview::NewPreview;
900 webPreview.timer.start(500);
902 case WebPreview::NewPreview:
903 case WebPreview::DelayPreview:
904 case WebPreview::ShowPreview:
905 // we're already waiting for the next step or showing the preview
907 case WebPreview::HidePreview:
908 // we still have a valid preview
909 webPreview.previewState = WebPreview::DelayPreview;
910 webPreview.timer.start(1000);
913 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
916 void ChatScene::webPreviewNextStep() {
917 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
918 switch(webPreview.previewState) {
919 case WebPreview::NoPreview:
921 case WebPreview::NewPreview:
922 Q_ASSERT(!webPreview.previewItem);
923 webPreview.previewItem = new WebPreviewItem(webPreview.url);
924 webPreview.previewState = WebPreview::DelayPreview;
925 webPreview.timer.start(1000);
927 case WebPreview::DelayPreview:
928 Q_ASSERT(webPreview.previewItem);
929 // calc position and show
931 qreal previewY = webPreview.urlRect.bottom();
932 qreal previewX = webPreview.urlRect.x();
933 if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
934 previewY = webPreview.urlRect.y() - webPreview.previewItem->boundingRect().height();
936 if(previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
937 previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
939 webPreview.previewItem->setPos(previewX, previewY);
941 addItem(webPreview.previewItem);
942 webPreview.previewState = WebPreview::ShowPreview;
944 case WebPreview::ShowPreview:
945 qWarning() << "ChatScene::webPreviewNextStep() called while in ShowPreview Step!";
946 qWarning() << "removing preview";
947 if(webPreview.previewItem && webPreview.previewItem->scene())
948 removeItem(webPreview.previewItem);
949 // Fall through to deletion!
950 case WebPreview::HidePreview:
951 if(webPreview.previewItem) {
952 delete webPreview.previewItem;
953 webPreview.previewItem = 0;
955 webPreview.parentItem = 0;
956 webPreview.url = QUrl();
957 webPreview.urlRect = QRectF();
958 webPreview.previewState = WebPreview::NoPreview;
960 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
963 void ChatScene::clearWebPreview(ChatItem *parentItem) {
964 // qDebug() << Q_FUNC_INFO << webPreview.previewState;
965 switch(webPreview.previewState) {
966 case WebPreview::NewPreview:
967 webPreview.previewState = WebPreview::NoPreview; // we haven't loaded anything yet
969 case WebPreview::ShowPreview:
970 if(parentItem == 0 || webPreview.parentItem == parentItem) {
971 if(webPreview.previewItem && webPreview.previewItem->scene())
972 removeItem(webPreview.previewItem);
974 // fall through into to set hidden state
975 case WebPreview::DelayPreview:
976 // we're just loading, so haven't shown the preview yet.
977 webPreview.previewState = WebPreview::HidePreview;
978 webPreview.timer.start(5000);
980 case WebPreview::NoPreview:
981 case WebPreview::HidePreview:
984 // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
988 // ========================================
989 // end of webkit only
990 // ========================================
992 void ChatScene::showWebPreviewChanged() {
993 ChatViewSettings settings;
994 _showWebPreview = settings.showWebPreview();