From f3817bc1a7bc3df360af9116dfd96f3181816472 Mon Sep 17 00:00:00 2001 From: Jerome Pasion Date: Mon, 4 Oct 2010 16:43:21 +0200 Subject: Added the QtWebKit bridge Image Analyzer example. Originally written by Jervey Kong. Task-number: QTBUG-14154 Reviewed-by: David Boddie --- doc/src/examples/webkit-bridge-imageanalyzer.qdoc | 155 ++++++++++++++ doc/src/getting-started/examples.qdoc | 7 +- doc/src/images/webkit-imageanalyzer-complete.png | Bin 0 -> 10257 bytes doc/src/images/webkit-imageanalyzer-progress.png | Bin 0 -> 18240 bytes doc/src/images/webkit-imageanalyzer-screenshot.png | Bin 0 -> 190353 bytes examples/webkit/imageanalyzer/README | 20 ++ examples/webkit/imageanalyzer/imageanalyzer.cpp | 222 +++++++++++++++++++++ examples/webkit/imageanalyzer/imageanalyzer.h | 96 +++++++++ examples/webkit/imageanalyzer/imageanalyzer.pro | 14 ++ examples/webkit/imageanalyzer/main.cpp | 53 +++++ examples/webkit/imageanalyzer/mainwindow.cpp | 80 ++++++++ examples/webkit/imageanalyzer/mainwindow.h | 63 ++++++ .../webkit/imageanalyzer/resources/images/README | 2 + .../imageanalyzer/resources/images/bellaCoola.jpg | Bin 0 -> 129886 bytes .../imageanalyzer/resources/images/flower.jpg | Bin 0 -> 67126 bytes .../imageanalyzer/resources/images/mtRainier.jpg | Bin 0 -> 104090 bytes .../imageanalyzer/resources/images/seaShell.jpg | Bin 0 -> 82690 bytes .../imageanalyzer/resources/images/trees.jpg | Bin 0 -> 98630 bytes examples/webkit/webkit.pro | 1 + 19 files changed, 711 insertions(+), 2 deletions(-) create mode 100644 doc/src/examples/webkit-bridge-imageanalyzer.qdoc create mode 100644 doc/src/images/webkit-imageanalyzer-complete.png create mode 100644 doc/src/images/webkit-imageanalyzer-progress.png create mode 100644 doc/src/images/webkit-imageanalyzer-screenshot.png create mode 100644 examples/webkit/imageanalyzer/README create mode 100644 examples/webkit/imageanalyzer/imageanalyzer.cpp create mode 100644 examples/webkit/imageanalyzer/imageanalyzer.h create mode 100644 examples/webkit/imageanalyzer/imageanalyzer.pro create mode 100644 examples/webkit/imageanalyzer/main.cpp create mode 100644 examples/webkit/imageanalyzer/mainwindow.cpp create mode 100644 examples/webkit/imageanalyzer/mainwindow.h create mode 100644 examples/webkit/imageanalyzer/resources/images/README create mode 100644 examples/webkit/imageanalyzer/resources/images/bellaCoola.jpg create mode 100644 examples/webkit/imageanalyzer/resources/images/flower.jpg create mode 100644 examples/webkit/imageanalyzer/resources/images/mtRainier.jpg create mode 100644 examples/webkit/imageanalyzer/resources/images/seaShell.jpg create mode 100644 examples/webkit/imageanalyzer/resources/images/trees.jpg diff --git a/doc/src/examples/webkit-bridge-imageanalyzer.qdoc b/doc/src/examples/webkit-bridge-imageanalyzer.qdoc new file mode 100644 index 0000000..efc5623 --- /dev/null +++ b/doc/src/examples/webkit-bridge-imageanalyzer.qdoc @@ -0,0 +1,155 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Free Documentation License +** 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ +/*! + \example webkit/imageanalyzer + \startpage {index.html} {Qt Reference Documentation} + \title The Webkit Bridge Tutorial - Hybrid Client Application + + In this example, we will show how to write a hybrid application using +\l{The QtWebKit Bridge}{QtWebKit Bridge}, which distinguishes itself from a +thin client in that it performs heavy calculations on the client side in C++, +like a native application, but presents nothing more than a \c QWebView for its +user interface, displaying web content written in HTML/JavaScript. The +application uses QtConcurrent to distribute its work across as many CPU cores as +are available from the system, so it can process each image in parallel. + +Initially, you will see a user interface with an empty list of images. Clicking +on some of the images in the lower pane below adds them to the list view above, +as shown in the screenshot below. + + \image webkit-imageanalyzer-screenshot.png + +Now, we can click on \bold Analyze, and each image is analyzed using some +computationally intensive C++ function, in parallel and on different cores. +Progress is shown while the analysis is proceeding. + + \image webkit-imageanalyzer-progress.png + +and in the end, we will see something like this, where the average RGB values of +each image are shown. + + \image webkit-imageanalyzer-complete.png + +The MainWindow is defined in C++, and creates a \c QNetworkCache and a +\c QWebView, and tells the \c QWebView to load the starting page, providing us +with a user interface for the client. + + \snippet examples/webkit/imageanalyzer/mainwindow.cpp MainWindow - constructor + +In this example, the sample content is addressed with the \tt qrc:/index.html +URL. \tt qrc:/ indicates that the file is stored as a Qt resource (attached to +the executable). In a real-world application, the content and images would +likely be retrieved from the network rather than from resources. + +We wish to initialize an object reference in the JavaScript web page to point +to our \tt ImageAnalyzer before any other scripts are run. To do this, we +connect the \c javaScriptWindowObjectCleared() signal to a slot which does the +object creation and handoff to JavaScript. + + \snippet examples/webkit/imageanalyzer/mainwindow.cpp MainWindow - addJSObject + +The ImageAnalyzer object is created and added to a JavaScript object on the web +page's mainFrame with \c addToJavaScriptWindowObject(). + + The start page is resources/index.html. + In one of its
regions, we have images, each + with an \c onClick() handler that calls \c addImage(). + + \snippet examples/webkit/imageanalyzer/resources/index.html sample images + + Clicking an image adds it to an images list. + + \snippet examples/webkit/imageanalyzer/resources/index.html addImage + +The \bold {Analyze} button at the bottom of the image list is clicked when we +want to start the analysis: + + \snippet examples/webkit/imageanalyzer/resources/index.html images list + +When the user clicks the \bold {Analyze} button, \c analyzeImages() is called, +another regular JavaScript method, shown below. +Notice it assumes the \c imageAnalyzer object is already defined and initialized +in JavaScript space, but we guaranteed that by connecting our setup slot to the +appropriate signal, \c javaScriptWindowObjectCleared(). + + \snippet examples/webkit/imageanalyzer/resources/index.html analyzeImages + +The only methods on \c ImageAnalyzer that we can or do call from JavaScript are +those which are exposed through Qt's MetaObject system: property getter/setter +methods, +\c public \c slots, \c signals, and other \c Q_INVOKABLE functions. + +\snippet examples/webkit/imageanalyzer/imageanalyzer.h ImageAnalyzer - public interface +\dots +\snippet examples/webkit/imageanalyzer/imageanalyzer.h ImageAnalyzer - private members + +Most of the members are set up in the constructor: + +\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - Constructor + +Back on the JavaScript side, we want to connect signals from this object to +JavaScript functions on our web page, after the web page is loaded, but before +the images are analyzed. + +From \c connectSlots(), we can see how to connect signals from the imageAnalyzer +object to regular JavaScript functions, which can also behave like slots. We use +this to monitor and display progress from the C++ side. + + \snippet examples/webkit/imageanalyzer/resources/index.html connect slots + +The only public slot is \c startAnalysis(), called to place +a list of URLs into the image analyzer's QtConcurrent processing queue +from JavaScript space. + +\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - startAnalysis + +The images need to be loaded again now, which is why fetchURLs first checks the +cache to see if we can save an extra network get. + +\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - fetchURLs + +For the images that were not in the cache, \c handleReply() +will load them into a QImage when the data is ready. + +\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - handleReply + +After the images are loaded, they are queued up in preparation to be +sent in a batch for analysis to a \c QFutureWatcher, which will distribute the +processing across multiple threads and cores, depending on how many are available. + +\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - queueImage + +The function that gets performed on each image is \c averageRGB(), +as specified in argument 2 to the \c QtConcurrent::mapped() function. +Notice it repeats the same calculations 100 times on each pixel to keep the CPU +very busy. This is done only for the purposes of the demo so that the analysis +takes a noticeable time to complete. + +\snippet examples/webkit/imageanalyzer/imageanalyzer.cpp ImageAnalyzer - averageRGB + +*/ + diff --git a/doc/src/getting-started/examples.qdoc b/doc/src/getting-started/examples.qdoc index bfe7be9..398eee8 100644 --- a/doc/src/getting-started/examples.qdoc +++ b/doc/src/getting-started/examples.qdoc @@ -32,7 +32,7 @@ Qt includes a set of examples that cover nearly every aspect of Qt development. They aren't meant to be impressive when you run them, but in each case the source code has been carefully written to - illustrate one or more best Qt programming practices. + illustrate one or more best Qt programming practices. You can run the examples from the \l{Examples and Demos Launcher} application (except see \l{QML Examples and Demos} {QML Examples} @@ -764,6 +764,9 @@ \row \o \l{webkit/googlechat}{Google Chat} \o A real-world example that shows how an existing Web-based service can be accessed using QtWebKit. + \row \o \l{webkit/imageanalyzer}{Image Analyzer}\raisedaster + \o A \l{The QtWebKit Bridge}{QtWebKit bridge} application which contains a + web interface and client-side processing. \row \o \l{webkit/previewer}{Previewer}\raisedaster \o Shows how to make a simple Web page previewer by using Qt's text input widgets together with a QWebView widget. @@ -776,7 +779,7 @@ */ /*! - \page examples-helpsystem.html + \page examples-helpsystem.html \ingroup all-examples \title Help System Examples \brief Adding interactive help to your Qt application. diff --git a/doc/src/images/webkit-imageanalyzer-complete.png b/doc/src/images/webkit-imageanalyzer-complete.png new file mode 100644 index 0000000..47c0ee1 Binary files /dev/null and b/doc/src/images/webkit-imageanalyzer-complete.png differ diff --git a/doc/src/images/webkit-imageanalyzer-progress.png b/doc/src/images/webkit-imageanalyzer-progress.png new file mode 100644 index 0000000..5bab890 Binary files /dev/null and b/doc/src/images/webkit-imageanalyzer-progress.png differ diff --git a/doc/src/images/webkit-imageanalyzer-screenshot.png b/doc/src/images/webkit-imageanalyzer-screenshot.png new file mode 100644 index 0000000..987f8a2 Binary files /dev/null and b/doc/src/images/webkit-imageanalyzer-screenshot.png differ diff --git a/examples/webkit/imageanalyzer/README b/examples/webkit/imageanalyzer/README new file mode 100644 index 0000000..9415b57 --- /dev/null +++ b/examples/webkit/imageanalyzer/README @@ -0,0 +1,20 @@ +This example demonstrates the use of Qt WebKit to make a hybrid application. + +Build instructions: + + On Mac OS X: + In this directory: + qmake -spec macx-g++ + make clean all + open imageanalyzer.app + On Linux/Unix: + In this directory: + qmake + make clean all + ./imageanalyzer + + On Windows: + In this directory: + qmake + nmake clean all + debug\imageanalyzer.exe diff --git a/examples/webkit/imageanalyzer/imageanalyzer.cpp b/examples/webkit/imageanalyzer/imageanalyzer.cpp new file mode 100644 index 0000000..c663cf3 --- /dev/null +++ b/examples/webkit/imageanalyzer/imageanalyzer.cpp @@ -0,0 +1,222 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** 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 Nokia Corporation 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 +#include +#include +#include +#include "imageanalyzer.h" + +/*! + * This class operates as follows: + * Parent calls the slot startAnalysis which shoves a list of QStrings into URLQueue and then calls fetchURLs. + * FetchURLs sends out HTTP GETs for each image it can't get out of the cache. + * As the responses come in, handleReply trys to create an image out of each and pushes those images into imageQueue. + * On the last (detected by no outstandingFetches and URLQueue.isEmpty()) call to queueImage (from handleReply) + * a thread is forked to process all the images. When it finishes, it emits a finished signal that is received + * by our JavaScript code. + */ + +//! [ ImageAnalyzer - Constructor ] +ImageAnalyzer::ImageAnalyzer(QNetworkDiskCache* netcache, QObject* parent) +: QObject(parent), m_cache(netcache), m_outstandingFetches(0) +{ + /* ImageAnalyzer only wants to receive http responses + for requests that it makes, so that's why it has its own + QNetworkAccessManager. */ + m_network = new QNetworkAccessManager(this); + m_watcher = new QFutureWatcher(this); + /* We want to share a cache with the web browser, + in case it has some images we want: */ + m_network->setCache(m_cache); + + QObject::connect(m_network, SIGNAL(finished(QNetworkReply*)), + this, SLOT(handleReply(QNetworkReply*))); + QObject::connect(m_watcher, SIGNAL(finished()), + this, SLOT(doneProcessing())); + QObject::connect(m_watcher, SIGNAL(progressValueChanged(int)), + this, SLOT(progressStatus(int))); +} +//! [ ImageAnalyzer - Constructor ] +ImageAnalyzer::~ImageAnalyzer() +{ + delete(m_watcher); +} + + +QRgb ImageAnalyzer::lastResults() +{ + int rTot = 0; + int bTot = 0; + int gTot = 0; + int count = m_watcher->future().results().size(); + foreach(const QRgb & triplet, m_watcher->future().results()) + { + rTot += qRed(triplet); + bTot += qBlue(triplet); + gTot += qGreen(triplet); + } + return qRgb(rTot/count, bTot/count, gTot/count); +} + +float ImageAnalyzer::lastRed() { return qRed(lastResults())/2.55; } +float ImageAnalyzer::lastGreen() { return qGreen(lastResults())/2.55; } +float ImageAnalyzer::lastBlue() { return qBlue(lastResults())/2.55; } + +void ImageAnalyzer::progressStatus(int newstat) +{ + emit updateProgress(newstat, m_watcher->progressMaximum()); +} + + +bool ImageAnalyzer::isBusy() +{ + return m_watcher->isRunning(); +} + + +//! [ ImageAnalyzer - startAnalysis ] +void ImageAnalyzer::startAnalysis(const QStringList & urls) +{ + m_URLQueue = urls; + fetchURLs(); +} +//! [ ImageAnalyzer - startAnalysis ] + +/*! + * Analyzes the entire queue - just starts all our http GETs. + */ +//! [ ImageAnalyzer - fetchURLs ] +void ImageAnalyzer::fetchURLs() +{ + while (!m_URLQueue.isEmpty()) + { + QString url = m_URLQueue.takeFirst(); + QUrl URL = QUrl(url); + QIODevice * pData = m_cache->data(URL); + // Is image already loaded in cache? + if (pData == 0) { + // HTTP Get image over network. + m_outstandingFetches++; + QNetworkRequest request = QNetworkRequest(URL); + request.setRawHeader("User-Agent", "Nokia - Custom QT app"); + m_network->get(request); + } else { + // Get image from cache + QImage image; + image.load(pData, 0); + if (!image.isNull()) + queueImage(image); + delete(pData); + } + } +} +//! [ ImageAnalyzer - fetchURLs ] +/* + * Slot to handle the incoming responses from our http GETs + */ +//! [ ImageAnalyzer - handleReply ] +void ImageAnalyzer::handleReply(QNetworkReply * pReply) +{ + m_outstandingFetches--; + if (pReply->error()) { + qDebug() << "Error code" << pReply->error(); + qDebug() << "Http code" << pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute); + return; + } + QImage image; + image.load(pReply, 0); + pReply->deleteLater(); + if (image.isNull()) { + qDebug() << "bad image"; + qDebug() << pReply->rawHeaderList(); + foreach(QByteArray element, pReply->rawHeaderList()) { + qDebug() << element << " = " << pReply->rawHeader(element); + } + return; + } + queueImage(image); +} +//! [ ImageAnalyzer - handleReply ] + +void ImageAnalyzer::doneProcessing() +{ + m_imageQueue = QList(); + emit finishedAnalysis(); +} +//! [ ImageAnalyzer - queueImage ] +void ImageAnalyzer::queueImage(QImage img) +{ + if (!img.isNull()) + m_imageQueue << img; + + if (m_outstandingFetches == 0 && m_URLQueue.isEmpty()) { + m_watcher->setFuture(QtConcurrent::mapped(m_imageQueue, averageRGB)); + } +} +//! [ ImageAnalyzer - queueImage ] + +//! [ ImageAnalyzer - averageRGB ] +QRgb averageRGB(const QImage &img) +{ + int pixelCount = img.width() * img.height(); + int rAvg, gAvg, bAvg; + + // We waste some time here: + for (int timeWaster=0; timeWaster < 100; timeWaster++) { + quint64 rTot = 0; + quint64 gTot = 0; + quint64 bTot = 0; + for (int i=0; i < img.width(); i++) { + for (int j=0; j < img.height(); j++) { + QRgb pixel = img.pixel(i,j); + rTot += qRed(pixel); + gTot += qGreen(pixel); + bTot += qBlue(pixel); + } + } + rAvg = (rTot)/(pixelCount); + gAvg = (gTot)/(pixelCount); + bAvg = (bTot)/(pixelCount); + } + return qRgb(rAvg, gAvg, bAvg); +} +//! [ ImageAnalyzer - averageRGB ] diff --git a/examples/webkit/imageanalyzer/imageanalyzer.h b/examples/webkit/imageanalyzer/imageanalyzer.h new file mode 100644 index 0000000..f228c0e --- /dev/null +++ b/examples/webkit/imageanalyzer/imageanalyzer.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** 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 Nokia Corporation 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 IMAGEANALYZER_H +#define IMAGEANALYZER_H +#include +#include + +class QNetworkAccessManager; +class QNetworkReply; +class QNetworkDiskCache; +//! [ ImageAnalyzer - public interface ] +class ImageAnalyzer : public QObject +{ + Q_OBJECT + public: + ImageAnalyzer(QNetworkDiskCache * netcache, QObject * parent=0); + + QRgb lastResults(); + float lastRed(); + float lastGreen(); + float lastBlue(); + bool isBusy(); + Q_PROPERTY(bool busy READ isBusy); + Q_PROPERTY(float red READ lastRed); + Q_PROPERTY(float green READ lastGreen); + Q_PROPERTY(float blue READ lastBlue); + ~ImageAnalyzer(); + public slots: + /*! initiates analysis of all the urls in the list */ + void startAnalysis(const QStringList & urls); + signals: + void finishedAnalysis(); + void updateProgress(int completed, int total); +//! [ ImageAnalyzer - public interface ] + private slots: + void handleReply(QNetworkReply*); + void doneProcessing(); + void progressStatus(int); +private: + QRgb processImages(); + void fetchURLs(); + void queueImage(QImage img); + +//! [ ImageAnalyzer - private members ] +private: + QNetworkAccessManager* m_network; + QNetworkDiskCache* m_cache; + QStringList m_URLQueue; + QList m_imageQueue; + int m_outstandingFetches; + QFutureWatcher * m_watcher; +//! [ ImageAnalyzer - private members ] +}; + +QRgb averageRGB(const QImage &img); + +#endif diff --git a/examples/webkit/imageanalyzer/imageanalyzer.pro b/examples/webkit/imageanalyzer/imageanalyzer.pro new file mode 100644 index 0000000..c9a0ed1 --- /dev/null +++ b/examples/webkit/imageanalyzer/imageanalyzer.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +HEADERS = imageanalyzer.h \ + mainwindow.h +SOURCES = imageanalyzer.cpp \ + main.cpp \ + mainwindow.cpp + +QT += network webkit + +RESOURCES = resources/imageanalyzer.qrc + +OTHER_FILES += html/index.html README ../webkit-bridge-tutorial.qdoc outline.txt + + diff --git a/examples/webkit/imageanalyzer/main.cpp b/examples/webkit/imageanalyzer/main.cpp new file mode 100644 index 0000000..c3f04de --- /dev/null +++ b/examples/webkit/imageanalyzer/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** 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 Nokia Corporation 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 "imageanalyzer.h" +#include "mainwindow.h" + +int main(int argc, char* argv[]) +{ + QApplication app(argc, argv); + + MainWin win; + win.show(); + return app.exec(); +} + diff --git a/examples/webkit/imageanalyzer/mainwindow.cpp b/examples/webkit/imageanalyzer/mainwindow.cpp new file mode 100644 index 0000000..d89098e --- /dev/null +++ b/examples/webkit/imageanalyzer/mainwindow.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** 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 Nokia Corporation 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 "mainwindow.h" +#include "imageanalyzer.h" + +#include +#include +#include + +/* + * Default Constructor + */ +//! [MainWindow - constructor] +MainWin::MainWin(QWidget * parent) : QWebView(parent) +{ + m_network = new QNetworkAccessManager(this); + m_cache = new QNetworkDiskCache(this); + m_cache->setCacheDirectory(QDesktopServices::storageLocation(QDesktopServices::CacheLocation) + "/imageanalyzer"); + m_cache->setMaximumCacheSize(1000000); //set the cache to 10megs + m_network->setCache(m_cache); + page()->setNetworkAccessManager(m_network); + + //! The object we will expose to JavaScript engine: + m_analyzer = new ImageAnalyzer(m_cache, this); + + // Signal is emitted before frame loads any web content: + QObject::connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), + this, SLOT(addJSObject())); + + // qrc:// URLs refer to resources. See imagenalayzer.qrc + QUrl startURL = QUrl("qrc:/index.html"); + + // Load web content now! + setUrl(startURL); +} +//! [MainWindow - constructor] + +//! [MainWindow - addJSObject] +void MainWin::addJSObject() { + // Add pAnalyzer to JavaScript Frame as member "imageAnalyzer". + page()->mainFrame()->addToJavaScriptWindowObject(QString("imageAnalyzer"), m_analyzer); +} +//! [MainWindow - addJSObject] diff --git a/examples/webkit/imageanalyzer/mainwindow.h b/examples/webkit/imageanalyzer/mainwindow.h new file mode 100644 index 0000000..4c6b950 --- /dev/null +++ b/examples/webkit/imageanalyzer/mainwindow.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** 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 Nokia Corporation 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 MAINWINDOW_H +#define MAINWINDOW_H +#include + + +class ImageAnalyzer; +class QNetworkDiskCache; + +class MainWin : public QWebView +{ + Q_OBJECT + + public: + explicit MainWin(QWidget * parent = 0); + private: + ImageAnalyzer * m_analyzer; + QNetworkAccessManager * m_network; + QNetworkDiskCache * m_cache; + private slots: + void addJSObject(); +}; +#endif diff --git a/examples/webkit/imageanalyzer/resources/images/README b/examples/webkit/imageanalyzer/resources/images/README new file mode 100644 index 0000000..176a1da --- /dev/null +++ b/examples/webkit/imageanalyzer/resources/images/README @@ -0,0 +1,2 @@ +The images are under public domain and were obtained from +http://publicdomainpictures.net diff --git a/examples/webkit/imageanalyzer/resources/images/bellaCoola.jpg b/examples/webkit/imageanalyzer/resources/images/bellaCoola.jpg new file mode 100644 index 0000000..f90ed54 Binary files /dev/null and b/examples/webkit/imageanalyzer/resources/images/bellaCoola.jpg differ diff --git a/examples/webkit/imageanalyzer/resources/images/flower.jpg b/examples/webkit/imageanalyzer/resources/images/flower.jpg new file mode 100644 index 0000000..6b7f6be Binary files /dev/null and b/examples/webkit/imageanalyzer/resources/images/flower.jpg differ diff --git a/examples/webkit/imageanalyzer/resources/images/mtRainier.jpg b/examples/webkit/imageanalyzer/resources/images/mtRainier.jpg new file mode 100644 index 0000000..d09a3f2 Binary files /dev/null and b/examples/webkit/imageanalyzer/resources/images/mtRainier.jpg differ diff --git a/examples/webkit/imageanalyzer/resources/images/seaShell.jpg b/examples/webkit/imageanalyzer/resources/images/seaShell.jpg new file mode 100644 index 0000000..c5005a9 Binary files /dev/null and b/examples/webkit/imageanalyzer/resources/images/seaShell.jpg differ diff --git a/examples/webkit/imageanalyzer/resources/images/trees.jpg b/examples/webkit/imageanalyzer/resources/images/trees.jpg new file mode 100644 index 0000000..083b26d Binary files /dev/null and b/examples/webkit/imageanalyzer/resources/images/trees.jpg differ diff --git a/examples/webkit/webkit.pro b/examples/webkit/webkit.pro index 76c8801..6a1d8f8 100644 --- a/examples/webkit/webkit.pro +++ b/examples/webkit/webkit.pro @@ -4,6 +4,7 @@ SUBDIRS += domtraversal \ previewer \ fancybrowser \ simpleselector \ + imageanalyzer \ framecapture contains(QT_CONFIG, openssl):SUBDIRS += googlechat -- cgit v0.12