Add option to display/hide localized names

Fix #49
This commit is contained in:
Rasmus Karlsson 2017-07-23 11:59:05 +02:00
parent 9b13581fa8
commit 0563136a34
4 changed files with 91 additions and 10 deletions

View file

@ -265,18 +265,50 @@ void TwitchMessageBuilder::parseUsername()
this->userName = this->tags.value(QLatin1String("login")).toString();
}
QString displayName;
QString username = this->userName;
QString localizedName;
iterator = this->tags.find("display-name");
if (iterator == this->tags.end()) {
displayName = this->userName;
} else {
displayName = iterator.value().toString();
if (iterator != this->tags.end()) {
QString displayName = iterator.value().toString();
if (QString::compare(displayName, this->userName, Qt::CaseInsensitive) == 0) {
username = displayName;
} else {
localizedName = displayName;
}
}
bool hasLocalizedName = QString::compare(displayName, ircMessage->account()) == 0;
QString userDisplayString =
displayName + (hasLocalizedName ? (" (" + ircMessage->account() + ")") : QString());
bool hasLocalizedName = !localizedName.isEmpty();
// The full string that will be rendered in the chat widget
QString usernameString;
pajlada::Settings::Setting<int> usernameDisplayMode(
"/appearance/messages/usernameDisplayMode", UsernameDisplayMode::UsernameAndLocalizedName);
switch (usernameDisplayMode.getValue()) {
case UsernameDisplayMode::Username: {
usernameString = username;
} break;
case UsernameDisplayMode::LocalizedName: {
if (hasLocalizedName) {
usernameString = localizedName;
} else {
usernameString = username;
}
} break;
default:
case UsernameDisplayMode::UsernameAndLocalizedName: {
if (hasLocalizedName) {
usernameString = username + "(" + localizedName + ")";
} else {
usernameString = username;
}
} break;
}
if (this->args.isSentWhisper) {
// TODO(pajlada): Re-implement
@ -289,10 +321,10 @@ void TwitchMessageBuilder::parseUsername()
}
if (!ircMessage->isAction()) {
userDisplayString += ": ";
usernameString += ": ";
}
this->appendWord(Word(userDisplayString, Word::Username, this->usernameColor, userDisplayString,
this->appendWord(Word(usernameString, Word::Username, this->usernameColor, usernameString,
QString(), Link(Link::UserInfo, this->userName)));
}

View file

@ -18,6 +18,12 @@ namespace twitch {
class TwitchMessageBuilder : public messages::MessageBuilder
{
public:
enum UsernameDisplayMode : int {
Username = 1, // Username
LocalizedName = 2, // Localized name
UsernameAndLocalizedName = 3, // Username (Localized name)
};
TwitchMessageBuilder() = delete;
explicit TwitchMessageBuilder(Channel *_channel, Resources &_resources,

View file

@ -1,5 +1,6 @@
#include "widgets/settingsdialog.hpp"
#include "accountmanager.hpp"
#include "twitch/twitchmessagebuilder.hpp"
#include "twitch/twitchuser.hpp"
#include "widgets/settingsdialogtab.hpp"
#include "windowmanager.hpp"
@ -20,6 +21,9 @@ namespace widgets {
SettingsDialog::SettingsDialog()
: snapshot(SettingsManager::getInstance().createSnapshot())
, usernameDisplayMode(
"/appearance/messages/usernameDisplayMode",
twitch::TwitchMessageBuilder::UsernameDisplayMode::UsernameAndLocalizedName)
{
QFile file(":/qss/settings.qss");
file.open(QFile::ReadOnly);
@ -239,6 +243,19 @@ void SettingsDialog::addTabs()
settings.allowDouplicateMessages));
v->addWidget(createCheckbox("Seperate messages", settings.seperateMessages));
v->addWidget(createCheckbox("Show message length", settings.showMessageLength));
v->addLayout(this->createCombobox(
"Username display mode", this->usernameDisplayMode,
{"Username (Localized name)", "Username", "Localized name"},
[](const QString &newValue, pajlada::Settings::Setting<int> &setting) {
if (newValue == "Username (Localized name)") {
setting =
twitch::TwitchMessageBuilder::UsernameDisplayMode::UsernameAndLocalizedName;
} else if (newValue == "Username") {
setting = twitch::TwitchMessageBuilder::UsernameDisplayMode::Username;
} else if (newValue == "Localized name") {
setting = twitch::TwitchMessageBuilder::UsernameDisplayMode::LocalizedName;
}
}));
group->setLayout(v);
@ -409,6 +426,26 @@ QCheckBox *SettingsDialog::createCheckbox(const QString &title,
return checkbox;
}
QHBoxLayout *SettingsDialog::createCombobox(
const QString &title, pajlada::Settings::Setting<int> &setting, QStringList items,
std::function<void(QString, pajlada::Settings::Setting<int> &)> cb)
{
auto box = new QHBoxLayout();
auto label = new QLabel(title);
auto widget = new QComboBox();
widget->addItems(items);
QObject::connect(widget, &QComboBox::currentTextChanged, this,
[&setting, cb](const QString &newValue) {
cb(newValue, setting); //
});
box->addWidget(label);
box->addWidget(widget);
return box;
}
void SettingsDialog::okButtonClicked()
{
this->close();

View file

@ -6,6 +6,7 @@
#include <QButtonGroup>
#include <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QListView>
@ -32,6 +33,8 @@ public:
private:
SettingsSnapshot snapshot;
pajlada::Settings::Setting<int> usernameDisplayMode;
struct {
QVBoxLayout tabs;
QVBoxLayout vbox;
@ -51,6 +54,9 @@ private:
/// Widget creation helpers
QCheckBox *createCheckbox(const QString &title, Setting<bool> &setting);
QCheckBox *createCheckbox(const QString &title, pajlada::Settings::Setting<bool> &setting);
QHBoxLayout *createCombobox(const QString &title, pajlada::Settings::Setting<int> &setting,
QStringList items,
std::function<void(QString, pajlada::Settings::Setting<int> &)> cb);
void okButtonClicked();
void cancelButtonClicked();