-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilswrapper.cpp
67 lines (56 loc) · 1.73 KB
/
utilswrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2016 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.
#include <QtCore/QVector>
#include <QtQml/QQmlEngine>
#include "utilswrapper.h"
#include "unicodeutils.h"
namespace com { namespace cutehacks { namespace emooj {
class JSEmojiReplacer : public AbstractEmojiReplacer
{
public:
JSEmojiReplacer(QQmlEngine * engine, const QJSValue& callback) :
m_engine(engine),
m_callback(callback)
{ }
QString replace(const Emoji &emoji) const
{
if (m_callback.isCallable()) {
QJSValue args = m_engine->newObject();
args.setProperty("unicode", emoji.unicode);
args.setProperty("name", emoji.name);
args.setProperty("shortName", emoji.shortName);
args.setProperty("sheetX", emoji.sheetX);
args.setProperty("sheetY", emoji.sheetY);
return m_callback.call(
QJSValueList() << args
).toString();
}
qWarning("Second argument to UnicodeUtils.replaceEmojis should be a function");
return m_callback.toString();
}
private:
QQmlEngine *m_engine;
mutable QJSValue m_callback;
};
UtilsWrapper::UtilsWrapper(QQmlEngine *engine) :
QObject(engine),
m_engine(engine)
{ }
/*!
Calculate the number of actual symbols in the QString and not
just the length of the string.
*/
int UtilsWrapper::length(const QString &str) const
{
return str.toUcs4().length();
}
/*!
Find all emoijs in 'src' and replace them with the result returned by
callback.
*/
QString UtilsWrapper::replaceEmojis(const QString &src, QJSValue callback)
{
JSEmojiReplacer replacer(m_engine, callback);
return UnicodeUtils::replaceEmojis(src, &replacer);
}
} } }