X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fwebpreviewitem.cpp;h=8a24d6f69210a59161d2f45b934fe33a946b25b6;hp=29dfedeca86fad5f73ac6ae2ab37b4acac0ad1f6;hb=e8a39b4c3c92e193ab861a3fea84a261bb6fbd24;hpb=17b8c981d7c47800392b7fd1c043c5ac1bcb19ef diff --git a/src/qtui/webpreviewitem.cpp b/src/qtui/webpreviewitem.cpp index 29dfedec..8a24d6f6 100644 --- a/src/qtui/webpreviewitem.cpp +++ b/src/qtui/webpreviewitem.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,49 +15,86 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include "webpreviewitem.h" -#ifdef HAVE_WEBKIT +#ifdef HAVE_WEBENGINE +#include +#include +#elif defined HAVE_WEBKIT +#include +#include +#endif + +#if defined HAVE_WEBKIT || defined HAVE_WEBENGINE #include #include -#include -WebPreviewItem::WebPreviewItem(const QString &url) - : QGraphicsItem(0), // needs to be a top level item as we otherwise cannot guarantee that it's on top of other chatlines + +#ifdef HAVE_WEBENGINE +// QGraphicsProxyWidget does not stay synced with QWebEngineView, therefore we modify QWebEngineView +// and manually trigger an update of the proxyItem on UpdateRequest Event. See: http://stackoverflow.com/a/30920209 +// and http://lists.qt-project.org/pipermail/development/2016-March/025280.html (At least in Qt5.6) +class CustomWebView : public QWebEngineView { + +private: + QGraphicsProxyWidget *proxyItem; +public: + CustomWebView(QGraphicsProxyWidget *pItem) { + proxyItem = pItem; + } + bool event(QEvent *event) override { + if (event->type() == QEvent::UpdateRequest) + { + proxyItem->update(); + } + + return QWebEngineView::event(event); + } +}; +#endif + +WebPreviewItem::WebPreviewItem(const QUrl &url) + : QGraphicsItem(nullptr), // needs to be a top level item as we otherwise cannot guarantee that it's on top of other chatlines _boundingRect(0, 0, 400, 300) { - qreal frameWidth = 5; + qreal frameWidth = 5; - QWebView *webView = new QWebView; - webView->load(url); - webView->resize(1000, 750); - QGraphicsProxyWidget *proxyItem = new QGraphicsProxyWidget(this); - proxyItem->setWidget(webView); - proxyItem->setAcceptHoverEvents(false); + QGraphicsProxyWidget *proxyItem = new QGraphicsProxyWidget(this); +#ifdef HAVE_WEBENGINE + QWebEngineView *webView = new CustomWebView(proxyItem); + webView->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false); +#elif defined HAVE_WEBKIT + QWebView *webView = new QWebView; + webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, false); +#endif + webView->load(url); + webView->setDisabled(true); + webView->resize(1000, 750); + proxyItem->setWidget(webView); + proxyItem->setAcceptHoverEvents(false); - qreal xScale = (_boundingRect.width() - 2 * frameWidth) / webView->width(); - qreal yScale = (_boundingRect.height() - 2 * frameWidth) / webView->height(); - proxyItem->scale(xScale, yScale); - proxyItem->setPos(frameWidth, frameWidth); + qreal xScale = (_boundingRect.width() - 2 * frameWidth) / webView->width(); + qreal yScale = (_boundingRect.height() - 2 * frameWidth) / webView->height(); + proxyItem->setTransform(QTransform::fromScale(xScale, yScale), true); + proxyItem->setPos(frameWidth, frameWidth); - setZValue(30); + setZValue(30); } -void WebPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - Q_UNUSED(option); Q_UNUSED(widget); - painter->setClipRect(boundingRect()); - painter->setPen(QPen(Qt::black, 5)); - painter->setBrush(Qt::black); - painter->setRenderHints(QPainter::Antialiasing); - painter->drawRoundedRect(boundingRect(), 10, 10); - - painter->setPen(QPen(Qt::green)); - QString text = QString::number(zValue()); - painter->drawText(_boundingRect.center(), text); + +void WebPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); Q_UNUSED(widget); + painter->setClipRect(boundingRect()); + painter->setPen(QPen(Qt::black, 5)); + painter->setBrush(Qt::black); + painter->setRenderHints(QPainter::Antialiasing); + painter->drawRoundedRect(boundingRect(), 10, 10); } -#endif //#ifdef HAVE_WEBKIT + +#endif //#ifdef HAVE_WEBKIT || HAVE_WEBENGINE