[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kemoticons] /: Add support for custom emoticon sizes
From:       Martin Klapetek <mklapetek () kde ! org>
Date:       2016-05-17 23:55:59
Message-ID: E1b2oqF-00055R-LU () scm ! kde ! org
[Download RAW message or body]

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<QString>("themeName");
+
+        QString basePath = QFINDTESTDATA("emoticon-parser-testcases");
+        QVERIFY(!basePath.isEmpty());
+        QDir testCasesDir(basePath);
+
+        QStringList inputFileNames = \
testCasesDir.entryList(QStringList(QStringLiteral("*.input"))); +        Q_FOREACH \
(const QString &fileName, inputFileNames) { +            QString outputFileName = \
fileName; +            outputFileName.replace(QStringLiteral("input"), \
QStringLiteral("output")); +            const QString baseName = \
fileName.section(QLatin1Char('-'), 0, 0); +            \
QTest::newRow(qPrintable(fileName.left(fileName.lastIndexOf('.')))) +            << \
(baseName == QLatin1String("xmpp") ? "xmpp-testtheme" : default_theme); +        }
+    }
+
+    void testPreferredSizes()
+    {
+        QFETCH(QString, themeName);
+        KEmoticons kemoticons;
+        kemoticons.setPreferredEmoticonSize(QSize(99, 77));
+
+        KEmoticonsTheme theme = kemoticons.theme(themeName);
+
+        const QString parsed = theme.parseEmoticons(":)");
+
+        QVERIFY(parsed.contains(QStringLiteral("width=\"99\"")));
+        QVERIFY(parsed.contains(QStringLiteral("height=\"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<QString, KEmoticonsTheme> 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 QString &name)
         if (QFile::exists(path)) {
             KEmoticonsProvider *provider = 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 = 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() == 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<QString, QStringList> m_emoticonsMap;
     QHash<QChar, QList<KEmoticonsProvider::Emoticon> > m_emoticonsIndex;
+    QSize m_preferredSize;
 };
 
 KEmoticonsProviderPrivate::KEmoticonsProviderPrivate()
@@ -147,7 +148,11 @@ void KEmoticonsProvider::addIndexItem(const QString &path, const \
QStringList &em  e.picPath = path;
         p.load(path);
 
-        e.picHTMLCode = QString("<img align=\"center\" title=\"%1\" alt=\"%1\" \
src=\"%2\" width=\"%3\" height=\"%4\" \
/>").arg(escaped).arg(path).arg(p.width()).arg(p.height()); +        const bool \
hasPreferredSize = d->m_preferredSize.isValid(); +        const int preferredHeight = \
hasPreferredSize ? d->m_preferredSize.height() : p.height(); +        const int \
preferredWidth = hasPreferredSize ? d->m_preferredSize.width() : p.width(); +
+        e.picHTMLCode = QString("<img align=\"center\" title=\"%1\" alt=\"%1\" \
src=\"%2\" width=\"%3\" height=\"%4\" />").arg(escaped, \
path).arg(preferredWidth).arg(preferredHeight);  
         e.matchTextEscaped = escaped;
         e.matchText = s;
@@ -191,3 +196,12 @@ void KEmoticonsProvider::removeIndexItem(const QString &path, \
const QStringList  }
 }
 
+void KEmoticonsProvider::setPreferredEmoticonSize(const QSize &size)
+{
+    d->m_preferredSize = 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() = 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() == 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


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic