+
+int ChatScene::rowByScenePos(qreal y) const {
+ // This is somewhat hacky... we look at the contents item that is at the given y position, since
+ // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
+ // ChatItems cover their ChatLine, so we won't get to the latter directly.
+ ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(_secondColHandle->sceneRight() + 1, y)));
+ if(!contentItem) return -1;
+ return contentItem->row();
+}
+
+void ChatScene::updateSceneRect(qreal width) {
+ if(_lines.isEmpty()) {
+ updateSceneRect(QRectF(0, 0, width, 0));
+ return;
+ }
+
+ // we hide day change messages at the top by making the scene rect smaller
+ // and by calling QGraphicsItem::hide() on all leading day change messages
+ // the first one is needed to ensure proper scrollbar ranges
+ // the second for cases where the viewport is larger then the set scenerect
+ // (in this case the items are shown anyways)
+ if(_firstLineRow == -1) {
+ int numRows = model()->rowCount();
+ _firstLineRow = 0;
+ QModelIndex firstLineIdx;
+ while(_firstLineRow < numRows) {
+ firstLineIdx = model()->index(_firstLineRow, 0);
+ if((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
+ break;
+ _lines.at(_firstLineRow)->hide();
+ _firstLineRow++;
+ }
+ }
+
+ // the following call should be safe. If it crashes something went wrong during insert/remove
+ if(_firstLineRow < _lines.count()) {
+ ChatLine *firstLine = _lines.at(_firstLineRow);
+ ChatLine *lastLine = _lines.last();
+ updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
+ } else {
+ // empty scene rect
+ updateSceneRect(QRectF(0, 0, width, 0));
+ }
+}
+
+void ChatScene::updateSceneRect(const QRectF &rect) {
+ _sceneRect = rect;
+ setSceneRect(rect);
+ update();
+}
+
+bool ChatScene::event(QEvent *e) {
+ if(e->type() == QEvent::ApplicationPaletteChange) {
+ _firstColHandle->setColor(QApplication::palette().windowText().color());
+ _secondColHandle->setColor(QApplication::palette().windowText().color());
+ }
+ return QGraphicsScene::event(e);
+}
+
+/******** WEB PREVIEW *****************************************************************************/
+
+void ChatScene::loadWebPreview(ChatItem *parentItem, const QString &url, const QRectF &urlRect) {
+#ifndef HAVE_WEBKIT
+ Q_UNUSED(parentItem)
+ Q_UNUSED(url)
+ Q_UNUSED(urlRect)
+#else
+ if(!_showWebPreview)
+ return;
+
+ if(webPreview.parentItem != parentItem)
+ webPreview.parentItem = parentItem;
+
+ if(webPreview.url != url) {
+ webPreview.url = url;
+ // load a new web view and delete the old one (if exists)
+ if(webPreview.previewItem && webPreview.previewItem->scene()) {
+ removeItem(webPreview.previewItem);
+ delete webPreview.previewItem;
+ }
+ webPreview.previewItem = new WebPreviewItem(url);
+ webPreview.delayTimer.start(2000);
+ webPreview.deleteTimer.stop();
+ } else if(webPreview.previewItem && !webPreview.previewItem->scene()) {
+ // we just have to readd the item to the scene
+ webPreview.delayTimer.start(2000);
+ webPreview.deleteTimer.stop();
+ }
+ if(webPreview.urlRect != urlRect) {
+ webPreview.urlRect = urlRect;
+ qreal previewY = urlRect.bottom();
+ qreal previewX = urlRect.x();
+ if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
+ previewY = urlRect.y() - webPreview.previewItem->boundingRect().height();
+
+ if(previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
+ previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
+
+ webPreview.previewItem->setPos(previewX, previewY);
+ }
+#endif
+}
+
+void ChatScene::showWebPreviewEvent() {
+#ifdef HAVE_WEBKIT
+ if(webPreview.previewItem)
+ addItem(webPreview.previewItem);
+#endif
+}
+
+void ChatScene::clearWebPreview(ChatItem *parentItem) {
+#ifndef HAVE_WEBKIT
+ Q_UNUSED(parentItem)
+#else
+ if(parentItem == 0 || webPreview.parentItem == parentItem) {
+ if(webPreview.previewItem && webPreview.previewItem->scene()) {
+ removeItem(webPreview.previewItem);
+ webPreview.deleteTimer.start();
+ }
+ webPreview.delayTimer.stop();
+ }
+#endif
+}
+
+void ChatScene::deleteWebPreviewEvent() {
+#ifdef HAVE_WEBKIT
+ if(webPreview.previewItem) {
+ delete webPreview.previewItem;
+ webPreview.previewItem = 0;
+ }
+ webPreview.parentItem = 0;
+ webPreview.url = QString();
+ webPreview.urlRect = QRectF();
+#endif
+}
+
+void ChatScene::showWebPreviewChanged() {
+ ChatViewSettings settings;
+ _showWebPreview = settings.showWebPreview();
+}