CompletionModel tagged strings now have types (i.e. bttv emote, name, twitch emote)

Usernames can be overriden (capitalized overrides lowercase, but not the other way around)
This commit is contained in:
Rasmus Karlsson 2018-03-30 12:06:02 +02:00 committed by fourtf
parent 6e1096710d
commit d9d699b6f1
2 changed files with 79 additions and 54 deletions

View file

@ -7,6 +7,8 @@
#include <QtAlgorithms> #include <QtAlgorithms>
#include <utility>
namespace chatterino { namespace chatterino {
CompletionModel::CompletionModel(const QString &_channelName) CompletionModel::CompletionModel(const QString &_channelName)
@ -24,40 +26,41 @@ void CompletionModel::refresh()
// TODO: Fix this so it properly updates with the proper api. oauth token needs proper scope // TODO: Fix this so it properly updates with the proper api. oauth token needs proper scope
for (const auto &m : emoteManager.twitchAccountEmotes) { for (const auto &m : emoteManager.twitchAccountEmotes) {
for (const auto &emoteName : m.second.emoteCodes) { for (const auto &emoteName : m.second.emoteCodes) {
this->addString(emoteName); // XXX: No way to discern between a twitch global emote and sub emote right now
this->addString(emoteName, TaggedString::Type::TwitchGlobalEmote);
} }
} }
// Global: BTTV Global Emotes // Global: BTTV Global Emotes
std::vector<std::string> &bttvGlobalEmoteCodes = emoteManager.bttvGlobalEmoteCodes; std::vector<std::string> &bttvGlobalEmoteCodes = emoteManager.bttvGlobalEmoteCodes;
for (const auto &m : bttvGlobalEmoteCodes) { for (const auto &m : bttvGlobalEmoteCodes) {
this->addString(m); this->addString(m, TaggedString::Type::BTTVGlobalEmote);
} }
// Global: FFZ Global Emotes // Global: FFZ Global Emotes
std::vector<std::string> &ffzGlobalEmoteCodes = emoteManager.ffzGlobalEmoteCodes; std::vector<std::string> &ffzGlobalEmoteCodes = emoteManager.ffzGlobalEmoteCodes;
for (const auto &m : ffzGlobalEmoteCodes) { for (const auto &m : ffzGlobalEmoteCodes) {
this->addString(m); this->addString(m, TaggedString::Type::FFZGlobalEmote);
} }
// Channel-specific: BTTV Channel Emotes // Channel-specific: BTTV Channel Emotes
std::vector<std::string> &bttvChannelEmoteCodes = std::vector<std::string> &bttvChannelEmoteCodes =
emoteManager.bttvChannelEmoteCodes[this->channelName.toStdString()]; emoteManager.bttvChannelEmoteCodes[this->channelName.toStdString()];
for (const auto &m : bttvChannelEmoteCodes) { for (const auto &m : bttvChannelEmoteCodes) {
this->addString(m); this->addString(m, TaggedString::Type::BTTVChannelEmote);
} }
// Channel-specific: FFZ Channel Emotes // Channel-specific: FFZ Channel Emotes
std::vector<std::string> &ffzChannelEmoteCodes = std::vector<std::string> &ffzChannelEmoteCodes =
emoteManager.ffzChannelEmoteCodes[this->channelName.toStdString()]; emoteManager.ffzChannelEmoteCodes[this->channelName.toStdString()];
for (const auto &m : ffzChannelEmoteCodes) { for (const auto &m : ffzChannelEmoteCodes) {
this->addString(m); this->addString(m, TaggedString::Type::FFZChannelEmote);
} }
// Global: Emojis // Global: Emojis
const auto &emojiShortCodes = emoteManager.emojiShortCodes; const auto &emojiShortCodes = emoteManager.emojiShortCodes;
for (const auto &m : emojiShortCodes) { for (const auto &m : emojiShortCodes) {
this->addString(":" + m + ":"); this->addString(":" + m + ":", TaggedString::Type::Emoji);
} }
// Channel-specific: Usernames // Channel-specific: Usernames
@ -76,22 +79,33 @@ void CompletionModel::refresh()
// } // }
} }
void CompletionModel::addString(const std::string &str) void CompletionModel::addString(const std::string &str, TaggedString::Type type)
{ {
// Always add a space at the end of completions // Always add a space at the end of completions
this->emotes.insert(this->createEmote(str + " ")); this->emotes.insert({qS(str + " "), type});
} }
void CompletionModel::addString(const QString &str) void CompletionModel::addString(const QString &str, TaggedString::Type type)
{ {
// Always add a space at the end of completions // Always add a space at the end of completions
this->emotes.insert(this->createEmote(str + " ")); this->emotes.insert({str + " ", type});
} }
void CompletionModel::addUser(const QString &str) void CompletionModel::addUser(const QString &str)
{ {
auto ts = this->createUser(str + " ");
// Always add a space at the end of completions // Always add a space at the end of completions
this->emotes.insert(this->createUser(str + " ")); std::pair<std::set<TaggedString>::iterator, bool> p = this->emotes.insert(ts);
if (!p.second) {
// No inseration was made, figure out if we need to replace the username.
if (p.first->str > ts.str) {
// Replace lowercase version of name with mixed-case version
this->emotes.erase(p.first);
auto result2 = this->emotes.insert(ts);
assert(result2.second);
}
}
} }
} // namespace chatterino } // namespace chatterino

View file

@ -4,7 +4,6 @@
#include <QAbstractListModel> #include <QAbstractListModel>
#include <map>
#include <set> #include <set>
#include <string> #include <string>
@ -12,6 +11,56 @@ namespace chatterino {
class CompletionModel : public QAbstractListModel class CompletionModel : public QAbstractListModel
{ {
struct TaggedString {
QString str;
// Type will help decide the lifetime of the tagged strings
enum Type {
Username,
// Emotes
FFZGlobalEmote = 20,
FFZChannelEmote,
BTTVGlobalEmote,
BTTVChannelEmote,
TwitchGlobalEmote,
TwitchSubscriberEmote,
Emoji,
} type;
bool IsEmote() const
{
return this->type >= 20;
}
bool operator<(const TaggedString &that) const
{
if (this->IsEmote()) {
if (that.IsEmote()) {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return this->str > that.str;
} else {
return k < 0;
}
} else {
return true;
}
} else {
if (that.IsEmote()) {
return false;
} else {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return false;
} else {
return k < 0;
}
}
}
}
};
public: public:
CompletionModel(const QString &_channelName); CompletionModel(const QString &_channelName);
@ -34,55 +83,17 @@ public:
} }
void refresh(); void refresh();
void addString(const std::string &str); void addString(const std::string &str, TaggedString::Type type);
void addString(const QString &str); void addString(const QString &str, TaggedString::Type type);
void addUser(const QString &str); void addUser(const QString &str);
private: private:
struct TaggedString {
QString str;
// emote == true
// username == false
bool isEmote = true;
bool operator<(const TaggedString &that) const
{
if (this->isEmote) {
if (that.isEmote) {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return this->str > that.str;
} else {
return k < 0;
}
} else
return true;
} else {
if (that.isEmote)
return false;
else {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return this->str > that.str;
} else {
return k < 0;
}
}
}
}
};
TaggedString createEmote(const std::string &str)
{
return TaggedString{qS(str), true};
}
TaggedString createEmote(const QString &str)
{
return TaggedString{str, true};
}
TaggedString createUser(const QString &str) TaggedString createUser(const QString &str)
{ {
return TaggedString{str, false}; return TaggedString{str, TaggedString::Type::Username};
} }
std::set<TaggedString> emotes; std::set<TaggedString> emotes;
QString channelName; QString channelName;