From 339ed024e6cf074108e39360e7db58ea0961761b Mon Sep 17 00:00:00 2001 From: Marcus Eggenberger Date: Sun, 28 Sep 2008 13:13:07 +0200 Subject: [PATCH] improved web preview removinatings --- src/qtui/CMakeLists.txt | 9 +-- src/qtui/chatitem.cpp | 119 ++++++++++++++++++++-------------------- src/qtui/chatitem.h | 38 ++++--------- 3 files changed, 73 insertions(+), 93 deletions(-) diff --git a/src/qtui/CMakeLists.txt b/src/qtui/CMakeLists.txt index 399d0de4..253268ee 100644 --- a/src/qtui/CMakeLists.txt +++ b/src/qtui/CMakeLists.txt @@ -74,21 +74,14 @@ set(MOC_HDRS topicwidget.h verticaldock.h) -if(HAVE_WEBKIT) - set(MOC_HDRS ${MOC_HDRS} chatitem.h) -endif(HAVE_WEBKIT) - set(HEADERS + chatitem.h chatline.h chatlinemodelitem.h chatviewsettings.h qtuisettings.h qtuistyle.h) -if(NOT HAVE_WEBKIT) - set(HEADERS ${HEADERS} chatitem.h) -endif(NOT HAVE_WEBKIT) - set(FORMS aboutdlg.ui bufferviewwidget.ui diff --git a/src/qtui/chatitem.cpp b/src/qtui/chatitem.cpp index 7e26f000..072ae1b0 100644 --- a/src/qtui/chatitem.cpp +++ b/src/qtui/chatitem.cpp @@ -380,11 +380,13 @@ QVector ContentsChatItem::additionalFormats() const { } void ContentsChatItem::endHoverMode() { - if(hasLayout() && privateData()->currentClickable.isValid()) { - setCursor(Qt::ArrowCursor); - privateData()->currentClickable = Clickable(); + if(hasLayout()) { + if(privateData()->currentClickable.isValid()) { + setCursor(Qt::ArrowCursor); + privateData()->currentClickable = Clickable(); + } #ifdef HAVE_WEBKIT - privateData()->clearPreview(); + clearWebPreview(); #endif update(); } @@ -441,21 +443,8 @@ void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { if(idx >= click.start && idx < click.start + click.length) { if(click.type == Clickable::Url) { onClickable = true; - - if(!hasLayout()) - updateLayout(); - #ifdef HAVE_WEBKIT - QTextLine line = layout()->lineForTextPosition(click.start); - qreal x = line.cursorToX(click.start); - qreal width = line.cursorToX(click.start + click.length) - x; - qreal height = line.height(); - qreal y = height * line.lineNumber(); - QRectF urlRect(x, y, width, height); - QString url = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length); - if(!url.contains("://")) - url = "http://" + url; - privateData()->loadWebPreview(url, urlRect); + showWebPreview(click); #endif } else if(click.type == Clickable::Channel) { // TODO: don't make clickable if it's our own name @@ -473,59 +462,73 @@ void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { event->accept(); } +#ifdef HAVE_WEBKIT +void ContentsChatItem::showWebPreview(const Clickable &click) { + if(!hasLayout()) + updateLayout(); + + QTextLine line = layout()->lineForTextPosition(click.start); + qreal x = line.cursorToX(click.start); + qreal width = line.cursorToX(click.start + click.length) - x; + qreal height = line.height(); + qreal y = height * line.lineNumber(); + QRectF urlRect(x, y, width, height); + QString url = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length); + if(!url.contains("://")) + url = "http://" + url; + privateData()->loadWebPreview(url, urlRect); +} + +void ContentsChatItem::clearWebPreview() { + if(hasLayout()) + privateData()->clearWebPreview(); +} +#endif + // **************************************** // ContentsChatItemPrivate // **************************************** -ContentsChatItemPrivate::~ContentsChatItemPrivate() { #ifdef HAVE_WEBKIT - clearPreview(); -#endif +ContentsChatItemPrivate::~ContentsChatItemPrivate() { + clearWebPreview(); } -#ifdef HAVE_WEBKIT void ContentsChatItemPrivate::loadWebPreview(const QString &url, const QRectF &urlRect) { - if(!controller) - controller = new PreviewController(contentsItem); - controller->loadPage(url, urlRect); -} - -void ContentsChatItemPrivate::clearPreview() { - delete controller; - controller = 0; + if(url != previewUrl) { + previewUrl = url; + // load a new web view and delete the old one (if exists) + if(previewItem) { + contentsItem->scene()->removeItem(previewItem); + delete previewItem; + } + QWebView *view = new QWebView; + view->load(url); + previewItem = new ContentsChatItemPrivate::PreviewItem(view); + contentsItem->scene()->addItem(previewItem); + } + if(urlRect != previewUrlRect) { + previewUrlRect = urlRect; + QPointF sPos = contentsItem->scenePos(); + qreal previewY = sPos.y() + urlRect.y() + urlRect.height(); // bottom of url; + qreal previewX = sPos.x() + urlRect.x(); + if(previewY + previewItem->boundingRect().height() > contentsItem->scene()->sceneRect().bottom()) + previewY = sPos.y() + urlRect.y() - previewItem->boundingRect().height(); + + if(previewX + previewItem->boundingRect().width() > contentsItem->scene()->sceneRect().width()) + previewX = contentsItem->scene()->sceneRect().right() - previewItem->boundingRect().width(); + + previewItem->setPos(previewX, previewY); + } } -ContentsChatItemPrivate::PreviewController::~PreviewController() { +void ContentsChatItemPrivate::clearWebPreview() { if(previewItem) { contentsItem->scene()->removeItem(previewItem); delete previewItem; + previewItem = 0; } -} - -void ContentsChatItemPrivate::PreviewController::loadPage(const QString &newUrl, const QRectF &urlRect) { - if(newUrl.isEmpty() || newUrl == url) - return; - - url = newUrl; - QWebView *view = new QWebView; - connect(view, SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded(bool))); - view->load(url); - previewItem = new ContentsChatItemPrivate::PreviewItem(view); - - QPointF sPos = contentsItem->scenePos(); - qreal previewY = sPos.y() + urlRect.y() + urlRect.height(); // bottom of url; - qreal previewX = sPos.x() + urlRect.x(); - if(previewY + previewItem->boundingRect().height() > contentsItem->scene()->sceneRect().bottom()) - previewY = sPos.y() + urlRect.y() - previewItem->boundingRect().height(); - - if(previewX + previewItem->boundingRect().width() > contentsItem->scene()->sceneRect().width()) - previewX = contentsItem->scene()->sceneRect().right() - previewItem->boundingRect().width(); - - previewItem->setPos(previewX, previewY); - contentsItem->scene()->addItem(previewItem); -} - -void ContentsChatItemPrivate::PreviewController::pageLoaded(bool success) { - Q_UNUSED(success) + previewUrl = QString(); + previewUrlRect = QRectF(); } ContentsChatItemPrivate::PreviewItem::PreviewItem(QWebView *webView) diff --git a/src/qtui/chatitem.h b/src/qtui/chatitem.h index f572bb44..dd926bb4 100644 --- a/src/qtui/chatitem.h +++ b/src/qtui/chatitem.h @@ -166,7 +166,10 @@ private: QList findClickables(); void endHoverMode(); + void showWebPreview(const Clickable &click); + void clearWebPreview(); + // WARNING: setGeometry and setHeight should not be used without either: // a) calling prepareGeometryChange() immediately before setColumns() // b) calling Chatline::setPos() immediately afterwards @@ -202,43 +205,24 @@ struct ContentsChatItemPrivate : ChatItemPrivate { ContentsChatItem::Clickable currentClickable; bool hasDragged; -#ifdef HAVE_WEBKIT - ContentsChatItemPrivate(QTextLayout *l, const QList &c, ContentsChatItem *parent) : ChatItemPrivate(l), contentsItem(parent), clickables(c), hasDragged(false), controller(0) {} -#else +#ifndef HAVE_WEBKIT ContentsChatItemPrivate(QTextLayout *l, const QList &c, ContentsChatItem *parent) : ChatItemPrivate(l), contentsItem(parent), clickables(c), hasDragged(false) {} -#endif +#else + ContentsChatItemPrivate(QTextLayout *l, const QList &c, ContentsChatItem *parent) : ChatItemPrivate(l), contentsItem(parent), clickables(c), hasDragged(false), previewItem(0) {} ~ContentsChatItemPrivate(); -#ifdef HAVE_WEBKIT void loadWebPreview(const QString &url, const QRectF &urlRect); - void clearPreview(); + void clearWebPreview(); private: - class PreviewController; class PreviewItem; - PreviewController *controller; -#endif //#ifdef HAVE_WEBKIT + PreviewItem *previewItem; + QString previewUrl; + QRectF previewUrlRect; +#endif //#ifndef HAVE_WEBKIT }; #ifdef HAVE_WEBKIT -class ContentsChatItemPrivate::PreviewController : public QObject { - Q_OBJECT -public: - PreviewController(ContentsChatItem *contentsItem) : contentsItem(contentsItem), previewItem(0) {} - ~PreviewController(); - - void loadPage(const QString &url, const QRectF &urlRect); - -private slots: - void pageLoaded(bool success); - -private: - ContentsChatItem *contentsItem; - ContentsChatItemPrivate::PreviewItem *previewItem; - - QString url; -}; - class QWebView; class ContentsChatItemPrivate::PreviewItem : public QGraphicsItem { public: -- 2.20.1