ebee5eac05f92d5388ee7a9d081dbedd2379c453
[quassel.git] / src / qtui / webpreviewitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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 WebPreviewItem::WebPreviewItem(const QUrl &url)
37     : QGraphicsItem(0), // needs to be a top level item as we otherwise cannot guarantee that it's on top of other chatlines
38     _boundingRect(0, 0, 400, 300)
39 {
40     qreal frameWidth = 5;
41
42 #ifdef HAVE_WEBENGINE
43     QWebEngineView *webView = new QWebEngineView;
44     webView->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
45 #elif defined HAVE_WEBKIT
46     QWebView *webView = new QWebView;
47     webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
48 #endif
49     webView->load(url);
50     webView->setDisabled(true);
51     webView->resize(1000, 750);
52     QGraphicsProxyWidget *proxyItem = new QGraphicsProxyWidget(this);
53     proxyItem->setWidget(webView);
54     proxyItem->setAcceptHoverEvents(false);
55
56     qreal xScale = (_boundingRect.width() - 2 * frameWidth) / webView->width();
57     qreal yScale = (_boundingRect.height() - 2 * frameWidth) / webView->height();
58     proxyItem->setTransform(QTransform::fromScale(xScale, yScale), true);
59     proxyItem->setPos(frameWidth, frameWidth);
60
61     setZValue(30);
62 }
63
64
65 void WebPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
66 {
67     Q_UNUSED(option); Q_UNUSED(widget);
68     painter->setClipRect(boundingRect());
69     painter->setPen(QPen(Qt::black, 5));
70     painter->setBrush(Qt::black);
71     painter->setRenderHints(QPainter::Antialiasing);
72     painter->drawRoundedRect(boundingRect(), 10, 10);
73 }
74
75
76 #endif //#ifdef HAVE_WEBKIT || HAVE_WEBENGINE