ginebra2/PageItem.cpp
author hgs
Fri, 06 Aug 2010 17:23:08 -0400
changeset 9 b39122337a00
parent 3 0954f5dd2cd0
permissions -rw-r--r--
201031
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
*
hgs
parents:
diff changeset
     5
* This program is free software: you can redistribute it and/or modify
hgs
parents:
diff changeset
     6
* it under the terms of the GNU Lesser General Public License as published by
hgs
parents:
diff changeset
     7
* the Free Software Foundation, version 2.1 of the License.
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* This program is distributed in the hope that it will be useful,
hgs
parents:
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
hgs
parents:
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
hgs
parents:
diff changeset
    12
* GNU Lesser General Public License for more details.
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* You should have received a copy of the GNU Lesser General Public License
hgs
parents:
diff changeset
    15
* along with this program.  If not,
hgs
parents:
diff changeset
    16
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
hgs
parents:
diff changeset
    17
*
hgs
parents:
diff changeset
    18
* Description:
hgs
parents:
diff changeset
    19
*
hgs
parents:
diff changeset
    20
*/
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
#include "PageItem.h"
hgs
parents:
diff changeset
    23
#include "GWebTouchNavigation.h"
hgs
parents:
diff changeset
    24
#include "Utilities.h"
hgs
parents:
diff changeset
    25
#include "GSuperWebPage.h"
hgs
parents:
diff changeset
    26
#include "ChromeWidget.h"
hgs
parents:
diff changeset
    27
hgs
parents:
diff changeset
    28
#include <QGraphicsWebView>
hgs
parents:
diff changeset
    29
#include <QTimer>
hgs
parents:
diff changeset
    30
hgs
parents:
diff changeset
    31
namespace GVA {
hgs
parents:
diff changeset
    32
hgs
parents:
diff changeset
    33
/// Helper web view class that disables the default context menu.
hgs
parents:
diff changeset
    34
class PageItemWebView : public QGraphicsWebView {
hgs
parents:
diff changeset
    35
public:
hgs
parents:
diff changeset
    36
    PageItemWebView(QGraphicsItem * parent = 0)
hgs
parents:
diff changeset
    37
        : QGraphicsWebView(parent)
hgs
parents:
diff changeset
    38
    {}
hgs
parents:
diff changeset
    39
    void contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
hgs
parents:
diff changeset
    40
        // Do nothing.
hgs
parents:
diff changeset
    41
        event->accept();
hgs
parents:
diff changeset
    42
    }
hgs
parents:
diff changeset
    43
};
hgs
parents:
diff changeset
    44
hgs
parents:
diff changeset
    45
// ---------------------------------
hgs
parents:
diff changeset
    46
hgs
parents:
diff changeset
    47
PageItem::PageItem(ChromeSnippet * snippet, ChromeWidget* chrome)
hgs
parents:
diff changeset
    48
  : NativeChromeItem(snippet, chrome->layout()),
hgs
parents:
diff changeset
    49
    m_webView(0),
hgs
parents:
diff changeset
    50
    m_touchNavigation(0),
hgs
parents:
diff changeset
    51
    m_superPage(0),
hgs
parents:
diff changeset
    52
    m_page(0),
hgs
parents:
diff changeset
    53
    m_chrome(chrome),
hgs
parents:
diff changeset
    54
    m_cleanupTimer(0),
hgs
parents:
diff changeset
    55
    m_isInstantiated(false)
hgs
parents:
diff changeset
    56
{
hgs
parents:
diff changeset
    57
    setVisible(false);
hgs
parents:
diff changeset
    58
}
hgs
parents:
diff changeset
    59
hgs
parents:
diff changeset
    60
PageItem::~PageItem() {
hgs
parents:
diff changeset
    61
    if (!m_isInstantiated) return;
hgs
parents:
diff changeset
    62
    cleanUp();
hgs
parents:
diff changeset
    63
}
hgs
parents:
diff changeset
    64
hgs
parents:
diff changeset
    65
void PageItem::instantiate() {
hgs
parents:
diff changeset
    66
    if (m_isInstantiated) {
hgs
parents:
diff changeset
    67
        m_cleanupTimer->stop(); // make sure cleanup not scheduled
hgs
parents:
diff changeset
    68
        return;
hgs
parents:
diff changeset
    69
    }
hgs
parents:
diff changeset
    70
hgs
parents:
diff changeset
    71
    // Create the web page.
hgs
parents:
diff changeset
    72
    m_page = new WebPageWrapper(this, "Page snippet javascript error");
hgs
parents:
diff changeset
    73
    m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
hgs
parents:
diff changeset
    74
    m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
hgs
parents:
diff changeset
    75
hgs
parents:
diff changeset
    76
    // Change page item base brush to transparent -
hgs
parents:
diff changeset
    77
    // this allows HTML to set the background color.
hgs
parents:
diff changeset
    78
    QPalette pagePalette = palette();
hgs
parents:
diff changeset
    79
    pagePalette.setBrush(QPalette::Base, Qt::transparent);
hgs
parents:
diff changeset
    80
    m_page->setPalette(pagePalette);
hgs
parents:
diff changeset
    81
hgs
parents:
diff changeset
    82
    // Use a super page here because they know how to inject our javascript objects
hgs
parents:
diff changeset
    83
    // into their javascript engines.
hgs
parents:
diff changeset
    84
    m_superPage = new GSuperWebPage(m_page, m_chrome);
hgs
parents:
diff changeset
    85
hgs
parents:
diff changeset
    86
    // Create the web view.
hgs
parents:
diff changeset
    87
    m_webView = new PageItemWebView(this);
hgs
parents:
diff changeset
    88
    m_webView->setPage(m_page);
hgs
parents:
diff changeset
    89
hgs
parents:
diff changeset
    90
    m_touchNavigation = new GWebTouchNavigation(m_page, m_webView);
hgs
parents:
diff changeset
    91
hgs
parents:
diff changeset
    92
    // create and setup timer to delay cleanup
hgs
parents:
diff changeset
    93
    m_cleanupTimer = new QTimer();
hgs
parents:
diff changeset
    94
    m_cleanupTimer->setSingleShot(true);
hgs
parents:
diff changeset
    95
    m_cleanupTimer->setInterval(0);
hgs
parents:
diff changeset
    96
    connect(m_cleanupTimer, SIGNAL(timeout()), this, SLOT(cleanUp()));
hgs
parents:
diff changeset
    97
hgs
parents:
diff changeset
    98
    if (!m_url.isEmpty())
hgs
parents:
diff changeset
    99
        m_webView->setUrl(m_url);
hgs
parents:
diff changeset
   100
    else if (!m_html.isEmpty()) {
hgs
parents:
diff changeset
   101
        m_webView->setHtml(m_html);
hgs
parents:
diff changeset
   102
        m_html = QString::null;
hgs
parents:
diff changeset
   103
    }
hgs
parents:
diff changeset
   104
hgs
parents:
diff changeset
   105
    m_isInstantiated = true;
hgs
parents:
diff changeset
   106
}
hgs
parents:
diff changeset
   107
hgs
parents:
diff changeset
   108
void PageItem::cleanUpOnTimer() {
hgs
parents:
diff changeset
   109
    if (!m_isInstantiated) return;
hgs
parents:
diff changeset
   110
hgs
parents:
diff changeset
   111
    m_cleanupTimer->start();
hgs
parents:
diff changeset
   112
}
hgs
parents:
diff changeset
   113
hgs
parents:
diff changeset
   114
void PageItem::cleanUp() {   // slot
hgs
parents:
diff changeset
   115
    delete m_cleanupTimer;
hgs
parents:
diff changeset
   116
    m_cleanupTimer = 0;
hgs
parents:
diff changeset
   117
    delete m_touchNavigation;
hgs
parents:
diff changeset
   118
    m_touchNavigation = 0;
hgs
parents:
diff changeset
   119
    delete m_webView;
hgs
parents:
diff changeset
   120
    m_webView = 0;
hgs
parents:
diff changeset
   121
    delete m_superPage;
hgs
parents:
diff changeset
   122
    m_superPage = 0;
hgs
parents:
diff changeset
   123
    m_isInstantiated = false;
hgs
parents:
diff changeset
   124
}
hgs
parents:
diff changeset
   125
hgs
parents:
diff changeset
   126
void PageItem::resizeEvent(::QGraphicsSceneResizeEvent *event) {
hgs
parents:
diff changeset
   127
    setWebViewSize(event->newSize());
hgs
parents:
diff changeset
   128
}
hgs
parents:
diff changeset
   129
hgs
parents:
diff changeset
   130
void PageItem::setSize(const QSizeF &size) {
hgs
parents:
diff changeset
   131
    resize(size);
hgs
parents:
diff changeset
   132
    setWebViewSize(size);
hgs
parents:
diff changeset
   133
}
hgs
parents:
diff changeset
   134
hgs
parents:
diff changeset
   135
void PageItem::setWebViewSize(const QSizeF &size) {
hgs
parents:
diff changeset
   136
    if (m_webView) {
hgs
parents:
diff changeset
   137
        m_webView->resize(size);
hgs
parents:
diff changeset
   138
        m_webView->page()->setViewportSize(size.toSize());
hgs
parents:
diff changeset
   139
    }
hgs
parents:
diff changeset
   140
}
hgs
parents:
diff changeset
   141
hgs
parents:
diff changeset
   142
void PageItem::showEvent(QShowEvent *event) {
hgs
parents:
diff changeset
   143
    instantiate();
hgs
parents:
diff changeset
   144
    NativeChromeItem::showEvent(event);
hgs
parents:
diff changeset
   145
}
hgs
parents:
diff changeset
   146
hgs
parents:
diff changeset
   147
void PageItem::setUrl(const QString &value) {
hgs
parents:
diff changeset
   148
    m_url = value;
hgs
parents:
diff changeset
   149
    if (m_webView)
hgs
parents:
diff changeset
   150
        m_webView->setUrl(value);
hgs
parents:
diff changeset
   151
}
hgs
parents:
diff changeset
   152
hgs
parents:
diff changeset
   153
QString PageItem::url() const {
hgs
parents:
diff changeset
   154
    if (m_webView) {
hgs
parents:
diff changeset
   155
        return m_webView->url().toString();
hgs
parents:
diff changeset
   156
    }
hgs
parents:
diff changeset
   157
    return QString();
hgs
parents:
diff changeset
   158
}
hgs
parents:
diff changeset
   159
hgs
parents:
diff changeset
   160
void PageItem::setHtml(const QString &value) {
hgs
parents:
diff changeset
   161
    if (m_webView)
hgs
parents:
diff changeset
   162
        m_webView->setHtml(value);
hgs
parents:
diff changeset
   163
    else
hgs
parents:
diff changeset
   164
        m_html = value;
hgs
parents:
diff changeset
   165
}
hgs
parents:
diff changeset
   166
hgs
parents:
diff changeset
   167
QString PageItem::html() const {
hgs
parents:
diff changeset
   168
    if (m_webView) {
hgs
parents:
diff changeset
   169
        QWebFrame *frame = m_webView->page()->mainFrame();
hgs
parents:
diff changeset
   170
        if (frame)
hgs
parents:
diff changeset
   171
            return frame->toHtml();
hgs
parents:
diff changeset
   172
    }
hgs
parents:
diff changeset
   173
    return QString();
hgs
parents:
diff changeset
   174
}
hgs
parents:
diff changeset
   175
hgs
parents:
diff changeset
   176
//void PageItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* opt, QWidget* widget) {
hgs
parents:
diff changeset
   177
//    Q_UNUSED(opt)
hgs
parents:
diff changeset
   178
//    Q_UNUSED(widget)
hgs
parents:
diff changeset
   179
//
hgs
parents:
diff changeset
   180
//    // Paint the background.
hgs
parents:
diff changeset
   181
//    painter->fillRect(QRectF(0,0, geometry().width(), geometry().height()), Qt::blue);
hgs
parents:
diff changeset
   182
//}
hgs
parents:
diff changeset
   183
hgs
parents:
diff changeset
   184
QVariant PageItem::evaluateJavaScript(const QString &expression) {
hgs
parents:
diff changeset
   185
    if (m_webView) {
hgs
parents:
diff changeset
   186
        QWebFrame *frame = m_webView->page()->mainFrame();
hgs
parents:
diff changeset
   187
        if (frame)
hgs
parents:
diff changeset
   188
            return frame->evaluateJavaScript(expression);
hgs
parents:
diff changeset
   189
    }
hgs
parents:
diff changeset
   190
    return QVariant();
hgs
parents:
diff changeset
   191
}
hgs
parents:
diff changeset
   192
hgs
parents:
diff changeset
   193
} // GVA namespace