diff --git a/src/completionmanager.cpp b/src/completionmanager.cpp index 9c4ba7362..51dfa41a3 100644 --- a/src/completionmanager.cpp +++ b/src/completionmanager.cpp @@ -1,9 +1,65 @@ #include "completionmanager.hpp" #include "common.hpp" +#include "debug/log.hpp" #include "emotemanager.hpp" namespace chatterino { +CompletionModel::CompletionModel(const std::string &_channelName) + : channelName(_channelName) +{ +} + +void CompletionModel::refresh() +{ + // debug::Log("[CompletionModel:{}] Refreshing...]", this->channelName); + + auto &emoteManager = EmoteManager::getInstance(); + this->emotes.clear(); + + // 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 &bttvGlobalEmoteCodes = emoteManager.bttvGlobalEmoteCodes; + for (const auto &m : bttvGlobalEmoteCodes) { + this->addString(m); + } + + // Global: FFZ Global Emotes + std::vector &ffzGlobalEmoteCodes = emoteManager.ffzGlobalEmoteCodes; + for (const auto &m : ffzGlobalEmoteCodes) { + this->addString(m); + } + + // Channel-specific: BTTV Channel Emotes + std::vector &bttvChannelEmoteCodes = + emoteManager.bttvChannelEmoteCodes[this->channelName]; + for (const auto &m : bttvChannelEmoteCodes) { + this->addString(m); + } + + // Channel-specific: FFZ Channel Emotes + std::vector &ffzChannelEmoteCodes = + emoteManager.ffzChannelEmoteCodes[this->channelName]; + for (const auto &m : ffzChannelEmoteCodes) { + this->addString(m); + } + + // Global: Emojis + const auto &emojiShortCodes = emoteManager.emojiShortCodes; + for (const auto &m : emojiShortCodes) { + this->addString(":" + m + ":"); + } + + // TODO: Add Channel-specific: Usernames +} + void CompletionModel::addString(const std::string &str) { // Always add a space at the end of completions @@ -12,67 +68,15 @@ void CompletionModel::addString(const std::string &str) CompletionModel *CompletionManager::createModel(const std::string &channelName) { - CompletionModel *ret = new CompletionModel(); - auto &emoteManager = EmoteManager::getInstance(); + auto it = this->models.find(channelName); + if (it != this->models.end()) { + return it->second; + } - this->updateModel(ret, channelName); - - emoteManager.bttvGlobalEmoteCodes.updated.connect([=]() { - this->updateModel(ret, channelName); // - }); - - emoteManager.ffzGlobalEmoteCodes.updated.connect([=]() { - this->updateModel(ret, channelName); // - }); - - emoteManager.bttvChannelEmoteCodes[channelName].updated.connect([=]() { - this->updateModel(ret, channelName); // - }); - - emoteManager.ffzChannelEmoteCodes[channelName].updated.connect([=]() { - this->updateModel(ret, channelName); // - }); + CompletionModel *ret = new CompletionModel(channelName); + this->models[channelName] = ret; return ret; } -void CompletionManager::updateModel(CompletionModel *model, const std::string &channelName) -{ - auto &emoteManager = EmoteManager::getInstance(); - - model->emotes.clear(); - - for (const auto &m : emoteManager.twitchAccountEmotes) { - for (const auto &emoteName : m.second.emoteCodes) { - model->addString(emoteName); - } - } - - std::vector &bttvGlobalEmoteCodes = emoteManager.bttvGlobalEmoteCodes; - for (const auto &m : bttvGlobalEmoteCodes) { - model->addString(m); - } - - std::vector &ffzGlobalEmoteCodes = emoteManager.ffzGlobalEmoteCodes; - for (const auto &m : ffzGlobalEmoteCodes) { - model->addString(m); - } - - std::vector &bttvChannelEmoteCodes = - emoteManager.bttvChannelEmoteCodes[channelName]; - for (const auto &m : bttvChannelEmoteCodes) { - model->addString(m); - } - - std::vector &ffzChannelEmoteCodes = emoteManager.ffzChannelEmoteCodes[channelName]; - for (const auto &m : ffzChannelEmoteCodes) { - model->addString(m); - } - - const auto &emojiShortCodes = emoteManager.emojiShortCodes; - for (const auto &m : emojiShortCodes) { - model->addString(":" + m + ":"); - } -} - } // namespace chatterino diff --git a/src/completionmanager.hpp b/src/completionmanager.hpp index 686644839..778b80706 100644 --- a/src/completionmanager.hpp +++ b/src/completionmanager.hpp @@ -3,15 +3,16 @@ #include #include +#include #include namespace chatterino { -class EmoteManager; - class CompletionModel : public QAbstractListModel { public: + CompletionModel(const std::string &_channelName); + virtual int columnCount(const QModelIndex & /*parent*/) const override { return 1; @@ -28,15 +29,22 @@ public: return this->emotes.size(); } + void refresh(); + +private: void addString(const std::string &str); QVector emotes; + + std::string channelName; }; class CompletionManager { CompletionManager() = default; + std::map models; + public: static CompletionManager &getInstance() { @@ -45,7 +53,6 @@ public: } CompletionModel *createModel(const std::string &channelName); - void updateModel(CompletionModel *model, const std::string &channelName = std::string()); }; } // namespace chatterino diff --git a/src/widgets/helper/resizingtextedit.cpp b/src/widgets/helper/resizingtextedit.cpp index 0dd2b2f3b..10a286735 100644 --- a/src/widgets/helper/resizingtextedit.cpp +++ b/src/widgets/helper/resizingtextedit.cpp @@ -1,4 +1,5 @@ #include "widgets/helper/resizingtextedit.hpp" +#include "completionmanager.hpp" ResizingTextEdit::ResizingTextEdit() { @@ -86,6 +87,10 @@ void ResizingTextEdit::keyPressEvent(QKeyEvent *event) return; } + auto *completionModel = + static_cast(this->completer->model()); + completionModel->refresh(); + if (!this->nextCompletion) { // first selection this->completer->setCompletionPrefix(currentCompletionPrefix);