forked from SmartisanTech/Wrench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emojimodel.cpp
124 lines (113 loc) · 3.67 KB
/
emojimodel.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "emojimodel.h"
#include <QDebug>
#include <QFont>
#include <QBrush>
#include <lua.hpp>
#include <QSettings>
#include <QTimer>
EmojiModel::EmojiModel(QObject *parent) :
FilteringModel(parent)
{
L = luaL_newstate(); /* opens Lua */
luaL_openlibs(L); /* opens the standard libraries */
int error = luaL_loadstring(L, "emojis = require('emojis')") || lua_pcall(L, 0, 0, 0);
if (error) {
qDebug() << "Error loading emojis: " << QString::fromUtf8(lua_tolstring(L, -1, NULL));
return;
}
lua_getglobal(L, "emojis");
int n = luaL_len(L, 1);
for (int i = 1; i <= n; i++) {
lua_rawgeti(L, -1, i);
lua_rawgeti(L, -1, 2);
QString key = QString::fromUtf8(lua_tolstring(L, -1, NULL));
key = QString().sprintf("%04d", i) + key;
lua_settop(L, -2);
lua_rawgeti(L, -1, 3);
QString pngPath = QString::fromUtf8(lua_tolstring(L, -1, NULL));
lua_settop(L, 1); // finished with this small table
mEmojiIconPathMap[key] = pngPath;
mEmojis << key;
}
mLoadEmojiTimer.setSingleShot(true);
mLoadEmojiTimer.setInterval(100);
connect(&mLoadEmojiTimer, SIGNAL(timeout()), this, SLOT(loadAllEmojis()));
lua_settop(L, 0);
initHistory();
}
const int maxLoad = 50;
void EmojiModel::loadAllEmojis()
{
qDebug() << "load all Emojis" << mEmojiIconMap.size();
int loaded = 0;
int checked = 0;
if (! mSelectedItems.isEmpty()) {
foreach (const SelectedItem& si, mSelectedItems) {
checked++;
const QString& emoji = si.displayText;
if (!mEmojiIconMap.contains(emoji) && loaded < maxLoad) {
mEmojiIconMap[emoji] = QPixmap(mEmojiIconPathMap[emoji]);
loaded++;
} else if (loaded == maxLoad) {
mLoadEmojiTimer.start();
if (checked < loaded * 2) {
emit iconsUpdated();
} else {
// if I checked a lot of selected item to load
// maxLoad, then there's no need to do
// iconsUpdated.
}
return;
}
}
}
if (loaded != 0) {
emit iconsUpdated();
return;
}
foreach (const QString& emoji, mEmojis) {
if (!mEmojiIconMap.contains(emoji) && loaded < maxLoad) {
mEmojiIconMap[emoji] = QPixmap(mEmojiIconPathMap[emoji]);
loaded++;
} else if (loaded == maxLoad) {
mLoadEmojiTimer.start();
break;
}
}
}
void EmojiModel::filterSelectedItems(const QStringList& split)
{
bool startedTimer = false;
QPixmap defaultPixmap;
foreach (const QString& emoji, mEmojis) {
bool match = 1;
foreach(const QString& stem, split) {
if (!emoji.contains(stem, Qt::CaseInsensitive)) {
match = 0;
break;
}
}
if (match) {
QPixmap emojiPixmap;
if (!mEmojiIconMap.contains(emoji) && !startedTimer) {
mLoadEmojiTimer.start();
startedTimer = true;
}
if (mEmojiIconMap.contains(emoji)) {
emojiPixmap = mEmojiIconMap[emoji];
} else {
if (defaultPixmap.isNull()) {
defaultPixmap = QPixmap("loading.png");
}
emojiPixmap = defaultPixmap;
}
SelectedItem si(mEmojiIconPathMap[emoji], emoji, emojiPixmap);
mSelectedItems << si;
mSelectedItemsRevMap[emoji] = si;
}
}
}
QString EmojiModel::getHistoryName()
{
return "emoji-history";
}