]> rtime.felk.cvut.cz Git - coffee/qtwebbrowser.git/blob - src/browserwindow.cpp
Factor out utils.h to engine.h and cpp
[coffee/qtwebbrowser.git] / src / browserwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtBrowser project.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 2 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.GPLv2 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU General Public License version 2 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 3.0 as published by the Free Software
28 ** Foundation and appearing in the file LICENSE.GPL included in the
29 ** packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 3.0 requirements will be
31 ** met: http://www.gnu.org/copyleft/gpl.html.
32 **
33 **
34 ** $QT_END_LICENSE$
35 **
36 ****************************************************************************/
37
38 #include "browserwindow.h"
39 #include "engine.h"
40
41 #include <QList>
42 #include <QQmlContext>
43 #include <QQmlEngine>
44 #include <QQuickItem>
45 #include <QRectF>
46 #include <QUrl>
47 #include <QVariant>
48
49 void BrowserWindow::ensureProfileInstance()
50 {
51     if (m_lazyProfileInstance)
52         return;
53     QQmlComponent *component =  new QQmlComponent(engine(), this);
54
55     component->setData(
56                 QByteArrayLiteral("import QtQuick 2.0\n"
57                                   "import QtWebEngine 1.1\n"
58                                   "WebEngineProfile {\n"
59                                   "  storageName: \"YABProfile\"\n"
60                                   "}")
61                 , QUrl());
62     m_lazyProfileInstance = component->create(engine()->rootContext());
63     Q_ASSERT(m_lazyProfileInstance);
64     QQmlEngine::setObjectOwnership(m_lazyProfileInstance, QQmlEngine::JavaScriptOwnership);
65 }
66
67 QObject *BrowserWindow::defaultProfile()
68 {
69     ensureProfileInstance();
70     return m_lazyProfileInstance;
71 }
72
73 BrowserWindow::BrowserWindow(QWindow *)
74     : m_lazyProfileInstance(0)
75 {
76     setTitle("Yet Another Browser");
77     setFlags(Qt::Window | Qt::WindowTitleHint);
78     setResizeMode(QQuickView::SizeRootObjectToView);
79     setColor(Qt::black);
80
81     engine()->rootContext()->setContextProperty("engine", new Engine(this));
82     setSource(QUrl("qrc:///qml/BrowserWindow.qml"));
83 }
84
85 BrowserWindow::~BrowserWindow()
86 {
87 }
88
89 void BrowserWindow::updateVisualMockTouchPoints(const QList<QTouchEvent::TouchPoint>& touchPoints)
90 {
91     if (touchPoints.isEmpty()) {
92         // Hide all touch indicator items.
93         foreach (QQuickItem* item, m_activeMockComponents.values())
94             item->setProperty("pressed", false);
95
96         return;
97     }
98
99     foreach (const QTouchEvent::TouchPoint& touchPoint, touchPoints) {
100         QQuickItem* mockTouchPointItem = m_activeMockComponents.value(touchPoint.id());
101
102         if (!mockTouchPointItem) {
103             QQmlComponent touchMockPointComponent(engine(), QUrl("qrc:///qml/MockTouchPoint.qml"));
104             mockTouchPointItem = qobject_cast<QQuickItem*>(touchMockPointComponent.create());
105             Q_ASSERT(mockTouchPointItem);
106             m_activeMockComponents.insert(touchPoint.id(), mockTouchPointItem);
107             mockTouchPointItem->setProperty("pointId", QVariant(touchPoint.id()));
108             mockTouchPointItem->setParent(rootObject());
109             mockTouchPointItem->setParentItem(rootObject());
110         }
111
112         QRectF touchRect = touchPoint.rect();
113         mockTouchPointItem->setX(touchRect.center().x());
114         mockTouchPointItem->setY(touchRect.center().y());
115         mockTouchPointItem->setWidth(touchRect.width());
116         mockTouchPointItem->setHeight(touchRect.height());
117         mockTouchPointItem->setProperty("pressed", QVariant(touchPoint.state() != Qt::TouchPointReleased));
118     }
119 }