mirror-chatterino2/src/widgets/settingspages/SettingsPage.cpp

161 lines
4.1 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "SettingsPage.hpp"
2018-01-12 23:09:05 +01:00
#include "Application.hpp"
#include "singletons/WindowManager.hpp"
2018-01-12 23:09:05 +01:00
#include <QDebug>
2018-09-04 22:29:21 +02:00
#include <QPainter>
#include <util/FunctionEventFilter.hpp>
2018-01-12 23:09:05 +01:00
namespace chatterino {
2019-09-02 16:39:21 +02:00
bool filterItemsRec(QObject *object, const QString &query)
{
2019-09-02 16:39:21 +02:00
bool any{};
for (auto &&child : object->children())
{
2019-09-02 16:39:21 +02:00
auto setOpacity = [&](auto *widget, bool condition) {
any |= condition;
widget->greyedOut = !condition;
widget->update();
};
if (auto x = dynamic_cast<SCheckBox *>(child); x)
{
setOpacity(x, x->text().contains(query, Qt::CaseInsensitive));
}
else if (auto x = dynamic_cast<SLabel *>(child); x)
{
setOpacity(x, x->text().contains(query, Qt::CaseInsensitive));
}
else if (auto x = dynamic_cast<SComboBox *>(child); x)
{
setOpacity(x, [=]() {
for (int i = 0; i < x->count(); i++)
{
if (x->itemText(i).contains(query, Qt::CaseInsensitive))
return true;
}
return false;
}());
}
else
{
2019-09-02 16:39:21 +02:00
any |= filterItemsRec(child, query);
}
}
2019-09-02 16:39:21 +02:00
return any;
}
2018-07-06 19:23:47 +02:00
SettingsPage::SettingsPage(const QString &name, const QString &iconResource)
: name_(name)
, iconResource_(iconResource)
2018-01-12 23:09:05 +01:00
{
}
2019-09-02 16:39:21 +02:00
bool SettingsPage::filterElements(const QString &query)
{
2019-09-02 16:39:21 +02:00
return filterItemsRec(this, query) || query.isEmpty() ||
2019-09-02 19:00:17 +02:00
this->name_.contains(query, Qt::CaseInsensitive);
}
2018-01-12 23:09:05 +01:00
const QString &SettingsPage::getName()
{
2018-07-06 19:23:47 +02:00
return this->name_;
2018-01-12 23:09:05 +01:00
}
2018-01-13 00:18:18 +01:00
const QString &SettingsPage::getIconResource()
{
2018-07-06 19:23:47 +02:00
return this->iconResource_;
2018-01-13 00:18:18 +01:00
}
SettingsDialogTab *SettingsPage::tab() const
{
return this->tab_;
}
void SettingsPage::setTab(SettingsDialogTab *tab)
{
this->tab_ = tab;
}
2018-01-12 23:09:05 +01:00
void SettingsPage::cancel()
{
2018-07-06 19:23:47 +02:00
this->onCancel_.invoke();
2018-01-12 23:09:05 +01:00
}
2018-08-06 21:17:03 +02:00
QCheckBox *SettingsPage::createCheckBox(
const QString &text, pajlada::Settings::Setting<bool> &setting)
2018-01-12 23:09:05 +01:00
{
QCheckBox *checkbox = new SCheckBox(text);
2018-01-12 23:09:05 +01:00
// update when setting changes
setting.connect(
[checkbox](const bool &value, auto) {
checkbox->setChecked(value); //
},
2018-07-06 19:23:47 +02:00
this->managedConnections_);
2018-01-12 23:09:05 +01:00
// update setting on toggle
2018-08-06 21:17:03 +02:00
QObject::connect(checkbox, &QCheckBox::toggled, this,
[&setting](bool state) {
setting = state;
getApp()->windows->forceLayoutChannelViews();
2018-08-06 21:17:03 +02:00
});
2018-01-12 23:09:05 +01:00
return checkbox;
}
2018-08-06 21:17:03 +02:00
QComboBox *SettingsPage::createComboBox(
const QStringList &items, pajlada::Settings::Setting<QString> &setting)
2018-01-12 23:09:05 +01:00
{
QComboBox *combo = new SComboBox();
2018-01-12 23:09:05 +01:00
// update setting on toogle
combo->addItems(items);
// update when setting changes
2018-08-06 21:17:03 +02:00
setting.connect(
[combo](const QString &value, auto) { combo->setCurrentText(value); },
this->managedConnections_);
2018-01-12 23:09:05 +01:00
2018-08-06 21:17:03 +02:00
QObject::connect(
combo, &QComboBox::currentTextChanged,
[&setting](const QString &newValue) { setting = newValue; });
2018-01-12 23:09:05 +01:00
return combo;
}
2018-08-06 21:17:03 +02:00
QLineEdit *SettingsPage::createLineEdit(
pajlada::Settings::Setting<QString> &setting)
2018-01-12 23:09:05 +01:00
{
QLineEdit *edit = new QLineEdit();
edit->setText(setting);
// update when setting changes
2018-08-06 21:17:03 +02:00
QObject::connect(
edit, &QLineEdit::textChanged,
[&setting](const QString &newValue) { setting = newValue; });
2018-01-12 23:09:05 +01:00
return edit;
}
2018-01-12 23:33:04 +01:00
2018-08-06 21:17:03 +02:00
QSpinBox *SettingsPage::createSpinBox(pajlada::Settings::Setting<int> &setting,
int min, int max)
{
QSpinBox *w = new QSpinBox;
w->setMinimum(min);
w->setMaximum(max);
setting.connect([w](const int &value, auto) { w->setValue(value); });
QObject::connect(w, QOverload<int>::of(&QSpinBox::valueChanged),
[&setting](int value) { setting.setValue(value); });
return w;
}
2018-01-12 23:09:05 +01:00
} // namespace chatterino