diff options
author | Simon Hausmann <simon.hausmann@nokia.com> | 2012-01-06 14:44:00 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2012-01-06 14:44:00 +0100 |
commit | 40736c5763bf61337c8c14e16d8587db021a87d4 (patch) | |
tree | b17a9c00042ad89cb1308e2484491799aa14e9f8 /Source/WebKit/qt/docs/webkitsnippets | |
download | qtwebkit-40736c5763bf61337c8c14e16d8587db021a87d4.tar.gz |
Imported WebKit commit 2ea9d364d0f6efa8fa64acf19f451504c59be0e4 (http://svn.webkit.org/repository/webkit/trunk@104285)
Diffstat (limited to 'Source/WebKit/qt/docs/webkitsnippets')
11 files changed, 545 insertions, 0 deletions
diff --git a/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_bridge_snippets.cpp b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_bridge_snippets.cpp new file mode 100644 index 000000000..b18456b0f --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_bridge_snippets.cpp @@ -0,0 +1,178 @@ + +void wrapInFunction() +{ + + //! [0] + // ... + QWebFrame *frame = myWebPage->mainFrame(); + frame->addToJavaScriptWindowObject("someNameForMyObject", myObject); + // ... + //! [0] +#if 0 + //! [1] + { + width: ..., + height: ..., + toDataURL: function() { ... }, + assignToHTMLImageElement: function(element) { ... } + toImageData: function() { ... } + } + //! [1] +#endif + //! [2] + class MyObject : QObject { + Q_OBJECT + Q_PROPERTY(QPixmap myPixmap READ getPixmap) + + public: + QPixmap getPixmap() const; + }; + + /* ... */ + + MyObject myObject; + myWebPage.mainFrame()->addToJavaScriptWindowObject("myObject", &myObject); + + //! [2] +#if 0 + //! [3] + <html> + <head> + <script> + function loadImage() + { + myObject.myPixmap.assignToHTMLImageElement(document.getElementById("imageElement")); + var context = document.getElementById("canvasElement").getContext("2d"); + context.putImageData(myObject.myPixmap.toImageData()); + } + </script> + </head> + <body onload="loadImage()"> + <img id="imageElement" width="300" height="200" /> + <canvas id="canvasElement" width="300" height="200" /> + </body> + </html> +//! [3] +#endif +//! [4] +class MyObject : QObject { + Q_OBJECT + + public slots: + void doSomethingWithWebElement(const QWebElement&); + }; + + /* ... */ + + MyObject myObject; + myWebPage.mainFrame()->addToJavaScriptWindowObject("myObject", &myObject); + + //! [4] +#if 0 + //! [5] + <html> + <head> + <script> + function runExample() { + myObject.doSomethingWithWebElement(document.getElementById("someElement")); + } + </script> + </head> + <body onload="runExample()"> + <span id="someElement">Text</span> + </body> + </html> + //! [5] + //! [6] + connect(function); + //! [6] + //! [7] + function myInterestingScriptFunction() { ... } + ... + myQObject.somethingChanged.connect(myInterestingScriptFunction); + //! [7] + //! [8] + myQObject.somethingChanged.connect(myOtherQObject.doSomething); + //! [8] + //! [9] + myQObject.somethingChanged.disconnect(myInterestingFunction); + myQObject.somethingChanged.disconnect(myOtherQObject.doSomething); + //! [9] + //! [10] + myQObject.somethingChanged.connect(thisObject, function) + //! [10] + //! [11] + var form = { x: 123 }; + var onClicked = function() { print(this.x); }; + myButton.clicked.connect(form, onClicked); + //! [11] + //! [12] + myQObject.somethingChanged.disconnect(thisObject, function); + //! [12] + //! [13] + connect(function); + //! [13] + //! [14] + myQObject.somethingChanged.connect(thisObject, "functionName") + //! [14] + //! [15] + var obj = { x: 123, fun: function() { print(this.x); } }; + myQObject.somethingChanged.connect(obj, "fun"); + //! [15] + //! [16] + connect(function); + //! [16] + //! [17] + myQObject.somethingChanged.disconnect(thisObject, "functionName"); + //! [17] + //! [18] + try { + myQObject.somethingChanged.connect(myQObject, "slotThatDoesntExist"); + } catch (e) { + print(e); + } + //! [18] + //! [19] + myQObject.somethingChanged("hello"); + //! [19] + //! [20] + myQObject.myOverloadedSlot(10); // will call the int overload + myQObject.myOverloadedSlot("10"); // will call the QString overload + //! [20] + //! [21] + myQObject['myOverloadedSlot(int)']("10"); // call int overload; the argument is converted to an int + myQObject['myOverloadedSlot(QString)'](10); // call QString overload; the argument is converted to a string + //! [21] + //! [22] + class MyObject : public QObject + { + Q_OBJECT + + public: + Q_INVOKABLE void thisMethodIsInvokableInJavaScript(); + void thisMethodIsNotInvokableInJavaScript(); + + ... + }; + //! [22] + //! [23] + Q_PROPERTY(bool enabled READ enabled WRITE setEnabled) + //! [23] + //! [24] + myQObject.enabled = true; + + ... + + myQObject.enabled = !myQObject.enabled; + //! [24] + //! [25] + myDialog.okButton + //! [25] + //! [26] + myDialog.okButton + myDialog.okButton.objectName = "cancelButton"; + // from now on, myDialog.cancelButton references the button + //! [26] +#endif +} + diff --git a/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_build_snippet.qdoc b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_build_snippet.qdoc new file mode 100644 index 000000000..d4fc2bd85 --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_build_snippet.qdoc @@ -0,0 +1,8 @@ +//! [0] +QT += webkit +//! [0] + + +//! [1] +#include <QtWebKit> +//! [1] diff --git a/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_goes_mobile_snippets.cpp b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_goes_mobile_snippets.cpp new file mode 100644 index 000000000..8126fbff6 --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_goes_mobile_snippets.cpp @@ -0,0 +1,61 @@ +#if 0 +// ! [0] +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + const int width = 640; + const int height = 480; + + QGraphicsScene scene; + + QGraphicsView view(&scene); + view.setFrameShape(QFrame::NoFrame); + view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + + QGraphicsWebView webview; + webview.resize(width, height); + webview.load(QUrl("http://doc.qt.nokia.com/")); + + scene.addItem(&webview); + + view.resize(width, height); + view.show(); + + return app.exec(); +} +// ! [0] + + +// ! [1] +webview.setResizesToContents(true); +// ! [1] + +// ! [2] +class MobileWebView : public QGraphicsWidget { + Q_OBJECT +public: + MobileWebView(QGraphicsItem *parent = 0); + ~MobileWebView(); + + bool mousePress(const QPoint &value); + void mouseMove(const QPoint &value); + void mouseRelease(const QPoint &value); + +private: + QGraphicsWebView* webView; +}; +// ! [2] + +// ! [3] +webview.page()->setPreferredContentsSize(QSize(desiredWidth, desiredHeight)); +// ! [3] + +// ! [4] +QWebSettings::globalSettings()->setAttribute(QWebSettings::TiledBackingStoreEnabled, true); +// ! [4] + +// ! [5] +QWebSettings::globalSettings()->setAttribute(QWebSettings::FrameFlatteningEnable, true); +// ! [5] +#endif diff --git a/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_qwebinspector_snippet.cpp b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_qwebinspector_snippet.cpp new file mode 100644 index 000000000..07f1d452a --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_qwebinspector_snippet.cpp @@ -0,0 +1,15 @@ + +void wrapInFunction() +{ + +//! [0] + // ... + QWebPage *page = new QWebPage; + // ... + + QWebInspector *inspector = new QWebInspector; + inspector->setPage(page); +//! [0] + +} + diff --git a/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_qwebview_snippet.cpp b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_qwebview_snippet.cpp new file mode 100644 index 000000000..f04cd2915 --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_qwebview_snippet.cpp @@ -0,0 +1,35 @@ + +void wrapInFunction() +{ + +//! [0] + view->page()->history(); +//! [0] + + +//! [1] + view->page()->settings(); +//! [1] + + +//! [2] + view->triggerAction(QWebPage::Copy); +//! [2] + + +//! [3] + view->page()->triggerPageAction(QWebPage::Stop); +//! [3] + + +//! [4] + view->page()->triggerPageAction(QWebPage::GoBack); +//! [4] + + +//! [5] + view->page()->triggerPageAction(QWebPage::GoForward); +//! [5] + +} + diff --git a/Source/WebKit/qt/docs/webkitsnippets/simple/main.cpp b/Source/WebKit/qt/docs/webkitsnippets/simple/main.cpp new file mode 100644 index 000000000..408630eb1 --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/simple/main.cpp @@ -0,0 +1,34 @@ +/* + Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include <QApplication> +#include <QUrl> +#include <QWebView> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QWidget *parent = 0; +//! [Using QWebView] + QWebView *view = new QWebView(parent); + view->load(QUrl("http://qt.nokia.com/")); + view->show(); +//! [Using QWebView] + return app.exec(); +} diff --git a/Source/WebKit/qt/docs/webkitsnippets/simple/simple.pro b/Source/WebKit/qt/docs/webkitsnippets/simple/simple.pro new file mode 100644 index 000000000..61cd3bfcd --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/simple/simple.pro @@ -0,0 +1,2 @@ +QT += webkit +SOURCES = main.cpp diff --git a/Source/WebKit/qt/docs/webkitsnippets/webelement/main.cpp b/Source/WebKit/qt/docs/webkitsnippets/webelement/main.cpp new file mode 100644 index 000000000..b1781a6f4 --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/webelement/main.cpp @@ -0,0 +1,125 @@ +/* + Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include <QApplication> +#include <QUrl> +#include <qwebview.h> +#include <qwebframe.h> +#include <qwebelement.h> + +static QWebFrame *frame; + +static void traverse() +{ +//! [Traversing with QWebElement] + frame->setHtml("<html><body><p>First Paragraph</p><p>Second Paragraph</p></body></html>"); + QWebElement doc = frame->documentElement(); + QWebElement body = doc.firstChild(); + QWebElement firstParagraph = body.firstChild(); + QWebElement secondParagraph = firstParagraph.nextSibling(); +//! [Traversing with QWebElement] +} + +static void findButtonAndClick() +{ + + frame->setHtml("<form name=\"myform\" action=\"submit_form.asp\" method=\"get\">" + "<input type=\"text\" name=\"myfield\">" + "<input type=\"submit\" value=\"Submit\">" + "</form>"); + +//! [Calling a DOM element method] + + QWebElement document = frame->documentElement(); + /* Assume that the document has the following structure: + + <form name="myform" action="submit_form.asp" method="get"> + <input type="text" name="myfield"> + <input type="submit" value="Submit"> + </form> + + */ + + QWebElement button = document.findFirst("input[type=submit]"); + button.evaluateJavaScript("click()"); + +//! [Calling a DOM element method] + + } + +static void autocomplete1() +{ + QWebElement document = frame->documentElement(); + +//! [autocomplete1] + QWebElement firstTextInput = document.findFirst("input[type=text]"); + QString storedText = firstTextInput.attribute("value"); +//! [autocomplete1] + +} + + +static void autocomplete2() +{ + + QWebElement document = frame->documentElement(); + QString storedText = "text"; + +//! [autocomplete2] + QWebElement firstTextInput = document.findFirst("input[type=text]"); + textInput.setAttribute("value", storedText); +//! [autocomplete2] + +} + + +static void findAll() +{ +//! [FindAll] + QWebElement document = frame->documentElement(); + /* Assume the document has the following structure: + + <p class=intro> + <span>Intro</span> + <span>Snippets</span> + </p> + <p> + <span>Content</span> + <span>Here</span> + </p> + */ + +//! [FindAll intro] + QWebElementCollection allSpans = document.findAll("span"); + QWebElementCollection introSpans = document.findAll("p.intro span"); +//! [FindAll intro] //! [FindAll] +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QWebView *view = new QWebView(0); + frame = view->page()->mainFrame(); + traverse(); + findAll(); + findButtonAndClick(); + autocomplete1(); + autocomplete2(); + return 0; +} diff --git a/Source/WebKit/qt/docs/webkitsnippets/webelement/webelement.pro b/Source/WebKit/qt/docs/webkitsnippets/webelement/webelement.pro new file mode 100644 index 000000000..fbe701314 --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/webelement/webelement.pro @@ -0,0 +1,2 @@ +TEMPLATE = app +SOURCES = main.cpp diff --git a/Source/WebKit/qt/docs/webkitsnippets/webpage/main.cpp b/Source/WebKit/qt/docs/webkitsnippets/webpage/main.cpp new file mode 100644 index 000000000..491935a00 --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/webpage/main.cpp @@ -0,0 +1,82 @@ +/* + Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include <QApplication> +#include <QPainter> +#include <QWebFrame> +#include <QWebPage> + +//! [0] +class Thumbnailer : public QObject +{ + Q_OBJECT + +public: + Thumbnailer(const QUrl &url); + +signals: + void finished(); + +private slots: + void render(); + +private: + QWebPage page; + +}; +//! [0] + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + Thumbnailer thumbnail(QUrl("http://qt.nokia.com")); + + QObject::connect(&thumbnail, SIGNAL(finished()), + &app, SLOT(quit())); + + return app.exec(); +} + +//! [1] +Thumbnailer::Thumbnailer(const QUrl &url) +{ + page.mainFrame()->load(url); + connect(&page, SIGNAL(loadFinished(bool)), + this, SLOT(render())); +} +//! [1] + +//! [2] +void Thumbnailer::render() +{ + page.setViewportSize(page.mainFrame()->contentsSize()); + QImage image(page.viewportSize(), QImage::Format_ARGB32); + QPainter painter(&image); + + page.mainFrame()->render(&painter); + painter.end(); + + QImage thumbnail = image.scaled(400, 400); + thumbnail.save("thumbnail.png"); + + emit finished(); +} +//! [2] +#include "main.moc" diff --git a/Source/WebKit/qt/docs/webkitsnippets/webpage/webpage.pro b/Source/WebKit/qt/docs/webkitsnippets/webpage/webpage.pro new file mode 100644 index 000000000..fcad03b15 --- /dev/null +++ b/Source/WebKit/qt/docs/webkitsnippets/webpage/webpage.pro @@ -0,0 +1,3 @@ +CONFIG += console +QT += webkit +SOURCES = main.cpp
\ No newline at end of file |