diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2014-02-05 15:15:01 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-02-16 00:18:32 +0100 |
commit | d24cceebf2bad8e1172cf992644263cba2098501 (patch) | |
tree | 72c53b44a5569190179829b51412f6510c7dd5b1 /src | |
parent | 6afc34a72f1fbf4b8a379228c64f9a34a1c4433f (diff) |
QFont: add missing qHash overload
The properties that make up the hash value are chosen to be the same as those
that make up QFontDef's op<() and op==(). Indeed, the implementation for QFont
simply delegates to the one of QFontDef, which has been added for this purpose,
but may prove useful in its own right down the line.
The code would greatly benefit from a qHash(qreal) implementation. Lacking
this, the patch uses multiplication with 10000 and qRound64() to convert
the one floating-point property used in the hash to an integer. This is
probably the right thing to do anyway, to avoid epsilon problems.
[ChangeLog][QtGui][QFont] Added qHash overload for this class.
[ChangeLog][QtCore][QHash] Allowed QFont to be used as a key in QHash/QSet.
Change-Id: I2c1cb5d9da53e26cb2c0f1a7c357731e73eea78e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/text/qfont.cpp | 12 | ||||
-rw-r--r-- | src/gui/text/qfont.h | 2 | ||||
-rw-r--r-- | src/gui/text/qfont_p.h | 17 |
3 files changed, 31 insertions, 0 deletions
diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index 12761d0d3e..83f2d7190b 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -2073,6 +2073,18 @@ QString QFont::toString() const QString::number((int) rawMode()); } +/*! + Returns the hash value for \a font. If specified, \a seed is used + to initialize the hash. + + \relates QFont + \since 5.3 +*/ +uint qHash(const QFont &font, uint seed) Q_DECL_NOTHROW +{ + return qHash(QFontPrivate::get(font)->request, seed); +} + /*! Sets this font to match the description \a descrip. The description diff --git a/src/gui/text/qfont.h b/src/gui/text/qfont.h index 6d36f7839b..a207a1d60e 100644 --- a/src/gui/text/qfont.h +++ b/src/gui/text/qfont.h @@ -315,6 +315,8 @@ private: Q_DECLARE_SHARED(QFont) +Q_GUI_EXPORT uint qHash(const QFont &font, uint seed = 0) Q_DECL_NOTHROW; + inline bool QFont::bold() const { return weight() > Normal; } diff --git a/src/gui/text/qfont_p.h b/src/gui/text/qfont_p.h index 5b7f918e21..6165554388 100644 --- a/src/gui/text/qfont_p.h +++ b/src/gui/text/qfont_p.h @@ -55,6 +55,7 @@ #include "QtGui/qfont.h" #include "QtCore/qmap.h" +#include "QtCore/qhash.h" #include "QtCore/qobject.h" #include "QtCore/qstringlist.h" #include <QtGui/qfontdatabase.h> @@ -133,6 +134,22 @@ struct QFontDef } }; +inline uint qHash(const QFontDef &fd, uint seed = 0) Q_DECL_NOTHROW +{ + return qHash(qRound64(fd.pixelSize*10000)) // use only 4 fractional digits + ^ qHash(fd.weight) + ^ qHash(fd.style) + ^ qHash(fd.stretch) + ^ qHash(fd.styleHint) + ^ qHash(fd.styleStrategy) + ^ qHash(fd.ignorePitch) + ^ qHash(fd.fixedPitch) + ^ qHash(fd.family, seed) + ^ qHash(fd.styleName) + ^ qHash(fd.hintingPreference) + ; +} + class QFontEngineData { public: |