common: Make SyncableObject non-copyable
[quassel.git] / src / qtui / webpreviewitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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 <QWebEngineSettings>
25 #    include <QWebEngineView>
26 #elif defined HAVE_WEBKIT
27 #    include <QWebSettings>
28 #    include <QWebView>
29 #endif
30
31 #if defined HAVE_WEBKIT || defined HAVE_WEBENGINE
32
33 #    include <QGraphicsProxyWidget>
34 #    include <QPainter>
35
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
41 {
42 private:
43     QGraphicsProxyWidget* proxyItem;
44
45 public:
46     CustomWebView(QGraphicsProxyWidget* pItem) { proxyItem = pItem; }
47     bool event(QEvent* event) override
48     {
49         if (event->type() == QEvent::UpdateRequest) {
50             proxyItem->update();
51         }
52
53         return QWebEngineView::event(event);
54     }
55 };
56 #    endif
57
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)
62 {
63     qreal frameWidth = 5;
64
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);
72 #    endif
73     webView->load(url);
74     webView->setDisabled(true);
75     webView->resize(1000, 750);
76     proxyItem->setWidget(webView);
77     proxyItem->setAcceptHoverEvents(false);
78
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);
83
84     setZValue(30);
85 }
86
87 void WebPreviewItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
88 {
89     Q_UNUSED(option);
90     Q_UNUSED(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);
96 }
97
98 #endif  //#ifdef HAVE_WEBKIT || HAVE_WEBENGINE