diff options
author | Jens Bache-Wiig <jbache@trolltech.com> | 2010-05-14 16:43:20 (GMT) |
---|---|---|
committer | Jens Bache-Wiig <jbache@trolltech.com> | 2010-05-14 16:49:02 (GMT) |
commit | c8d2e18c4b431870560f324a17f20904bb47ae68 (patch) | |
tree | 9999c914c99f4dd123012168cef7dd5854d3eb77 /src | |
parent | 25484ef9b55f0a017163a05228bb9b6b0e7a772d (diff) | |
download | Qt-c8d2e18c4b431870560f324a17f20904bb47ae68.zip Qt-c8d2e18c4b431870560f324a17f20904bb47ae68.tar.gz Qt-c8d2e18c4b431870560f324a17f20904bb47ae68.tar.bz2 |
More improvements to pixmap cache key generation
HexString is actually more optimal than fromRawData since we dont
need any allocations while using it. Arguably the code is also
a bit nicer.
Task-number: QTBUG-9850
Reviewed-by: ogoffart
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/image/qicon.cpp | 26 | ||||
-rw-r--r-- | src/gui/styles/qgtkpainter.cpp | 19 | ||||
-rw-r--r-- | src/gui/styles/qstylehelper.cpp | 30 | ||||
-rw-r--r-- | src/gui/styles/qstylehelper_p.h | 32 |
4 files changed, 57 insertions, 50 deletions
diff --git a/src/gui/image/qicon.cpp b/src/gui/image/qicon.cpp index 9f1eea2..7696632 100644 --- a/src/gui/image/qicon.cpp +++ b/src/gui/image/qicon.cpp @@ -55,6 +55,7 @@ #include "qcache.h" #include "qdebug.h" #include "private/qguiplatformplugin_p.h" +#include "private/qstylehelper_p.h" #ifdef Q_WS_MAC #include <private/qt_mac_p.h> @@ -261,24 +262,17 @@ QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::St if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height())) actualSize.scale(size, Qt::KeepAspectRatio); - int digits = sizeof(qint64)/sizeof(QChar); - quint64 tkey = pm.cacheKey(), - tmode = pe->mode, - tpalkey = QApplication::palette().cacheKey(), - twidth = actualSize.width(), - theight = actualSize.height(); - QString key = QLatin1Literal("qt_") - % QString::fromRawData((QChar*)&tkey, digits) - % QString::fromRawData((QChar*)&tmode, digits) - % QString::fromRawData((QChar*)&tpalkey, digits) - % QString::fromRawData((QChar*)&twidth, digits) - % QString::fromRawData((QChar*)&theight, digits); + % HexString<quint64>(pm.cacheKey()) + % HexString<uint>(pe->mode) + % HexString<quint64>(QApplication::palette().cacheKey()) + % HexString<uint>(actualSize.width()) + % HexString<uint>(actualSize.height()); if (mode == QIcon::Active) { - if (QPixmapCache::find(key + QString::number(mode), pm)) + if (QPixmapCache::find(key % HexString<uint>(mode), pm)) return pm; // horray - if (QPixmapCache::find(key + QString::number(QIcon::Normal), pm)) { + if (QPixmapCache::find(key % HexString<uint>(QIcon::Normal), pm)) { QStyleOption opt(0); opt.palette = QApplication::palette(); QPixmap active = QApplication::style()->generatedIconPixmap(QIcon::Active, pm, &opt); @@ -287,7 +281,7 @@ QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::St } } - if (!QPixmapCache::find(key + QString::number(mode), pm)) { + if (!QPixmapCache::find(key % HexString<uint>(mode), pm)) { if (pm.size() != actualSize) pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); if (pe->mode != mode && mode != QIcon::Normal) { @@ -297,7 +291,7 @@ QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::St if (!generated.isNull()) pm = generated; } - QPixmapCache::insert(key + QString::number(mode), pm); + QPixmapCache::insert(key % HexString<uint>(mode), pm); } return pm; } diff --git a/src/gui/styles/qgtkpainter.cpp b/src/gui/styles/qgtkpainter.cpp index a6686e2..79c53e9 100644 --- a/src/gui/styles/qgtkpainter.cpp +++ b/src/gui/styles/qgtkpainter.cpp @@ -47,6 +47,7 @@ // This class is primarily a wrapper around the gtk painter functions // and takes care of converting all such calls into cached Qt pixmaps. +#include <private/qstylehelper_p.h> #include <QtGui/QWidget> #include <QtGui/QStyleOption> #include <QtGui/QPixmapCache> @@ -154,21 +155,13 @@ QGtkPainter::QGtkPainter(QPainter *_painter) static QString uniqueName(const QString &key, GtkStateType state, GtkShadowType shadow, const QSize &size, GtkWidget *widget = 0) { - - int digits = sizeof(qint64)/sizeof(QChar); - quint64 tstate= state, - tshadow = shadow, - twidth = size.width(), - theight = size.height(), - twidget = quint64(widget); - // Note the widget arg should ideally use the widget path, though would compromise performance QString tmp = key - % QString::fromRawData((QChar*)&tstate, digits) - % QString::fromRawData((QChar*)&tshadow, digits) - % QString::fromRawData((QChar*)&twidth, digits) - % QString::fromRawData((QChar*)&theight, digits) - % QString::fromRawData((QChar*)&twidget, digits); + % HexString<uint>(state) + % HexString<uint>(shadow) + % HexString<uint>(size.width()) + % HexString<uint>(size.height()) + % HexString<quint64>(quint64(widget)); return tmp; } diff --git a/src/gui/styles/qstylehelper.cpp b/src/gui/styles/qstylehelper.cpp index 22f2014..d09d7fa 100644 --- a/src/gui/styles/qstylehelper.cpp +++ b/src/gui/styles/qstylehelper.cpp @@ -63,30 +63,18 @@ namespace QStyleHelper { QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size) { const QStyleOptionComplex *complexOption = qstyleoption_cast<const QStyleOptionComplex *>(option); - int digits = sizeof(qint64)/sizeof(QChar); - quint64 state = option->state, - direction = option->direction, - subcontrols = (complexOption ? uint(complexOption->activeSubControls) : 0u), - palettekey = option->palette.cacheKey(), - width = size.width(), - height = size.height(); - - QString tmp = key % QString::fromRawData((QChar*)&state, digits) - % QString::fromRawData((QChar*)&direction, digits) - % QString::fromRawData((QChar*)&subcontrols, digits) - % QString::fromRawData((QChar*)&palettekey, digits) - % QString::fromRawData((QChar*)&width, digits) - % QString::fromRawData((QChar*)&height, digits); + QString tmp = key % HexString<uint>(option->state) + % HexString<uint>(option->direction) + % HexString<uint>(complexOption ? uint(complexOption->activeSubControls) : 0u) + % HexString<quint64>(option->palette.cacheKey()) + % HexString<uint>(size.width()) + % HexString<uint>(size.height()); #ifndef QT_NO_SPINBOX if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option)) { - quint64 buttonsymbols = spinBox->buttonSymbols, - stepEnabled = spinBox->stepEnabled, - frame = spinBox->frame; - - tmp = tmp % QString::fromRawData((QChar*)&buttonsymbols, digits) - % QString::fromRawData((QChar*)&stepEnabled, digits) - % QString::fromRawData((QChar*)&frame, digits); + tmp = tmp % HexString<uint>(spinBox->buttonSymbols) + % HexString<uint>(spinBox->stepEnabled) + % QLatin1Char(spinBox->frame ? '1' : '0'); ; } #endif // QT_NO_SPINBOX return tmp; diff --git a/src/gui/styles/qstylehelper_p.h b/src/gui/styles/qstylehelper_p.h index 31cc4ed..555ad60 100644 --- a/src/gui/styles/qstylehelper_p.h +++ b/src/gui/styles/qstylehelper_p.h @@ -41,6 +41,7 @@ #include <QtCore/qglobal.h> #include <QtCore/qpoint.h> +#include <QtCore/qstring.h> #include <QtGui/qpolygon.h> #ifndef QSTYLEHELPER_P_H @@ -79,6 +80,37 @@ namespace QStyleHelper int bottom = 0); } +// internal helper. Converts an integer value to an unique string token +template <typename T> + struct HexString +{ + inline HexString(const T t) + : val(t) + {} + + inline void write(QChar *&dest) const + { + const ushort hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + const char *c = reinterpret_cast<const char *>(&val); + for (uint i = 0; i < sizeof(T); ++i) { + *dest++ = hexChars[*c & 0xf]; + *dest++ = hexChars[(*c & 0xf0) >> 4]; + ++c; + } + } + const T val; +}; + +// specialization to enable fast concatenating of our string tokens to a string +template <typename T> + struct QConcatenable<HexString<T> > +{ + typedef HexString<T> type; + enum { ExactSize = true }; + static int size(const HexString<T> &str) { return sizeof(str.val) * 2; } + static inline void appendTo(const HexString<T> &str, QChar *&out) { str.write(out); } +}; + QT_END_NAMESPACE #endif // QSTYLEHELPER_P_H |