1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "webpreviewitem.h"
24 # include <QWebEngineSettings>
25 # include <QWebEngineView>
26 #elif defined HAVE_WEBKIT
27 # include <QWebSettings>
31 #if defined HAVE_WEBKIT || defined HAVE_WEBENGINE
33 # include <QGraphicsProxyWidget>
36 # ifdef HAVE_WEBENGINE
37 // QGraphicsProxyWidget does not stay synced with QWebEngineView, therefore we modify QWebEngineView
38 // and manually trigger an update of the proxyItem on UpdateRequest Event. See: http://stackoverflow.com/a/30920209
39 // and http://lists.qt-project.org/pipermail/development/2016-March/025280.html (At least in Qt5.6)
40 class CustomWebView : public QWebEngineView
43 QGraphicsProxyWidget* proxyItem;
46 CustomWebView(QGraphicsProxyWidget* pItem) { proxyItem = pItem; }
47 bool event(QEvent* event) override
49 if (event->type() == QEvent::UpdateRequest) {
53 return QWebEngineView::event(event);
58 WebPreviewItem::WebPreviewItem(const QUrl& url)
59 : QGraphicsItem(nullptr)
60 , // needs to be a top level item as we otherwise cannot guarantee that it's on top of other chatlines
61 _boundingRect(0, 0, 400, 300)
65 QGraphicsProxyWidget* proxyItem = new QGraphicsProxyWidget(this);
66 # ifdef HAVE_WEBENGINE
67 QWebEngineView* webView = new CustomWebView(proxyItem);
68 webView->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
69 # elif defined HAVE_WEBKIT
70 QWebView* webView = new QWebView;
71 webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
74 webView->setDisabled(true);
75 webView->resize(1000, 750);
76 proxyItem->setWidget(webView);
77 proxyItem->setAcceptHoverEvents(false);
79 qreal xScale = (_boundingRect.width() - 2 * frameWidth) / webView->width();
80 qreal yScale = (_boundingRect.height() - 2 * frameWidth) / webView->height();
81 proxyItem->setTransform(QTransform::fromScale(xScale, yScale), true);
82 proxyItem->setPos(frameWidth, frameWidth);
87 void WebPreviewItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
91 painter->setClipRect(boundingRect());
92 painter->setPen(QPen(Qt::black, 5));
93 painter->setBrush(Qt::black);
94 painter->setRenderHints(QPainter::Antialiasing);
95 painter->drawRoundedRect(boundingRect(), 10, 10);
98 #endif //#ifdef HAVE_WEBKIT || HAVE_WEBENGINE