1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 <QWebEngineView>
25 #include <QWebEngineSettings>
26 #elif defined HAVE_WEBKIT
28 #include <QWebSettings>
31 #if defined HAVE_WEBKIT || defined HAVE_WEBENGINE
33 #include <QGraphicsProxyWidget>
38 // QGraphicsProxyWidget does not stay synced with QWebEngineView, therefore we modify QWebEngineView
39 // and manually trigger an update of the proxyItem on UpdateRequest Event. See: http://stackoverflow.com/a/30920209
40 // and http://lists.qt-project.org/pipermail/development/2016-March/025280.html (At least in Qt5.6)
41 class CustomWebView : public QWebEngineView {
44 QGraphicsProxyWidget *proxyItem;
46 CustomWebView(QGraphicsProxyWidget *pItem) {
49 bool event(QEvent *event) {
50 if (event->type() == QEvent::UpdateRequest)
55 return QWebEngineView::event(event);
60 WebPreviewItem::WebPreviewItem(const QUrl &url)
61 : QGraphicsItem(0), // needs to be a top level item as we otherwise cannot guarantee that it's on top of other chatlines
62 _boundingRect(0, 0, 400, 300)
66 QGraphicsProxyWidget *proxyItem = new QGraphicsProxyWidget(this);
68 QWebEngineView *webView = new CustomWebView(proxyItem);
69 webView->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
70 #elif defined HAVE_WEBKIT
71 QWebView *webView = new QWebView;
72 webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
75 webView->setDisabled(true);
76 webView->resize(1000, 750);
77 proxyItem->setWidget(webView);
78 proxyItem->setAcceptHoverEvents(false);
80 qreal xScale = (_boundingRect.width() - 2 * frameWidth) / webView->width();
81 qreal yScale = (_boundingRect.height() - 2 * frameWidth) / webView->height();
82 proxyItem->setTransform(QTransform::fromScale(xScale, yScale), true);
83 proxyItem->setPos(frameWidth, frameWidth);
89 void WebPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
91 Q_UNUSED(option); Q_UNUSED(widget);
92 painter->setClipRect(boundingRect());
93 painter->setPen(QPen(Qt::black, 5));
94 painter->setBrush(Qt::black);
95 painter->setRenderHints(QPainter::Antialiasing);
96 painter->drawRoundedRect(boundingRect(), 10, 10);
100 #endif //#ifdef HAVE_WEBKIT || HAVE_WEBENGINE