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