3f0b18de658647e143bf6ec1258bb51cf6b21be2
[quassel.git] / src / qtui / chatscene.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QApplication>
22 #include <QClipboard>
23 #include <QDrag>
24 #include <QGraphicsSceneMouseEvent>
25 #include <QMenu>
26 #include <QMenuBar>
27 #include <QPersistentModelIndex>
28
29 #ifdef HAVE_KDE
30 #  include <KMenuBar>
31 #else
32 #  include <QMenuBar>
33 #endif
34
35 #ifdef HAVE_WEBKIT
36 #  include <QWebView>
37 #endif
38
39 #include "chatitem.h"
40 #include "chatline.h"
41 #include "chatlinemodelitem.h"
42 #include "chatscene.h"
43 #include "chatview.h"
44 #include "client.h"
45 #include "clientbacklogmanager.h"
46 #include "columnhandleitem.h"
47 #include "contextmenuactionprovider.h"
48 #include "iconloader.h"
49 #include "messagefilter.h"
50 #include "qtui.h"
51 #include "qtuistyle.h"
52 #include "chatviewsettings.h"
53 #include "webpreviewitem.h"
54
55 const qreal minContentsWidth = 200;
56
57 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, ChatView *parent)
58   : QGraphicsScene(0, 0, width, 0, (QObject *)parent),
59     _chatView(parent),
60     _idString(idString),
61     _model(model),
62     _singleBufferId(BufferId()),
63     _sceneRect(0, 0, width, 0),
64     _firstLineRow(-1),
65     _viewportHeight(0),
66     _cutoffMode(CutoffRight),
67     _selectingItem(0),
68     _selectionStart(-1),
69     _isSelecting(false),
70     _clickMode(NoClick),
71     _clickHandled(true),
72     _leftButtonPressed(false)
73 {
74   MessageFilter *filter = qobject_cast<MessageFilter*>(model);
75   if(filter && filter->isSingleBufferFilter()) {
76     _singleBufferId = filter->singleBufferId();
77   }
78
79   ChatViewSettings defaultSettings;
80   int defaultFirstColHandlePos = defaultSettings.value("FirstColumnHandlePos", 80).toInt();
81   int defaultSecondColHandlePos = defaultSettings.value("SecondColumnHandlePos", 200).toInt();
82
83   ChatViewSettings viewSettings(this);
84   _firstColHandlePos = viewSettings.value("FirstColumnHandlePos", defaultFirstColHandlePos).toInt();
85   _secondColHandlePos = viewSettings.value("SecondColumnHandlePos", defaultSecondColHandlePos).toInt();
86
87   _firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator());
88   addItem(_firstColHandle);
89   _firstColHandle->setXPos(_firstColHandlePos);
90   connect(_firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(firstHandlePositionChanged(qreal)));
91   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _firstColHandle, SLOT(sceneRectChanged(const QRectF &)));
92
93   _secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator());
94   addItem(_secondColHandle);
95   _secondColHandle->setXPos(_secondColHandlePos);
96   connect(_secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(secondHandlePositionChanged(qreal)));
97
98   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _secondColHandle, SLOT(sceneRectChanged(const QRectF &)));
99
100   setHandleXLimits();
101
102   if(model->rowCount() > 0)
103     rowsInserted(QModelIndex(), 0, model->rowCount() - 1);
104
105   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
106           this, SLOT(rowsInserted(const QModelIndex &, int, int)));
107   connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
108           this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
109   connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), SLOT(dataChanged(QModelIndex, QModelIndex)));
110
111 #ifdef HAVE_WEBKIT
112   webPreview.timer.setSingleShot(true);
113   connect(&webPreview.timer, SIGNAL(timeout()), this, SLOT(webPreviewNextStep()));
114 #endif
115   _showWebPreview = defaultSettings.showWebPreview();
116   defaultSettings.notify("ShowWebPreview", this, SLOT(showWebPreviewChanged()));
117
118   _clickTimer.setInterval(QApplication::doubleClickInterval());
119   _clickTimer.setSingleShot(true);
120   connect(&_clickTimer, SIGNAL(timeout()), SLOT(clickTimeout()));
121
122   setItemIndexMethod(QGraphicsScene::NoIndex);
123 }
124
125 ChatScene::~ChatScene() {
126 }
127
128 ChatView *ChatScene::chatView() const {
129   return _chatView;
130 }
131
132 ColumnHandleItem *ChatScene::firstColumnHandle() const {
133   return _firstColHandle;
134 }
135
136 ColumnHandleItem *ChatScene::secondColumnHandle() const {
137   return _secondColHandle;
138 }
139
140 ChatItem *ChatScene::chatItemAt(const QPointF &scenePos) const {
141   ChatLine *line = qgraphicsitem_cast<ChatLine*>(itemAt(scenePos));
142   if(line)
143     return line->itemAt(line->mapFromScene(scenePos));
144   return 0;
145 }
146
147 bool ChatScene::containsBuffer(const BufferId &id) const {
148   MessageFilter *filter = qobject_cast<MessageFilter*>(model());
149   if(filter)
150     return filter->containsBuffer(id);
151   else
152     return false;
153 }
154
155 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
156   Q_UNUSED(index);
157
158
159 //   QModelIndex sidx = model()->index(start, 2);
160 //   QModelIndex eidx = model()->index(end, 2);
161 //   qDebug() << "rowsInserted:";
162 //   if(start > 0) {
163 //     QModelIndex ssidx = model()->index(start - 1, 2);
164 //     qDebug() << "Start--:" << start - 1 << ssidx.data(MessageModel::MsgIdRole).value<MsgId>()
165 //           << ssidx.data(Qt::DisplayRole).toString();
166 //   }
167 //   qDebug() << "Start:" << start << sidx.data(MessageModel::MsgIdRole).value<MsgId>()
168 //         << sidx.data(Qt::DisplayRole).toString();
169 //   qDebug() << "End:" << end << eidx.data(MessageModel::MsgIdRole).value<MsgId>()
170 //         << eidx.data(Qt::DisplayRole).toString();
171 //   if(end + 1 < model()->rowCount()) {
172 //     QModelIndex eeidx = model()->index(end + 1, 2);
173 //     qDebug() << "End++:" << end + 1 << eeidx.data(MessageModel::MsgIdRole).value<MsgId>()
174 //           << eeidx.data(Qt::DisplayRole).toString();
175 //   }
176
177   qreal h = 0;
178   qreal y = 0;
179   qreal width = _sceneRect.width();
180   bool atBottom = (start == _lines.count());
181   bool atTop = !atBottom && (start == 0);
182
183   if(start < _lines.count()) {
184     y = _lines.value(start)->y();
185   } else if(atBottom && !_lines.isEmpty()) {
186     y = _lines.last()->y() + _lines.last()->height();
187   }
188
189   qreal contentsWidth = width - secondColumnHandle()->sceneRight();
190   qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
191   qreal timestampWidth = firstColumnHandle()->sceneLeft();
192   QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
193   QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
194
195   if(atTop) {
196     for(int i = end; i >= start; i--) {
197       ChatLine *line = new ChatLine(i, model(),
198                                     width,
199                                     timestampWidth, senderWidth, contentsWidth,
200                                     senderPos, contentsPos);
201       h += line->height();
202       line->setPos(0, y-h);
203       _lines.insert(start, line);
204       addItem(line);
205     }
206   } else {
207     for(int i = start; i <= end; i++) {
208       ChatLine *line = new ChatLine(i, model(),
209                                     width,
210                                     timestampWidth, senderWidth, contentsWidth,
211                                     senderPos, contentsPos);
212       line->setPos(0, y+h);
213       h += line->height();
214       _lines.insert(i, line);
215       addItem(line);
216     }
217   }
218
219   // update existing items
220   for(int i = end+1; i < _lines.count(); i++) {
221     _lines[i]->setRow(i);
222   }
223
224   // update selection
225   if(_selectionStart >= 0) {
226     int offset = end - start + 1;
227     int oldStart = _selectionStart;
228     if(_selectionStart >= start)
229       _selectionStart += offset;
230     if(_selectionEnd >= start) {
231       _selectionEnd += offset;
232       if(_selectionStart == oldStart)
233         for(int i = start; i < start + offset; i++)
234           _lines[i]->setSelected(true);
235     }
236     if(_firstSelectionRow >= start)
237       _firstSelectionRow += offset;
238   }
239
240   // neither pre- or append means we have to do dirty work: move items...
241   if(!(atTop || atBottom)) {
242     ChatLine *line = 0;
243     for(int i = 0; i <= end; i++) {
244       line = _lines.at(i);
245       line->setPos(0, line->pos().y() - h);
246     }
247   }
248
249   // check if all went right
250   Q_ASSERT(start == 0 || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
251 //   if(start != 0) {
252 //     if(_lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() != _lines.at(start)->pos().y()) {
253 //       qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
254 //       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();
255 //       qDebug() << "line[start]" << _lines.at(start)->pos().y();
256 //       qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
257 //       Q_ASSERT(false)
258 //     }
259 //   }
260   Q_ASSERT(end + 1 == _lines.count() || _lines.at(end)->pos().y() + _lines.at(end)->height() == _lines.at(end + 1)->pos().y());
261 //   if(end + 1 < _lines.count()) {
262 //     if(_lines.at(end)->pos().y() + _lines.at(end)->height() != _lines.at(end + 1)->pos().y()) {
263 //       qDebug() << "lines:" << _lines.count() << "start:" << start << "end:" << end;
264 //       qDebug() << "line[end]:" << _lines.at(end)->pos().y() << "+" << _lines.at(end)->height() << "=" << _lines.at(end)->pos().y() + _lines.at(end)->height();
265 //       qDebug() << "line[end+1]" << _lines.at(end + 1)->pos().y();
266 //       qDebug() << "needed moving:" << !(atTop || atBottom) << moveTop << moveStart << moveEnd << offset;
267 //       Q_ASSERT(false);
268 //     }
269 //   }
270
271   if(!atBottom) {
272     if(start < _firstLineRow) {
273       int prevFirstLineRow = _firstLineRow + (end - start + 1);
274       for(int i = end + 1; i < prevFirstLineRow; i++) {
275         _lines.at(i)->show();
276       }
277     }
278     // force new search for first proper line
279     _firstLineRow = -1;
280   }
281   updateSceneRect();
282   if(atBottom) {
283     emit lastLineChanged(_lines.last(), h);
284   }
285 }
286
287 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
288   Q_UNUSED(parent);
289
290   qreal h = 0; // total height of removed items;
291
292   bool atTop = (start == 0);
293   bool atBottom = (end == _lines.count() - 1);
294   bool moveTop = false;
295
296   // clear selection
297   if(_selectingItem) {
298     int row = _selectingItem->row();
299     if(row >= start && row <= end)
300       setSelectingItem(0);
301   }
302
303   // remove items from scene
304   QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
305   int lineCount = start;
306   while(lineIter != _lines.end() && lineCount <= end) {
307     h += (*lineIter)->height();
308     delete *lineIter;
309     lineIter = _lines.erase(lineIter);
310     lineCount++;
311   }
312
313   // update rows of remaining chatlines
314   for(int i = start; i < _lines.count(); i++) {
315     _lines.at(i)->setRow(i);
316   }
317
318   // update selection
319   if(_selectionStart >= 0) {
320     int offset = end - start + 1;
321     if(_selectionStart >= start)
322       _selectionStart = qMax(_selectionStart -= offset, start);
323     if(_selectionEnd >= start)
324       _selectionEnd -= offset;
325     if(_firstSelectionRow >= start)
326       _firstSelectionRow -= offset;
327
328     if(_selectionEnd < _selectionStart) {
329       _isSelecting = false;
330       _selectionStart = -1;
331     }
332   }
333
334   // neither removing at bottom or top means we have to move items...
335   if(!(atTop || atBottom)) {
336     qreal offset = h;
337     int moveStart = 0;
338     int moveEnd = _lines.count() - 1;
339     if(start < _lines.count() - start) {
340       // move top part
341       moveTop = true;
342       moveEnd = start - 1;
343     } else {
344       // move bottom part
345       moveStart = start;
346       offset = -offset;
347     }
348     ChatLine *line = 0;
349     for(int i = moveStart; i <= moveEnd; i++) {
350       line = _lines.at(i);
351       line->setPos(0, line->pos().y() + offset);
352     }
353   }
354
355   Q_ASSERT(start == 0 || start >= _lines.count() || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
356
357   // update sceneRect
358   // when searching for the first non-date-line we have to take into account that our
359   // model still contains the just removed lines so we cannot simply call updateSceneRect()
360   int numRows = model()->rowCount();
361   QModelIndex firstLineIdx;
362   _firstLineRow = -1;
363   bool needOffset = false;
364   do {
365     _firstLineRow++;
366     if(_firstLineRow >= start && _firstLineRow <= end) {
367       _firstLineRow = end + 1;
368       needOffset = true;
369     }
370     firstLineIdx = model()->index(_firstLineRow, 0);
371   } while((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) == Message::DayChange && _firstLineRow < numRows);
372
373   if(needOffset)
374     _firstLineRow -= end - start + 1;
375   updateSceneRect();
376 }
377
378 void ChatScene::dataChanged(const QModelIndex &tl, const QModelIndex &br) {
379   layout(tl.row(), br.row(), _sceneRect.width());
380 }
381
382 void ChatScene::updateForViewport(qreal width, qreal height) {
383   _viewportHeight = height;
384   setWidth(width);
385 }
386
387 void ChatScene::setWidth(qreal width) {
388   if(width == _sceneRect.width())
389     return;
390   layout(0, _lines.count()-1, width);
391 }
392
393 void ChatScene::layout(int start, int end, qreal width) {
394   // clock_t startT = clock();
395
396   // disabling the index while doing this complex updates is about
397   // 2 to 10 times faster!
398   //setItemIndexMethod(QGraphicsScene::NoIndex);
399
400   if(end >= 0) {
401     int row = end;
402     qreal linePos = _lines.at(row)->scenePos().y() + _lines.at(row)->height();
403     qreal contentsWidth = width - secondColumnHandle()->sceneRight();
404     while(row >= start) {
405       _lines.at(row--)->setGeometryByWidth(width, contentsWidth, linePos);
406     }
407
408     if(row >= 0) {
409       // remaining items don't need geometry changes, but maybe repositioning?
410       ChatLine *line = _lines.at(row);
411       qreal offset = linePos - (line->scenePos().y() + line->height());
412       if(offset != 0) {
413         while(row >= 0) {
414           line = _lines.at(row--);
415           line->setPos(0, line->scenePos().y() + offset);
416         }
417       }
418     }
419   }
420
421   //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
422
423   updateSceneRect(width);
424   setHandleXLimits();
425   emit layoutChanged();
426
427 //   clock_t endT = clock();
428 //   qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
429 }
430
431 void ChatScene::firstHandlePositionChanged(qreal xpos) {
432   if(_firstColHandlePos == xpos)
433     return;
434
435   _firstColHandlePos = xpos >= 0 ? xpos : 0;
436   ChatViewSettings viewSettings(this);
437   viewSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
438   ChatViewSettings defaultSettings;
439   defaultSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
440
441   // clock_t startT = clock();
442
443   // disabling the index while doing this complex updates is about
444   // 2 to 10 times faster!
445   //setItemIndexMethod(QGraphicsScene::NoIndex);
446
447   QList<ChatLine *>::iterator lineIter = _lines.end();
448   QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
449   qreal timestampWidth = firstColumnHandle()->sceneLeft();
450   qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
451   QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
452
453   while(lineIter != lineIterBegin) {
454     lineIter--;
455     (*lineIter)->setFirstColumn(timestampWidth, senderWidth, senderPos);
456   }
457   //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
458
459   setHandleXLimits();
460
461 //   clock_t endT = clock();
462 //   qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
463 }
464
465 void ChatScene::secondHandlePositionChanged(qreal xpos) {
466   if(_secondColHandlePos == xpos)
467     return;
468
469   _secondColHandlePos = xpos;
470   ChatViewSettings viewSettings(this);
471   viewSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
472   ChatViewSettings defaultSettings;
473   defaultSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
474
475   // clock_t startT = clock();
476
477   // disabling the index while doing this complex updates is about
478   // 2 to 10 times faster!
479   //setItemIndexMethod(QGraphicsScene::NoIndex);
480
481   QList<ChatLine *>::iterator lineIter = _lines.end();
482   QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
483   qreal linePos = _sceneRect.y() + _sceneRect.height();
484   qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
485   qreal contentsWidth = _sceneRect.width() - secondColumnHandle()->sceneRight();
486   QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
487   while(lineIter != lineIterBegin) {
488     lineIter--;
489     (*lineIter)->setSecondColumn(senderWidth, contentsWidth, contentsPos, linePos);
490   }
491   //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
492
493   updateSceneRect();
494   setHandleXLimits();
495   emit layoutChanged();
496
497 //   clock_t endT = clock();
498 //   qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
499 }
500
501 void ChatScene::setHandleXLimits() {
502   _firstColHandle->setXLimits(0, _secondColHandle->sceneLeft());
503   _secondColHandle->setXLimits(_firstColHandle->sceneRight(), width() - minContentsWidth);
504   update();
505 }
506
507 void ChatScene::setSelectingItem(ChatItem *item) {
508   if(_selectingItem) _selectingItem->clearSelection();
509   _selectingItem = item;
510 }
511
512 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
513   _selectionStart = _selectionEnd = _firstSelectionRow = item->row();
514   _selectionStartCol = _selectionMinCol = item->column();
515   _isSelecting = true;
516   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
517   updateSelection(item->mapToScene(itemPos));
518 }
519
520 void ChatScene::updateSelection(const QPointF &pos) {
521   int curRow = rowByScenePos(pos);
522   if(curRow < 0) return;
523   int curColumn = (int)columnByScenePos(pos);
524   ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
525   if(minColumn != _selectionMinCol) {
526     _selectionMinCol = minColumn;
527     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
528       _lines[l]->setSelected(true, minColumn);
529     }
530   }
531   int newstart = qMin(curRow, _firstSelectionRow);
532   int newend = qMax(curRow, _firstSelectionRow);
533   if(newstart < _selectionStart) {
534     for(int l = newstart; l < _selectionStart; l++)
535       _lines[l]->setSelected(true, minColumn);
536   }
537   if(newstart > _selectionStart) {
538     for(int l = _selectionStart; l < newstart; l++)
539       _lines[l]->setSelected(false);
540   }
541   if(newend > _selectionEnd) {
542     for(int l = _selectionEnd+1; l <= newend; l++)
543       _lines[l]->setSelected(true, minColumn);
544   }
545   if(newend < _selectionEnd) {
546     for(int l = newend+1; l <= _selectionEnd; l++)
547       _lines[l]->setSelected(false);
548   }
549
550   _selectionStart = newstart;
551   _selectionEnd = newend;
552
553   if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
554     if(!_selectingItem) {
555       // _selectingItem has been removed already
556       return;
557     }
558     _lines[curRow]->setSelected(false);
559     _isSelecting = false;
560     _selectionStart = -1;
561     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
562   }
563 }
564
565 bool ChatScene::isPosOverSelection(const QPointF &pos) const {
566   ChatItem *chatItem = chatItemAt(pos);
567   if(!chatItem)
568     return false;
569   if(hasGlobalSelection()) {
570     int row = chatItem->row();
571     if(row >= qMin(_selectionStart, _selectionEnd) && row <= qMax(_selectionStart, _selectionEnd))
572       return columnByScenePos(pos) >= _selectionMinCol;
573   } else {
574     return chatItem->isPosOverSelection(chatItem->mapFromScene(pos));
575   }
576   return false;
577 }
578
579 bool ChatScene::isScrollingAllowed() const {
580   if(_isSelecting)
581     return false;
582
583   // TODO: Handle clicks and single-item selections too
584
585   return true;
586 }
587
588 /******** MOUSE HANDLING **************************************************************************/
589
590 void ChatScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
591   QPointF pos = event->scenePos();
592   QMenu menu;
593
594   // zoom actions and similar
595   chatView()->addActionsToMenu(&menu, pos);
596   menu.addSeparator();
597
598   if(isPosOverSelection(pos))
599     menu.addAction(SmallIcon("edit-copy"), tr("Copy Selection"),
600                     this, SLOT(selectionToClipboard()),
601                     QKeySequence::Copy);
602
603   // item-specific options (select link etc)
604   ChatItem *item = chatItemAt(pos);
605   if(item)
606     item->addActionsToMenu(&menu, item->mapFromScene(pos));
607   else
608     // no item -> default scene actions
609     GraphicalUi::contextMenuActionProvider()->addActions(&menu, filter(), BufferId());
610
611   if (QtUi::mainWindow()->menuBar()->isHidden())
612     menu.addAction(QtUi::actionCollection("General")->action("ToggleMenuBar"));
613
614   menu.exec(event->screenPos());
615
616 }
617
618 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
619   if(event->buttons() == Qt::LeftButton) {
620     if(!_clickHandled && (event->scenePos() - _clickPos).toPoint().manhattanLength() >= QApplication::startDragDistance()) {
621       if(_clickTimer.isActive())
622         _clickTimer.stop();
623       if(_clickMode == SingleClick && isPosOverSelection(_clickPos))
624         initiateDrag(event->widget());
625       else {
626         _clickMode = DragStartClick;
627         handleClick(Qt::LeftButton, _clickPos);
628       }
629       _clickMode = NoClick;
630     }
631     if(_isSelecting) {
632       updateSelection(event->scenePos());
633       emit mouseMoveWhileSelecting(event->scenePos());
634       event->accept();
635     } else if(_clickHandled && _clickMode < DoubleClick)
636       QGraphicsScene::mouseMoveEvent(event);
637   } else
638     QGraphicsScene::mouseMoveEvent(event);
639 }
640
641 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
642   if(event->buttons() == Qt::LeftButton) {
643     _leftButtonPressed = true;
644     _clickHandled = false;
645     if(!isPosOverSelection(event->scenePos())) {
646       // immediately clear selection if clicked outside; otherwise, wait for potential drag
647       clearSelection();
648     }
649     if(_clickMode != NoClick && _clickTimer.isActive()) {
650       switch(_clickMode) {
651         case NoClick: _clickMode = SingleClick; break;
652         case SingleClick: _clickMode = DoubleClick; break;
653         case DoubleClick: _clickMode = TripleClick; break;
654         case TripleClick: _clickMode = DoubleClick; break;
655         case DragStartClick: break;
656       }
657       handleClick(Qt::LeftButton, _clickPos);
658     } else {
659       _clickMode = SingleClick;
660       _clickPos = event->scenePos();
661     }
662     _clickTimer.start();
663   }
664   if(event->type() == QEvent::GraphicsSceneMouseDoubleClick)
665     QGraphicsScene::mouseDoubleClickEvent(event);
666   else
667     QGraphicsScene::mousePressEvent(event);
668 }
669
670 void ChatScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
671   // we check for doubleclick ourselves, so just call press handler
672   mousePressEvent(event);
673 }
674
675 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
676   if(event->button() == Qt::LeftButton && _leftButtonPressed) {
677     _leftButtonPressed = false;
678     if(_clickMode != NoClick) {
679       if(_clickMode == SingleClick)
680         clearSelection();
681       event->accept();
682       if(!_clickTimer.isActive())
683         handleClick(Qt::LeftButton, _clickPos);
684     } else {
685       // no click -> drag or selection move
686       if(isGloballySelecting()) {
687         selectionToClipboard(QClipboard::Selection);
688         _isSelecting = false;
689         event->accept();
690         return;
691       }
692     }
693   }
694   QGraphicsScene::mouseReleaseEvent(event);
695 }
696
697 void ChatScene::clickTimeout() {
698   if(!_leftButtonPressed && _clickMode == SingleClick)
699     handleClick(Qt::LeftButton, _clickPos);
700 }
701
702 void ChatScene::handleClick(Qt::MouseButton button, const QPointF &scenePos) {
703   if(button == Qt::LeftButton) {
704     clearSelection();
705
706     // Now send click down to items
707     ChatItem *chatItem = chatItemAt(scenePos);
708     if(chatItem) {
709       chatItem->handleClick(chatItem->mapFromScene(scenePos), _clickMode);
710     }
711     _clickHandled = true;
712   }
713 }
714
715 void ChatScene::initiateDrag(QWidget *source) {
716   QDrag *drag = new QDrag(source);
717   QMimeData *mimeData = new QMimeData;
718   mimeData->setText(selection());
719   drag->setMimeData(mimeData);
720
721   drag->exec(Qt::CopyAction);
722 }
723
724 /******** SELECTIONS ******************************************************************************/
725
726 void ChatScene::selectionToClipboard(QClipboard::Mode mode) {
727   if(!hasSelection())
728     return;
729
730   stringToClipboard(selection(), mode);
731 }
732
733 void ChatScene::stringToClipboard(const QString &str_, QClipboard::Mode mode) {
734   QString str = str_;
735   // remove trailing linefeeds
736   if(str.endsWith('\n'))
737     str.chop(1);
738
739   switch(mode) {
740     case QClipboard::Clipboard:
741       QApplication::clipboard()->setText(str);
742       break;
743     case QClipboard::Selection:
744       if(QApplication::clipboard()->supportsSelection())
745         QApplication::clipboard()->setText(str, QClipboard::Selection);
746       break;
747     default:
748       break;
749   };
750 }
751
752 //!\brief Convert current selection to human-readable string.
753 QString ChatScene::selection() const {
754   //TODO Make selection format configurable!
755   if(hasGlobalSelection()) {
756     int start = qMin(_selectionStart, _selectionEnd);
757     int end = qMax(_selectionStart, _selectionEnd);
758     if(start < 0 || end >= _lines.count()) {
759       qDebug() << "Invalid selection range:" << start << end;
760       return QString();
761     }
762     QString result;
763     for(int l = start; l <= end; l++) {
764       if(_selectionMinCol == ChatLineModel::TimestampColumn)
765         result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
766       if(_selectionMinCol <= ChatLineModel::SenderColumn)
767         result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " ";
768       result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n";
769     }
770     return result;
771   } else if(selectingItem())
772     return selectingItem()->selection();
773   return QString();
774 }
775
776 bool ChatScene::hasSelection() const {
777   return hasGlobalSelection() || (selectingItem() && selectingItem()->hasSelection());
778 }
779
780 bool ChatScene::hasGlobalSelection() const {
781   return _selectionStart >= 0;
782 }
783
784 bool ChatScene::isGloballySelecting() const {
785   return _isSelecting;
786 }
787
788 void ChatScene::clearGlobalSelection() {
789   if(hasGlobalSelection()) {
790     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++)
791       _lines[l]->setSelected(false);
792     _isSelecting = false;
793     _selectionStart = -1;
794   }
795 }
796
797 void ChatScene::clearSelection() {
798   clearGlobalSelection();
799   if(selectingItem())
800     selectingItem()->clearSelection();
801 }
802
803 /******** *************************************************************************************/
804
805 void ChatScene::requestBacklog() {
806   MessageFilter *filter = qobject_cast<MessageFilter*>(model());
807   if(filter)
808     return filter->requestBacklog();
809   return;
810 }
811
812 ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) const {
813   if(x < _firstColHandle->x())
814     return ChatLineModel::TimestampColumn;
815   if(x < _secondColHandle->x())
816     return ChatLineModel::SenderColumn;
817
818   return ChatLineModel::ContentsColumn;
819 }
820
821 int ChatScene::rowByScenePos(qreal y) const {
822   QList<QGraphicsItem*> itemList = items(QPointF(0, y));
823
824   // ChatLine should be at the bottom of the list
825   for(int i = itemList.count()-1; i >= 0; i--) {
826     ChatLine *line = qgraphicsitem_cast<ChatLine *>(itemList.at(i));
827     if(line)
828       return line->row();
829   }
830   return -1;
831 }
832
833 void ChatScene::updateSceneRect(qreal width) {
834   if(_lines.isEmpty()) {
835     updateSceneRect(QRectF(0, 0, width, 0));
836     return;
837   }
838
839   // we hide day change messages at the top by making the scene rect smaller
840   // and by calling QGraphicsItem::hide() on all leading day change messages
841   // the first one is needed to ensure proper scrollbar ranges
842   // the second for cases where the viewport is larger then the set scenerect
843   //  (in this case the items are shown anyways)
844   if(_firstLineRow == -1) {
845     int numRows = model()->rowCount();
846     _firstLineRow = 0;
847     QModelIndex firstLineIdx;
848     while(_firstLineRow < numRows) {
849       firstLineIdx = model()->index(_firstLineRow, 0);
850       if((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
851         break;
852       _lines.at(_firstLineRow)->hide();
853       _firstLineRow++;
854     }
855   }
856
857   // the following call should be safe. If it crashes something went wrong during insert/remove
858   if(_firstLineRow < _lines.count()) {
859     ChatLine *firstLine = _lines.at(_firstLineRow);
860     ChatLine *lastLine = _lines.last();
861     updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
862   } else {
863     // empty scene rect
864     updateSceneRect(QRectF(0, 0, width, 0));
865   }
866 }
867
868 void ChatScene::updateSceneRect(const QRectF &rect) {
869   _sceneRect = rect;
870   setSceneRect(rect);
871   update();
872 }
873
874 bool ChatScene::event(QEvent *e) {
875   if(e->type() == QEvent::ApplicationPaletteChange) {
876     _firstColHandle->setColor(QApplication::palette().windowText().color());
877     _secondColHandle->setColor(QApplication::palette().windowText().color());
878   }
879   return QGraphicsScene::event(e);
880 }
881
882 // ========================================
883 //  Webkit Only stuff
884 // ========================================
885 #ifdef HAVE_WEBKIT
886 void ChatScene::loadWebPreview(ChatItem *parentItem, const QUrl &url, const QRectF &urlRect) {
887   if(!_showWebPreview)
888     return;
889
890   if(webPreview.urlRect != urlRect)
891     webPreview.urlRect = urlRect;
892
893   if(webPreview.parentItem != parentItem)
894     webPreview.parentItem = parentItem;
895
896   if(webPreview.url != url) {
897     webPreview.url = url;
898     // prepare to load a different URL
899     if(webPreview.previewItem) {
900       if(webPreview.previewItem->scene())
901         removeItem(webPreview.previewItem);
902       delete webPreview.previewItem;
903       webPreview.previewItem = 0;
904     }
905     webPreview.previewState = WebPreview::NoPreview;
906   }
907
908   if(webPreview.url.isEmpty())
909     return;
910
911   // qDebug() << Q_FUNC_INFO << webPreview.previewState;
912   switch(webPreview.previewState) {
913   case WebPreview::NoPreview:
914     webPreview.previewState = WebPreview::NewPreview;
915     webPreview.timer.start(500);
916     break;
917   case WebPreview::NewPreview:
918   case WebPreview::DelayPreview:
919   case WebPreview::ShowPreview:
920     // we're already waiting for the next step or showing the preview
921     break;
922   case WebPreview::HidePreview:
923     // we still have a valid preview
924     webPreview.previewState = WebPreview::DelayPreview;
925     webPreview.timer.start(1000);
926     break;
927   }
928   // qDebug() << "  new State:" << webPreview.previewState << webPreview.timer.isActive();
929 }
930
931 void ChatScene::webPreviewNextStep() {
932   // qDebug() << Q_FUNC_INFO << webPreview.previewState;
933   switch(webPreview.previewState) {
934   case WebPreview::NoPreview:
935     break;
936   case WebPreview::NewPreview:
937     Q_ASSERT(!webPreview.previewItem);
938     webPreview.previewItem = new WebPreviewItem(webPreview.url);
939     webPreview.previewState = WebPreview::DelayPreview;
940     webPreview.timer.start(1000);
941     break;
942   case WebPreview::DelayPreview:
943     Q_ASSERT(webPreview.previewItem);
944     // calc position and show
945     {
946       qreal previewY = webPreview.urlRect.bottom();
947       qreal previewX = webPreview.urlRect.x();
948       if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
949         previewY = webPreview.urlRect.y() - webPreview.previewItem->boundingRect().height();
950
951       if(previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
952         previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
953
954       webPreview.previewItem->setPos(previewX, previewY);
955     }
956     addItem(webPreview.previewItem);
957     webPreview.previewState = WebPreview::ShowPreview;
958     break;
959   case WebPreview::ShowPreview:
960     qWarning() << "ChatScene::webPreviewNextStep() called while in ShowPreview Step!";
961     qWarning() << "removing preview";
962     if(webPreview.previewItem && webPreview.previewItem->scene())
963       removeItem(webPreview.previewItem);
964     // Fall through to deletion!
965   case WebPreview::HidePreview:
966     if(webPreview.previewItem) {
967       delete webPreview.previewItem;
968       webPreview.previewItem = 0;
969     }
970     webPreview.parentItem = 0;
971     webPreview.url = QUrl();
972     webPreview.urlRect = QRectF();
973     webPreview.previewState = WebPreview::NoPreview;
974   }
975   // qDebug() << "  new State:" << webPreview.previewState << webPreview.timer.isActive();
976 }
977
978 void ChatScene::clearWebPreview(ChatItem *parentItem) {
979   // qDebug() << Q_FUNC_INFO << webPreview.previewState;
980   switch(webPreview.previewState) {
981   case WebPreview::NewPreview:
982     webPreview.previewState = WebPreview::NoPreview; // we haven't loaded anything yet
983     break;
984   case WebPreview::ShowPreview:
985     if(parentItem == 0 || webPreview.parentItem == parentItem) {
986       if(webPreview.previewItem && webPreview.previewItem->scene())
987         removeItem(webPreview.previewItem);
988     }
989     // fall through into to set hidden state
990   case WebPreview::DelayPreview:
991     // we're just loading, so haven't shown the preview yet.
992     webPreview.previewState = WebPreview::HidePreview;
993     webPreview.timer.start(5000);
994     break;
995   case WebPreview::NoPreview:
996   case WebPreview::HidePreview:
997     break;
998   }
999   // qDebug() << "  new State:" << webPreview.previewState << webPreview.timer.isActive();
1000 }
1001 #endif
1002
1003 // ========================================
1004 //  end of webkit only
1005 // ========================================
1006
1007 void ChatScene::showWebPreviewChanged() {
1008   ChatViewSettings settings;
1009   _showWebPreview = settings.showWebPreview();
1010 }