diff options
author | Jerome Pasion <jerome.pasion@nokia.com> | 2010-10-08 10:16:51 (GMT) |
---|---|---|
committer | Jerome Pasion <jerome.pasion@nokia.com> | 2010-10-08 10:16:51 (GMT) |
commit | 18e4aaebb8cd1764eba1fce64bb202b63d2a74b8 (patch) | |
tree | 756acbb13f2f9e0030874c2333374f0df314cb8d | |
parent | a5dc543041511c73b87fbda685c041febc81b7d9 (diff) | |
download | Qt-18e4aaebb8cd1764eba1fce64bb202b63d2a74b8.zip Qt-18e4aaebb8cd1764eba1fce64bb202b63d2a74b8.tar.gz Qt-18e4aaebb8cd1764eba1fce64bb202b63d2a74b8.tar.bz2 |
Resized image, added missing files, and reformatted documentation of
QtWebKit Image Analyzer example.
-rw-r--r-- | doc/src/examples/webkit-bridge-imageanalyzer.qdoc | 24 | ||||
-rw-r--r-- | doc/src/images/webkit-imageanalyzer-screenshot.png | bin | 190353 -> 56053 bytes | |||
-rw-r--r-- | examples/webkit/imageanalyzer/imageanalyzer.cpp | 8 | ||||
-rw-r--r-- | examples/webkit/imageanalyzer/imageanalyzer.h | 73 | ||||
-rw-r--r-- | examples/webkit/imageanalyzer/mainwindow.h | 21 | ||||
-rw-r--r-- | examples/webkit/imageanalyzer/resources/imageanalyzer.qrc | 10 | ||||
-rw-r--r-- | examples/webkit/imageanalyzer/resources/index.html | 133 |
7 files changed, 212 insertions, 57 deletions
diff --git a/doc/src/examples/webkit-bridge-imageanalyzer.qdoc b/doc/src/examples/webkit-bridge-imageanalyzer.qdoc index efc5623..b8c42c6 100644 --- a/doc/src/examples/webkit-bridge-imageanalyzer.qdoc +++ b/doc/src/examples/webkit-bridge-imageanalyzer.qdoc @@ -37,6 +37,9 @@ 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. +For the full reference documentation of QtWebKit hybrid development, see +\l{qtwebkit-bridge.html}{The QtWebKit Bridge}. + 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. @@ -54,8 +57,8 @@ 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 +The MainWindow is defined in C++, and creates a \l QNetworkDiskCache and a +\l QWebView, and tells the \l QWebView to load the starting page, providing us with a user interface for the client. \snippet examples/webkit/imageanalyzer/mainwindow.cpp MainWindow - constructor @@ -67,8 +70,8 @@ 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. +connect the \l{QWebFrame::}{javaScriptWindowObjectCleared()} signal to a slot +which does the object creation and handoff to JavaScript. \snippet examples/webkit/imageanalyzer/mainwindow.cpp MainWindow - addJSObject @@ -94,14 +97,15 @@ 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(). +appropriate signal, \l{QWebFrame::}{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. +those which are exposed through \{The Meta-Object System}{Qt's MetaObject} +system: \l{The Property System}{property} getter/setter methods, +\c public \l {Signals & Slots}{signals and slots}, and other +\l{Q_INVOKABLE}{Q_INVOKABLE} functions. \snippet examples/webkit/imageanalyzer/imageanalyzer.h ImageAnalyzer - public interface \dots @@ -138,13 +142,13 @@ 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 +sent in a batch for analysis to a \l 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. +as specified in argument 2 to the \l{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. diff --git a/doc/src/images/webkit-imageanalyzer-screenshot.png b/doc/src/images/webkit-imageanalyzer-screenshot.png Binary files differindex 987f8a2..c96371a 100644 --- a/doc/src/images/webkit-imageanalyzer-screenshot.png +++ b/doc/src/images/webkit-imageanalyzer-screenshot.png diff --git a/examples/webkit/imageanalyzer/imageanalyzer.cpp b/examples/webkit/imageanalyzer/imageanalyzer.cpp index c663cf3..1d0ee45 100644 --- a/examples/webkit/imageanalyzer/imageanalyzer.cpp +++ b/examples/webkit/imageanalyzer/imageanalyzer.cpp @@ -57,7 +57,7 @@ //! [ ImageAnalyzer - Constructor ] ImageAnalyzer::ImageAnalyzer(QNetworkDiskCache* netcache, QObject* parent) -: QObject(parent), m_cache(netcache), m_outstandingFetches(0) + : 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 @@ -69,11 +69,11 @@ ImageAnalyzer::ImageAnalyzer(QNetworkDiskCache* netcache, QObject* parent) m_network->setCache(m_cache); QObject::connect(m_network, SIGNAL(finished(QNetworkReply*)), - this, SLOT(handleReply(QNetworkReply*))); + this, SLOT(handleReply(QNetworkReply*))); QObject::connect(m_watcher, SIGNAL(finished()), - this, SLOT(doneProcessing())); + this, SLOT(doneProcessing())); QObject::connect(m_watcher, SIGNAL(progressValueChanged(int)), - this, SLOT(progressStatus(int))); + this, SLOT(progressStatus(int))); } //! [ ImageAnalyzer - Constructor ] ImageAnalyzer::~ImageAnalyzer() diff --git a/examples/webkit/imageanalyzer/imageanalyzer.h b/examples/webkit/imageanalyzer/imageanalyzer.h index f228c0e..1bb25dc 100644 --- a/examples/webkit/imageanalyzer/imageanalyzer.h +++ b/examples/webkit/imageanalyzer/imageanalyzer.h @@ -47,48 +47,53 @@ class QNetworkAccessManager; class QNetworkReply; class QNetworkDiskCache; + //! [ ImageAnalyzer - public interface ] class ImageAnalyzer : public QObject { Q_OBJECT - public: - ImageAnalyzer(QNetworkDiskCache * netcache, QObject * parent=0); +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); - 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); + QRgb processImages(); + void fetchURLs(); + void queueImage(QImage img); -//! [ ImageAnalyzer - private members ] + //! [ ImageAnalyzer - private members ] private: - QNetworkAccessManager* m_network; - QNetworkDiskCache* m_cache; - QStringList m_URLQueue; - QList<QImage> m_imageQueue; - int m_outstandingFetches; - QFutureWatcher<QRgb> * m_watcher; -//! [ ImageAnalyzer - private members ] + QNetworkAccessManager* m_network; + QNetworkDiskCache* m_cache; + QStringList m_URLQueue; + QList<QImage> m_imageQueue; + int m_outstandingFetches; + QFutureWatcher<QRgb> * m_watcher; + //! [ ImageAnalyzer - private members ] }; QRgb averageRGB(const QImage &img); diff --git a/examples/webkit/imageanalyzer/mainwindow.h b/examples/webkit/imageanalyzer/mainwindow.h index 4c6b950..076e586 100644 --- a/examples/webkit/imageanalyzer/mainwindow.h +++ b/examples/webkit/imageanalyzer/mainwindow.h @@ -41,8 +41,8 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include <QWebView> +#include <QWebView> class ImageAnalyzer; class QNetworkDiskCache; @@ -50,14 +50,17 @@ 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(); - 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/imageanalyzer.qrc b/examples/webkit/imageanalyzer/resources/imageanalyzer.qrc new file mode 100644 index 0000000..fe9a5df --- /dev/null +++ b/examples/webkit/imageanalyzer/resources/imageanalyzer.qrc @@ -0,0 +1,10 @@ +<!DOCTYPE RCC><RCC version="1.0"> + <qresource> + <file>index.html</file> + <file>images/mtRainier.jpg</file> + <file>images/bellaCoola.jpg</file> + <file>images/trees.jpg</file> + <file>images/flower.jpg</file> + <file>images/seaShell.jpg</file> +</qresource> +</RCC> diff --git a/examples/webkit/imageanalyzer/resources/index.html b/examples/webkit/imageanalyzer/resources/index.html new file mode 100644 index 0000000..6532951 --- /dev/null +++ b/examples/webkit/imageanalyzer/resources/index.html @@ -0,0 +1,133 @@ + +<html> + <body> + <!-- [ images list ] --> + <div style="float:right; width:50%; border-left: solid 1px black"> + <div id="listdiv" align="center"> + <h5>Images to be analyzed:</h5> + <select multiple size=10 id=imglist style="width:80%"></select> + <br /> + <input type="button" id="evalbutton" value="Analyze" onclick="analyzeImages()" /> + </div> + </div> + <!-- [ images list ] --> + <div style="width:50%"> + <div id="titleblock" align="center"> + <h2>Image Analyzer</h2> + </div> + <div id=outputdiv align=center> + <h4>Status: <span id=status>Idle</span></h4> + <h5> + Latest Results:<br /> + Red: <span id=redval style="color:red">n/a</span><br /> + Green: <span id=greenval style="color:green">n/a</span><br /> + Blue: <span id=blueval style="color:blue">n/a</span><br /> + </h5> + <h3>Click on images below to select for analysis</h3> + </div> + </div> + <!-- [ sample images ] --> + <div id=imagediv style="clear:both; text-align:center"> + <hr/> + <img src="images/mtRainier.jpg" height=150px onclick='return addImage(this);' /> + <img src="images/bellaCoola.jpg" height=150px onclick='return addImage(this);'/> + <img src="images/seaShell.jpg" height=150px onclick='return addImage(this);'/> + <!-- [ sample images ] --> + <img src="images/flower.jpg" height=150px onclick='return addImage(this);'/> + <img src="images/trees.jpg" height=150px onclick='return addImage(this);'/> + </div> + + </body> +</html> + +<script type="text/javascript"> + var remaining = 0; + var connected = false; + //We use this function because connect statements resolve their target once, imediately + //not at signal emission so they must be connected once the imageAnalyzer object has been added to the frame + //! <!-- [ connect slots ] --> + function connectSlots() + { + if ( !connected ) { + connected = true; + imageAnalyzer.finishedAnalysis.connect(this, finished); + imageAnalyzer.updateProgress.connect(this, updateProg); + } + } + //! <!-- [ connect slots ] --> + + function finished() { + setStatus('Idle'); + setResults(imageAnalyzer.red.toFixed(2), imageAnalyzer.green.toFixed(2), imageAnalyzer.blue.toFixed(2)); + } + //This will function as the recieving "slot" for the progress signal + function updateProg(complete, max) + { + var oldRemaining = remaining; + remaining = max - complete; + pullList(oldRemaining - remaining); + //Prevent results getting messed up if we don't get signals in order + if( imageAnalyzer.busy ) { + setStatus('Processing (' + complete + ' of ' + max + ' completed)'); + setResults('Calculating','Calculating','Calculating'); + } + } + +//! <!-- [ analyzeImages ] --> +function analyzeImages() { + connectSlots(); + var imglist = document.getElementsByTagName('option'); + if (imglist.length > 0) { + stringlist = []; + for(var i=0; i<imglist.length; i++) { + stringlist[i]=imglist[i].value; + } + if (!imageAnalyzer.busy) { + remaining = stringlist.length; + imageAnalyzer.startAnalysis(stringlist); + } else { + alert("Processing, please wait until finished."); + } +//! <!-- [ analyzeImages ] --> + } else { + alert('No images selected. Click on one or more images to select them for analysis.'); + } +} +function clearList() { + var imglist = document.getElementById('imglist'); + while(imglist.length > 0) { + imglist.removeChild(imglist.childNodes[0]); + } +} +function pullList(count) { + var imglist = document.getElementById('imglist'); + while(imglist.length > 0 && count > 0) { + imglist.removeChild(imglist.childNodes[0]); + count--; + } +} +function setStatus(statusString) { + document.getElementById('status').innerHTML = statusString; +} + +function setResults(red, green, blue) { + if (! isNaN(red) ) { red += " %"; } + if (! isNaN(green) ) { green += " %"; } + if (! isNaN(blue) ) { blue += " %"; } + document.getElementById('redval').innerHTML = red; + document.getElementById('greenval').innerHTML = green; + document.getElementById('blueval').innerHTML = blue; +} +//! <!-- [ addImage ] --> +function addImage(newimg) { + var imglist = document.getElementById('imglist'); + var curChildren = imglist.childNodes; + var newline = document.createElement('option'); + newline.innerHTML = newimg.src.substring(newimg.src.lastIndexOf('/')+1); + newline.value = newimg.src; + imglist.appendChild(newline); +} +//! <!-- [ addImage ] --> +</script> + + |