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