summaryrefslogtreecommitdiff
path: root/browser/browserview.cpp
blob: d06ba0e8e35bcf54b3f24390890a64ce9ab29e4b (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
/**
 * 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 <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");

    setWindowFlags(Qt::FramelessWindowHint);

    connect(&m_webview, SIGNAL (loadStarted()),      this, SIGNAL (pageLoadStarted ()));
    connect(&m_webview, SIGNAL (loadFinished(bool)), this, SLOT   (loadFinished (bool)));
    connect(&m_webview, SIGNAL (loadProgress(int)),  this, SLOT   (loadProgress(int)));
    connect(&m_inputHandler, SIGNAL (onInputText(QString, QString, int, int, int, int, int)), 
        this, SIGNAL (onInputText(QString, QString, int, int, int, int, int)));
}

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);");

    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();
}