summaryrefslogtreecommitdiff
path: root/examples/webkitwidgets/domtraversal
diff options
context:
space:
mode:
authorhjk <qthjk@ovi.com>2012-12-10 17:21:29 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-12-10 18:37:29 +0100
commit129bcf57471d09f15cbfe6dc3ab397689e6eb245 (patch)
treeb4901c52b1f75212303be606e6c40530c2afcd53 /examples/webkitwidgets/domtraversal
parent539c47c9c93335196dbde56f7ba98de0e6600a38 (diff)
downloadqtwebkit-examples-129bcf57471d09f15cbfe6dc3ab397689e6eb245.tar.gz
Adjust webkit example project install targets.
This follows suit with aeb036e in qtbase. Change-Id: Ifc85b327df81d15f3579b911e07c95a3b7a3bb9d Reviewed-by: Michael Bruning <michael.bruning@digia.com> Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'examples/webkitwidgets/domtraversal')
-rw-r--r--examples/webkitwidgets/domtraversal/doc/images/webkit-domtraversal.pngbin0 -> 109327 bytes
-rw-r--r--examples/webkitwidgets/domtraversal/doc/src/domtraversal.qdoc125
-rw-r--r--examples/webkitwidgets/domtraversal/domtraversal.pro9
-rw-r--r--examples/webkitwidgets/domtraversal/main.cpp52
-rw-r--r--examples/webkitwidgets/domtraversal/window.cpp89
-rw-r--r--examples/webkitwidgets/domtraversal/window.h72
-rw-r--r--examples/webkitwidgets/domtraversal/window.ui89
7 files changed, 436 insertions, 0 deletions
diff --git a/examples/webkitwidgets/domtraversal/doc/images/webkit-domtraversal.png b/examples/webkitwidgets/domtraversal/doc/images/webkit-domtraversal.png
new file mode 100644
index 0000000..8b6f34a
--- /dev/null
+++ b/examples/webkitwidgets/domtraversal/doc/images/webkit-domtraversal.png
Binary files differ
diff --git a/examples/webkitwidgets/domtraversal/doc/src/domtraversal.qdoc b/examples/webkitwidgets/domtraversal/doc/src/domtraversal.qdoc
new file mode 100644
index 0000000..52764f3
--- /dev/null
+++ b/examples/webkitwidgets/domtraversal/doc/src/domtraversal.qdoc
@@ -0,0 +1,125 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Free Documentation License Usage
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file. Please review the following information to ensure
+** the GNU Free Documentation License version 1.3 requirements
+** will be met: http://www.gnu.org/copyleft/fdl.html.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*!
+ \example domtraversal
+ \title DOM Traversal Example
+
+ \brief The DOM Traversal example shows how to use the QWebElement class to access
+ the structure of a Web page.
+
+ \image webkit-domtraversal.png
+
+ The QWebElement class provides an API that can be used to examine the structure
+ and content of a Web page via a Document Object Model (DOM) interface. It can be
+ used for basic traversal of the document structure, to search for particular
+ elements (see the \l{Simple Selector Example}), and to modify content in-place.
+
+ This example uses a QWebView widget to display the Web page, and a dock widget
+ holds the QTreeWidget that shows the document structure. These widgets are
+ placed in an instance of the \c Window class, which we describe below.
+
+ \section1 Window Class Definition
+
+ The \c Window class is derived from QMainWindow and its user interface is created
+ using \l{Qt Designer}. As a result, the class is also derived from the user
+ interface class created by \l uic:
+
+ \snippet domtraversal/window.h Window class definition
+
+ Two important functions to note are the \c on_webView_loadFinished() slot and
+ the \c examineChildElements() function. The former is automatically called
+ when the QWebView widget finishes loading a page. See the
+ \l{#Further Reading}{Further Reading} section for more information on this
+ mechanism.
+
+ The \c examineChildElements() function is used to traverse the document structure
+ and add items to the QTreeWidget.
+
+ \section1 Window Class Implementation
+
+ In the \c Window class constructor, we call the \l{QWidget::}{setupUi()} function
+ to set up the user interface described in the \c{window.ui} file:
+
+ \snippet domtraversal/window.cpp Window constructor
+
+ When the Web page is loaded, the \c on_webView_loadFinished() slot is called. Here,
+ we clear the tree widget and begin inspection of the document by obtaining the
+ document element from the page's main frame:
+
+ \snippet domtraversal/window.cpp begin document inspection
+
+ At this point, we call the \c examineChildElements() function to traverse the
+ document, starting with the child elements of the document element for which we
+ will create top level items in the tree widget.
+
+ The \c examineChildElements() function accepts a parent element and a parent item.
+ Starting with the first child element, which we obtain with the element's
+ \l{QWebElement::}{firstChild()} function, we examine each child element of the
+ parent item. For each valid (non-null) element, which we check by calling its
+ \l{QWebElement::}{isNull()} function, we create a new QTreeWidgetItem instance with
+ the element name and add it to the parent item.
+
+ \snippet domtraversal/window.cpp traverse document
+
+ We recursively examine the child elements for each element by calling
+ \c examineChildElements() with the current child element and the newly-created item.
+ To obtain the next element at the same level in the document, we call its
+ \l{QWebElement::}{nextSibling()} function.
+
+ This recursive approach to reading the document makes it easy to create a simple
+ representation of the document structure in a tree widget.
+
+ For completeness, we show the \c setUrl() function, which is provided to allow the
+ document URL to be set from the example's \c main() function.
+
+ \snippet domtraversal/window.cpp set URL
+
+ \section1 Starting the Example
+
+ We set up the application, create
+ a \c Window instance, set its URL, and show it:
+
+ \snippet domtraversal/main.cpp main program
+
+ When the application's event loop is run, the Qt home page will load, and the
+ tree widget will be updated to show the document structure. Navigating to another
+ page will cause the tree widget to be updated to show the document structure of
+ the new page.
+
+ \section1 Further Reading
+
+ The QWebElement documentation contains more information about DOM access for the
+ QtWebKit classes.
+
+ In this example, we take advantage of Qt's
+ \l{Using a Designer UI File in Your Application#Automatic Connections}{auto-connection}
+ feature to avoid explicitly connecting signals to slots. The user interface
+ contains a QWebView widget called \c webView whose \l{QWebView::}{loadFinished()}
+ signal is automatically connected to the \c on_webView_loadFinished() slot when
+ we call \l{QWidget::}{setupUi()} in the \c Window constructor.
+*/
diff --git a/examples/webkitwidgets/domtraversal/domtraversal.pro b/examples/webkitwidgets/domtraversal/domtraversal.pro
new file mode 100644
index 0000000..8c53258
--- /dev/null
+++ b/examples/webkitwidgets/domtraversal/domtraversal.pro
@@ -0,0 +1,9 @@
+QT += webkitwidgets network widgets
+FORMS = window.ui
+HEADERS = window.h
+SOURCES = main.cpp \
+ window.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/webkitwidgets/domtraversal
+INSTALLS += target
diff --git a/examples/webkitwidgets/domtraversal/main.cpp b/examples/webkitwidgets/domtraversal/main.cpp
new file mode 100644
index 0000000..d105e47
--- /dev/null
+++ b/examples/webkitwidgets/domtraversal/main.cpp
@@ -0,0 +1,52 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+//! [main program]
+#include <QApplication>
+#include "window.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+ Window window;
+ window.show();
+ window.setUrl(QUrl("http://qt-project.org/"));
+ return app.exec();
+}
+//! [main program]
diff --git a/examples/webkitwidgets/domtraversal/window.cpp b/examples/webkitwidgets/domtraversal/window.cpp
new file mode 100644
index 0000000..f96b9be
--- /dev/null
+++ b/examples/webkitwidgets/domtraversal/window.cpp
@@ -0,0 +1,89 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+#include <QtWebKitWidgets>
+
+#include "window.h"
+
+//! [Window constructor]
+Window::Window(QWidget *parent)
+ : QMainWindow(parent)
+{
+ setupUi(this);
+}
+//! [Window constructor]
+
+//! [set URL]
+void Window::setUrl(const QUrl &url)
+{
+ webView->setUrl(url);
+}
+//! [set URL]
+
+//! [begin document inspection]
+void Window::on_webView_loadFinished()
+{
+ treeWidget->clear();
+
+ QWebFrame *frame = webView->page()->mainFrame();
+ QWebElement document = frame->documentElement();
+
+ examineChildElements(document, treeWidget->invisibleRootItem());
+}
+//! [begin document inspection]
+
+//! [traverse document]
+void Window::examineChildElements(const QWebElement &parentElement,
+ QTreeWidgetItem *parentItem)
+{
+ QWebElement element = parentElement.firstChild();
+ while (!element.isNull()) {
+
+ QTreeWidgetItem *item = new QTreeWidgetItem();
+ item->setText(0, element.tagName());
+ parentItem->addChild(item);
+
+ examineChildElements(element, item);
+
+ element = element.nextSibling();
+ }
+}
+//! [traverse document]
diff --git a/examples/webkitwidgets/domtraversal/window.h b/examples/webkitwidgets/domtraversal/window.h
new file mode 100644
index 0000000..f9ce2f7
--- /dev/null
+++ b/examples/webkitwidgets/domtraversal/window.h
@@ -0,0 +1,72 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef WINDOW_H
+#define WINDOW_H
+
+#include <QMainWindow>
+#include <QUrl>
+#include <QWebElement>
+
+QT_BEGIN_NAMESPACE
+class QTreeWidgetItem;
+QT_END_NAMESPACE
+
+//! [Window class definition]
+#include "ui_window.h"
+
+class Window : public QMainWindow, private Ui::Window
+{
+ Q_OBJECT
+
+public:
+ Window(QWidget *parent = 0);
+ void setUrl(const QUrl &url);
+
+public slots:
+ void on_webView_loadFinished();
+
+private:
+ void examineChildElements(const QWebElement &parentElement,
+ QTreeWidgetItem *parentItem);
+};
+//! [Window class definition]
+
+#endif
diff --git a/examples/webkitwidgets/domtraversal/window.ui b/examples/webkitwidgets/domtraversal/window.ui
new file mode 100644
index 0000000..e245aa6
--- /dev/null
+++ b/examples/webkitwidgets/domtraversal/window.ui
@@ -0,0 +1,89 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Window</class>
+ <widget class="QMainWindow" name="Window">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>600</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Web Element DOM Traversal</string>
+ </property>
+ <widget class="QWidget" name="centralwidget">
+ <layout class="QVBoxLayout" name="verticalLayout_2">
+ <property name="topMargin">
+ <number>4</number>
+ </property>
+ <property name="bottomMargin">
+ <number>4</number>
+ </property>
+ <item>
+ <widget class="QWebView" name="webView">
+ <property name="url">
+ <url>
+ <string>http://qt-project.org/</string>
+ </url>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ <widget class="QMenuBar" name="menubar">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>27</height>
+ </rect>
+ </property>
+ </widget>
+ <widget class="QStatusBar" name="statusbar"/>
+ <widget class="QDockWidget" name="dockWidget">
+ <property name="allowedAreas">
+ <set>Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea</set>
+ </property>
+ <property name="windowTitle">
+ <string>Document Structure</string>
+ </property>
+ <attribute name="dockWidgetArea">
+ <number>1</number>
+ </attribute>
+ <widget class="QWidget" name="dockWidgetContents">
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <property name="margin">
+ <number>4</number>
+ </property>
+ <item>
+ <widget class="QTreeWidget" name="treeWidget">
+ <attribute name="headerVisible">
+ <bool>false</bool>
+ </attribute>
+ <attribute name="headerVisible">
+ <bool>false</bool>
+ </attribute>
+ <column>
+ <property name="text">
+ <string notr="true">1</string>
+ </property>
+ </column>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </widget>
+ </widget>
+ <customwidgets>
+ <customwidget>
+ <class>QWebView</class>
+ <extends>QWidget</extends>
+ <header>QtWebKitWidgets/QWebView</header>
+ </customwidget>
+ </customwidgets>
+ <resources/>
+ <connections/>
+</ui>