clean up
[quassel.git] / src / qtui / chatscene.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 <QGraphicsSceneMouseEvent>
24 #include <QPersistentModelIndex>
25 #include <QWebView>
26
27 #include "chatitem.h"
28 #include "chatline.h"
29 #include "chatlinemodelitem.h"
30 #include "chatscene.h"
31 #include "client.h"
32 #include "clientbacklogmanager.h"
33 #include "columnhandleitem.h"
34 #include "messagefilter.h"
35 #include "qtui.h"
36 #include "qtuistyle.h"
37 #include "chatviewsettings.h"
38 #include "webpreviewitem.h"
39
40 const qreal minContentsWidth = 200;
41
42 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, QObject *parent)
43   : QGraphicsScene(0, 0, width, 0, parent),
44     _idString(idString),
45     _model(model),
46     _singleBufferScene(false),
47     _sceneRect(0, 0, width, 0),
48     _firstLineRow(-1),
49     _viewportHeight(0),
50     _selectingItem(0),
51     _selectionStart(-1),
52     _isSelecting(false)
53 {
54   MessageFilter *filter = qobject_cast<MessageFilter*>(model);
55   if(filter) {
56     _singleBufferScene = filter->isSingleBufferFilter();
57   }
58
59   ChatViewSettings defaultSettings;
60   int defaultFirstColHandlePos = defaultSettings.value("FirstColumnHandlePos", 80).toInt();
61   int defaultSecondColHandlePos = defaultSettings.value("SecondColumnHandlePos", 200).toInt();
62
63   ChatViewSettings viewSettings(this);
64   _firstColHandlePos = viewSettings.value("FirstColumnHandlePos", defaultFirstColHandlePos).toInt();
65   _secondColHandlePos = viewSettings.value("SecondColumnHandlePos", defaultSecondColHandlePos).toInt();
66
67   _firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator());
68   addItem(_firstColHandle);
69   _firstColHandle->setXPos(_firstColHandlePos);
70   connect(_firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(firstHandlePositionChanged(qreal)));
71   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _firstColHandle, SLOT(sceneRectChanged(const QRectF &)));
72
73   _secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator());
74   addItem(_secondColHandle);
75   _secondColHandle->setXPos(_secondColHandlePos);
76   connect(_secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(secondHandlePositionChanged(qreal)));
77   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), _secondColHandle, SLOT(sceneRectChanged(const QRectF &)));
78
79   setHandleXLimits();
80
81   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
82           this, SLOT(rowsInserted(const QModelIndex &, int, int)));
83   connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
84           this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
85
86   if(model->rowCount() > 0)
87     rowsInserted(QModelIndex(), 0, model->rowCount() - 1);
88
89 #ifdef HAVE_WEBKIT
90   webPreview.delayTimer.setSingleShot(true);
91   connect(&webPreview.delayTimer, SIGNAL(timeout()), this, SLOT(showWebPreviewEvent()));
92   webPreview.deleteTimer.setInterval(600000);
93   connect(&webPreview.deleteTimer, SIGNAL(timeout()), this, SLOT(deleteWebPreviewEvent()));
94 #endif
95
96   setItemIndexMethod(QGraphicsScene::NoIndex);
97 }
98
99 ChatScene::~ChatScene() {
100 }
101
102 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
103   Q_UNUSED(index);
104 //   QModelIndex sidx = model()->index(start, 0);
105 //   QModelIndex eidx = model()->index(end, 0);
106 //   qDebug() << "rowsInserted" << start << end << "-" << sidx.data(MessageModel::MsgIdRole).value<MsgId>() << eidx.data(MessageModel::MsgIdRole).value<MsgId>();
107
108   qreal h = 0;
109   qreal y = _sceneRect.y();
110   qreal width = _sceneRect.width();
111   bool atTop = true;
112   bool atBottom = false;
113   bool moveTop = false;
114
115   if(start > 0 && start < _lines.count()) {
116     y = _lines.value(start)->y();
117     atTop = false;
118   }
119   if(start == _lines.count()) {
120     y = _sceneRect.bottom();
121     atTop = false;
122     atBottom = true;
123   }
124
125   qreal contentsWidth = width - secondColumnHandle()->sceneRight();
126   qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
127   qreal timestampWidth = firstColumnHandle()->sceneLeft();
128   QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
129   QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
130
131   if(atTop) {
132     for(int i = end; i >= start; i--) {
133       ChatLine *line = new ChatLine(i, model(),
134                                     width,
135                                     timestampWidth, senderWidth, contentsWidth,
136                                     senderPos, contentsPos);
137       h += line->height();
138       line->setPos(0, y-h);
139       _lines.insert(start, line);
140       addItem(line);
141     }
142   } else {
143     for(int i = start; i <= end; i++) {
144       ChatLine *line = new ChatLine(i, model(),
145                                     width,
146                                     timestampWidth, senderWidth, contentsWidth,
147                                     senderPos, contentsPos);
148       line->setPos(0, y+h);
149       h += line->height();
150       _lines.insert(i, line);
151       addItem(line);
152     }
153   }
154
155   // update existing items
156   for(int i = end+1; i < _lines.count(); i++) {
157     _lines[i]->setRow(i);
158   }
159
160   // update selection
161   if(_selectionStart >= 0) {
162     int offset = end - start + 1;
163     if(_selectionStart >= start) _selectionStart += offset;
164     if(_selectionEnd >= start) _selectionEnd += offset;
165     if(_firstSelectionRow >= start) _firstSelectionRow += offset;
166     if(_lastSelectionRow >= start) _lastSelectionRow += offset;
167   }
168
169   // neither pre- or append means we have to do dirty work: move items...
170   if(!(atTop || atBottom)) {
171     qreal offset = h;
172     int moveStart = 0;
173     int moveEnd = _lines.count() - 1;
174     // move top means: moving 0 to end (aka: end + 1)
175     // move top means: moving end + 1 to _lines.count() - 1 (aka: _lines.count() - (end + 1)
176     if(end + 1 < _lines.count() - end - 1) {
177       // move top part
178       moveTop = true;
179       offset = -offset;
180       moveEnd = end;
181     } else {
182       // move bottom part
183       moveStart = end + 1;
184     }
185     ChatLine *line = 0;
186     for(int i = moveStart; i <= moveEnd; i++) {
187       line = _lines.at(i);
188       line->setPos(0, line->pos().y() + offset);
189     }
190   }
191
192   // check if all went right
193   Q_ASSERT(start == 0 || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
194   Q_ASSERT(end + 1 == _lines.count() || _lines.at(end)->pos().y() + _lines.at(end)->height() == _lines.at(end + 1)->pos().y());
195
196   if(!atBottom) {
197     if(start < _firstLineRow) {
198       int prevFirstLineRow = _firstLineRow + (end - start + 1);
199       for(int i = end + 1; i < prevFirstLineRow; i++) {
200         _lines.at(i)->show();
201       }
202     }
203     // force new search for first proper line
204     _firstLineRow = -1;
205   }
206   updateSceneRect();
207   if(atBottom || (!atTop && !moveTop)) {
208     emit lastLineChanged(_lines.last(), h);
209   }
210 }
211
212 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
213   Q_UNUSED(parent);
214
215   qreal h = 0; // total height of removed items;
216
217   bool atTop = (start == 0);
218   bool atBottom = (end == _lines.count() - 1);
219   bool moveTop = false;
220
221   // remove items from scene
222   QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
223   int lineCount = start;
224   while(lineIter != _lines.end() && lineCount <= end) {
225     h += (*lineIter)->height();
226     delete *lineIter;
227     lineIter = _lines.erase(lineIter);
228     lineCount++;
229   }
230
231   // update rows of remaining chatlines
232   for(int i = start; i < _lines.count(); i++) {
233     _lines.at(i)->setRow(i);
234   }
235
236   // update selection
237   if(_selectionStart >= 0) {
238     int offset = end - start + 1;
239     if(_selectionStart >= start)
240       _selectionStart -= offset;
241     if(_selectionEnd >= start)
242       _selectionEnd -= offset;
243     if(_firstSelectionRow >= start)
244       _firstSelectionRow -= offset;
245     if(_lastSelectionRow >= start)
246       _lastSelectionRow -= offset;
247   }
248
249   // neither removing at bottom or top means we have to move items...
250   if(!(atTop || atBottom)) {
251     qreal offset = h;
252     int moveStart = 0;
253     int moveEnd = _lines.count() - 1;
254     if(start < _lines.count() - start) {
255       // move top part
256       moveTop = true;
257       moveEnd = start - 1;
258     } else {
259       // move bottom part
260       moveStart = start;
261       offset = -offset;
262     }
263     ChatLine *line = 0;
264     for(int i = moveStart; i <= moveEnd; i++) {
265       line = _lines.at(i);
266       line->setPos(0, line->pos().y() + offset);
267     }
268   }
269
270   Q_ASSERT(start == 0 || start >= _lines.count() || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
271
272   // update sceneRect
273   // when searching for the first non-date-line we have to take into account that our
274   // model still contains the just removed lines so we cannot simply call updateSceneRect()
275   int numRows = model()->rowCount();
276   QModelIndex firstLineIdx;
277   _firstLineRow = -1;
278   bool needOffset = false;
279   do {
280     _firstLineRow++;
281     if(_firstLineRow >= start && _firstLineRow <= end) {
282       _firstLineRow = end + 1;
283       needOffset = true;
284     }
285     firstLineIdx = model()->index(_firstLineRow, 0);
286   } while((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) == Message::DayChange && _firstLineRow < numRows);
287
288   if(needOffset)
289     _firstLineRow -= end - start + 1;
290   updateSceneRect();
291 }
292
293 void ChatScene::updateForViewport(qreal width, qreal height) {
294   _viewportHeight = height;
295   setWidth(width);
296 }
297
298 void ChatScene::setWidth(qreal width) {
299   if(width == _sceneRect.width())
300     return;
301
302   // clock_t startT = clock();
303
304   // disabling the index while doing this complex updates is about
305   // 2 to 10 times faster!
306   //setItemIndexMethod(QGraphicsScene::NoIndex);
307
308   QList<ChatLine *>::iterator lineIter = _lines.end();
309   QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
310   qreal linePos = _sceneRect.y() + _sceneRect.height();
311   qreal contentsWidth = width - secondColumnHandle()->sceneRight();
312   while(lineIter != lineIterBegin) {
313     lineIter--;
314     (*lineIter)->setGeometryByWidth(width, contentsWidth, linePos);
315   }
316   //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
317
318   updateSceneRect(width);
319   setHandleXLimits();
320
321 //   clock_t endT = clock();
322 //   qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
323 }
324
325 void ChatScene::firstHandlePositionChanged(qreal xpos) {
326   if(_firstColHandlePos == xpos)
327     return;
328
329   _firstColHandlePos = xpos;
330   ChatViewSettings viewSettings(this);
331   viewSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
332   ChatViewSettings defaultSettings;
333   defaultSettings.setValue("FirstColumnHandlePos", _firstColHandlePos);
334
335   // clock_t startT = clock();
336
337   // disabling the index while doing this complex updates is about
338   // 2 to 10 times faster!
339   //setItemIndexMethod(QGraphicsScene::NoIndex);
340
341   QList<ChatLine *>::iterator lineIter = _lines.end();
342   QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
343   qreal timestampWidth = firstColumnHandle()->sceneLeft();
344   qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
345   QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
346
347   while(lineIter != lineIterBegin) {
348     lineIter--;
349     (*lineIter)->setFirstColumn(timestampWidth, senderWidth, senderPos);
350   }
351   //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
352
353   setHandleXLimits();
354
355 //   clock_t endT = clock();
356 //   qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
357 }
358
359 void ChatScene::secondHandlePositionChanged(qreal xpos) {
360   if(_secondColHandlePos == xpos)
361     return;
362
363   _secondColHandlePos = xpos;
364   ChatViewSettings viewSettings(this);
365   viewSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
366   ChatViewSettings defaultSettings;
367   defaultSettings.setValue("SecondColumnHandlePos", _secondColHandlePos);
368
369   // clock_t startT = clock();
370
371   // disabling the index while doing this complex updates is about
372   // 2 to 10 times faster!
373   //setItemIndexMethod(QGraphicsScene::NoIndex);
374
375   QList<ChatLine *>::iterator lineIter = _lines.end();
376   QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
377   qreal linePos = _sceneRect.y() + _sceneRect.height();
378   qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
379   qreal contentsWidth = _sceneRect.width() - secondColumnHandle()->sceneRight();
380   QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
381   while(lineIter != lineIterBegin) {
382     lineIter--;
383     (*lineIter)->setSecondColumn(senderWidth, contentsWidth, contentsPos, linePos);
384   }
385   //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
386
387   setHandleXLimits();
388
389 //   clock_t endT = clock();
390 //   qDebug() << "resized" << _lines.count() << "in" << (float)(endT - startT) / CLOCKS_PER_SEC << "sec";
391 }
392
393 void ChatScene::setHandleXLimits() {
394   _firstColHandle->setXLimits(0, _secondColHandle->sceneLeft());
395   _secondColHandle->setXLimits(_firstColHandle->sceneRight(), width() - minContentsWidth);
396 }
397
398 void ChatScene::setSelectingItem(ChatItem *item) {
399   if(_selectingItem) _selectingItem->clearSelection();
400   _selectingItem = item;
401 }
402
403 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
404   _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
405   _selectionStartCol = _selectionMinCol = item->column();
406   _isSelecting = true;
407   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
408   updateSelection(item->mapToScene(itemPos));
409 }
410
411 void ChatScene::updateSelection(const QPointF &pos) {
412   // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
413   // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
414   ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(_secondColHandle->sceneRight() + 1, pos.y())));
415   if(!contentItem) return;
416
417   int curRow = contentItem->row();
418   int curColumn;
419   if(pos.x() > _secondColHandle->sceneRight()) curColumn = ChatLineModel::ContentsColumn;
420   else if(pos.x() > _firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
421   else curColumn = ChatLineModel::TimestampColumn;
422
423   ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
424   if(minColumn != _selectionMinCol) {
425     _selectionMinCol = minColumn;
426     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
427       _lines[l]->setSelected(true, minColumn);
428     }
429   }
430   int newstart = qMin(curRow, _firstSelectionRow);
431   int newend = qMax(curRow, _firstSelectionRow);
432   if(newstart < _selectionStart) {
433     for(int l = newstart; l < _selectionStart; l++)
434       _lines[l]->setSelected(true, minColumn);
435   }
436   if(newstart > _selectionStart) {
437     for(int l = _selectionStart; l < newstart; l++)
438       _lines[l]->setSelected(false);
439   }
440   if(newend > _selectionEnd) {
441     for(int l = _selectionEnd+1; l <= newend; l++)
442       _lines[l]->setSelected(true, minColumn);
443   }
444   if(newend < _selectionEnd) {
445     for(int l = newend+1; l <= _selectionEnd; l++)
446       _lines[l]->setSelected(false);
447   }
448
449   _selectionStart = newstart;
450   _selectionEnd = newend;
451   _lastSelectionRow = curRow;
452
453   if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
454     if(!_selectingItem) {
455       qWarning() << "WARNING: ChatScene::updateSelection() has a null _selectingItem, this should never happen! Please report.";
456       return;
457     }
458     _lines[curRow]->setSelected(false);
459     _isSelecting = false;
460     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
461   }
462 }
463
464 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
465   if(_isSelecting && event->buttons() == Qt::LeftButton) {
466     updateSelection(event->scenePos());
467     event->accept();
468   } else {
469     QGraphicsScene::mouseMoveEvent(event);
470   }
471 }
472
473 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
474   if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
475     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
476       _lines[l]->setSelected(false);
477     }
478     _selectionStart = -1;
479     QGraphicsScene::mousePressEvent(event);  // so we can start a new local selection
480   } else {
481     QGraphicsScene::mousePressEvent(event);
482   }
483 }
484
485 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
486   if(_isSelecting && !event->buttons() & Qt::LeftButton) {
487     putToClipboard(selectionToString());
488     _isSelecting = false;
489     event->accept();
490   } else {
491     QGraphicsScene::mouseReleaseEvent(event);
492   }
493 }
494
495 void ChatScene::putToClipboard(const QString &selection) {
496   // TODO Configure clipboards
497 #   ifdef Q_WS_X11
498   QApplication::clipboard()->setText(selection, QClipboard::Selection);
499 #   endif
500 //# else
501   QApplication::clipboard()->setText(selection);
502 //# endif
503 }
504
505 //!\brief Convert current selection to human-readable string.
506 QString ChatScene::selectionToString() const {
507   //TODO Make selection format configurable!
508   if(!_isSelecting) return QString();
509   int start = qMin(_selectionStart, _selectionEnd);
510   int end = qMax(_selectionStart, _selectionEnd);
511   if(start < 0 || end >= _lines.count()) {
512     qDebug() << "Invalid selection range:" << start << end;
513     return QString();
514   }
515   QString result;
516   for(int l = start; l <= end; l++) {
517     if(_selectionMinCol == ChatLineModel::TimestampColumn)
518       result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
519     if(_selectionMinCol <= ChatLineModel::SenderColumn)
520       result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
521     result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
522   }
523   return result;
524 }
525
526 void ChatScene::requestBacklog() {
527   MessageFilter *filter = qobject_cast<MessageFilter*>(model());
528   if(filter)
529     return filter->requestBacklog();
530   return;
531 }
532
533 int ChatScene::sectionByScenePos(int x) {
534   if(x < _firstColHandle->x())
535     return ChatLineModel::TimestampColumn;
536   if(x < _secondColHandle->x())
537     return ChatLineModel::SenderColumn;
538
539   return ChatLineModel::ContentsColumn;
540 }
541
542 void ChatScene::updateSceneRect(qreal width) {
543   if(_lines.isEmpty()) {
544     updateSceneRect(QRectF(0, 0, width, 0));
545     return;
546   }
547
548   // we hide day change messages at the top by making the scene rect smaller
549   // and by calling QGraphicsItem::hide() on all leading day change messages
550   // the first one is needed to ensure proper scrollbar ranges
551   // the second for cases where the viewport is larger then the set scenerect
552   //  (in this case the items are shown anyways)
553   if(_firstLineRow == -1) {
554     int numRows = model()->rowCount();
555     _firstLineRow = 0;
556     QModelIndex firstLineIdx;
557     while(_firstLineRow < numRows) {
558       firstLineIdx = model()->index(_firstLineRow, 0);
559       if((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
560         break;
561       _lines.at(_firstLineRow)->hide();
562       _firstLineRow++;
563     }
564   }
565
566   // the following call should be safe. If it crashes something went wrong during insert/remove
567   if(_firstLineRow < _lines.count()) {
568     ChatLine *firstLine = _lines.at(_firstLineRow);
569     ChatLine *lastLine = _lines.last();
570     updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
571   } else {
572     // empty scene rect
573     updateSceneRect(QRectF(0, 0, width, 0));
574   }
575 }
576
577 void ChatScene::updateSceneRect(const QRectF &rect) {
578   _sceneRect = rect;
579   setSceneRect(rect);
580   update();
581 }
582
583 void ChatScene::customEvent(QEvent *event) {
584   switch(event->type()) {
585   default:
586     return;
587   }
588 }
589
590 void ChatScene::loadWebPreview(ChatItem *parentItem, const QString &url, const QRectF &urlRect) {
591 #ifndef HAVE_WEBKIT
592   Q_UNUSED(parentItem)
593   Q_UNUSED(url)
594   Q_UNUSED(urlRect)
595 #else
596   if(webPreview.parentItem != parentItem)
597     webPreview.parentItem = parentItem;
598
599   if(webPreview.url != url) {
600     webPreview.url = url;
601     // load a new web view and delete the old one (if exists)
602     if(webPreview.previewItem && webPreview.previewItem->scene()) {
603       removeItem(webPreview.previewItem);
604       delete webPreview.previewItem;
605     }
606     webPreview.previewItem = new WebPreviewItem(url);
607     webPreview.delayTimer.start(2000);
608     webPreview.deleteTimer.stop();
609   } else if(webPreview.previewItem && !webPreview.previewItem->scene()) {
610       // we just have to readd the item to the scene
611       webPreview.delayTimer.start(2000);
612       webPreview.deleteTimer.stop();
613   }
614   if(webPreview.urlRect != urlRect) {
615     webPreview.urlRect = urlRect;
616     qreal previewY = urlRect.bottom();
617     qreal previewX = urlRect.x();
618     if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
619       previewY = urlRect.y() - webPreview.previewItem->boundingRect().height();
620
621     if(previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
622       previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
623
624     webPreview.previewItem->setPos(previewX, previewY);
625   }
626 #endif
627 }
628
629 void ChatScene::showWebPreviewEvent() {
630 #ifdef HAVE_WEBKIT
631   if(webPreview.previewItem)
632     addItem(webPreview.previewItem);
633 #endif
634 }
635
636 void ChatScene::clearWebPreview(ChatItem *parentItem) {
637 #ifndef HAVE_WEBKIT
638   Q_UNUSED(parentItem)
639 #else
640   if(parentItem == 0 || webPreview.parentItem == parentItem) {
641     if(webPreview.previewItem && webPreview.previewItem->scene()) {
642       removeItem(webPreview.previewItem);
643       webPreview.deleteTimer.start();
644     }
645     webPreview.delayTimer.stop();
646   }
647 #endif
648 }
649
650 void ChatScene::deleteWebPreviewEvent() {
651 #ifdef HAVE_WEBKIT
652   if(webPreview.previewItem) {
653     delete webPreview.previewItem;
654     webPreview.previewItem = 0;
655   }
656   webPreview.parentItem = 0;
657   webPreview.url = QString();
658   webPreview.urlRect = QRectF();
659 #endif
660 }