diff options
author | Martin Jones <martin.jones@nokia.com> | 2009-07-31 05:35:02 (GMT) |
---|---|---|
committer | Martin Jones <martin.jones@nokia.com> | 2009-07-31 05:35:02 (GMT) |
commit | a8eec858efcfef9e7fb3225589f56fd716ac8c0a (patch) | |
tree | 161010093453b0743c75522cda3b7c27c1afe6e3 /src/declarative/fx | |
parent | 5672de53a0c4181d210d86e369f3dd141af3ef84 (diff) | |
parent | fc5af680d26aba684f4cbd4caa0440aabc3153d4 (diff) | |
download | Qt-a8eec858efcfef9e7fb3225589f56fd716ac8c0a.zip Qt-a8eec858efcfef9e7fb3225589f56fd716ac8c0a.tar.gz Qt-a8eec858efcfef9e7fb3225589f56fd716ac8c0a.tar.bz2 |
Merge branch 'kinetic-declarativeui' of git@scm.dev.nokia.troll.no:qt/kinetic into kinetic-declarativeui
Diffstat (limited to 'src/declarative/fx')
-rw-r--r-- | src/declarative/fx/qfximage.cpp | 207 | ||||
-rw-r--r-- | src/declarative/fx/qfximage.h | 14 | ||||
-rw-r--r-- | src/declarative/fx/qfximage_p.h | 8 | ||||
-rw-r--r-- | src/declarative/fx/qfxitem.cpp | 20 | ||||
-rw-r--r-- | src/declarative/fx/qfxitem.h | 2 | ||||
-rw-r--r-- | src/declarative/fx/qfxitem_p.h | 5 | ||||
-rw-r--r-- | src/declarative/fx/qfxlayouts.cpp | 10 | ||||
-rw-r--r-- | src/declarative/fx/qfxlayouts.h | 2 | ||||
-rw-r--r-- | src/declarative/fx/qfxpixmap.cpp | 83 |
9 files changed, 198 insertions, 153 deletions
diff --git a/src/declarative/fx/qfximage.cpp b/src/declarative/fx/qfximage.cpp index 15dc620..98e8985 100644 --- a/src/declarative/fx/qfximage.cpp +++ b/src/declarative/fx/qfximage.cpp @@ -60,7 +60,10 @@ QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Image,QFxImage) \brief The Image element allows you to add bitmaps to a scene. \inherits Item - The Image element supports untransformed, stretched, grid-scaled and tiled images. + The Image element supports untransformed, stretched, tiled, and grid-scaled images. + + For an explanation of stretching and tiling, see the fillMode property description. + For an explanation of grid-scaling see the scaleGrid property description or the QFxScaleGrid class description. @@ -74,23 +77,53 @@ QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Image,QFxImage) \endqml \row \o \image declarative-qtlogo2.png - \o Stretched + \o fillMode: Stretch (default) \qml - Image { width: 160; height: 160; source: "pics/qtlogo.png" } + Image { + width: 160 + height: 160 + source: "pics/qtlogo.png" + } \endqml \row \o \image declarative-qtlogo4.png - \o Grid-scaled + \o Grid-scaled (only with fillMode: Stretch) \qml Image { scaleGrid.left: 20; scaleGrid.right: 10 scaleGrid.top: 14; scaleGrid.bottom: 14 - width: 160; height: 160; source: "pics/qtlogo.png" } + width: 160; height: 160 + source: "pics/qtlogo.png" + } \endqml \row \o \image declarative-qtlogo3.png - \o Tiled + \o fillMode: Tile + \qml + Image { + fillMode: "Tile" + width: 160; height: 160 + source: "pics/qtlogo.png" + } + \endqml + \row + \o \image declarative-qtlogo6.png + \o fillMode: TileVertically + \qml + Image { + fillMode: "TileVertically" + width: 160; height: 160 + source: "pics/qtlogo.png" + } + \endqml + \row + \o \image declarative-qtlogo5.png + \o fillMode: TileHorizontally \qml - Image { tile: true; width: 160; height: 160; source: "pics/qtlogo.png" } + Image { + fillMode: "TileHorizontally" + width: 160; height: 160 + source: "pics/qtlogo.png" + } \endqml \endtable */ @@ -193,33 +226,40 @@ QFxScaleGrid *QFxImage::scaleGrid() } /*! - \qmlproperty bool Image::tile + \qmlproperty FillMode Image::fillMode - Set this property to enable image tiling. Normally the Image element scales the - bitmap file to its size. If tiling is enabled, the bitmap is repeated as a set - of unscaled tiles, clipped to the size of the Image. + Set this property to define what happens when the image set for the item is smaller + than the size of the item. - \qml - Item { - Image { source: "tile.png" } - Image { x: 80; width: 100; height: 100; source: "tile.png" } - Image { x: 190; width: 100; height: 100; tile: true; source: "tile.png" } - } - \endqml - \image declarative-image_tile.png + \list + \o Stretch - the image is scaled to fit + \o PreserveAspect - the image is scaled uniformly to fit + \o Tile - the image is duplicated horizontally and vertically + \o TileVertically - the image is stretched horizontally and tiled vertically + \o TileHorizontally - the image is stretched vertically and tiled horizontally + \endlist + + \image declarative-image_fillMode.gif - If both tiling and the scaleGrid are set, tiling takes precedence. + Only fillMode: Stretch can be used with scaleGrid. Other settings + will cause a run-time warning. + + \sa examples/declarative/aspectratio */ -bool QFxImage::isTiled() const +QFxImage::FillMode QFxImage::fillMode() const { Q_D(const QFxImage); - return d->tiled; + return d->fillMode; } -void QFxImage::setTiled(bool tile) +void QFxImage::setFillMode(FillMode mode) { Q_D(QFxImage); - d->tiled = tile; + if (d->fillMode == mode) + return; + d->fillMode = mode; + update(); + emit fillModeChanged(); } void QFxImage::componentComplete() @@ -283,51 +323,72 @@ void QFxImage::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) QPixmap pix = d->pix; - if (d->tiled) { - p->save(); - p->setClipRect(0, 0, width(), height(), Qt::IntersectClip); - QRect me = QRect(0, 0, width(), height()); - - int pw = pix.width(); - int ph = pix.height(); - int yy = 0; - - while(yy < height()) { - int xx = 0; - while(xx < width()) { - p->drawPixmap(xx, yy, pix); - xx += pw; - } - yy += ph; - } - - p->restore(); - } else if (!d->scaleGrid || d->scaleGrid->isNull()) { + if (!d->scaleGrid || d->scaleGrid->isNull()) { if (width() != pix.width() || height() != pix.height()) { - qreal widthScale = width() / qreal(pix.width()); - qreal heightScale = height() / qreal(pix.height()); - - QTransform scale; - - if (d->preserveAspect) { - if (widthScale < heightScale) { - heightScale = widthScale; - scale.translate(0, (height() - heightScale * pix.height()) / 2); - } else if(heightScale < widthScale) { - widthScale = heightScale; - scale.translate((width() - widthScale * pix.width()) / 2, 0); + if (d->fillMode >= Tile) { + p->save(); + p->setClipRect(0, 0, width(), height(), Qt::IntersectClip); + + if (d->fillMode == Tile) { + const int pw = pix.width(); + const int ph = pix.height(); + int yy = 0; + + while(yy < height()) { + int xx = 0; + while(xx < width()) { + p->drawPixmap(xx, yy, pix); + xx += pw; + } + yy += ph; + } + } else if (d->fillMode == TileVertically) { + const int ph = pix.height(); + int yy = 0; + + while(yy < height()) { + p->drawPixmap(QRect(0, yy, width(), ph), pix); + yy += ph; + } + } else { + const int pw = pix.width(); + int xx = 0; + + while(xx < width()) { + p->drawPixmap(QRect(xx, 0, pw, height()), pix); + xx += pw; + } } - } - scale.scale(widthScale, heightScale); - QTransform old = p->transform(); - p->setWorldTransform(scale * old); - p->drawPixmap(0, 0, pix); - p->setWorldTransform(old); + p->restore(); + } else { + qreal widthScale = width() / qreal(pix.width()); + qreal heightScale = height() / qreal(pix.height()); + + QTransform scale; + + if (d->fillMode == PreserveAspect) { + if (widthScale < heightScale) { + heightScale = widthScale; + scale.translate(0, (height() - heightScale * pix.height()) / 2); + } else if(heightScale < widthScale) { + widthScale = heightScale; + scale.translate((width() - widthScale * pix.width()) / 2, 0); + } + } + + scale.scale(widthScale, heightScale); + QTransform old = p->transform(); + p->setWorldTransform(scale * old); + p->drawPixmap(0, 0, pix); + p->setWorldTransform(old); + } } else { p->drawPixmap(0, 0, pix); } } else { + if (d->fillMode != Stretch) + qWarning("Only fillmode:Stretch supported for scale grid images"); int sgl = d->scaleGrid->left(); int sgr = d->scaleGrid->right(); int sgt = d->scaleGrid->top(); @@ -458,28 +519,6 @@ QUrl QFxImage::source() const return d->url; } -/*! - \qmlproperty bool Image::preserveAspect - - Whether the image's aspect ratio should be preserved when resizing. By default this - is false. -*/ -bool QFxImage::preserveAspect() const -{ - Q_D(const QFxImage); - return d->preserveAspect; -} - -void QFxImage::setPreserveAspect(bool p) -{ - Q_D(QFxImage); - - if (p == d->preserveAspect) - return; - d->preserveAspect = p; - update(); -} - void QFxImage::setSource(const QUrl &url) { #ifdef Q_ENABLE_PERFORMANCE_LOG diff --git a/src/declarative/fx/qfximage.h b/src/declarative/fx/qfximage.h index 633289f..9573ce5 100644 --- a/src/declarative/fx/qfximage.h +++ b/src/declarative/fx/qfximage.h @@ -57,24 +57,26 @@ class Q_DECLARATIVE_EXPORT QFxImage : public QFxItem { Q_OBJECT Q_ENUMS(Status) + Q_ENUMS(FillMode) Q_PROPERTY(Status status READ status NOTIFY statusChanged) Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) Q_PROPERTY(QFxScaleGrid *scaleGrid READ scaleGrid) - Q_PROPERTY(bool tile READ isTiled WRITE setTiled) Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap DESIGNABLE false) Q_PROPERTY(bool smooth READ smoothTransform WRITE setSmoothTransform) - Q_PROPERTY(bool preserveAspect READ preserveAspect WRITE setPreserveAspect); + Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged); + public: QFxImage(QFxItem *parent=0); ~QFxImage(); QFxScaleGrid *scaleGrid(); - bool isTiled() const; - void setTiled(bool tile); + enum FillMode { Stretch, PreserveAspect, Tile, TileVertically, TileHorizontally }; + FillMode fillMode() const; + void setFillMode(FillMode); QPixmap pixmap() const; void setPixmap(const QPixmap &); @@ -86,9 +88,6 @@ public: Status status() const; qreal progress() const; - bool preserveAspect() const; - void setPreserveAspect(bool); - QUrl source() const; virtual void setSource(const QUrl &url); @@ -98,6 +97,7 @@ Q_SIGNALS: void sourceChanged(const QUrl &); void statusChanged(Status); void progressChanged(qreal progress); + void fillModeChanged(); protected: QFxImage(QFxImagePrivate &dd, QFxItem *parent); diff --git a/src/declarative/fx/qfximage_p.h b/src/declarative/fx/qfximage_p.h index 7792dbf..a9ad054 100644 --- a/src/declarative/fx/qfximage_p.h +++ b/src/declarative/fx/qfximage_p.h @@ -70,8 +70,9 @@ class QFxImagePrivate : public QFxItemPrivate public: QFxImagePrivate() - : scaleGrid(0), tiled(false), smooth(false), opaque(false), - preserveAspect(false), status(QFxImage::Idle), sciReply(0), + : scaleGrid(0), smooth(false), opaque(false), + fillMode(QFxImage::Stretch), + status(QFxImage::Idle), sciReply(0), progress(0.0) { } @@ -92,11 +93,10 @@ public: QFxScaleGrid *scaleGrid; QPixmap pix; - bool tiled : 1; bool smooth : 1; bool opaque : 1; - bool preserveAspect : 1; + QFxImage::FillMode fillMode; QFxImage::Status status; QUrl url; QUrl sciurl; diff --git a/src/declarative/fx/qfxitem.cpp b/src/declarative/fx/qfxitem.cpp index 5768684..0881f07 100644 --- a/src/declarative/fx/qfxitem.cpp +++ b/src/declarative/fx/qfxitem.cpp @@ -1640,28 +1640,17 @@ bool QFxItem::sceneEvent(QEvent *event) QVariant QFxItem::itemChange(GraphicsItemChange change, const QVariant &value) { - Q_D(QFxItem); - if (change == ItemSceneHasChanged) { - d->canvas = qvariant_cast<QGraphicsScene *>(value); - } else if (change == ItemChildAddedChange || - change == ItemChildRemovedChange) { - childrenChanged(); - } else if (change == ItemParentHasChanged) { + if (change == ItemParentHasChanged) { emit parentChanged(); } return QGraphicsItem::itemChange(change, value); } - void QFxItem::mouseUngrabEvent() { } -void QFxItem::childrenChanged() -{ -} - QRectF QFxItem::boundingRect() const { Q_D(const QFxItem); @@ -1896,8 +1885,13 @@ void QFxItem::setOptions(Options options, bool set) setFlag(QGraphicsItem::ItemAutoDetectsFocusProxy, d->options & IsFocusRealm); } -void QFxItem::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) +void QFxItem::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) +{ +} + +bool QFxItem::event(QEvent *ev) { + return QGraphicsObject::event(ev); } QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxitem.h b/src/declarative/fx/qfxitem.h index d33c07e..d9a7621 100644 --- a/src/declarative/fx/qfxitem.h +++ b/src/declarative/fx/qfxitem.h @@ -227,9 +227,9 @@ Q_SIGNALS: protected: bool isComponentComplete() const; - virtual void childrenChanged(); virtual bool sceneEventFilter(QGraphicsItem *, QEvent *); virtual bool sceneEvent(QEvent *); + virtual bool event(QEvent *); virtual QVariant itemChange(GraphicsItemChange, const QVariant &); virtual bool mouseFilter(QGraphicsSceneMouseEvent *); virtual void mouseUngrabEvent(); diff --git a/src/declarative/fx/qfxitem_p.h b/src/declarative/fx/qfxitem_p.h index 862171b..ae95fb5 100644 --- a/src/declarative/fx/qfxitem_p.h +++ b/src/declarative/fx/qfxitem_p.h @@ -77,7 +77,7 @@ public: _baselineOffset(0), _componentComplete(true), _keepMouse(false), _anchorLines(0), - _stateGroup(0), canvas(0), origin(QFxItem::TopLeft), + _stateGroup(0), origin(QFxItem::TopLeft), options(QFxItem::NoOption), widthValid(false), heightValid(false), width(0), height(0) {} @@ -176,9 +176,6 @@ public: QmlStateGroup *states(); QmlStateGroup *_stateGroup; - - QGraphicsScene *canvas; - QFxItem::TransformOrigin origin:4; int options:10; bool widthValid:1; diff --git a/src/declarative/fx/qfxlayouts.cpp b/src/declarative/fx/qfxlayouts.cpp index da34b8c..836e3e4 100644 --- a/src/declarative/fx/qfxlayouts.cpp +++ b/src/declarative/fx/qfxlayouts.cpp @@ -253,9 +253,15 @@ void QFxBaseLayout::componentComplete() preLayout(); } -void QFxBaseLayout::childrenChanged() +QVariant QFxBaseLayout::itemChange(GraphicsItemChange change, + const QVariant &value) { - preLayout(); + if (change == ItemChildAddedChange || + change == ItemChildRemovedChange) { + preLayout(); + } + + return QFxItem::itemChange(change, value); } bool QFxBaseLayout::event(QEvent *e) diff --git a/src/declarative/fx/qfxlayouts.h b/src/declarative/fx/qfxlayouts.h index 03ddf3e..e9c7f51 100644 --- a/src/declarative/fx/qfxlayouts.h +++ b/src/declarative/fx/qfxlayouts.h @@ -88,7 +88,7 @@ public: protected: virtual void componentComplete(); - virtual void childrenChanged(); + virtual QVariant itemChange(GraphicsItemChange, const QVariant &); virtual bool event(QEvent *); QSet<QFxItem *>* newItems(); QSet<QFxItem *>* leavingItems(); diff --git a/src/declarative/fx/qfxpixmap.cpp b/src/declarative/fx/qfxpixmap.cpp index e4d79f4..6647b21 100644 --- a/src/declarative/fx/qfxpixmap.cpp +++ b/src/declarative/fx/qfxpixmap.cpp @@ -87,6 +87,39 @@ public: QFxPixmapPrivate() {} QPixmap pixmap; + + bool readImage(QIODevice *dev) + { + QImageReader imgio(dev); + +//#define QT_TEST_SCALED_SIZE +#ifdef QT_TEST_SCALED_SIZE + /* + Some mechanism is needed for loading images at a limited size, especially + for remote images. Loading only thumbnails of remote progressive JPEG + images can be efficient. (Qt jpeg handler does not do so currently) + */ + + QSize limit(60,60); + QSize sz = imgio.size(); + if (sz.width() > limit.width() || sz.height() > limit.height()) { + sz.scale(limit,Qt::KeepAspectRatio); + imgio.setScaledSize(sz); + } +#endif + + QImage img; + if (imgio.read(&img)) { +#ifdef QT_TEST_SCALED_SIZE + if (!sz.isValid()) + img = img.scaled(limit,Qt::KeepAspectRatio); +#endif + pixmap = QPixmap::fromImage(img); + return true; + } else { + return false; + } + } }; /*! @@ -109,55 +142,31 @@ QFxPixmap::QFxPixmap(const QUrl &url) #ifdef Q_ENABLE_PERFORMANCE_LOG QFxPerfTimer<QFxPerf::PixmapLoad> perf; #endif + QString key = url.toString(); + if (!QPixmapCache::find(key,&d->pixmap)) { #ifndef QT_NO_LOCALFILE_OPTIMIZED_QML - if (url.scheme()==QLatin1String("file")) { - d->pixmap.load(url.toLocalFile()); - } else + if (url.scheme()==QLatin1String("file")) { + QFile f(url.toLocalFile()); + if (f.open(QIODevice::ReadOnly)) + if (!d->readImage(&f)) + qWarning() << "Format error loading" << url; + } else #endif - { - QString key = url.toString(); - if (!QPixmapCache::find(key,&d->pixmap)) { + { QFxSharedNetworkReplyHash::Iterator iter = qfxActiveNetworkReplies.find(key); if (iter == qfxActiveNetworkReplies.end()) { // API usage error qWarning() << "QFxPixmap: URL not loaded" << url; } else { - if ((*iter)->reply->error()) { + if ((*iter)->reply->error()) qWarning() << "Network error loading" << url << (*iter)->reply->errorString(); - } else { - QImageReader imgio((*iter)->reply); - -//#define QT_TEST_SCALED_SIZE -#ifdef QT_TEST_SCALED_SIZE - /* - Some mechanism is needed for loading images at a limited size, especially - for remote images. Loading only thumbnails of remote progressive JPEG - images can be efficient. (Qt jpeg handler does not do so currently) - */ - - QSize limit(60,60); - QSize sz = imgio.size(); - if (sz.width() > limit.width() || sz.height() > limit.height()) { - sz.scale(limit,Qt::KeepAspectRatio); - imgio.setScaledSize(sz); - } -#endif - - QImage img; - if (imgio.read(&img)) { -#ifdef QT_TEST_SCALED_SIZE - if (!sz.isValid()) - img = img.scaled(limit,Qt::KeepAspectRatio); -#endif - d->pixmap = QPixmap::fromImage(img); - QPixmapCache::insert(key, d->pixmap); - } else { + else + if (!d->readImage((*iter)->reply)) qWarning() << "Format error loading" << url; - } - } (*iter)->release(); } } + QPixmapCache::insert(key, d->pixmap); } } |