modernize: Use nullptr
[quassel.git] / src / qtui / webpreviewitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "webpreviewitem.h"
22
23 #ifdef HAVE_WEBENGINE
24 #include <QWebEngineView>
25 #include <QWebEngineSettings>
26 #elif defined HAVE_WEBKIT
27 #include <QWebView>
28 #include <QWebSettings>
29 #endif
30
31 #if defined HAVE_WEBKIT || defined HAVE_WEBENGINE
32
33 #include <QGraphicsProxyWidget>
34 #include <QPainter>
35
36
37 #ifdef HAVE_WEBENGINE
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 {
42
43 private:
44     QGraphicsProxyWidget *proxyItem;
45 public:
46     CustomWebView(QGraphicsProxyWidget *pItem) {
47         proxyItem = pItem;
48     }
49     bool event(QEvent *event) {
50         if (event->type() == QEvent::UpdateRequest)
51         {
52             proxyItem->update();
53         }
54
55         return QWebEngineView::event(event);
56     }
57 };
58 #endif
59
60 WebPreviewItem::WebPreviewItem(const QUrl &url)
61     : QGraphicsItem(nullptr), // 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)
63 {
64     qreal frameWidth = 5;
65
66     QGraphicsProxyWidget *proxyItem = new QGraphicsProxyWidget(this);
67 #ifdef HAVE_WEBENGINE
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);
73 #endif
74     webView->load(url);
75     webView->setDisabled(true);
76     webView->resize(1000, 750);
77     proxyItem->setWidget(webView);
78     proxyItem->setAcceptHoverEvents(false);
79
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);
84
85     setZValue(30);
86 }
87
88
89 void WebPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
90 {
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);
97 }
98
99
100 #endif //#ifdef HAVE_WEBKIT || HAVE_WEBENGINE