2 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "qdeclarativewebview_p.h"
23 #include <QtCore/QDebug>
24 #include <QtCore/QEvent>
25 #include <QtCore/QFile>
26 #include <QtDeclarative/QDeclarativeContext>
27 #include <QtDeclarative/QDeclarativeEngine>
28 #include <QtDeclarative/qdeclarative.h>
29 #include <QtGui/QApplication>
30 #include <QtGui/QGraphicsSceneMouseEvent>
31 #include <QtGui/QKeyEvent>
32 #include <QtGui/QMouseEvent>
34 #include "qwebelement.h"
35 #include "qwebframe.h"
37 #include "qwebsettings.h"
41 class QDeclarativeWebViewPrivate {
43 QDeclarativeWebViewPrivate(QDeclarativeWebView* qq)
48 , status(QDeclarativeWebView::Null)
49 , pending(PendingNone)
50 , newWindowComponent(0)
56 QDeclarativeWebView* q;
58 QUrl url; // page url might be different if it has not loaded yet
59 GraphicsWebView* view;
61 int preferredwidth, preferredheight;
63 QDeclarativeWebView::Status status;
65 enum { PendingNone, PendingUrl, PendingHtml, PendingContent } pending;
67 QString pendingString;
68 QByteArray pendingData;
69 mutable QDeclarativeWebSettings settings;
70 QDeclarativeComponent* newWindowComponent;
71 QDeclarativeItem* newWindowParent;
73 static void windowObjectsAppend(QDeclarativeListProperty<QObject>* prop, QObject* o)
75 static_cast<QDeclarativeWebViewPrivate*>(prop->data)->windowObjects.append(o);
76 static_cast<QDeclarativeWebViewPrivate*>(prop->data)->updateWindowObjects();
79 void updateWindowObjects();
80 QObjectList windowObjects;
85 GraphicsWebView::GraphicsWebView(QDeclarativeWebView* parent)
86 : QGraphicsWebView(parent)
92 void GraphicsWebView::mousePressEvent(QGraphicsSceneMouseEvent* event)
94 pressPoint = event->pos();
96 pressTimer.start(pressTime, this);
97 parent->setKeepMouseGrab(false);
100 parent->setKeepMouseGrab(true);
102 QGraphicsWebView::mousePressEvent(event);
104 QWebHitTestResult hit = page()->mainFrame()->hitTestContent(pressPoint.toPoint());
105 if (hit.isContentEditable())
106 parent->forceActiveFocus();
110 void GraphicsWebView::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
112 QGraphicsWebView::mouseReleaseEvent(event);
114 parent->setKeepMouseGrab(false);
118 void GraphicsWebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
120 QMouseEvent* me = new QMouseEvent(QEvent::MouseButtonDblClick, (event->pos() / parent->contentsScale()).toPoint(), event->button(), event->buttons(), 0);
121 emit doubleClick(event->pos().x(), event->pos().y());
125 void GraphicsWebView::timerEvent(QTimerEvent* event)
127 if (event->timerId() == pressTimer.timerId()) {
130 parent->setKeepMouseGrab(true);
134 void GraphicsWebView::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
136 if (pressTimer.isActive()) {
137 if ((event->pos() - pressPoint).manhattanLength() > QApplication::startDragDistance())
140 if (parent->keepMouseGrab())
141 QGraphicsWebView::mouseMoveEvent(event);
144 bool GraphicsWebView::sceneEvent(QEvent *event)
146 bool rv = QGraphicsWebView::sceneEvent(event);
147 if (event->type() == QEvent::UngrabMouse) {
149 parent->setKeepMouseGrab(false);
155 \qmlclass WebView QDeclarativeWebView
156 \ingroup qml-view-elements
158 \brief The WebView item allows you to add Web content to a canvas.
161 A WebView renders Web content based on a URL.
163 This type is made available by importing the \c QtWebKit module:
165 \bold{import QtWebKit 1.0}
167 The WebView item includes no scrolling, scaling, toolbars, or other common browser
168 components. These must be implemented around WebView. See the \l{QML Web Browser}
169 example for a demonstration of this.
171 The page to be displayed by the item is specified using the \l url property,
172 and this can be changed to fetch and display a new page. While the page loads,
173 the \l progress property is updated to indicate how much of the page has been
178 If the width and height of the item is not set, they will dynamically adjust
179 to a size appropriate for the content. This width may be large for typical
180 online web pages, typically greater than 800 by 600 pixels.
182 If the \l{Item::}{width} or \l{Item::}{height} is explictly set, the rendered Web site will be
183 clipped, not scaled, to fit into the set dimensions.
185 If the preferredWidth property is set, the width will be this amount or larger,
186 usually laying out the Web content to fit the preferredWidth.
188 The appearance of the content can be controlled to a certain extent by changing
189 the settings.standardFontFamily property and other settings related to fonts.
191 The page can be zoomed by calling the heuristicZoom() method, which performs a
192 series of tests to determine whether zoomed content will be displayed in an
193 appropriate way in the space allocated to the item.
195 \section1 User Interaction and Navigation
197 By default, certain mouse and touch events are delivered to other items in
198 preference to the Web content. For example, when a scrolling view is created
199 by placing a WebView in a Flickable, move events are delivered to the Flickable
200 so that the user can scroll the page. This prevents the user from accidentally
201 selecting text in a Web page instead of scrolling.
203 The pressGrabTime property defines the time the user must touch or press a
204 mouse button over the WebView before the Web content will receive the move
205 events it needs to select text and images.
207 When this item has keyboard focus, all keyboard input will be sent directly to
210 When the navigates by clicking on links, the item records the pages visited
211 in its internal history
213 Because this item is designed to be used as a component in a browser, it
214 exposes \l{Action}{actions} for \l back, \l forward, \l reload and \l stop.
215 These can be triggered to change the current page displayed by the item.
217 \section1 Example Usage
220 \inlineimage webview.png
223 The following example displays a scaled down Web page at a fixed size.
225 \snippet doc/src/snippets/declarative/webview/webview.qml document
229 \sa {declarative/modelviews/webview}{WebView example}, {demos/declarative/webbrowser}{Web Browser demo}
234 \class QDeclarativeWebView
235 \brief The QDeclarativeWebView class allows you to add web content to a QDeclarativeView.
237 A WebView renders web content base on a URL.
241 The item includes no scrolling, scaling,
242 toolbars, etc., those must be implemented around WebView. See the WebBrowser example
243 for a demonstration of this.
245 A QDeclarativeWebView object can be instantiated in Qml using the tag \l WebView.
248 QDeclarativeWebView::QDeclarativeWebView(QDeclarativeItem *parent) : QDeclarativeItem(parent)
253 QDeclarativeWebView::~QDeclarativeWebView()
258 void QDeclarativeWebView::init()
260 d = new QDeclarativeWebViewPrivate(this);
262 if (QWebSettings::iconDatabasePath().isNull() &&
263 QWebSettings::globalSettings()->localStoragePath().isNull() &&
264 QWebSettings::offlineStoragePath().isNull() &&
265 QWebSettings::offlineWebApplicationCachePath().isNull())
266 QWebSettings::enablePersistentStorage();
268 setAcceptedMouseButtons(Qt::LeftButton);
269 setFlag(QGraphicsItem::ItemHasNoContents, true);
270 setFlag(QGraphicsItem::ItemIsFocusScope, true);
273 d->view = new GraphicsWebView(this);
274 d->view->setResizesToContents(true);
276 QWebPage* wp = new QDeclarativeWebPage(this);
278 if (!preferredWidth())
279 setPreferredWidth(d->view->preferredWidth());
280 if (!preferredHeight())
281 setPreferredHeight(d->view->preferredHeight());
282 connect(d->view, SIGNAL(geometryChanged()), this, SLOT(updateDeclarativeWebViewSize()));
283 connect(d->view, SIGNAL(doubleClick(int, int)), this, SIGNAL(doubleClick(int, int)));
284 connect(d->view, SIGNAL(scaleChanged()), this, SIGNAL(contentsScaleChanged()));
287 void QDeclarativeWebView::componentComplete()
289 QDeclarativeItem::componentComplete();
290 page()->setNetworkAccessManager(qmlEngine(this)->networkAccessManager());
292 switch (d->pending) {
293 case QDeclarativeWebViewPrivate::PendingUrl:
294 setUrl(d->pendingUrl);
296 case QDeclarativeWebViewPrivate::PendingHtml:
297 setHtml(d->pendingString, d->pendingUrl);
299 case QDeclarativeWebViewPrivate::PendingContent:
300 setContent(d->pendingData, d->pendingString, d->pendingUrl);
305 d->pending = QDeclarativeWebViewPrivate::PendingNone;
306 d->updateWindowObjects();
309 QDeclarativeWebView::Status QDeclarativeWebView::status() const
316 \qmlproperty real WebView::progress
317 This property holds the progress of loading the current URL, from 0 to 1.
319 If you just want to know when progress gets to 1, use
320 WebView::onLoadFinished() or WebView::onLoadFailed() instead.
322 qreal QDeclarativeWebView::progress() const
327 void QDeclarativeWebView::doLoadStarted()
329 if (!d->url.isEmpty()) {
331 emit statusChanged(d->status);
336 void QDeclarativeWebView::doLoadProgress(int p)
338 if (d->progress == p / 100.0)
340 d->progress = p / 100.0;
341 emit progressChanged();
344 void QDeclarativeWebView::pageUrlChanged()
346 updateContentsSize();
348 if ((d->url.isEmpty() && page()->mainFrame()->url() != QUrl(QLatin1String("about:blank")))
349 || (d->url != page()->mainFrame()->url() && !page()->mainFrame()->url().isEmpty()))
351 d->url = page()->mainFrame()->url();
352 if (d->url == QUrl(QLatin1String("about:blank")))
358 void QDeclarativeWebView::doLoadFinished(bool ok)
361 d->status = d->url.isEmpty() ? Null : Ready;
367 emit statusChanged(d->status);
371 \qmlproperty url WebView::url
372 This property holds the URL to the page displayed in this item. It can be set,
373 but also can change spontaneously (eg. because of network redirection).
375 If the url is empty, the page is blank.
377 The url is always absolute (QML will resolve relative URL strings in the context
378 of the containing QML document).
380 QUrl QDeclarativeWebView::url() const
385 void QDeclarativeWebView::setUrl(const QUrl& url)
390 if (isComponentComplete()) {
392 updateContentsSize();
394 if (seturl.isEmpty())
395 seturl = QUrl(QLatin1String("about:blank"));
397 Q_ASSERT(!seturl.isRelative());
399 page()->mainFrame()->load(seturl);
403 d->pending = d->PendingUrl;
409 \qmlproperty int WebView::preferredWidth
410 This property holds the ideal width for displaying the current URL.
412 int QDeclarativeWebView::preferredWidth() const
414 return d->preferredwidth;
417 void QDeclarativeWebView::setPreferredWidth(int width)
419 if (d->preferredwidth == width)
421 d->preferredwidth = width;
422 updateContentsSize();
423 emit preferredWidthChanged();
427 \qmlproperty int WebView::preferredHeight
428 This property holds the ideal height for displaying the current URL.
429 This only affects the area zoomed by heuristicZoom().
431 int QDeclarativeWebView::preferredHeight() const
433 return d->preferredheight;
436 void QDeclarativeWebView::setPreferredHeight(int height)
438 if (d->preferredheight == height)
440 d->preferredheight = height;
441 updateContentsSize();
442 emit preferredHeightChanged();
446 \qmlmethod bool WebView::evaluateJavaScript(string scriptSource)
448 Evaluates the \a scriptSource JavaScript inside the context of the
449 main web frame, and returns the result of the last executed statement.
451 Note that this JavaScript does \e not have any access to QML objects
452 except as made available as windowObjects.
454 QVariant QDeclarativeWebView::evaluateJavaScript(const QString& scriptSource)
456 return this->page()->mainFrame()->evaluateJavaScript(scriptSource);
459 void QDeclarativeWebView::updateDeclarativeWebViewSize()
461 QSizeF size = d->view->geometry().size() * contentsScale();
462 setImplicitWidth(size.width());
463 setImplicitHeight(size.height());
466 void QDeclarativeWebView::initialLayout()
468 // nothing useful to do at this point
471 void QDeclarativeWebView::updateContentsSize()
474 page()->setPreferredContentsSize(QSize(
475 d->preferredwidth>0 ? d->preferredwidth : width(),
476 d->preferredheight>0 ? d->preferredheight : height()));
480 void QDeclarativeWebView::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
482 QWebPage* webPage = page();
483 if (newGeometry.size() != oldGeometry.size() && webPage) {
484 QSize contentSize = webPage->preferredContentsSize();
486 contentSize.setWidth(width());
488 contentSize.setHeight(height());
489 if (contentSize != webPage->preferredContentsSize())
490 webPage->setPreferredContentsSize(contentSize);
492 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
496 \qmlproperty list<object> WebView::javaScriptWindowObjects
498 A list of QML objects to expose to the web page.
500 Each object will be added as a property of the web frame's window object. The
501 property name is controlled by the value of \c WebView.windowObjectName
504 Exposing QML objects to a web page allows JavaScript executing in the web
505 page itself to communicate with QML, by reading and writing properties and
506 by calling methods of the exposed QML objects.
508 This example shows how to call into a QML method using a window object.
512 javaScriptWindowObjects: QtObject {
513 WebView.windowObjectName: "qml"
516 console.log("This call is in QML!");
520 html: "<script>console.log(\"This is in WebKit!\"); window.qml.qmlCall();</script>"
524 The output of the example will be:
530 If Javascript is not enabled for the page, then this property does nothing.
532 QDeclarativeListProperty<QObject> QDeclarativeWebView::javaScriptWindowObjects()
534 return QDeclarativeListProperty<QObject>(this, d, &QDeclarativeWebViewPrivate::windowObjectsAppend);
537 QDeclarativeWebViewAttached* QDeclarativeWebView::qmlAttachedProperties(QObject* o)
539 return new QDeclarativeWebViewAttached(o);
542 void QDeclarativeWebViewPrivate::updateWindowObjects()
544 if (!q->isComponentCompletePublic() || !q->page())
547 for (int i = 0; i < windowObjects.count(); ++i) {
548 QObject* object = windowObjects.at(i);
549 QDeclarativeWebViewAttached* attached = static_cast<QDeclarativeWebViewAttached *>(qmlAttachedPropertiesObject<QDeclarativeWebView>(object));
550 if (attached && !attached->windowObjectName().isEmpty())
551 q->page()->mainFrame()->addToJavaScriptWindowObject(attached->windowObjectName(), object);
555 bool QDeclarativeWebView::renderingEnabled() const
560 void QDeclarativeWebView::setRenderingEnabled(bool enabled)
562 if (d->rendering == enabled)
564 d->rendering = enabled;
565 emit renderingEnabledChanged();
566 d->view->setTiledBackingStoreFrozen(!enabled);
570 \qmlsignal WebView::onDoubleClick(int clickx, int clicky)
572 The WebView does not pass double-click events to the web engine, but rather
577 \qmlmethod bool WebView::heuristicZoom(int clickX, int clickY, real maxzoom)
581 \i shows a whole item
582 \i includes (\a clickX, \a clickY)
583 \i fits into the preferredWidth and preferredHeight
584 \i zooms by no more than \a maxZoom
585 \i is more than 10% above the current zoom
588 If such a zoom exists, emits zoomTo(zoom,centerX,centerY) and returns true; otherwise,
589 no signal is emitted and returns false.
591 bool QDeclarativeWebView::heuristicZoom(int clickX, int clickY, qreal maxZoom)
593 if (contentsScale() >= maxZoom / scale())
595 qreal ozf = contentsScale();
596 QRect showArea = elementAreaAt(clickX, clickY, d->preferredwidth / maxZoom, d->preferredheight / maxZoom);
597 qreal z = qMin(qreal(d->preferredwidth) / showArea.width(), qreal(d->preferredheight) / showArea.height());
598 if (z > maxZoom / scale())
599 z = maxZoom / scale();
601 QRectF r(showArea.left() * z, showArea.top() * z, showArea.width() * z, showArea.height() * z);
602 emit zoomTo(z, r.x() + r.width() / 2, r.y() + r.height() / 2);
609 \qmlproperty int WebView::pressGrabTime
611 The number of milliseconds the user must press before the WebView
612 starts passing move events through to the Web engine (rather than
613 letting other QML elements such as a Flickable take them).
615 Defaults to 400ms. Set to 0 to always grab and pass move events to
618 int QDeclarativeWebView::pressGrabTime() const
620 return d->view->pressTime;
623 void QDeclarativeWebView::setPressGrabTime(int millis)
625 if (d->view->pressTime == millis)
627 d->view->pressTime = millis;
628 emit pressGrabTimeChanged();
633 \qmlproperty action WebView::back
634 This property holds the action for causing the previous URL in the history to be displayed.
636 QAction* QDeclarativeWebView::backAction() const
638 return page()->action(QWebPage::Back);
642 \qmlproperty action WebView::forward
643 This property holds the action for causing the next URL in the history to be displayed.
645 QAction* QDeclarativeWebView::forwardAction() const
647 return page()->action(QWebPage::Forward);
651 \qmlproperty action WebView::reload
652 This property holds the action for reloading with the current URL
654 QAction* QDeclarativeWebView::reloadAction() const
656 return page()->action(QWebPage::Reload);
660 \qmlproperty action WebView::stop
661 This property holds the action for stopping loading with the current URL
663 QAction* QDeclarativeWebView::stopAction() const
665 return page()->action(QWebPage::Stop);
667 #endif // QT_NO_ACTION
670 \qmlproperty string WebView::title
671 This property holds the title of the web page currently viewed
673 By default, this property contains an empty string.
675 QString QDeclarativeWebView::title() const
677 return page()->mainFrame()->title();
681 \qmlproperty pixmap WebView::icon
682 This property holds the icon associated with the web page currently viewed
684 QPixmap QDeclarativeWebView::icon() const
686 return page()->mainFrame()->icon().pixmap(QSize(256, 256));
690 \qmlproperty string WebView::statusText
692 This property is the current status suggested by the current web page. In a web browser,
693 such status is often shown in some kind of status bar.
695 void QDeclarativeWebView::setStatusText(const QString& text)
697 d->statusText = text;
698 emit statusTextChanged();
701 void QDeclarativeWebView::windowObjectCleared()
703 d->updateWindowObjects();
706 QString QDeclarativeWebView::statusText() const
708 return d->statusText;
711 QWebPage* QDeclarativeWebView::page() const
713 return d->view->page();
716 // The QObject interface to settings().
718 \qmlproperty string WebView::settings.standardFontFamily
719 \qmlproperty string WebView::settings.fixedFontFamily
720 \qmlproperty string WebView::settings.serifFontFamily
721 \qmlproperty string WebView::settings.sansSerifFontFamily
722 \qmlproperty string WebView::settings.cursiveFontFamily
723 \qmlproperty string WebView::settings.fantasyFontFamily
725 \qmlproperty int WebView::settings.minimumFontSize
726 \qmlproperty int WebView::settings.minimumLogicalFontSize
727 \qmlproperty int WebView::settings.defaultFontSize
728 \qmlproperty int WebView::settings.defaultFixedFontSize
730 \qmlproperty bool WebView::settings.autoLoadImages
731 \qmlproperty bool WebView::settings.javascriptEnabled
732 \qmlproperty bool WebView::settings.javaEnabled
733 \qmlproperty bool WebView::settings.pluginsEnabled
734 \qmlproperty bool WebView::settings.privateBrowsingEnabled
735 \qmlproperty bool WebView::settings.javascriptCanOpenWindows
736 \qmlproperty bool WebView::settings.javascriptCanAccessClipboard
737 \qmlproperty bool WebView::settings.developerExtrasEnabled
738 \qmlproperty bool WebView::settings.linksIncludedInFocusChain
739 \qmlproperty bool WebView::settings.zoomTextOnly
740 \qmlproperty bool WebView::settings.printElementBackgrounds
741 \qmlproperty bool WebView::settings.offlineStorageDatabaseEnabled
742 \qmlproperty bool WebView::settings.offlineWebApplicationCacheEnabled
743 \qmlproperty bool WebView::settings.localStorageDatabaseEnabled
744 \qmlproperty bool WebView::settings.localContentCanAccessRemoteUrls
746 These properties give access to the settings controlling the web view.
748 See QWebSettings for details of these properties.
752 settings.pluginsEnabled: true
753 settings.standardFontFamily: "Arial"
758 QDeclarativeWebSettings* QDeclarativeWebView::settingsObject() const
760 d->settings.s = page()->settings();
764 void QDeclarativeWebView::setPage(QWebPage* page)
766 if (d->view->page() == page)
769 d->view->setPage(page);
770 updateContentsSize();
771 page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
772 page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
773 connect(page->mainFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(pageUrlChanged()));
774 connect(page->mainFrame(), SIGNAL(titleChanged(QString)), this, SIGNAL(titleChanged(QString)));
775 connect(page->mainFrame(), SIGNAL(titleChanged(QString)), this, SIGNAL(iconChanged()));
776 connect(page->mainFrame(), SIGNAL(iconChanged()), this, SIGNAL(iconChanged()));
777 connect(page->mainFrame(), SIGNAL(initialLayoutCompleted()), this, SLOT(initialLayout()));
778 connect(page->mainFrame(), SIGNAL(contentsSizeChanged(QSize)), this, SIGNAL(contentsSizeChanged(QSize)));
780 connect(page, SIGNAL(loadStarted()), this, SLOT(doLoadStarted()));
781 connect(page, SIGNAL(loadProgress(int)), this, SLOT(doLoadProgress(int)));
782 connect(page, SIGNAL(loadFinished(bool)), this, SLOT(doLoadFinished(bool)));
783 connect(page, SIGNAL(statusBarMessage(QString)), this, SLOT(setStatusText(QString)));
785 connect(page->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(windowObjectCleared()));
787 page->settings()->setAttribute(QWebSettings::TiledBackingStoreEnabled, true);
792 \qmlsignal WebView::onLoadStarted()
794 This handler is called when the web engine begins loading
795 a page. Later, WebView::onLoadFinished() or WebView::onLoadFailed()
800 \qmlsignal WebView::onLoadFinished()
802 This handler is called when the web engine \e successfully
803 finishes loading a page, including any component content
804 (WebView::onLoadFailed() will be emitted otherwise).
810 \qmlsignal WebView::onLoadFailed()
812 This handler is called when the web engine fails loading
813 a page or any component content
814 (WebView::onLoadFinished() will be emitted on success).
817 void QDeclarativeWebView::load(const QNetworkRequest& request, QNetworkAccessManager::Operation operation, const QByteArray& body)
819 page()->mainFrame()->load(request, operation, body);
822 QString QDeclarativeWebView::html() const
824 return page()->mainFrame()->toHtml();
828 \qmlproperty string WebView::html
829 This property holds HTML text set directly
831 The html property can be set as a string.
835 html: "<p>This is <b>HTML</b>."
839 void QDeclarativeWebView::setHtml(const QString& html, const QUrl& baseUrl)
841 updateContentsSize();
842 if (isComponentComplete())
843 page()->mainFrame()->setHtml(html, baseUrl);
845 d->pending = d->PendingHtml;
846 d->pendingUrl = baseUrl;
847 d->pendingString = html;
852 void QDeclarativeWebView::setContent(const QByteArray& data, const QString& mimeType, const QUrl& baseUrl)
854 updateContentsSize();
856 if (isComponentComplete())
857 page()->mainFrame()->setContent(data, mimeType, qmlContext(this)->resolvedUrl(baseUrl));
859 d->pending = d->PendingContent;
860 d->pendingUrl = baseUrl;
861 d->pendingString = mimeType;
862 d->pendingData = data;
866 QWebHistory* QDeclarativeWebView::history() const
868 return page()->history();
871 QWebSettings* QDeclarativeWebView::settings() const
873 return page()->settings();
876 QDeclarativeWebView* QDeclarativeWebView::createWindow(QWebPage::WebWindowType type)
879 case QWebPage::WebBrowserWindow: {
880 if (!d->newWindowComponent && d->newWindowParent)
881 qWarning("WebView::newWindowComponent not set - WebView::newWindowParent ignored");
882 else if (d->newWindowComponent && !d->newWindowParent)
883 qWarning("WebView::newWindowParent not set - WebView::newWindowComponent ignored");
884 else if (d->newWindowComponent && d->newWindowParent) {
885 QDeclarativeWebView* webview = 0;
886 QDeclarativeContext* windowContext = new QDeclarativeContext(qmlContext(this));
888 QObject* newObject = d->newWindowComponent->create(windowContext);
890 windowContext->setParent(newObject);
891 QDeclarativeItem* item = qobject_cast<QDeclarativeItem *>(newObject);
895 webview = item->findChild<QDeclarativeWebView*>();
899 newObject->setParent(d->newWindowParent);
900 static_cast<QGraphicsObject*>(item)->setParentItem(d->newWindowParent);
904 delete windowContext;
910 case QWebPage::WebModalDialog: {
918 \qmlproperty component WebView::newWindowComponent
920 This property holds the component to use for new windows.
921 The component must have a WebView somewhere in its structure.
923 When the web engine requests a new window, it will be an instance of
926 The parent of the new window is set by newWindowParent. It must be set.
928 QDeclarativeComponent* QDeclarativeWebView::newWindowComponent() const
930 return d->newWindowComponent;
933 void QDeclarativeWebView::setNewWindowComponent(QDeclarativeComponent* newWindow)
935 if (newWindow == d->newWindowComponent)
937 d->newWindowComponent = newWindow;
938 emit newWindowComponentChanged();
943 \qmlproperty item WebView::newWindowParent
945 The parent item for new windows.
947 \sa newWindowComponent
949 QDeclarativeItem* QDeclarativeWebView::newWindowParent() const
951 return d->newWindowParent;
954 void QDeclarativeWebView::setNewWindowParent(QDeclarativeItem* parent)
956 if (parent == d->newWindowParent)
958 if (d->newWindowParent && parent) {
959 QList<QGraphicsItem *> children = d->newWindowParent->childItems();
960 for (int i = 0; i < children.count(); ++i)
961 children.at(i)->setParentItem(parent);
963 d->newWindowParent = parent;
964 emit newWindowParentChanged();
967 QSize QDeclarativeWebView::contentsSize() const
969 return page()->mainFrame()->contentsSize() * contentsScale();
972 qreal QDeclarativeWebView::contentsScale() const
974 return d->view->scale();
977 void QDeclarativeWebView::setContentsScale(qreal scale)
979 if (scale == d->view->scale())
981 d->view->setScale(scale);
982 updateDeclarativeWebViewSize();
983 emit contentsScaleChanged();
986 #if QT_VERSION >= 0x040703
988 \qmlproperty color WebView::backgroundColor
990 This property holds the background color of the view.
993 QColor QDeclarativeWebView::backgroundColor() const
995 return d->view->palette().base().color();
998 void QDeclarativeWebView::setBackgroundColor(const QColor& color)
1000 QPalette palette = d->view->palette();
1001 if (palette.base().color() == color)
1003 palette.setBrush(QPalette::Base, color);
1004 d->view->setPalette(palette);
1005 emit backgroundColorChanged();
1010 Returns the area of the largest element at position (\a x,\a y) that is no larger
1011 than \a maxWidth by \a maxHeight pixels.
1013 May return an area larger in the case when no smaller element is at the position.
1015 QRect QDeclarativeWebView::elementAreaAt(int x, int y, int maxWidth, int maxHeight) const
1017 QWebHitTestResult hit = page()->mainFrame()->hitTestContent(QPoint(x, y));
1018 QRect hitRect = hit.boundingRect();
1019 QWebElement element = hit.enclosingBlockElement();
1023 maxHeight = INT_MAX;
1024 while (!element.parent().isNull() && element.geometry().width() <= maxWidth && element.geometry().height() <= maxHeight) {
1025 hitRect = element.geometry();
1026 element = element.parent();
1033 \class QDeclarativeWebPage
1034 \brief The QDeclarativeWebPage class is a QWebPage that can create QML plugins.
1036 \sa QDeclarativeWebView
1038 QDeclarativeWebPage::QDeclarativeWebPage(QDeclarativeWebView* parent) :
1043 QDeclarativeWebPage::~QDeclarativeWebPage()
1047 QString QDeclarativeWebPage::chooseFile(QWebFrame* originatingFrame, const QString& oldFile)
1049 // Not supported (it's modal)
1050 Q_UNUSED(originatingFrame)
1056 \qmlsignal WebView::onAlert(string message)
1058 The handler is called when the web engine sends a JavaScript alert. The \a message is the text
1059 to be displayed in the alert to the user.
1063 void QDeclarativeWebPage::javaScriptAlert(QWebFrame* originatingFrame, const QString& msg)
1065 Q_UNUSED(originatingFrame)
1066 emit viewItem()->alert(msg);
1069 bool QDeclarativeWebPage::javaScriptConfirm(QWebFrame* originatingFrame, const QString& msg)
1071 // Not supported (it's modal)
1072 Q_UNUSED(originatingFrame)
1077 bool QDeclarativeWebPage::javaScriptPrompt(QWebFrame* originatingFrame, const QString& msg, const QString& defaultValue, QString* result)
1079 // Not supported (it's modal)
1080 Q_UNUSED(originatingFrame)
1082 Q_UNUSED(defaultValue)
1088 QDeclarativeWebView* QDeclarativeWebPage::viewItem()
1090 return static_cast<QDeclarativeWebView*>(parent());
1093 QWebPage* QDeclarativeWebPage::createWindow(WebWindowType type)
1095 QDeclarativeWebView* newView = viewItem()->createWindow(type);
1097 return newView->page();