+
+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);
+}
+
+// ========================================
+// Webkit Only stuff
+// ========================================
+#ifdef HAVE_WEBKIT
+void ChatScene::loadWebPreview(ChatItem *parentItem, const QUrl &url, const QRectF &urlRect) {
+ if(!_showWebPreview)
+ return;
+
+ if(webPreview.urlRect != urlRect)
+ webPreview.urlRect = urlRect;
+
+ if(webPreview.parentItem != parentItem)
+ webPreview.parentItem = parentItem;
+
+ if(webPreview.url != url) {
+ webPreview.url = url;
+ // prepare to load a different URL
+ if(webPreview.previewItem) {
+ if(webPreview.previewItem->scene())
+ removeItem(webPreview.previewItem);
+ delete webPreview.previewItem;
+ webPreview.previewItem = 0;
+ }
+ webPreview.previewState = WebPreview::NoPreview;
+ }
+
+ if(webPreview.url.isEmpty())
+ return;
+
+ // qDebug() << Q_FUNC_INFO << webPreview.previewState;
+ switch(webPreview.previewState) {
+ case WebPreview::NoPreview:
+ webPreview.previewState = WebPreview::NewPreview;
+ webPreview.timer.start(500);
+ break;
+ case WebPreview::NewPreview:
+ case WebPreview::DelayPreview:
+ case WebPreview::ShowPreview:
+ // we're already waiting for the next step or showing the preview
+ break;
+ case WebPreview::HidePreview:
+ // we still have a valid preview
+ webPreview.previewState = WebPreview::DelayPreview;
+ webPreview.timer.start(1000);
+ break;
+ }
+ // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
+}
+
+void ChatScene::webPreviewNextStep() {
+ // qDebug() << Q_FUNC_INFO << webPreview.previewState;
+ switch(webPreview.previewState) {
+ case WebPreview::NoPreview:
+ break;
+ case WebPreview::NewPreview:
+ Q_ASSERT(!webPreview.previewItem);
+ webPreview.previewItem = new WebPreviewItem(webPreview.url);
+ webPreview.previewState = WebPreview::DelayPreview;
+ webPreview.timer.start(1000);
+ break;
+ case WebPreview::DelayPreview:
+ Q_ASSERT(webPreview.previewItem);
+ // calc position and show
+ {
+ qreal previewY = webPreview.urlRect.bottom();
+ qreal previewX = webPreview.urlRect.x();
+ if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
+ previewY = webPreview.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);
+ }
+ addItem(webPreview.previewItem);
+ webPreview.previewState = WebPreview::ShowPreview;
+ break;
+ case WebPreview::ShowPreview:
+ qWarning() << "ChatScene::webPreviewNextStep() called while in ShowPreview Step!";
+ qWarning() << "removing preview";
+ if(webPreview.previewItem && webPreview.previewItem->scene())
+ removeItem(webPreview.previewItem);
+ // Fall through to deletion!
+ case WebPreview::HidePreview:
+ if(webPreview.previewItem) {
+ delete webPreview.previewItem;
+ webPreview.previewItem = 0;
+ }
+ webPreview.parentItem = 0;
+ webPreview.url = QUrl();
+ webPreview.urlRect = QRectF();
+ webPreview.previewState = WebPreview::NoPreview;
+ }
+ // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
+}
+
+void ChatScene::clearWebPreview(ChatItem *parentItem) {
+ // qDebug() << Q_FUNC_INFO << webPreview.previewState;
+ switch(webPreview.previewState) {
+ case WebPreview::NewPreview:
+ webPreview.previewState = WebPreview::NoPreview; // we haven't loaded anything yet
+ break;
+ case WebPreview::ShowPreview:
+ if(parentItem == 0 || webPreview.parentItem == parentItem) {
+ if(webPreview.previewItem && webPreview.previewItem->scene())
+ removeItem(webPreview.previewItem);
+ }
+ // fall through into to set hidden state
+ case WebPreview::DelayPreview:
+ // we're just loading, so haven't shown the preview yet.
+ webPreview.previewState = WebPreview::HidePreview;
+ webPreview.timer.start(5000);
+ break;
+ case WebPreview::NoPreview:
+ case WebPreview::HidePreview:
+ break;
+ }
+ // qDebug() << " new State:" << webPreview.previewState << webPreview.timer.isActive();
+}
+#endif
+
+// ========================================
+// end of webkit only
+// ========================================
+
+void ChatScene::showWebPreviewChanged() {
+ ChatViewSettings settings;
+ _showWebPreview = settings.showWebPreview();
+}