1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example webkit/fancybrowser
|
---|
30 | \title Fancy Browser Example
|
---|
31 |
|
---|
32 | The Fancy Browser example shows how to use jQuery with QtWebKit to
|
---|
33 | create a web browser with special effects and content
|
---|
34 | manipulation.
|
---|
35 |
|
---|
36 | \image fancybrowser-example.png
|
---|
37 |
|
---|
38 | The application makes use of QWebFrame::evaluateJavaScript to
|
---|
39 | evaluate the jQuery JavaScript code. A QMainWindow with a QWebView
|
---|
40 | as central widget builds up the browser itself.
|
---|
41 |
|
---|
42 | \section1 MainWindow Class Definition
|
---|
43 |
|
---|
44 | The \c MainWindow class inherits QMainWindow. It implements a number of
|
---|
45 | slots to perform actions on both the application and on the web content.
|
---|
46 |
|
---|
47 | \snippet examples/webkit/fancybrowser/mainwindow.h 1
|
---|
48 |
|
---|
49 | We also declare a QString that contains the jQuery, a QWebView
|
---|
50 | that displays the web content, and a QLineEdit that acts as the
|
---|
51 | address bar.
|
---|
52 |
|
---|
53 | \section1 MainWindow Class Implementation
|
---|
54 |
|
---|
55 | We start by implementing the constructor.
|
---|
56 |
|
---|
57 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 1
|
---|
58 |
|
---|
59 | The first part of the constructor sets the value of \c progress to
|
---|
60 | 0. This value will be used later in the code to visualize the
|
---|
61 | loading of a webpage.
|
---|
62 |
|
---|
63 | Next, the jQuery library is loaded using a QFile and reading the file
|
---|
64 | content. The jQuery library is a JavaScript library that provides different
|
---|
65 | functions for manipulating HTML.
|
---|
66 |
|
---|
67 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 2
|
---|
68 |
|
---|
69 | The second part of the constructor creates a QWebView and connects
|
---|
70 | slots to the views signals. Furthermore, we create a QLineEdit as
|
---|
71 | the browsers address bar. We then set the horizontal QSizePolicy
|
---|
72 | to fill the available area in the browser at all times. We add the
|
---|
73 | QLineEdit to a QToolbar together with a set of navigation actions
|
---|
74 | from QWebView::pageAction.
|
---|
75 |
|
---|
76 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 3
|
---|
77 |
|
---|
78 | The third and last part of the constructor implements two QMenus and assigns
|
---|
79 | a set of actions to them. The last line sets the QWebView as the central
|
---|
80 | widget in the QMainWindow.
|
---|
81 |
|
---|
82 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 4
|
---|
83 |
|
---|
84 | When the page is loaded, \c adjustLocation() updates the address
|
---|
85 | bar; \c adjustLocation() is triggered by the \c loadFinished()
|
---|
86 | signal in QWebView. In \c changeLocation() we create a QUrl
|
---|
87 | object, and then use it to load the page into the QWebView. When
|
---|
88 | the new web page has finished loading, \c adjustLocation() will be
|
---|
89 | run once more to update the address bar.
|
---|
90 |
|
---|
91 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 5
|
---|
92 |
|
---|
93 | \c adjustTitle() sets the window title and displays the loading
|
---|
94 | progress. This slot is triggered by the \c titleChanged() signal
|
---|
95 | in QWebView.
|
---|
96 |
|
---|
97 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 6
|
---|
98 |
|
---|
99 | When a web page has loaded, \c finishLoading() is triggered by the
|
---|
100 | \c loadFinished() signal in QWebView. \c finishLoading() then updates the
|
---|
101 | progress in the title bar and calls \c evaluateJavaScript() to evaluate the
|
---|
102 | jQuery library. This evaluates the JavaScript against the current web page.
|
---|
103 | What that means is that the JavaScript can be viewed as part of the content
|
---|
104 | loaded into the QWebView, and therefore needs to be loaded every time a new
|
---|
105 | page is loaded. Once the jQuery library is loaded, we can start executing
|
---|
106 | the different jQuery functions in the browser.
|
---|
107 |
|
---|
108 | The rotateImages() function is then called explicitely to make sure
|
---|
109 | that the images of the newly loaded page respect the state of the toggle
|
---|
110 | action.
|
---|
111 |
|
---|
112 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 7
|
---|
113 |
|
---|
114 | The first jQuery-based function, \c highlightAllLinks(), is designed to
|
---|
115 | highlight all links in the current webpage. The JavaScript code looks
|
---|
116 | for web elements named \e {a}, which is the tag for a hyperlink.
|
---|
117 | For each such element, the background color is set to be yellow by
|
---|
118 | using CSS.
|
---|
119 |
|
---|
120 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 8
|
---|
121 |
|
---|
122 | The \c rotateImages() function rotates the images on the current
|
---|
123 | web page. Webkit supports CSS transforms and this JavaScript code
|
---|
124 | looks up all \e {img} elements and rotates the images 180 degrees
|
---|
125 | and then back again.
|
---|
126 |
|
---|
127 | \snippet examples/webkit/fancybrowser/mainwindow.cpp 9
|
---|
128 |
|
---|
129 | The remaining four methods remove different elements from the current web
|
---|
130 | page. \c removeGifImages() removes all Gif images on the page by looking up
|
---|
131 | the \e {src} attribute of all the elements on the web page. Any element with
|
---|
132 | a \e {gif} file as its source is removed. \c removeInlineFrames() removes all
|
---|
133 | \e {iframe} or inline elements. \c removeObjectElements() removes all
|
---|
134 | \e {object} elements, and \c removeEmbeddedElements() removes any elements
|
---|
135 | such as plugins embedded on the page using the \e {embed} tag.
|
---|
136 |
|
---|
137 | */
|
---|
138 |
|
---|