[844] | 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
|
---|
| 35 | thin client in that it performs heavy calculations on the client side in C++,
|
---|
| 36 | like a native application, but presents nothing more than a \c QWebView for its
|
---|
| 37 | user interface, displaying web content written in HTML/JavaScript. The
|
---|
| 38 | application uses QtConcurrent to distribute its work across as many CPU cores as
|
---|
| 39 | are available from the system, so it can process each image in parallel.
|
---|
| 40 |
|
---|
| 41 | For the full reference documentation of QtWebKit hybrid development, see
|
---|
| 42 | \l{qtwebkit-bridge.html}{The QtWebKit Bridge}.
|
---|
| 43 |
|
---|
| 44 | Initially, you will see a user interface with an empty list of images. Clicking
|
---|
| 45 | on some of the images in the lower pane below adds them to the list view above,
|
---|
| 46 | as shown in the screenshot below.
|
---|
| 47 |
|
---|
| 48 | \image webkit-imageanalyzer-screenshot.png
|
---|
| 49 |
|
---|
| 50 | Now, we can click on \bold Analyze, and each image is analyzed using some
|
---|
| 51 | computationally intensive C++ function, in parallel and on different cores.
|
---|
| 52 | Progress is shown while the analysis is proceeding.
|
---|
| 53 |
|
---|
| 54 | \image webkit-imageanalyzer-progress.png
|
---|
| 55 |
|
---|
| 56 | and in the end, we will see something like this, where the average RGB values of
|
---|
| 57 | each image are shown.
|
---|
| 58 |
|
---|
| 59 | \image webkit-imageanalyzer-complete.png
|
---|
| 60 |
|
---|
| 61 | The 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
|
---|
| 63 | with a user interface for the client.
|
---|
| 64 |
|
---|
| 65 | \snippet examples/webkit/imageanalyzer/mainwindow.cpp MainWindow - constructor
|
---|
| 66 |
|
---|
| 67 | In this example, the sample content is addressed with the \tt qrc:/index.html
|
---|
| 68 | URL. \tt qrc:/ indicates that the file is stored as a Qt resource (attached to
|
---|
| 69 | the executable). In a real-world application, the content and images would
|
---|
| 70 | likely be retrieved from the network rather than from resources.
|
---|
| 71 |
|
---|
| 72 | We wish to initialize an object reference in the JavaScript web page to point
|
---|
| 73 | to our \tt ImageAnalyzer before any other scripts are run. To do this, we
|
---|
| 74 | connect the \l{QWebFrame::}{javaScriptWindowObjectCleared()} signal to a slot
|
---|
| 75 | which does the object creation and handoff to JavaScript.
|
---|
| 76 |
|
---|
| 77 | \snippet examples/webkit/imageanalyzer/mainwindow.cpp MainWindow - addJSObject
|
---|
| 78 |
|
---|
| 79 | The ImageAnalyzer object is created and added to a JavaScript object on the web
|
---|
| 80 | page'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 |
|
---|
| 88 | Clicking an image adds it to an images list.
|
---|
| 89 |
|
---|
| 90 | \snippet examples/webkit/imageanalyzer/resources/index.html addImage
|
---|
| 91 |
|
---|
| 92 | The \bold {Analyze} button at the bottom of the image list is clicked when we
|
---|
| 93 | want to start the analysis:
|
---|
| 94 |
|
---|
| 95 | \snippet examples/webkit/imageanalyzer/resources/index.html images list
|
---|
| 96 |
|
---|
| 97 | When the user clicks the \bold {Analyze} button, \c analyzeImages() is called,
|
---|
| 98 | another regular JavaScript method, shown below.
|
---|
| 99 | Notice it assumes the \c imageAnalyzer object is already defined and initialized
|
---|
| 100 | in JavaScript space, but we guaranteed that by connecting our setup slot to the
|
---|
| 101 | appropriate signal, \l{QWebFrame::}{javaScriptWindowObjectCleared()}.
|
---|
| 102 |
|
---|
| 103 | \snippet examples/webkit/imageanalyzer/resources/index.html analyzeImages
|
---|
| 104 |
|
---|
| 105 | The only methods on \c ImageAnalyzer that we can or do call from JavaScript are
|
---|
| 106 | those which are exposed through \{The Meta-Object System}{Qt's MetaObject}
|
---|
| 107 | system: \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 |
|
---|
| 115 | Most of the members are set up in the constructor:
|
---|
| 116 |
|
---|
| 117 | \snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - Constructor
|
---|
| 118 |
|
---|
| 119 | Back on the JavaScript side, we want to connect signals from this object to
|
---|
| 120 | JavaScript functions on our web page, after the web page is loaded, but before
|
---|
| 121 | the images are analyzed.
|
---|
| 122 |
|
---|
| 123 | From \c connectSlots(), we can see how to connect signals from the imageAnalyzer
|
---|
| 124 | object to regular JavaScript functions, which can also behave like slots. We use
|
---|
| 125 | this to monitor and display progress from the C++ side.
|
---|
| 126 |
|
---|
| 127 | \snippet examples/webkit/imageanalyzer/resources/index.html connect slots
|
---|
| 128 |
|
---|
| 129 | The only public slot is \c startAnalysis(), called to place
|
---|
| 130 | a list of URLs into the image analyzer's QtConcurrent processing queue
|
---|
| |
---|