Move CompletionModel to a more appropriate folder

This commit is contained in:
Rasmus Karlsson
2018-03-24 12:13:09 +01:00
parent d9bd39e8a4
commit b2f041989c
5 changed files with 7 additions and 11 deletions
+97
View File
@@ -0,0 +1,97 @@
#include "util/completionmodel.hpp"
#include "common.hpp"
#include "debug/log.hpp"
#include "singletons/channelmanager.hpp"
#include "singletons/emotemanager.hpp"
#include <QtAlgorithms>
namespace chatterino {
CompletionModel::CompletionModel(const QString &_channelName)
: channelName(_channelName)
{
}
void CompletionModel::refresh()
{
debug::Log("[CompletionModel:{}] Refreshing...]", this->channelName);
auto &emoteManager = singletons::EmoteManager::getInstance();
// User-specific: Twitch Emotes
// 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 &emoteName : m.second.emoteCodes) {
this->addString(emoteName);
}
}
// Global: BTTV Global Emotes
std::vector<std::string> &bttvGlobalEmoteCodes = emoteManager.bttvGlobalEmoteCodes;
for (const auto &m : bttvGlobalEmoteCodes) {
this->addString(m);
}
// Global: FFZ Global Emotes
std::vector<std::string> &ffzGlobalEmoteCodes = emoteManager.ffzGlobalEmoteCodes;
for (const auto &m : ffzGlobalEmoteCodes) {
this->addString(m);
}
// Channel-specific: BTTV Channel Emotes
std::vector<std::string> &bttvChannelEmoteCodes =
emoteManager.bttvChannelEmoteCodes[this->channelName.toStdString()];
for (const auto &m : bttvChannelEmoteCodes) {
this->addString(m);
}
// Channel-specific: FFZ Channel Emotes
std::vector<std::string> &ffzChannelEmoteCodes =
emoteManager.ffzChannelEmoteCodes[this->channelName.toStdString()];
for (const auto &m : ffzChannelEmoteCodes) {
this->addString(m);
}
// Global: Emojis
const auto &emojiShortCodes = emoteManager.emojiShortCodes;
for (const auto &m : emojiShortCodes) {
this->addString(":" + m + ":");
}
// Channel-specific: Usernames
// fourtf: only works with twitch chat
// auto c = singletons::ChannelManager::getInstance().getTwitchChannel(this->channelName);
// auto usernames = c->getUsernamesForCompletions();
// for (const auto &name : usernames) {
// assert(!name.displayName.isEmpty());
// this->addString(name.displayName);
// this->addString('@' + name.displayName);
// if (!name.localizedName.isEmpty()) {
// this->addString(name.localizedName);
// this->addString('@' + name.localizedName);
// }
// }
}
void CompletionModel::addString(const std::string &str)
{
// Always add a space at the end of completions
this->emotes.insert(this->createEmote(str + " "));
}
void CompletionModel::addString(const QString &str)
{
// Always add a space at the end of completions
this->emotes.insert(this->createEmote(str + " "));
}
void CompletionModel::addUser(const QString &str)
{
// Always add a space at the end of completions
this->emotes.insert(this->createUser(str + " "));
}
} // namespace chatterino
+91
View File
@@ -0,0 +1,91 @@
#pragma once
#include "common.hpp"
#include <QAbstractListModel>
#include <map>
#include <set>
#include <string>
namespace chatterino {
class CompletionModel : public QAbstractListModel
{
public:
CompletionModel(const QString &_channelName);
virtual int columnCount(const QModelIndex &) const override
{
return 1;
}
virtual QVariant data(const QModelIndex &index, int) const override
{
// TODO: Implement more safely
auto it = this->emotes.begin();
std::advance(it, index.row());
return QVariant(it->str);
}
virtual int rowCount(const QModelIndex &) const override
{
return this->emotes.size();
}
void refresh();
void addString(const std::string &str);
void addString(const QString &str);
void addUser(const QString &str);
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)
{
return TaggedString{str, false};
}
std::set<TaggedString> emotes;
QString channelName;
};
} // namespace chatterino