From d1bb572b9fb5b0286df992c8ae560d91c9dc3388 Mon Sep 17 00:00:00 2001 From: Ian Walters Date: Tue, 5 May 2009 15:46:31 +1000 Subject: Rename QFxImageItem to QFxPaintedItem. Done to avoid confusion with QFxImage and QFxAnimatedImage(Item) --- src/declarative/fx/fx.pri | 6 +- src/declarative/fx/qfximageitem.cpp | 346 ---------------------------------- src/declarative/fx/qfximageitem.h | 100 ---------- src/declarative/fx/qfximageitem_p.h | 95 ---------- src/declarative/fx/qfxpainteditem.cpp | 346 ++++++++++++++++++++++++++++++++++ src/declarative/fx/qfxpainteditem.h | 97 ++++++++++ src/declarative/fx/qfxpainteditem_p.h | 95 ++++++++++ src/declarative/fx/qfxtextedit.cpp | 20 +- src/declarative/fx/qfxtextedit.h | 4 +- src/declarative/fx/qfxtextedit_p.h | 4 +- src/declarative/fx/qfxwebview.cpp | 26 +-- src/declarative/fx/qfxwebview.h | 4 +- 12 files changed, 570 insertions(+), 573 deletions(-) delete mode 100644 src/declarative/fx/qfximageitem.cpp delete mode 100644 src/declarative/fx/qfximageitem.h delete mode 100644 src/declarative/fx/qfximageitem_p.h create mode 100644 src/declarative/fx/qfxpainteditem.cpp create mode 100644 src/declarative/fx/qfxpainteditem.h create mode 100644 src/declarative/fx/qfxpainteditem_p.h diff --git a/src/declarative/fx/fx.pri b/src/declarative/fx/fx.pri index 7e04459..90820fa 100644 --- a/src/declarative/fx/fx.pri +++ b/src/declarative/fx/fx.pri @@ -17,8 +17,8 @@ HEADERS += \ fx/qfxgridview.h \ fx/qfxhighlightfilter.h \ fx/qfximage.h \ - fx/qfximageitem.h \ - fx/qfximageitem_p.h \ + fx/qfxpainteditem.h \ + fx/qfxpainteditem_p.h \ fx/qfximage_p.h \ fx/qfxitem.h \ fx/qfxitem_p.h \ @@ -65,7 +65,7 @@ SOURCES += \ fx/qfxgridview.cpp \ fx/qfxhighlightfilter.cpp \ fx/qfximage.cpp \ - fx/qfximageitem.cpp \ + fx/qfxpainteditem.cpp \ fx/qfxitem.cpp \ fx/qfxkeyactions.cpp \ fx/qfxkeyproxy.cpp \ diff --git a/src/declarative/fx/qfximageitem.cpp b/src/declarative/fx/qfximageitem.cpp deleted file mode 100644 index d751845..0000000 --- a/src/declarative/fx/qfximageitem.cpp +++ /dev/null @@ -1,346 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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 either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qfximageitem.h" -#include "qfximageitem_p.h" - -#include -#include -#include -#include -#include -#include - -#if defined(QFX_RENDER_OPENGL2) -#include -#include -#endif - -QT_BEGIN_NAMESPACE - -/*! - \class QFxImageItem - \brief The QFxImageItem class is an abstract base class for QFxView items that want cached painting. - \ingroup group_coreitems - - This is a convenience class allowing easy use of cached painting within a custom - item. The contents of the item are are cached behind the scenes. - The dirtyCache() function should be called if the contents change to - ensure the cache is refreshed the next time painting occurs. - - To subclass QFxImageItem, you must reimplement drawContents() to draw - the contents of the item. -*/ - -/*! - \fn void QFxImageItem::drawContents(QPainter *painter, const QRect &rect) - - This function is called when the cache needs to be refreshed. When - sub-classing QFxImageItem this function should be implemented so as to - paint the contents of the item using the given \a painter for the - area of the contents specified by \a rect. -*/ - -/*! - \property QFxImageItem::contentsSize - \brief The size of the contents - - The contents size is the size of the item in regards to how it is painted - using the drawContents() function. This is distinct from the size of the - item in regards to height() and width(). -*/ - -/*! - \property QFxImageItem::smooth - \brief Setting for whether smooth scaling is enabled. -*/ - -/*! - Marks areas of the cache that intersect with the given \a rect as dirty and - in need of being refreshed. - - \sa clearCache() -*/ -void QFxImageItem::dirtyCache(const QRect& rect) -{ - Q_D(QFxImageItem); - for (int i=0; i < d->imagecache.count(); ) { - if (d->imagecache[i]->area.intersects(rect)) { - d->imagecache.removeAt(i); - } else { - ++i; - } - } -} - -/*! - Marks the entirety of the contents cache as dirty. - - \sa dirtyCache() -*/ -void QFxImageItem::clearCache() -{ - Q_D(QFxImageItem); - qDeleteAll(d->imagecache); - d->imagecache.clear(); -} - -/*! - Returns if smooth scaling of the cache contents is enabled. - - \sa setSmooth() -*/ -bool QFxImageItem::isSmooth() const -{ - Q_D(const QFxImageItem); - return d->smooth; -} - -/*! - Returns the size of the contents. - - \sa setContentsSize() -*/ -QSize QFxImageItem::contentsSize() const -{ - Q_D(const QFxImageItem); - return d->contentsSize; -} - -/*! - If \a smooth is true sets the image item to enable smooth scaling of - the cache contents. - - \sa isSmooth() -*/ -void QFxImageItem::setSmooth(bool smooth) -{ - Q_D(QFxImageItem); - if (d->smooth == smooth) return; - d->smooth = smooth; - clearCache(); - update(); -} - -/*! - Sets the size of the contents to the given \a size. - - \sa contentsSize() -*/ -void QFxImageItem::setContentsSize(const QSize &size) -{ - Q_D(QFxImageItem); - if (d->contentsSize == size) return; - d->contentsSize = size; - clearCache(); - update(); -} - -/*! - Constructs a new QFxImageItem with the given \a parent. -*/ -QFxImageItem::QFxImageItem(QFxItem *parent) - : QFxItem(*(new QFxImageItemPrivate), parent) -{ - init(); -} - -/*! - \internal - Constructs a new QFxImageItem with the given \a parent and - initialized private data member \a dd. -*/ -QFxImageItem::QFxImageItem(QFxImageItemPrivate &dd, QFxItem *parent) - : QFxItem(dd, parent) -{ - init(); -} - -/*! - Destroys the image item. -*/ -QFxImageItem::~QFxImageItem() -{ -} - -/*! - \internal -*/ -void QFxImageItem::init() -{ - connect(this,SIGNAL(widthChanged()),this,SLOT(clearCache())); - connect(this,SIGNAL(heightChanged()),this,SLOT(clearCache())); - connect(this,SIGNAL(visibleChanged()),this,SLOT(clearCache())); -} - -#if defined(QFX_RENDER_QPAINTER) -/*! - \reimp -*/ -void QFxImageItem::paintContents(QPainter &p) -#elif defined(QFX_RENDER_OPENGL) -/*! - \reimp -*/ -void QFxImageItem::paintGLContents(GLPainter &p) -#else -#error "What render?" -#endif -{ - Q_D(QFxImageItem); - const QRect content(QPoint(0,0),d->contentsSize); - if (content.width() <= 0 || content.height() <= 0) - return; - -#if defined(QFX_RENDER_QPAINTER) - bool oldAntiAliasing = p.testRenderHint(QPainter::Antialiasing); - bool oldSmoothPixmap = p.testRenderHint(QPainter::SmoothPixmapTransform); - if (d->smooth) { - p.setRenderHints(QPainter::Antialiasing, true); - p.setRenderHints(QPainter::SmoothPixmapTransform, true); - } - QRectF clipf = p.clipRegion().boundingRect(); - if (clipf.isEmpty()) - clipf = mapToScene(content); // ### Inefficient: Maps toScene and then fromScene - else - clipf = mapToScene(clipf); - -#elif defined(QFX_RENDER_OPENGL2) - p.useTextureShader(); - const QRectF clipf = p.sceneClipRect; - -#elif defined(QFX_RENDER_OPENGL1) - p.useTextureShader(); - const QRectF clipf = p.sceneClipRect; -#endif - - qreal hscale = widthValid() ? qreal(width()) / content.width() : heightValid() ? qreal(height()) / content.height() : 1.0; - qreal vscale = heightValid() ? qreal(height()) / content.height() : widthValid() ? qreal(width()) / content.width() : 1.0; - const QRect clip = mapFromScene(QRectF(clipf.x()/hscale,clipf.y()/vscale,clipf.width()/hscale,clipf.height()/vscale)).toRect(); - - QRegion topaint(clip); - topaint &= content; - QRegion uncached(content); - -#if defined(QFX_RENDER_OPENGL2) - glEnableVertexAttribArray(SingleTextureShader::Vertices); - glEnableVertexAttribArray(SingleTextureShader::TextureCoords); -#elif defined(QFX_RENDER_OPENGL1) - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); -#endif - - int cachesize=0; - for (int i=0; iimagecache.count(); ++i) { - QRect area = d->imagecache[i]->area; - if (topaint.contains(area)) { - QRectF target(area.x()*hscale, area.y()*vscale, area.width()*hscale, area.height()*vscale); - p.drawImage(target.toRect(), d->imagecache[i]->image); - topaint -= area; - d->imagecache[i]->age=0; - } else { - d->imagecache[i]->age++; - } - cachesize += area.width()*area.height(); - uncached -= area; - } - - if (!topaint.isEmpty()) { - // Find a sensible larger area, otherwise will paint lots of tiny images. - QRect biggerrect = topaint.boundingRect().adjusted(-64,-64,128,128); - cachesize += biggerrect.width() * biggerrect.height(); - while (d->imagecache.count() && cachesize > d->max_imagecache_size) { - int oldest=-1; - int age=-1; - for (int i=0; iimagecache.count(); ++i) { - int a = d->imagecache[i]->age; - if (a > age) { - oldest = i; - age = a; - } - } - cachesize -= d->imagecache[oldest]->area.width()*d->imagecache[oldest]->area.height(); - uncached += d->imagecache[oldest]->area; - d->imagecache.removeAt(oldest); - } - const QRegion bigger = QRegion(biggerrect) & uncached; - const QVector rects = bigger.rects(); - for (int i = 0; i < rects.count(); ++i) { - const QRect &r = rects.at(i); -#if defined(QFX_RENDER_QPAINTER) - QImage img(r.size(),QImage::Format_ARGB32_Premultiplied); -#else - QImage img(r.size(),QImage::Format_ARGB32); -#endif - img.fill(0); - { - QPainter qp(&img); - qp.translate(-r.x(),-r.y()); - drawContents(&qp, r); - } - QFxImageItemPrivate::ImageCacheItem *newitem = new QFxImageItemPrivate::ImageCacheItem; - newitem->area = r; -#if defined(QFX_RENDER_QPAINTER) - newitem->image = QSimpleCanvasConfig::Image(QSimpleCanvasConfig::toImage(img)); -#else - newitem->image.setImage(img); -#endif - d->imagecache.append(newitem); - QRectF target(r.x()*hscale, r.y()*vscale, r.width()*hscale, r.height()*vscale); - p.drawImage(target.toRect(), newitem->image); - } - } -#if defined(QFX_RENDER_OPENGL2) - glDisableVertexAttribArray(SingleTextureShader::Vertices); - glDisableVertexAttribArray(SingleTextureShader::TextureCoords); -#elif defined(QFX_RENDER_OPENGL1) - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); -#endif -#if defined(QFX_RENDER_QPAINTER) - if (d->smooth) { - p.setRenderHints(QPainter::Antialiasing, oldAntiAliasing); - p.setRenderHints(QPainter::SmoothPixmapTransform, oldSmoothPixmap); - } -#endif -} - -QT_END_NAMESPACE diff --git a/src/declarative/fx/qfximageitem.h b/src/declarative/fx/qfximageitem.h deleted file mode 100644 index 9ffd44e..0000000 --- a/src/declarative/fx/qfximageitem.h +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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 either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QFXIMAGEITEM_H -#define QFXIMAGEITEM_H - -#include -#include - - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -QT_MODULE(Declarative) -/* -WARNING: SHORT TERM CLASS. INTENDED TO MERGE INTO QFxPainted -*/ - -class QFxImageItemPrivate; -class Q_DECLARATIVE_EXPORT QFxImageItem : public QFxItem -{ - Q_OBJECT -public: - QFxImageItem(QFxItem *parent=0); - ~QFxImageItem(); - - Q_PROPERTY(QSize contentsSize READ contentsSize WRITE setContentsSize); - Q_PROPERTY(bool smooth READ isSmooth WRITE setSmooth); - -#if defined(QFX_RENDER_QPAINTER) - void paintContents(QPainter &painter); -#elif defined(QFX_RENDER_OPENGL) - void paintGLContents(GLPainter &); -#endif - - bool isSmooth() const; - QSize contentsSize() const; - - void setSmooth(bool); - void setContentsSize(const QSize &); -protected: - QFxImageItem(QFxImageItemPrivate &dd, QFxItem *parent); - - virtual void drawContents(QPainter *p, const QRect &) = 0; - -protected Q_SLOTS: - void dirtyCache(const QRect &); - void clearCache(); - -private: - void init(); - Q_DISABLE_COPY(QFxImageItem) - Q_DECLARE_PRIVATE(QFxImageItem) -}; -QML_DECLARE_TYPE(QFxImageItem); - - -QT_END_NAMESPACE - -QT_END_HEADER -#endif diff --git a/src/declarative/fx/qfximageitem_p.h b/src/declarative/fx/qfximageitem_p.h deleted file mode 100644 index 80450ec..0000000 --- a/src/declarative/fx/qfximageitem_p.h +++ /dev/null @@ -1,95 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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 either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QFXIMAGEITEM_P_H -#define QFXIMAGEITEM_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qfxitem_p.h" -#include - -#if defined(QFX_RENDER_OPENGL) -#include "gltexture.h" -#endif - -QT_BEGIN_NAMESPACE - -class QFxImageItemPrivate : public QFxItemPrivate -{ - Q_DECLARE_PUBLIC(QFxImageItem) - -public: - QFxImageItemPrivate() - : max_imagecache_size(1000*1000), smooth(false) - { - } - - struct ImageCacheItem { - ImageCacheItem() : age(0) {} - ~ImageCacheItem() { } - int age; - QRect area; -#if defined(QFX_RENDER_QPAINTER) - QSimpleCanvasConfig::Image image; -#else - GLTexture image; -#endif - }; - - QList imagecache; - - const int max_imagecache_size; - bool smooth; - QSize contentsSize; -}; - -QT_END_NAMESPACE -#endif diff --git a/src/declarative/fx/qfxpainteditem.cpp b/src/declarative/fx/qfxpainteditem.cpp new file mode 100644 index 0000000..950b468 --- /dev/null +++ b/src/declarative/fx/qfxpainteditem.cpp @@ -0,0 +1,346 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qfxpainteditem.h" +#include "qfxpainteditem_p.h" + +#include +#include +#include +#include +#include +#include + +#if defined(QFX_RENDER_OPENGL2) +#include +#include +#endif + +QT_BEGIN_NAMESPACE + +/*! + \class QFxPaintedItem + \brief The QFxPaintedItem class is an abstract base class for QFxView items that want cached painting. + \ingroup group_coreitems + + This is a convenience class for implementing items that paint their contents + using a QPainter. The contents of the item are are cached behind the scenes. + The dirtyCache() function should be called if the contents change to + ensure the cache is refreshed the next time painting occurs. + + To subclass QFxPaintedItem, you must reimplement drawContents() to draw + the contents of the item. +*/ + +/*! + \fn void QFxPaintedItem::drawContents(QPainter *painter, const QRect &rect) + + This function is called when the cache needs to be refreshed. When + sub-classing QFxPaintedItem this function should be implemented so as to + paint the contents of the item using the given \a painter for the + area of the contents specified by \a rect. +*/ + +/*! + \property QFxPaintedItem::contentsSize + \brief The size of the contents + + The contents size is the size of the item in regards to how it is painted + using the drawContents() function. This is distinct from the size of the + item in regards to height() and width(). +*/ + +/*! + \property QFxPaintedItem::smooth + \brief Setting for whether smooth scaling is enabled. +*/ + +/*! + Marks areas of the cache that intersect with the given \a rect as dirty and + in need of being refreshed. + + \sa clearCache() +*/ +void QFxPaintedItem::dirtyCache(const QRect& rect) +{ + Q_D(QFxPaintedItem); + for (int i=0; i < d->imagecache.count(); ) { + if (d->imagecache[i]->area.intersects(rect)) { + d->imagecache.removeAt(i); + } else { + ++i; + } + } +} + +/*! + Marks the entirety of the contents cache as dirty. + + \sa dirtyCache() +*/ +void QFxPaintedItem::clearCache() +{ + Q_D(QFxPaintedItem); + qDeleteAll(d->imagecache); + d->imagecache.clear(); +} + +/*! + Returns if smooth scaling of the cache contents is enabled. + + \sa setSmooth() +*/ +bool QFxPaintedItem::isSmooth() const +{ + Q_D(const QFxPaintedItem); + return d->smooth; +} + +/*! + Returns the size of the contents. + + \sa setContentsSize() +*/ +QSize QFxPaintedItem::contentsSize() const +{ + Q_D(const QFxPaintedItem); + return d->contentsSize; +} + +/*! + If \a smooth is true sets the image item to enable smooth scaling of + the cache contents. + + \sa isSmooth() +*/ +void QFxPaintedItem::setSmooth(bool smooth) +{ + Q_D(QFxPaintedItem); + if (d->smooth == smooth) return; + d->smooth = smooth; + clearCache(); + update(); +} + +/*! + Sets the size of the contents to the given \a size. + + \sa contentsSize() +*/ +void QFxPaintedItem::setContentsSize(const QSize &size) +{ + Q_D(QFxPaintedItem); + if (d->contentsSize == size) return; + d->contentsSize = size; + clearCache(); + update(); +} + +/*! + Constructs a new QFxPaintedItem with the given \a parent. +*/ +QFxPaintedItem::QFxPaintedItem(QFxItem *parent) + : QFxItem(*(new QFxPaintedItemPrivate), parent) +{ + init(); +} + +/*! + \internal + Constructs a new QFxPaintedItem with the given \a parent and + initialized private data member \a dd. +*/ +QFxPaintedItem::QFxPaintedItem(QFxPaintedItemPrivate &dd, QFxItem *parent) + : QFxItem(dd, parent) +{ + init(); +} + +/*! + Destroys the image item. +*/ +QFxPaintedItem::~QFxPaintedItem() +{ +} + +/*! + \internal +*/ +void QFxPaintedItem::init() +{ + connect(this,SIGNAL(widthChanged()),this,SLOT(clearCache())); + connect(this,SIGNAL(heightChanged()),this,SLOT(clearCache())); + connect(this,SIGNAL(visibleChanged()),this,SLOT(clearCache())); +} + +#if defined(QFX_RENDER_QPAINTER) +/*! + \reimp +*/ +void QFxPaintedItem::paintContents(QPainter &p) +#elif defined(QFX_RENDER_OPENGL) +/*! + \reimp +*/ +void QFxPaintedItem::paintGLContents(GLPainter &p) +#else +#error "What render?" +#endif +{ + Q_D(QFxPaintedItem); + const QRect content(QPoint(0,0),d->contentsSize); + if (content.width() <= 0 || content.height() <= 0) + return; + +#if defined(QFX_RENDER_QPAINTER) + bool oldAntiAliasing = p.testRenderHint(QPainter::Antialiasing); + bool oldSmoothPixmap = p.testRenderHint(QPainter::SmoothPixmapTransform); + if (d->smooth) { + p.setRenderHints(QPainter::Antialiasing, true); + p.setRenderHints(QPainter::SmoothPixmapTransform, true); + } + QRectF clipf = p.clipRegion().boundingRect(); + if (clipf.isEmpty()) + clipf = mapToScene(content); // ### Inefficient: Maps toScene and then fromScene + else + clipf = mapToScene(clipf); + +#elif defined(QFX_RENDER_OPENGL2) + p.useTextureShader(); + const QRectF clipf = p.sceneClipRect; + +#elif defined(QFX_RENDER_OPENGL1) + p.useTextureShader(); + const QRectF clipf = p.sceneClipRect; +#endif + + qreal hscale = widthValid() ? qreal(width()) / content.width() : heightValid() ? qreal(height()) / content.height() : 1.0; + qreal vscale = heightValid() ? qreal(height()) / content.height() : widthValid() ? qreal(width()) / content.width() : 1.0; + const QRect clip = mapFromScene(QRectF(clipf.x()/hscale,clipf.y()/vscale,clipf.width()/hscale,clipf.height()/vscale)).toRect(); + + QRegion topaint(clip); + topaint &= content; + QRegion uncached(content); + +#if defined(QFX_RENDER_OPENGL2) + glEnableVertexAttribArray(SingleTextureShader::Vertices); + glEnableVertexAttribArray(SingleTextureShader::TextureCoords); +#elif defined(QFX_RENDER_OPENGL1) + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); +#endif + + int cachesize=0; + for (int i=0; iimagecache.count(); ++i) { + QRect area = d->imagecache[i]->area; + if (topaint.contains(area)) { + QRectF target(area.x()*hscale, area.y()*vscale, area.width()*hscale, area.height()*vscale); + p.drawImage(target.toRect(), d->imagecache[i]->image); + topaint -= area; + d->imagecache[i]->age=0; + } else { + d->imagecache[i]->age++; + } + cachesize += area.width()*area.height(); + uncached -= area; + } + + if (!topaint.isEmpty()) { + // Find a sensible larger area, otherwise will paint lots of tiny images. + QRect biggerrect = topaint.boundingRect().adjusted(-64,-64,128,128); + cachesize += biggerrect.width() * biggerrect.height(); + while (d->imagecache.count() && cachesize > d->max_imagecache_size) { + int oldest=-1; + int age=-1; + for (int i=0; iimagecache.count(); ++i) { + int a = d->imagecache[i]->age; + if (a > age) { + oldest = i; + age = a; + } + } + cachesize -= d->imagecache[oldest]->area.width()*d->imagecache[oldest]->area.height(); + uncached += d->imagecache[oldest]->area; + d->imagecache.removeAt(oldest); + } + const QRegion bigger = QRegion(biggerrect) & uncached; + const QVector rects = bigger.rects(); + for (int i = 0; i < rects.count(); ++i) { + const QRect &r = rects.at(i); +#if defined(QFX_RENDER_QPAINTER) + QImage img(r.size(),QImage::Format_ARGB32_Premultiplied); +#else + QImage img(r.size(),QImage::Format_ARGB32); +#endif + img.fill(0); + { + QPainter qp(&img); + qp.translate(-r.x(),-r.y()); + drawContents(&qp, r); + } + QFxPaintedItemPrivate::ImageCacheItem *newitem = new QFxPaintedItemPrivate::ImageCacheItem; + newitem->area = r; +#if defined(QFX_RENDER_QPAINTER) + newitem->image = QSimpleCanvasConfig::Image(QSimpleCanvasConfig::toImage(img)); +#else + newitem->image.setImage(img); +#endif + d->imagecache.append(newitem); + QRectF target(r.x()*hscale, r.y()*vscale, r.width()*hscale, r.height()*vscale); + p.drawImage(target.toRect(), newitem->image); + } + } +#if defined(QFX_RENDER_OPENGL2) + glDisableVertexAttribArray(SingleTextureShader::Vertices); + glDisableVertexAttribArray(SingleTextureShader::TextureCoords); +#elif defined(QFX_RENDER_OPENGL1) + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); +#endif +#if defined(QFX_RENDER_QPAINTER) + if (d->smooth) { + p.setRenderHints(QPainter::Antialiasing, oldAntiAliasing); + p.setRenderHints(QPainter::SmoothPixmapTransform, oldSmoothPixmap); + } +#endif +} + +QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxpainteditem.h b/src/declarative/fx/qfxpainteditem.h new file mode 100644 index 0000000..015a035 --- /dev/null +++ b/src/declarative/fx/qfxpainteditem.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QFXIMAGEITEM_H +#define QFXIMAGEITEM_H + +#include +#include + + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QFxPaintedItemPrivate; +class Q_DECLARATIVE_EXPORT QFxPaintedItem : public QFxItem +{ + Q_OBJECT +public: + QFxPaintedItem(QFxItem *parent=0); + ~QFxPaintedItem(); + + Q_PROPERTY(QSize contentsSize READ contentsSize WRITE setContentsSize); + Q_PROPERTY(bool smooth READ isSmooth WRITE setSmooth); + +#if defined(QFX_RENDER_QPAINTER) + void paintContents(QPainter &painter); +#elif defined(QFX_RENDER_OPENGL) + void paintGLContents(GLPainter &); +#endif + + bool isSmooth() const; + QSize contentsSize() const; + + void setSmooth(bool); + void setContentsSize(const QSize &); +protected: + QFxPaintedItem(QFxPaintedItemPrivate &dd, QFxItem *parent); + + virtual void drawContents(QPainter *p, const QRect &) = 0; + +protected Q_SLOTS: + void dirtyCache(const QRect &); + void clearCache(); + +private: + void init(); + Q_DISABLE_COPY(QFxPaintedItem) + Q_DECLARE_PRIVATE(QFxPaintedItem) +}; +QML_DECLARE_TYPE(QFxPaintedItem); + + +QT_END_NAMESPACE + +QT_END_HEADER +#endif diff --git a/src/declarative/fx/qfxpainteditem_p.h b/src/declarative/fx/qfxpainteditem_p.h new file mode 100644 index 0000000..b0432ac --- /dev/null +++ b/src/declarative/fx/qfxpainteditem_p.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QFXIMAGEITEM_P_H +#define QFXIMAGEITEM_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qfxitem_p.h" +#include + +#if defined(QFX_RENDER_OPENGL) +#include "gltexture.h" +#endif + +QT_BEGIN_NAMESPACE + +class QFxPaintedItemPrivate : public QFxItemPrivate +{ + Q_DECLARE_PUBLIC(QFxPaintedItem) + +public: + QFxPaintedItemPrivate() + : max_imagecache_size(1000*1000), smooth(false) + { + } + + struct ImageCacheItem { + ImageCacheItem() : age(0) {} + ~ImageCacheItem() { } + int age; + QRect area; +#if defined(QFX_RENDER_QPAINTER) + QSimpleCanvasConfig::Image image; +#else + GLTexture image; +#endif + }; + + QList imagecache; + + const int max_imagecache_size; + bool smooth; + QSize contentsSize; +}; + +QT_END_NAMESPACE +#endif diff --git a/src/declarative/fx/qfxtextedit.cpp b/src/declarative/fx/qfxtextedit.cpp index 84e209b..a1b5484 100644 --- a/src/declarative/fx/qfxtextedit.cpp +++ b/src/declarative/fx/qfxtextedit.cpp @@ -103,7 +103,7 @@ TextEdit { Constructs a new QFxTextEdit. */ QFxTextEdit::QFxTextEdit(QFxItem *parent) -: QFxImageItem(*(new QFxTextEditPrivate), parent) +: QFxPaintedItem(*(new QFxTextEditPrivate), parent) { Q_D(QFxTextEdit); d->init(); @@ -113,7 +113,7 @@ QFxTextEdit::QFxTextEdit(QFxItem *parent) \internal */ QFxTextEdit::QFxTextEdit(QFxTextEditPrivate &dd, QFxItem *parent) - : QFxImageItem(dd, parent) + : QFxPaintedItem(dd, parent) { Q_D(QFxTextEdit); d->init(); @@ -383,7 +383,7 @@ void QFxTextEdit::geometryChanged(const QRectF &newGeometry, { if (newGeometry.width() != oldGeometry.width()) updateSize(); - QFxImageItem::geometryChanged(newGeometry, oldGeometry); + QFxPaintedItem::geometryChanged(newGeometry, oldGeometry); } /*! @@ -393,7 +393,7 @@ void QFxTextEdit::dump(int depth) { QByteArray ba(depth * 4, ' '); qWarning() << ba.constData() << propertyInfo(); - QFxImageItem::dump(depth); + QFxPaintedItem::dump(depth); } /*! @@ -412,7 +412,7 @@ QString QFxTextEdit::propertyInfo() const void QFxTextEdit::componentComplete() { Q_D(QFxTextEdit); - QFxImageItem::componentComplete(); + QFxPaintedItem::componentComplete(); if (d->dirty) { updateSize(); d->dirty = false; @@ -588,7 +588,7 @@ Handles the given focus \a event. void QFxTextEdit::focusInEvent(QFocusEvent *event) { Q_D(QFxTextEdit); - QFxImageItem::focusInEvent(event); + QFxPaintedItem::focusInEvent(event); d->control->processEvent(event, QPointF(0, 0)); } @@ -599,7 +599,7 @@ Handles the given focus \a event. void QFxTextEdit::focusOutEvent(QFocusEvent *event) { Q_D(QFxTextEdit); - QFxImageItem::focusOutEvent(event); + QFxPaintedItem::focusOutEvent(event); d->control->processEvent(event, QPointF(0, 0)); } @@ -638,7 +638,7 @@ void QFxTextEdit::mousePressEvent(QGraphicsSceneMouseEvent *event) event->setAccepted(me->isAccepted()); delete me; if (!event->isAccepted()) - QFxImageItem::mousePressEvent(event); + QFxPaintedItem::mousePressEvent(event); } /*! @@ -653,7 +653,7 @@ void QFxTextEdit::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) event->setAccepted(me->isAccepted()); delete me; if (!event->isAccepted()) - QFxImageItem::mousePressEvent(event); + QFxPaintedItem::mousePressEvent(event); } /*! @@ -668,7 +668,7 @@ void QFxTextEdit::mouseMoveEvent(QGraphicsSceneMouseEvent *event) event->setAccepted(me->isAccepted()); delete me; if (!event->isAccepted()) - QFxImageItem::mousePressEvent(event); + QFxPaintedItem::mousePressEvent(event); } /*! diff --git a/src/declarative/fx/qfxtextedit.h b/src/declarative/fx/qfxtextedit.h index be84a3c..b017635 100644 --- a/src/declarative/fx/qfxtextedit.h +++ b/src/declarative/fx/qfxtextedit.h @@ -43,7 +43,7 @@ #define QFXTEXTEDIT_H #include -#include +#include #include #include @@ -60,7 +60,7 @@ QT_MODULE(Declarative) WARNING: SHORT TERM CLASS. INTENDED TO MERGE INTO QFxTextItem */ class QFxTextEditPrivate; -class Q_DECLARATIVE_EXPORT QFxTextEdit : public QFxImageItem +class Q_DECLARATIVE_EXPORT QFxTextEdit : public QFxPaintedItem { Q_OBJECT Q_ENUMS(VAlignment) diff --git a/src/declarative/fx/qfxtextedit_p.h b/src/declarative/fx/qfxtextedit_p.h index aa07484..b583dbe 100644 --- a/src/declarative/fx/qfxtextedit_p.h +++ b/src/declarative/fx/qfxtextedit_p.h @@ -54,7 +54,7 @@ // #include "qfxitem.h" -#include "qfximageitem_p.h" +#include "qfxpainteditem_p.h" #include "qml.h" @@ -62,7 +62,7 @@ QT_BEGIN_NAMESPACE class QTextLayout; class QTextDocument; class QTextControl; -class QFxTextEditPrivate : public QFxImageItemPrivate +class QFxTextEditPrivate : public QFxPaintedItemPrivate { Q_DECLARE_PUBLIC(QFxTextEdit) diff --git a/src/declarative/fx/qfxwebview.cpp b/src/declarative/fx/qfxwebview.cpp index 9b18a9e..3f05846 100644 --- a/src/declarative/fx/qfxwebview.cpp +++ b/src/declarative/fx/qfxwebview.cpp @@ -68,7 +68,7 @@ #include "qfxwebview.h" #include -#include +#include QT_BEGIN_NAMESPACE QML_DEFINE_TYPE(QFxWebView,WebView); @@ -142,13 +142,13 @@ public: }; -class QFxWebViewPrivate : public QFxImageItemPrivate +class QFxWebViewPrivate : public QFxPaintedItemPrivate { Q_DECLARE_PUBLIC(QFxWebView) public: QFxWebViewPrivate() - : QFxImageItemPrivate(), page(0), idealwidth(0), idealheight(0), interactive(true), lastPress(0), lastRelease(0), mouseX(0), mouseY(0), + : QFxPaintedItemPrivate(), page(0), idealwidth(0), idealheight(0), interactive(true), lastPress(0), lastRelease(0), mouseX(0), mouseY(0), max_imagecache_size(100000), progress(1.0), pending(PendingNone) { } @@ -253,13 +253,13 @@ public: */ QFxWebView::QFxWebView(QFxItem *parent) - : QFxImageItem(*(new QFxWebViewPrivate), parent) + : QFxPaintedItem(*(new QFxWebViewPrivate), parent) { init(); } QFxWebView::QFxWebView(QFxWebViewPrivate &dd, QFxItem *parent) - : QFxImageItem(dd, parent) + : QFxPaintedItem(dd, parent) { init(); } @@ -283,7 +283,7 @@ void QFxWebView::init() void QFxWebView::componentComplete() { - QFxImageItem::componentComplete(); + QFxPaintedItem::componentComplete(); Q_D(QFxWebView); switch (d->pending) { case QFxWebViewPrivate::PendingUrl: @@ -491,7 +491,7 @@ void QFxWebView::geometryChanged(const QRectF &newGeometry, { if (newGeometry.size() != oldGeometry.size()) expandToWebPage(); - QFxImageItem::geometryChanged(newGeometry, oldGeometry); + QFxPaintedItem::geometryChanged(newGeometry, oldGeometry); } void QFxWebView::paintPage(const QRect& r) @@ -554,7 +554,7 @@ void QFxWebView::dump(int depth) { QByteArray ba(depth * 4, ' '); qWarning() << ba.constData() << "url:" << url(); - QFxImageItem::dump(depth); + QFxPaintedItem::dump(depth); } void QFxWebView::drawContents(QPainter *p, const QRect &r) @@ -656,7 +656,7 @@ void QFxWebView::mousePressEvent(QGraphicsSceneMouseEvent *event) event->setAccepted(false); } if (!event->isAccepted()) - QFxImageItem::mousePressEvent(event); + QFxPaintedItem::mousePressEvent(event); } void QFxWebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) @@ -670,7 +670,7 @@ void QFxWebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) event->setAccepted(false); } if (!event->isAccepted()) - QFxImageItem::mouseReleaseEvent(event); + QFxPaintedItem::mouseReleaseEvent(event); } void QFxWebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event) @@ -691,7 +691,7 @@ void QFxWebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event) event->setAccepted(false); } if (!event->isAccepted()) - QFxImageItem::mouseMoveEvent(event); + QFxPaintedItem::mouseMoveEvent(event); } void QFxWebView::keyPressEvent(QKeyEvent* event) @@ -700,7 +700,7 @@ void QFxWebView::keyPressEvent(QKeyEvent* event) if (d->interactive) page()->event(event); if (!event->isAccepted()) - QFxImageItem::keyPressEvent(event); + QFxPaintedItem::keyPressEvent(event); } void QFxWebView::keyReleaseEvent(QKeyEvent* event) @@ -709,7 +709,7 @@ void QFxWebView::keyReleaseEvent(QKeyEvent* event) if (d->interactive) page()->event(event); if (!event->isAccepted()) - QFxImageItem::keyReleaseEvent(event); + QFxPaintedItem::keyReleaseEvent(event); } /*! diff --git a/src/declarative/fx/qfxwebview.h b/src/declarative/fx/qfxwebview.h index 17037ca..afd5b0f 100644 --- a/src/declarative/fx/qfxwebview.h +++ b/src/declarative/fx/qfxwebview.h @@ -45,7 +45,7 @@ #include #include #include -#include +#include #include #include @@ -74,7 +74,7 @@ private: }; -class Q_DECLARATIVE_EXPORT QFxWebView : public QFxImageItem +class Q_DECLARATIVE_EXPORT QFxWebView : public QFxPaintedItem { Q_OBJECT -- cgit v0.12