source: trunk/doc/src/examples/webkit-bridge-imageanalyzer.qdoc@ 1168

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 6.9 KB
Line 
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/imageanalyzer
30 \startpage {index.html}{Qt Reference Documentation}
31 \title The Webkit Bridge Tutorial - Hybrid Client Application
32
33 In this example, we will show how to write a hybrid application using
34\l{The QtWebKit Bridge}{QtWebKit Bridge}, which distinguishes itself from a
35thin client in that it performs heavy calculations on the client side in C++,
36like a native application, but presents nothing more than a \c QWebView for its
37user interface, displaying web content written in HTML/JavaScript. The
38application uses QtConcurrent to distribute its work across as many CPU cores as
39are available from the system, so it can process each image in parallel.
40
41For the full reference documentation of QtWebKit hybrid development, see
42\l{qtwebkit-bridge.html}{The QtWebKit Bridge}.
43
44Initially, you will see a user interface with an empty list of images. Clicking
45on some of the images in the lower pane below adds them to the list view above,
46as shown in the screenshot below.
47
48 \image webkit-imageanalyzer-screenshot.png
49
50Now, we can click on \bold Analyze, and each image is analyzed using some
51computationally intensive C++ function, in parallel and on different cores.
52Progress is shown while the analysis is proceeding.
53
54 \image webkit-imageanalyzer-progress.png
55
56and in the end, we will see something like this, where the average RGB values of
57each image are shown.
58
59 \image webkit-imageanalyzer-complete.png
60
61The MainWindow is defined in C++, and creates a \l QNetworkDiskCache and a
62\l QWebView, and tells the \l QWebView to load the starting page, providing us
63with a user interface for the client.
64
65 \snippet examples/webkit/imageanalyzer/mainwindow.cpp MainWindow - constructor
66
67In this example, the sample content is addressed with the \tt qrc:/index.html
68URL. \tt qrc:/ indicates that the file is stored as a Qt resource (attached to
69the executable). In a real-world application, the content and images would
70likely be retrieved from the network rather than from resources.
71
72We wish to initialize an object reference in the JavaScript web page to point
73to our \tt ImageAnalyzer before any other scripts are run. To do this, we
74connect the \l{QWebFrame::}{javaScriptWindowObjectCleared()} signal to a slot
75which does the object creation and handoff to JavaScript.
76
77 \snippet examples/webkit/imageanalyzer/mainwindow.cpp MainWindow - addJSObject
78
79The ImageAnalyzer object is created and added to a JavaScript object on the web
80page's mainFrame with \c addToJavaScriptWindowObject().
81
82 The start page is resources/index.html.
83 In one of its <div> regions, we have images, each
84 with an \c onClick() handler that calls \c addImage().
85
86 \snippet examples/webkit/imageanalyzer/resources/index.html sample images
87
88Clicking an image adds it to an images list.
89
90 \snippet examples/webkit/imageanalyzer/resources/index.html addImage
91
92The \bold {Analyze} button at the bottom of the image list is clicked when we
93want to start the analysis:
94
95 \snippet examples/webkit/imageanalyzer/resources/index.html images list
96
97When the user clicks the \bold {Analyze} button, \c analyzeImages() is called,
98another regular JavaScript method, shown below.
99Notice it assumes the \c imageAnalyzer object is already defined and initialized
100in JavaScript space, but we guaranteed that by connecting our setup slot to the
101appropriate signal, \l{QWebFrame::}{javaScriptWindowObjectCleared()}.
102
103 \snippet examples/webkit/imageanalyzer/resources/index.html analyzeImages
104
105The only methods on \c ImageAnalyzer that we can or do call from JavaScript are
106those which are exposed through \{The Meta-Object System}{Qt's MetaObject}
107system: \l{The Property System}{property} getter/setter methods,
108\c public \l {Signals & Slots}{signals and slots}, and other
109\l{Q_INVOKABLE}{Q_INVOKABLE} functions.
110
111\snippet examples/webkit/imageanalyzer/imageanalyzer.h ImageAnalyzer - public interface
112\dots
113\snippet examples/webkit/imageanalyzer/imageanalyzer.h ImageAnalyzer - private members
114
115Most of the members are set up in the constructor:
116
117\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - Constructor
118
119Back on the JavaScript side, we want to connect signals from this object to
120JavaScript functions on our web page, after the web page is loaded, but before
121the images are analyzed.
122
123From \c connectSlots(), we can see how to connect signals from the imageAnalyzer
124object to regular JavaScript functions, which can also behave like slots. We use
125this to monitor and display progress from the C++ side.
126
127 \snippet examples/webkit/imageanalyzer/resources/index.html connect slots
128
129The only public slot is \c startAnalysis(), called to place
130a list of URLs into the image analyzer's QtConcurrent processing queue
131from JavaScript space.
132
133\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - startAnalysis
134
135The images need to be loaded again now, which is why fetchURLs first checks the
136cache to see if we can save an extra network get.
137
138\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - fetchURLs
139
140For the images that were not in the cache, \c handleReply()
141will load them into a QImage when the data is ready.
142
143\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - handleReply
144
145After the images are loaded, they are queued up in preparation to be
146sent in a batch for analysis to a \l QFutureWatcher, which will distribute the
147processing across multiple threads and cores, depending on how many are available.
148
149\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - queueImage
150
151The function that gets performed on each image is \c averageRGB(),
152as specified in argument 2 to the \l{QtConcurrent::mapped()} function.
153Notice it repeats the same calculations 100 times on each pixel to keep the CPU
154very busy. This is done only for the purposes of the demo so that the analysis
155takes a noticeable time to complete.
156
157\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - averageRGB
158
159*/
160
Note: See TracBrowser for help on using the repository browser.