From 2073447df79eaf98e365b60897d17c437636fecc Mon Sep 17 00:00:00 2001 From: fourtf Date: Sat, 13 Jan 2018 02:00:02 +0100 Subject: [PATCH] added highlights page again --- src/util/layoutcreator.hpp | 15 ++ src/widgets/helper/settingsdialogtab.cpp | 2 +- src/widgets/settingsdialog.cpp | 6 +- src/widgets/settingspages/aboutpage.cpp | 2 +- .../settingspages/highlightingpage.cpp | 232 ++++++++++++++++++ .../settingspages/highlightingpage.hpp | 15 ++ 6 files changed, 267 insertions(+), 5 deletions(-) diff --git a/src/util/layoutcreator.hpp b/src/util/layoutcreator.hpp index 16b4b57f5..886886eeb 100644 --- a/src/util/layoutcreator.hpp +++ b/src/util/layoutcreator.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -62,6 +63,20 @@ public: return *this; } + template ::value, int>::type = 0> + LayoutCreator appendTab(T2 *item, const QString &title) + { + static_assert(std::is_base_of::value, "needs to be QLayout"); + + QWidget *widget = new QWidget; + widget->setLayout(item); + + this->item->addTab(widget, title); + + return LayoutCreator(item); + } + private: T *item; diff --git a/src/widgets/helper/settingsdialogtab.cpp b/src/widgets/helper/settingsdialogtab.cpp index 43006af40..4f0282530 100644 --- a/src/widgets/helper/settingsdialogtab.cpp +++ b/src/widgets/helper/settingsdialogtab.cpp @@ -50,7 +50,7 @@ void SettingsDialogTab::paintEvent(QPaintEvent *) int a = (this->height() - 20) / 2; QPixmap pixmap = this->ui.icon.pixmap(QSize(20, 20)); - painter.drawPixmap(0, a, pixmap); + painter.drawPixmap(0, a + a, pixmap); a = a + a + 20; diff --git a/src/widgets/settingsdialog.cpp b/src/widgets/settingsdialog.cpp index d201302cd..3422dcd9d 100644 --- a/src/widgets/settingsdialog.cpp +++ b/src/widgets/settingsdialog.cpp @@ -42,9 +42,9 @@ void SettingsDialog::initUi() .assign(&this->ui.tabContainer); // right side layout - auto right = layoutCreator.emplace(); + auto right = layoutCreator.emplace().withoutMargin(); { - right.emplace().assign(&this->ui.pageStack).emplace("NaM"); + right.emplace().assign(&this->ui.pageStack).withoutMargin(); auto buttons = right.emplace(Qt::Horizontal); { @@ -79,7 +79,7 @@ void SettingsDialog::addTabs() this->addTab(new settingspages::BehaviourPage); this->addTab(new settingspages::CommandPage); this->addTab(new settingspages::EmotesPage); - // this->addTab(new settingspages::HighlightingPage); + this->addTab(new settingspages::HighlightingPage); // this->addTab(new settingspages::LogsPage); // this->addTab(new settingspages::ModerationPage); this->ui.tabContainer->addStretch(1); diff --git a/src/widgets/settingspages/aboutpage.cpp b/src/widgets/settingspages/aboutpage.cpp index 6c4ec6ba5..766d593d7 100644 --- a/src/widgets/settingspages/aboutpage.cpp +++ b/src/widgets/settingspages/aboutpage.cpp @@ -16,7 +16,7 @@ AboutPage::AboutPage() { util::LayoutCreator layoutCreator(this); - auto layout = layoutCreator.emplace(); + auto layout = layoutCreator.emplace().withoutMargin(); { QPixmap pixmap; pixmap.load(":/images/aboutlogo.png"); diff --git a/src/widgets/settingspages/highlightingpage.cpp b/src/widgets/settingspages/highlightingpage.cpp index e6384bb96..bd0c491bf 100644 --- a/src/widgets/settingspages/highlightingpage.cpp +++ b/src/widgets/settingspages/highlightingpage.cpp @@ -1,11 +1,243 @@ #include "highlightingpage.hpp" +#include +#include +#include +#include +#include + +#include "debug/log.hpp" +#include "singletons/settingsmanager.hpp" +#include "util/layoutcreator.hpp" + +#define ENABLE_HIGHLIGHTS "Enable Highlighting" +#define HIGHLIGHT_MSG "Highlight messages containing your name" +#define PLAY_SOUND "Play sound when your name is mentioned" +#define FLASH_TASKBAR "Flash taskbar when your name is mentioned" +#define ALWAYS_PLAY "Always play highlight sound (Even if Chatterino is focused)" + namespace chatterino { namespace widgets { namespace settingspages { HighlightingPage::HighlightingPage() : SettingsPage("Highlights", ":/images/VSO_Link_blue_16x.png") { + singletons::SettingManager &settings = singletons::SettingManager::getInstance(); + util::LayoutCreator layoutCreator(this); + + auto layout = layoutCreator.emplace().withoutMargin(); + { + // GENERAL + layout.append(this->createCheckBox(ENABLE_HIGHLIGHTS, settings.enableHighlights)); + layout.append(this->createCheckBox(HIGHLIGHT_MSG, settings.enableHighlightsSelf)); + layout.append(this->createCheckBox(PLAY_SOUND, settings.enableHighlightSound)); + layout.append(this->createCheckBox(FLASH_TASKBAR, settings.enableHighlightTaskbar)); + + auto customSound = layout.emplace().withoutMargin(); + { + customSound.append(this->createCheckBox("Custom sound", settings.customHighlightSound)); + auto selectFile = customSound.emplace("Select custom sound file"); + QObject::connect(selectFile.getElement(), &QPushButton::clicked, this, + [&settings, this] { + auto fileName = QFileDialog::getOpenFileName( + this, tr("Open Sound"), "", tr("Audio Files (*.mp3 *.wav)")); + settings.pathHighlightSound = fileName; + }); + } + + layout.append(createCheckBox(ALWAYS_PLAY, settings.highlightAlwaysPlaySound)); + + // TABS + auto tabs = layout.emplace(); + { + // HIGHLIGHTS + auto highlights = tabs.appendTab(new QVBoxLayout, "Highlights"); + { + highlights.emplace().assign(&this->highlightList); + + auto buttons = highlights.emplace(); + + buttons.emplace("Add").assign(&this->highlightAdd); + buttons.emplace("Edit").assign(&this->highlightEdit); + buttons.emplace("Remove").assign(&this->highlightRemove); + + this->addHighlightTabSignals(); + } + // DISABLED USERS + auto disabledUsers = tabs.appendTab(new QVBoxLayout, "Disabled Users"); + { + auto text = disabledUsers.emplace().getElement(); + + QObject::connect(text, &QTextEdit::textChanged, this, + [this] { this->disabledUsersChangedTimer.start(200); }); + + QObject::connect( + &this->disabledUsersChangedTimer, &QTimer::timeout, this, [text, &settings]() { + QStringList list = text->toPlainText().split("\n", QString::SkipEmptyParts); + list.removeDuplicates(); + settings.highlightUserBlacklist = list.join("\n") + "\n"; + }); + + settings.highlightUserBlacklist.connect([=](const QString &str, auto) { + text->setPlainText(str); // + }); + } + } + } + + // ---- misc + this->disabledUsersChangedTimer.setSingleShot(true); +} + +// +// DISCLAIMER: +// +// If you are trying to learn from reading the chatterino code please ignore this segment. +// +void HighlightingPage::addHighlightTabSignals() +{ + singletons::SettingManager &settings = singletons::SettingManager::getInstance(); + + auto addBtn = this->highlightAdd; + auto editBtn = this->highlightEdit; + auto delBtn = this->highlightRemove; + auto highlights = this->highlightList; + + // Open "Add new highlight" dialog + QObject::connect(addBtn, &QPushButton::clicked, this, [highlights, this, &settings] { + auto show = new QWidget(); + auto box = new QBoxLayout(QBoxLayout::TopToBottom); + + auto edit = new QLineEdit(); + auto add = new QPushButton("Add"); + + auto sound = new QCheckBox("Play sound"); + auto task = new QCheckBox("Flash taskbar"); + + // Save highlight + QObject::connect(add, &QPushButton::clicked, this, [=, &settings] { + if (edit->text().length()) { + QString highlightKey = edit->text(); + highlights->addItem(highlightKey); + + auto properties = settings.highlightProperties.getValue(); + + messages::HighlightPhrase newHighlightProperty; + newHighlightProperty.key = highlightKey; + newHighlightProperty.sound = sound->isChecked(); + newHighlightProperty.alert = task->isChecked(); + + properties.push_back(newHighlightProperty); + + settings.highlightProperties = properties; + + show->close(); + } + }); + box->addWidget(edit); + box->addWidget(add); + box->addWidget(sound); + box->addWidget(task); + show->setLayout(box); + show->show(); + }); + + // Open "Edit selected highlight" dialog + QObject::connect(editBtn, &QPushButton::clicked, this, [highlights, this, &settings] { + if (highlights->selectedItems().isEmpty()) { + // No item selected + return; + } + + QListWidgetItem *selectedHighlight = highlights->selectedItems().first(); + QString highlightKey = selectedHighlight->text(); + auto properties = settings.highlightProperties.getValue(); + auto highlightIt = std::find_if(properties.begin(), properties.end(), + [highlightKey](const auto &highlight) { + return highlight.key == highlightKey; // + }); + + if (highlightIt == properties.end()) { + debug::Log("Unable to find highlight key {} in highlight properties. " + "This is weird", + highlightKey); + return; + } + + messages::HighlightPhrase &selectedSetting = *highlightIt; + auto show = new QWidget(); + auto box = new QBoxLayout(QBoxLayout::TopToBottom); + + auto edit = new QLineEdit(highlightKey); + auto apply = new QPushButton("Apply"); + + auto sound = new QCheckBox("Play sound"); + sound->setChecked(selectedSetting.sound); + auto task = new QCheckBox("Flash taskbar"); + task->setChecked(selectedSetting.alert); + + // Apply edited changes + QObject::connect(apply, &QPushButton::clicked, this, [=, &settings] { + QString newHighlightKey = edit->text(); + + if (newHighlightKey.length() == 0) { + return; + } + + auto properties = settings.highlightProperties.getValue(); + auto highlightIt = + std::find_if(properties.begin(), properties.end(), [=](const auto &highlight) { + return highlight.key == highlightKey; // + }); + + if (highlightIt == properties.end()) { + debug::Log("Unable to find highlight key {} in highlight properties. " + "This is weird", + highlightKey); + return; + } + auto &highlightProperty = *highlightIt; + highlightProperty.key = newHighlightKey; + highlightProperty.sound = sound->isCheckable(); + highlightProperty.alert = task->isCheckable(); + + settings.highlightProperties = properties; + + selectedHighlight->setText(newHighlightKey); + selectedHighlight->setText(newHighlightKey); + + show->close(); + }); + + box->addWidget(edit); + box->addWidget(apply); + box->addWidget(sound); + box->addWidget(task); + show->setLayout(box); + show->show(); + }); + + // Delete selected highlight + QObject::connect(delBtn, &QPushButton::clicked, this, [highlights, &settings] { + if (highlights->selectedItems().isEmpty()) { + // No highlight selected + return; + } + + QListWidgetItem *selectedHighlight = highlights->selectedItems().first(); + QString highlightKey = selectedHighlight->text(); + + auto properties = settings.highlightProperties.getValue(); + properties.erase(std::remove_if(properties.begin(), properties.end(), + [highlightKey](const auto &highlight) { + return highlight.key == highlightKey; // + }), + properties.end()); + + settings.highlightProperties = properties; + + delete selectedHighlight; + }); } } // namespace settingspages } // namespace widgets diff --git a/src/widgets/settingspages/highlightingpage.hpp b/src/widgets/settingspages/highlightingpage.hpp index 6aefea098..12cd20ec5 100644 --- a/src/widgets/settingspages/highlightingpage.hpp +++ b/src/widgets/settingspages/highlightingpage.hpp @@ -1,7 +1,12 @@ #pragma once +#include + #include "widgets/settingspages/settingspage.hpp" +class QPushButton; +class QListWidget; + namespace chatterino { namespace widgets { namespace settingspages { @@ -10,6 +15,16 @@ class HighlightingPage : public SettingsPage { public: HighlightingPage(); + +private: + QListWidget *highlightList; + QPushButton *highlightAdd; + QPushButton *highlightEdit; + QPushButton *highlightRemove; + + QTimer disabledUsersChangedTimer; + + void addHighlightTabSignals(); }; } // namespace settingspages