summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2010-07-08 03:19:22 (GMT)
committerJason McDonald <jason.mcdonald@nokia.com>2010-07-13 04:39:09 (GMT)
commit7c1ff382b0927d8b003468c16e31333816c3ab6b (patch)
treebbab99dca0c96625f7a4194f2ffa03d492572dbf /examples
parentca259d99473757190152e81e899aa200355235a5 (diff)
downloadQt-7c1ff382b0927d8b003468c16e31333816c3ab6b.zip
Qt-7c1ff382b0927d8b003468c16e31333816c3ab6b.tar.gz
Qt-7c1ff382b0927d8b003468c16e31333816c3ab6b.tar.bz2
Extend QDeclarativeImageProvider to support QPixmap loading and
synchronous loading of QImages. (QPixmaps can only be created in the main thread so they will always be loaded synchronously). This changes request() to requestImage() and adds requestPixmap() for pixmap support. Task-number: QTBUG-11989 (cherry picked from commit a258456bcb35ec4211751a702ea94a1881d82a07)
Diffstat (limited to 'examples')
-rw-r--r--examples/declarative/cppextensions/imageprovider/imageprovider-example.qml28
-rw-r--r--examples/declarative/cppextensions/imageprovider/imageprovider.cpp73
2 files changed, 39 insertions, 62 deletions
diff --git a/examples/declarative/cppextensions/imageprovider/imageprovider-example.qml b/examples/declarative/cppextensions/imageprovider/imageprovider-example.qml
index 5890c91..1ef97fa 100644
--- a/examples/declarative/cppextensions/imageprovider/imageprovider-example.qml
+++ b/examples/declarative/cppextensions/imageprovider/imageprovider-example.qml
@@ -37,29 +37,13 @@
** $QT_END_LICENSE$
**
****************************************************************************/
-
import Qt 4.7
-import "ImageProviderCore"
-//![0]
-ListView {
- width: 100; height: 100
- anchors.fill: parent
-
- model: myModel
+import "ImageProviderCore" // import the plugin that registers the color image provider
- delegate: Component {
- Item {
- width: 100
- height: 50
- Text {
- text: "Loading..."
- anchors.centerIn: parent
- }
- Image {
- source: modelData
- sourceSize: "50x25"
- }
- }
- }
+//![0]
+Column {
+ Image { source: "image://colors/yellow" }
+ Image { source: "image://colors/red" }
}
//![0]
+
diff --git a/examples/declarative/cppextensions/imageprovider/imageprovider.cpp b/examples/declarative/cppextensions/imageprovider/imageprovider.cpp
index 0281b4a..995192a 100644
--- a/examples/declarative/cppextensions/imageprovider/imageprovider.cpp
+++ b/examples/declarative/cppextensions/imageprovider/imageprovider.cpp
@@ -42,7 +42,6 @@
#include <qdeclarativeextensionplugin.h>
#include <qdeclarativeengine.h>
-#include <qdeclarativecontext.h>
#include <qdeclarative.h>
#include <qdeclarativeitem.h>
#include <qdeclarativeimageprovider.h>
@@ -50,62 +49,57 @@
#include <QImage>
#include <QPainter>
-/*
- This example illustrates using a QDeclarativeImageProvider to serve
- images asynchronously.
-*/
-
//![0]
class ColorImageProvider : public QDeclarativeImageProvider
{
public:
- // This is run in a low priority thread.
- QImage request(const QString &id, QSize *size, const QSize &req_size)
+ ColorImageProvider()
+ : QDeclarativeImageProvider(Pixmap)
{
- if (size) *size = QSize(100,50);
- QImage image(
- req_size.width() > 0 ? req_size.width() : 100,
- req_size.height() > 0 ? req_size.height() : 50,
- QImage::Format_RGB32);
- image.fill(QColor(id).rgba());
- QPainter p(&image);
- QFont f = p.font();
- f.setPixelSize(30);
- p.setFont(f);
- p.setPen(Qt::black);
- if (req_size.isValid())
- p.scale(req_size.width()/100.0, req_size.height()/50.0);
- p.drawText(QRectF(0,0,100,50),Qt::AlignCenter,id);
- return image;
+ }
+
+ QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
+ {
+ int width = 100;
+ int height = 50;
+
+ if (size)
+ *size = QSize(width, height);
+ QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
+ requestedSize.height() > 0 ? requestedSize.height() : height);
+ pixmap.fill(QColor(id).rgba());
+//![0]
+
+ // write the color name
+ QPainter painter(&pixmap);
+ QFont f = painter.font();
+ f.setPixelSize(20);
+ painter.setFont(f);
+ painter.setPen(Qt::black);
+ if (requestedSize.isValid())
+ painter.scale(requestedSize.width() / width, requestedSize.height() / height);
+ painter.drawText(QRectF(0, 0, width, height), Qt::AlignCenter, id);
+
+//![1]
+ return pixmap;
}
};
+//![1]
class ImageProviderExtensionPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
- void registerTypes(const char *uri) {
+ void registerTypes(const char *uri)
+ {
Q_UNUSED(uri);
-
}
- void initializeEngine(QDeclarativeEngine *engine, const char *uri) {
+ void initializeEngine(QDeclarativeEngine *engine, const char *uri)
+ {
Q_UNUSED(uri);
-
engine->addImageProvider("colors", new ColorImageProvider);
-
- QStringList dataList;
- dataList.append("image://colors/red");
- dataList.append("image://colors/green");
- dataList.append("image://colors/blue");
- dataList.append("image://colors/brown");
- dataList.append("image://colors/orange");
- dataList.append("image://colors/purple");
- dataList.append("image://colors/yellow");
-
- QDeclarativeContext *ctxt = engine->rootContext();
- ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
}
};
@@ -113,5 +107,4 @@ public:
#include "imageprovider.moc"
Q_EXPORT_PLUGIN(ImageProviderExtensionPlugin);
-//![0]