mirror-chatterino2/src/widgets/dialogs/SettingsDialog.cpp

316 lines
8.4 KiB
C++
Raw Normal View History

2018-06-26 15:11:45 +02:00
#include "widgets/dialogs/SettingsDialog.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2019-09-02 18:59:37 +02:00
#include "singletons/Resources.hpp"
2018-06-26 14:09:39 +02:00
#include "util/LayoutCreator.hpp"
2019-09-02 18:59:37 +02:00
#include "widgets/helper/Button.hpp"
2018-06-26 14:09:39 +02:00
#include "widgets/helper/SettingsDialogTab.hpp"
#include "widgets/settingspages/AboutPage.hpp"
#include "widgets/settingspages/AccountsPage.hpp"
#include "widgets/settingspages/CommandPage.hpp"
2018-06-26 17:42:35 +02:00
#include "widgets/settingspages/ExternalToolsPage.hpp"
2018-10-31 19:45:51 +01:00
#include "widgets/settingspages/GeneralPage.hpp"
2018-06-26 14:09:39 +02:00
#include "widgets/settingspages/HighlightingPage.hpp"
2018-07-05 16:45:34 +02:00
#include "widgets/settingspages/IgnoresPage.hpp"
2018-06-26 17:42:35 +02:00
#include "widgets/settingspages/KeyboardSettingsPage.hpp"
2018-06-26 14:09:39 +02:00
#include "widgets/settingspages/ModerationPage.hpp"
#include "widgets/settingspages/NotificationPage.hpp"
2017-01-02 03:02:32 +01:00
2018-01-12 23:33:04 +01:00
#include <QDialogButtonBox>
#include <QLineEdit>
2017-01-01 18:43:52 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
2018-01-02 02:15:11 +01:00
SettingsDialog *SettingsDialog::handle = nullptr;
2017-01-01 18:43:52 +01:00
SettingsDialog::SettingsDialog()
2019-09-08 21:45:46 +02:00
: BaseWindow(BaseWindow::DisableCustomScaling)
2017-01-01 18:43:52 +01:00
{
2019-09-03 12:46:22 +02:00
this->setWindowTitle("Chatterino Settings");
this->themeChangedEvent();
this->scaleChangedEvent(this->scale());
2018-01-12 23:09:05 +01:00
this->initUi();
2017-07-02 12:36:50 +02:00
this->addTabs();
2018-10-31 20:57:29 +01:00
this->overrideBackgroundColor_ = QColor("#111111");
this->scaleChangedEvent(
this->scale()); // execute twice to fix performance + width of item
2018-10-31 19:45:51 +01:00
this->resize(815, 600);
2017-01-02 03:02:32 +01:00
}
2018-01-12 23:09:05 +01:00
void SettingsDialog::initUi()
{
2019-09-08 21:45:46 +02:00
auto outerBox = LayoutCreator<QWidget>(this->getLayoutContainer())
2019-09-03 11:15:38 +02:00
.setLayoutType<QVBoxLayout>()
.withoutSpacing();
// TOP
auto title = outerBox.emplace<PageHeader>();
auto edit = LayoutCreator<PageHeader>(title.getElement())
.setLayoutType<QHBoxLayout>()
.withoutMargin()
.emplace<QLineEdit>()
.assign(&this->ui_.search);
edit->setPlaceholderText("Find in settings...");
QObject::connect(edit.getElement(), &QLineEdit::textChanged, this,
&SettingsDialog::filterElements);
// CENTER
auto centerBox =
outerBox.emplace<QHBoxLayout>().withoutMargin().withoutSpacing();
// left side (tabs)
centerBox.emplace<QWidget>()
2018-06-11 15:04:54 +02:00
.assign(&this->ui_.tabContainerContainer)
2019-09-03 11:15:38 +02:00
.setLayoutType<QVBoxLayout>()
2018-01-12 23:09:05 +01:00
.withoutMargin()
2018-06-11 15:04:54 +02:00
.assign(&this->ui_.tabContainer);
2017-01-02 03:02:32 +01:00
2019-09-03 11:15:38 +02:00
// right side (pages)
2019-09-02 18:59:37 +02:00
auto right =
2019-09-03 11:15:38 +02:00
centerBox.emplace<QVBoxLayout>().withoutMargin().withoutSpacing();
2017-01-02 03:02:32 +01:00
{
2018-08-06 21:17:03 +02:00
right.emplace<QStackedLayout>()
.assign(&this->ui_.pageStack)
.withoutMargin();
}
2017-01-22 12:46:35 +01:00
2018-10-31 19:45:51 +01:00
this->ui_.pageStack->setMargin(0);
2019-09-03 11:15:38 +02:00
outerBox->addSpacing(12);
// BOTTOM
auto buttons = outerBox.emplace<QDialogButtonBox>(Qt::Horizontal);
{
this->ui_.okButton =
buttons->addButton("Ok", QDialogButtonBox::YesRole);
this->ui_.cancelButton =
buttons->addButton("Cancel", QDialogButtonBox::NoRole);
}
2018-01-12 23:09:05 +01:00
// ---- misc
2018-06-11 15:04:54 +02:00
this->ui_.tabContainerContainer->setObjectName("tabWidget");
this->ui_.pageStack->setObjectName("pages");
2017-01-22 12:46:35 +01:00
2018-08-06 21:17:03 +02:00
QObject::connect(this->ui_.okButton, &QPushButton::clicked, this,
&SettingsDialog::onOkClicked);
2018-06-11 15:04:54 +02:00
QObject::connect(this->ui_.cancelButton, &QPushButton::clicked, this,
2018-07-06 19:23:47 +02:00
&SettingsDialog::onCancelClicked);
}
2017-01-22 12:46:35 +01:00
2019-09-03 11:15:38 +02:00
void SettingsDialog::filterElements(const QString &text)
{
// filter elements and hide pages
for (auto &&page : this->pages_)
{
// filterElements returns true if anything on the page matches the search query
page->tab()->setVisible(page->filterElements(text));
}
// find next visible page
if (this->lastSelectedByUser_ && this->lastSelectedByUser_->isVisible())
{
this->selectTab(this->lastSelectedByUser_, false);
}
else if (!this->selectedTab_->isVisible())
{
for (auto &&tab : this->tabs_)
{
if (tab->isVisible())
{
this->selectTab(tab, false);
break;
}
}
}
// remove duplicate spaces
bool shouldShowSpace = false;
for (int i = 0; i < this->ui_.tabContainer->count(); i++)
{
auto item = this->ui_.tabContainer->itemAt(i);
if (auto x = dynamic_cast<QSpacerItem *>(item); x)
{
x->changeSize(10, shouldShowSpace ? int(16 * this->scale()) : 0);
shouldShowSpace = false;
}
else if (item->widget())
{
shouldShowSpace |= item->widget()->isVisible();
}
}
}
2018-01-12 23:09:05 +01:00
SettingsDialog *SettingsDialog::getHandle()
{
2018-01-12 23:09:05 +01:00
return SettingsDialog::handle;
}
2017-01-22 12:46:35 +01:00
2018-01-12 23:09:05 +01:00
void SettingsDialog::addTabs()
{
2018-10-31 19:45:51 +01:00
this->ui_.tabContainer->setMargin(0);
2018-06-11 15:04:54 +02:00
this->ui_.tabContainer->setSpacing(0);
2018-01-24 15:34:04 +01:00
2019-09-03 11:15:38 +02:00
this->ui_.tabContainer->setContentsMargins(0, 20, 0, 20);
2018-10-31 19:45:51 +01:00
this->addTab(new GeneralPage);
2018-04-25 20:35:32 +02:00
2018-06-11 15:04:54 +02:00
this->ui_.tabContainer->addSpacing(16);
2018-04-25 20:35:32 +02:00
2018-10-31 19:45:51 +01:00
this->addTab(new AccountsPage);
2018-06-11 15:04:54 +02:00
this->ui_.tabContainer->addSpacing(16);
2018-04-25 20:35:32 +02:00
2018-06-26 17:06:17 +02:00
this->addTab(new CommandPage);
this->addTab(new HighlightingPage);
2018-07-05 16:22:25 +02:00
this->addTab(new IgnoresPage);
2018-01-24 15:34:04 +01:00
2018-06-11 15:04:54 +02:00
this->ui_.tabContainer->addSpacing(16);
2018-01-24 15:34:04 +01:00
2018-06-26 17:06:17 +02:00
this->addTab(new KeyboardSettingsPage);
this->addTab(this->ui_.moderationPage = new ModerationPage);
this->addTab(new NotificationPage);
2018-06-26 17:06:17 +02:00
this->addTab(new ExternalToolsPage);
2018-06-11 15:04:54 +02:00
this->ui_.tabContainer->addStretch(1);
2018-06-26 17:06:17 +02:00
this->addTab(new AboutPage, Qt::AlignBottom);
2017-01-02 03:02:32 +01:00
}
2018-06-26 17:06:17 +02:00
void SettingsDialog::addTab(SettingsPage *page, Qt::Alignment alignment)
2017-01-02 03:02:32 +01:00
{
2018-01-13 00:18:18 +01:00
auto tab = new SettingsDialogTab(this, page, page->getIconResource());
page->setTab(tab);
2017-01-02 03:02:32 +01:00
2018-06-11 15:04:54 +02:00
this->ui_.pageStack->addWidget(page);
this->ui_.tabContainer->addWidget(tab, 0, alignment);
2018-07-06 19:23:47 +02:00
this->tabs_.push_back(tab);
this->pages_.push_back(page);
2017-01-02 03:02:32 +01:00
2018-10-21 13:43:02 +02:00
if (this->tabs_.size() == 1)
{
2018-07-06 19:23:47 +02:00
this->selectTab(tab);
2017-01-02 03:02:32 +01:00
}
}
void SettingsDialog::selectTab(SettingsDialogTab *tab, bool byUser)
2017-01-02 03:02:32 +01:00
{
2018-06-11 15:04:54 +02:00
this->ui_.pageStack->setCurrentWidget(tab->getSettingsPage());
2017-01-02 03:02:32 +01:00
2018-10-21 13:43:02 +02:00
if (this->selectedTab_ != nullptr)
{
2018-07-06 19:23:47 +02:00
this->selectedTab_->setSelected(false);
this->selectedTab_->setStyleSheet("color: #FFF");
2017-01-02 03:02:32 +01:00
}
2017-01-01 18:43:52 +01:00
2017-01-02 03:02:32 +01:00
tab->setSelected(true);
2018-10-31 20:57:29 +01:00
tab->setStyleSheet("background: #222; color: #4FC3F7;"
2019-09-03 11:15:38 +02:00
"/*border: 1px solid #555; border-right: none;*/");
2018-07-06 19:23:47 +02:00
this->selectedTab_ = tab;
if (byUser)
{
this->lastSelectedByUser_ = tab;
}
2017-01-01 18:43:52 +01:00
}
void SettingsDialog::selectPage(SettingsPage *page)
{
assert(page);
assert(page->tab());
this->selectTab(page->tab());
}
void SettingsDialog::showDialog(SettingsDialogPreference preferredTab)
{
static SettingsDialog *instance = new SettingsDialog();
instance->refresh();
2018-10-21 13:43:02 +02:00
switch (preferredTab)
{
case SettingsDialogPreference::Accounts:
instance->selectTab(instance->tabs_.at(1));
break;
case SettingsDialogPreference::ModerationActions:
instance->selectPage(instance->ui_.moderationPage);
instance->ui_.moderationPage->selectModerationActions();
break;
default:;
}
instance->show();
instance->activateWindow();
instance->raise();
instance->setFocus();
}
void SettingsDialog::refresh()
{
getSettings()->saveSnapshot();
2018-10-21 13:43:02 +02:00
for (auto *tab : this->tabs_)
{
tab->getSettingsPage()->onShow();
}
}
2018-01-25 20:49:49 +01:00
void SettingsDialog::scaleChangedEvent(float newDpi)
{
QFile file(":/qss/settings.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
styleSheet.replace("<font-size>", QString::number(int(14 * newDpi)));
styleSheet.replace("<checkbox-size>", QString::number(int(14 * newDpi)));
2018-10-21 13:43:02 +02:00
for (SettingsDialogTab *tab : this->tabs_)
{
tab->setFixedHeight(int(30 * newDpi));
}
this->setStyleSheet(styleSheet);
if (this->ui_.tabContainerContainer)
this->ui_.tabContainerContainer->setFixedWidth(int(150 * newDpi));
}
2018-07-06 17:11:37 +02:00
void SettingsDialog::themeChangedEvent()
2018-04-18 09:33:05 +02:00
{
2018-07-06 17:11:37 +02:00
BaseWindow::themeChangedEvent();
2018-04-18 09:33:05 +02:00
QPalette palette;
2018-10-31 20:57:29 +01:00
palette.setColor(QPalette::Background, QColor("#111"));
2018-04-18 09:33:05 +02:00
this->setPalette(palette);
}
void SettingsDialog::showEvent(QShowEvent *)
{
this->ui_.search->setText("");
}
2018-01-12 23:09:05 +01:00
///// Widget creation helpers
2018-07-06 19:23:47 +02:00
void SettingsDialog::onOkClicked()
2017-01-24 19:51:57 +01:00
{
2018-10-29 19:20:23 +01:00
pajlada::Settings::SettingManager::gSave();
2017-01-24 19:51:57 +01:00
this->close();
}
2018-07-06 19:23:47 +02:00
void SettingsDialog::onCancelClicked()
2017-01-24 19:51:57 +01:00
{
2018-10-21 13:43:02 +02:00
for (auto &tab : this->tabs_)
{
2018-01-12 23:09:05 +01:00
tab->getSettingsPage()->cancel();
}
getSettings()->restoreSnapshot();
2017-01-24 19:51:57 +01:00
this->close();
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino