summaryrefslogtreecommitdiff
path: root/browser/browserview.cpp
blob: 7d31ba0640946e8b0dbab6831aa899f6896b166b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
 * Copyright (C) 2014, Pelagicore
 *
 * Author: Jonatan PĂ„lsson <jonatan.palsson@pelagicore.com>
 *
 * This file is part of the GENIVI project Browser Proof-Of-Concept
 * For further information, see http://genivi.org/
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <QDebug>
#include <QWebFrame>
#include <QWebPage>
#include <QCoreApplication>

#include "browserview.h"
#include "../common/browserdefs.h"

BrowserView::BrowserView()
{
    if (!this->scene()) {
        this->setScene(new QGraphicsScene());
    }
    this->scene()->addItem (&m_webview);

    this->load("http://www.bmw.com");

    this->installEventFilter(this);

    setWindowFlags(Qt::FramelessWindowHint);
    m_webview.page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);

    connect(&m_webview, SIGNAL (loadStarted()),             this, SIGNAL (pageLoadStarted ()));
    connect(&m_webview, SIGNAL (statusBarMessage(QString)), this, SIGNAL (onStatusTextChanged(QString)));
    connect(&m_webview, SIGNAL (loadFinished(bool)),        this, SLOT   (loadFinished (bool)));
    connect(&m_webview, SIGNAL (loadProgress(int)),         this, SLOT   (loadProgress(int)));
    connect(&m_webview, SIGNAL (urlChanged(QUrl)),          this, SLOT   (urlChanged(QUrl)));
    connect(&m_webview, SIGNAL (titleChanged(QString)),     this, SLOT   (titleChanged(QString)));
    connect(&m_webview, SIGNAL (linkClicked(QUrl)),         this, SLOT   (linkClicked(QUrl)));

    connect(m_webview.page(), SIGNAL (selectionChanged(void)), this, SIGNAL(onSelectionChanged(void)));

    connect(&m_inputHandler, SIGNAL (onInputText(QString, QString, int, int, int, int, int)), 
        this, SIGNAL (onInputText(QString, QString, int, int, int, int, int)));
    connect(&m_inputHandler, SIGNAL(onScroll(uint,uint)), this, SLOT(scrollPositionChanged(uint,uint)));
}

bool BrowserView::load(const QString &a_Url)
{
    m_webview.load(a_Url);
    return true;
}

void BrowserView::loadProgress(int progress)
{
    m_currentProgress = progress;
    emit pageLoadProgress (progress);
}

void BrowserView::loadFinished(bool ok)
{
    // Inject some JS into the page, and hook it up to an object of ours, this
    // allows us to detect when the user toggles an input field

    m_webview.page()->mainFrame()->addToJavaScriptWindowObject("inputHandler", &m_inputHandler);

    m_webview.page()->mainFrame()->evaluateJavaScript(
    "document.addEventListener('focus', function(e){"
    "    window.inputHandler.setCurrentFocus(e.target);"
    "}, true);");

    m_webview.page()->mainFrame()->evaluateJavaScript(
    "document.addEventListener('scroll', function(){"
    "    window.inputHandler.setScrollPosition(window.pageXOffset, window.pageYOffset);"
    "}, true);");

    emit pageLoadFinished (ok);
}

void BrowserView::scroll (conn::brw::SCROLL_DIRECTION dir, conn::brw::SCROLL_TYPE type)
{
    int stepSize = 50;
    int xMultiplier = 0;
    int yMultiplier = 0;

    switch (dir) {
    case conn::brw::SD_TOP:
        yMultiplier = -1;
        break;
    case conn::brw::SD_BOTTOM:
        yMultiplier = 1;
        break;
    case conn::brw::SD_RIGHT:
        xMultiplier = 1;
        break;
    case conn::brw::SD_LEFT:
        xMultiplier = -1;
        break;
    default:
        qDebug() << "Invalid direction";
        break;
    }

    if (type == conn::brw::ST_PAGE)
        stepSize= this->height();

    if (m_webview.page() && m_webview.page()->mainFrame())
        m_webview.page()->mainFrame()->scroll(stepSize*xMultiplier,
                                              stepSize*yMultiplier);
}

void BrowserView::inputText (QString input)
{
    QInputMethodEvent event;
    event.setCommitString(input);
    QCoreApplication::sendEvent(m_webview.page(), &event);
}

void BrowserView::resizeEvent (QResizeEvent *event) {
    int w = event->size().width();
    int h = event->size().height();
    m_webview.setGeometry (QRect(0,0,w,h));
}

QSize BrowserView::contentSize()
{
    return this->viewport()->size();
}

void BrowserView::urlChanged (QUrl url)
{
    QString strUrl = url.toString();
    if (strUrl.compare("") != 0)
        emit onUrlChanged (strUrl);
}

void BrowserView::titleChanged (QString title)
{
    if (title.compare("") != 0)
        emit onTitleChanged (title);
}

void BrowserView::linkClicked(QUrl url) {
    QString strUrl = url.toString();
    this->load(strUrl);
    emit onLinkClicked(strUrl);
}

bool BrowserView::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type()      == QEvent::Show)
        emit onVisibilityChanged(true);
    else if (event->type() == QEvent::Hide)
        emit onVisibilityChanged(false);

    return QGraphicsView::eventFilter(obj, event);
}

void BrowserView::setZoomFactor(double factor)
{
    m_webview.setZoomFactor(factor);
    emit onZoomFactorChanged (factor);
}

double BrowserView::getZoomFactor()
{
    return m_webview.zoomFactor();
}

void BrowserView::scrollPositionChanged(uint x, uint y)
{
    m_scrollPositionX = x;
    m_scrollPositionY = y;
    emit onScrollPositionChanged(x,y);
}

void BrowserView::setScrollPosition(uint x, uint y)
{
    QString cmd = QString("window.scrollTo(%1,%2);").arg(QString::number(x), QString::number(y));
    m_webview.page()->mainFrame()->evaluateJavaScript(cmd); 
    m_scrollPositionX = x;
    m_scrollPositionY = y;
}

void BrowserView::getScrollPosition(uint &x, uint &y)
{
    x = m_scrollPositionX;
    y = m_scrollPositionY;
}