Git commit f00115a9bb0ddf48d0a34b737583e9d72c5013dd by Martin Klapetek. Committed on 17/05/2016 at 23:55. Pushed by mklapetek into branch 'master'. Add support for custom emoticon sizes Right now KEmoticonTheme::parse always returns the emoticons sized by the actual image file size, but there are cases where different size is needed, for example when parsing emoticons inline in a text area while typing. This also allows to ship bigger emoticon images and have them scaled down as/when needed. REVIEW: 127923 M +32 -0 autotests/kemoticontest.cpp M +14 -0 src/core/kemoticons.cpp M +19 -0 src/core/kemoticons.h M +15 -1 src/core/kemoticonsprovider.cpp M +19 -0 src/core/kemoticonsprovider.h http://commits.kde.org/kemoticons/f00115a9bb0ddf48d0a34b737583e9d72c5013dd diff --git a/autotests/kemoticontest.cpp b/autotests/kemoticontest.cpp index 4e0e539..fdada9d 100644 --- a/autotests/kemoticontest.cpp +++ b/autotests/kemoticontest.cpp @@ -156,6 +156,38 @@ private Q_SLOTS: } } = + void testPreferredSizes_data() + { + QTest::addColumn("themeName"); + + QString basePath =3D QFINDTESTDATA("emoticon-parser-testcases"); + QVERIFY(!basePath.isEmpty()); + QDir testCasesDir(basePath); + + QStringList inputFileNames =3D testCasesDir.entryList(QStringList(= QStringLiteral("*.input"))); + Q_FOREACH (const QString &fileName, inputFileNames) { + QString outputFileName =3D fileName; + outputFileName.replace(QStringLiteral("input"), QStringLiteral= ("output")); + const QString baseName =3D fileName.section(QLatin1Char('-'), = 0, 0); + QTest::newRow(qPrintable(fileName.left(fileName.lastIndexOf('.= ')))) + << (baseName =3D=3D QLatin1String("xmpp") ? "xmpp-testtheme" := default_theme); + } + } + + void testPreferredSizes() + { + QFETCH(QString, themeName); + KEmoticons kemoticons; + kemoticons.setPreferredEmoticonSize(QSize(99, 77)); + + KEmoticonsTheme theme =3D kemoticons.theme(themeName); + + const QString parsed =3D theme.parseEmoticons(":)"); + + QVERIFY(parsed.contains(QStringLiteral("width=3D\"99\""))); + QVERIFY(parsed.contains(QStringLiteral("height=3D\"77\""))); + } + private: QString themePath; }; diff --git a/src/core/kemoticons.cpp b/src/core/kemoticons.cpp index 9756c1f..0e216af 100644 --- a/src/core/kemoticons.cpp +++ b/src/core/kemoticons.cpp @@ -46,6 +46,7 @@ public: QHash m_themes; QFileSystemWatcher m_fileWatcher; KEmoticons *q; + QSize m_preferredSize; = //private slots void changeTheme(const QString &path); @@ -103,6 +104,9 @@ KEmoticonsTheme KEmoticonsPrivate::loadTheme(const QStr= ing &name) if (QFile::exists(path)) { KEmoticonsProvider *provider =3D loadProvider(m_loaded.at(i)); if (provider) { + if (m_preferredSize.isValid()) { + provider->setPreferredEmoticonSize(m_preferredSize); + } KEmoticonsTheme theme(provider); provider->loadTheme(path); m_themes.insert(name, theme); @@ -295,5 +299,15 @@ KEmoticonsTheme::ParseMode KEmoticons::parseMode() return (KEmoticonsTheme::ParseMode) config.readEntry("parseMode", int(= KEmoticonsTheme::RelaxedParse)); } = +void KEmoticons::setPreferredEmoticonSize(const QSize &size) +{ + d->m_preferredSize =3D size; +} + +QSize KEmoticons::preferredEmoticonSize() const +{ + return d->m_preferredSize; +} + #include "moc_kemoticons.cpp" = diff --git a/src/core/kemoticons.h b/src/core/kemoticons.h index 4718c00..2e2ee91 100644 --- a/src/core/kemoticons.h +++ b/src/core/kemoticons.h @@ -124,6 +124,25 @@ public: */ static KEmoticonsTheme::ParseMode parseMode(); = + /** + * If a preferred size is set, all parsed emoticons will be + * returned with the @p size + * + * @param size The desired QSize of parsed emoticons + * @since 5.23 + */ + void setPreferredEmoticonSize(const QSize &size); + + /** + * Returns size in which parsed emoticons will be returned. + * + * If the QSize returned is not valid (isValid() =3D=3D false), + * then the default will be used, that is the actual file size. + * + * @since 5.23 + */ + QSize preferredEmoticonSize() const; + private: /** * Private implementation class diff --git a/src/core/kemoticonsprovider.cpp b/src/core/kemoticonsprovider.= cpp index 5aabd82..5cd61a9 100644 --- a/src/core/kemoticonsprovider.cpp +++ b/src/core/kemoticonsprovider.cpp @@ -34,6 +34,7 @@ public: QString m_themePath; QHash m_emoticonsMap; QHash > m_emoticonsIndex; + QSize m_preferredSize; }; = KEmoticonsProviderPrivate::KEmoticonsProviderPrivate() @@ -147,7 +148,11 @@ void KEmoticonsProvider::addIndexItem(const QString &p= ath, const QStringList &em e.picPath =3D path; p.load(path); = - e.picHTMLCode =3D QString("3D\"%1\"").arg(escaped).= arg(path).arg(p.width()).arg(p.height()); + const bool hasPreferredSize =3D d->m_preferredSize.isValid(); + const int preferredHeight =3D hasPreferredSize ? d->m_preferredSiz= e.height() : p.height(); + const int preferredWidth =3D hasPreferredSize ? d->m_preferredSize= .width() : p.width(); + + e.picHTMLCode =3D QString("3D\"%1\"").arg(escaped, = path).arg(preferredWidth).arg(preferredHeight); = e.matchTextEscaped =3D escaped; e.matchText =3D s; @@ -191,3 +196,12 @@ void KEmoticonsProvider::removeIndexItem(const QString= &path, const QStringList } } = +void KEmoticonsProvider::setPreferredEmoticonSize(const QSize &size) +{ + d->m_preferredSize =3D size; +} + +QSize KEmoticonsProvider::preferredEmoticonSize() const +{ + return d->m_preferredSize; +} diff --git a/src/core/kemoticonsprovider.h b/src/core/kemoticonsprovider.h index d259bf8..e19e058 100644 --- a/src/core/kemoticonsprovider.h +++ b/src/core/kemoticonsprovider.h @@ -150,6 +150,25 @@ public: */ virtual void newTheme() =3D 0; = + /** + * If a preferred size is set, all parsed emoticons will be + * returned with the @p size + * + * @param size The desired QSize of parsed emoticons + * @since 5.23 + */ + void setPreferredEmoticonSize(const QSize &size); + + /** + * Returns size in which parsed emoticons will be returned. + * + * If the QSize returned is not valid (isValid() =3D=3D false), + * then the default will be used, that is the actual file size. + * + * @since 5.23 + */ + QSize preferredEmoticonSize() const; + protected: /** * Sets the theme inside the directory @p path