2017-12-31 00:50:07 +01:00
|
|
|
#pragma once
|
|
|
|
|
2018-03-24 12:02:07 +01:00
|
|
|
#include "common.hpp"
|
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
#include <QAbstractListModel>
|
|
|
|
|
2018-03-24 12:02:07 +01:00
|
|
|
#include <set>
|
2017-12-31 00:50:07 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace chatterino {
|
2018-03-24 12:02:07 +01:00
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
class CompletionModel : public QAbstractListModel
|
|
|
|
{
|
2018-03-30 12:06:02 +02:00
|
|
|
struct TaggedString {
|
|
|
|
QString str;
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-03-30 12:06:02 +02:00
|
|
|
// Type will help decide the lifetime of the tagged strings
|
|
|
|
enum Type {
|
|
|
|
Username,
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-03-30 12:06:02 +02:00
|
|
|
// Emotes
|
|
|
|
FFZGlobalEmote = 20,
|
|
|
|
FFZChannelEmote,
|
|
|
|
BTTVGlobalEmote,
|
|
|
|
BTTVChannelEmote,
|
|
|
|
TwitchGlobalEmote,
|
|
|
|
TwitchSubscriberEmote,
|
|
|
|
Emoji,
|
|
|
|
} type;
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-03-30 12:06:02 +02:00
|
|
|
bool IsEmote() const
|
|
|
|
{
|
|
|
|
return this->type >= 20;
|
|
|
|
}
|
2018-03-24 11:12:24 +01:00
|
|
|
|
|
|
|
bool operator<(const TaggedString &that) const
|
|
|
|
{
|
2018-03-30 12:06:02 +02:00
|
|
|
if (this->IsEmote()) {
|
|
|
|
if (that.IsEmote()) {
|
2018-03-24 11:12:24 +01:00
|
|
|
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
|
2018-03-24 12:02:07 +01:00
|
|
|
if (k == 0) {
|
|
|
|
return this->str > that.str;
|
|
|
|
} else {
|
|
|
|
return k < 0;
|
|
|
|
}
|
2018-03-30 12:06:02 +02:00
|
|
|
} else {
|
2018-03-24 11:12:24 +01:00
|
|
|
return true;
|
2018-03-30 12:06:02 +02:00
|
|
|
}
|
2018-03-24 11:12:24 +01:00
|
|
|
} else {
|
2018-03-30 12:06:02 +02:00
|
|
|
if (that.IsEmote()) {
|
2018-03-24 11:12:24 +01:00
|
|
|
return false;
|
2018-03-30 12:06:02 +02:00
|
|
|
} else {
|
2018-03-24 12:02:07 +01:00
|
|
|
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
|
|
|
|
if (k == 0) {
|
2018-03-30 12:06:02 +02:00
|
|
|
return false;
|
2018-03-24 12:02:07 +01:00
|
|
|
} else {
|
|
|
|
return k < 0;
|
|
|
|
}
|
2018-03-24 11:12:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-03-30 12:06:02 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
CompletionModel(const QString &_channelName);
|
|
|
|
|
|
|
|
virtual int columnCount(const QModelIndex &) const override
|
2018-03-24 11:12:24 +01:00
|
|
|
{
|
2018-03-30 12:06:02 +02:00
|
|
|
return 1;
|
2018-03-24 11:12:24 +01:00
|
|
|
}
|
2018-03-30 12:06:02 +02:00
|
|
|
|
|
|
|
virtual QVariant data(const QModelIndex &index, int) const override
|
2018-03-24 11:12:24 +01:00
|
|
|
{
|
2018-03-30 12:06:02 +02:00
|
|
|
// TODO: Implement more safely
|
|
|
|
auto it = this->emotes.begin();
|
|
|
|
std::advance(it, index.row());
|
|
|
|
return QVariant(it->str);
|
2018-03-24 11:12:24 +01:00
|
|
|
}
|
2018-03-30 12:06:02 +02:00
|
|
|
|
|
|
|
virtual int rowCount(const QModelIndex &) const override
|
|
|
|
{
|
|
|
|
return this->emotes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh();
|
|
|
|
void addString(const std::string &str, TaggedString::Type type);
|
|
|
|
void addString(const QString &str, TaggedString::Type type);
|
|
|
|
|
|
|
|
void addUser(const QString &str);
|
|
|
|
|
|
|
|
private:
|
2018-03-24 11:12:24 +01:00
|
|
|
TaggedString createUser(const QString &str)
|
|
|
|
{
|
2018-03-30 12:06:02 +02:00
|
|
|
return TaggedString{str, TaggedString::Type::Username};
|
2018-03-24 11:12:24 +01:00
|
|
|
}
|
2018-03-30 12:06:02 +02:00
|
|
|
|
2018-03-24 11:12:24 +01:00
|
|
|
std::set<TaggedString> emotes;
|
2017-12-31 00:50:07 +01:00
|
|
|
|
|
|
|
QString channelName;
|
|
|
|
};
|
2018-03-24 12:02:07 +01:00
|
|
|
|
2018-03-24 11:12:24 +01:00
|
|
|
} // namespace chatterino
|