looper/backends/ui/qt/preferences.cpp

95 lines
3.8 KiB
C++
Raw Normal View History

2024-11-20 08:15:45 -08:00
#include "preferences.h"
#include <QMenu>
#include <QBoxLayout>
#include <backend.hpp>
#include <options.hpp>
using namespace Looper::Options;
PrefsWindow::PrefsWindow() {
auto *root_layout = new QBoxLayout(QBoxLayout::TopToBottom);
this->setLayout(root_layout);
restart_warning = new QLabel("A restart is needed to apply some changes.");
restart_warning->hide();
root_layout->addWidget(restart_warning);
frontend_btn = new QPushButton();
frontend_menu = new QMenu();
for (auto &kv : UIBackend::backends) {
UIBackend *backend = kv.second;
const char *name = strdup(backend->get_name().c_str());
QAction *action = new QAction(name);
action->connect(action, &QAction::triggered, [=,this]() {
this->new_frontend = backend->get_id();
frontend_btn->setText(strdup(this->new_frontend.c_str()));
this->set_options_changed(true);
});
frontend_menu->addAction(action);
frontend_options.push_back(action);
}
frontend_btn->setMenu(frontend_menu);
root_layout->addWidget(frontend_btn);
QFrame *frame = new QFrame();
frame->setWindowTitle("Labels and Icons");
auto *label_settings_group = new QBoxLayout(QBoxLayout::TopToBottom);
frame->setLayout(label_settings_group);
labels_only = new QRadioButton("Labels Only");
labels_only->connect(labels_only, &QRadioButton::pressed, [=,this]() {
this->new_label_setting = "labels";
this->set_options_changed(true);
});
icons_only = new QRadioButton("Icons Only");
icons_only->connect(icons_only, &QRadioButton::pressed, [=,this]() {
this->new_label_setting = "icons";
this->set_options_changed(true);
});
both_labels_icons = new QRadioButton("Both");
both_labels_icons->connect(both_labels_icons, &QRadioButton::pressed, [=,this]() {
this->new_label_setting = "both";
this->set_options_changed(true);
});
label_settings_group->addWidget(labels_only);
label_settings_group->addWidget(icons_only);
label_settings_group->addWidget(both_labels_icons);
root_layout->addWidget(frame);
revert_btn = new QPushButton("Revert");
QObject::connect(revert_btn, &QPushButton::pressed, this, &PrefsWindow::revert);
apply_btn = new QPushButton("Apply");
QObject::connect(apply_btn, &QPushButton::pressed, this, &PrefsWindow::apply);
QWidget *btn_view = new QWidget();
QBoxLayout *btn_box = new QBoxLayout(QBoxLayout::LeftToRight);
btn_view->setLayout(btn_box);
btn_box->addWidget(revert_btn);
btn_box->addWidget(apply_btn);
root_layout->addWidget(btn_view);
revert();
}
void PrefsWindow::set_options_changed(bool changed) {
this->revert_btn->setEnabled(changed);
this->apply_btn->setEnabled(changed);
}
void PrefsWindow::update_label_setting() {
bool labels_enabled = true;
bool icons_enabled = true;
if (new_label_setting == "icons") {
labels_enabled = false;
} else if (new_label_setting == "labels") {
icons_enabled = false;
}
emit(settings_changed(labels_enabled, icons_enabled));
}
void PrefsWindow::revert() {
set_options_changed(false);
new_label_setting = get_option<std::string>("ui.label_setting", "icons");
new_frontend = get_option<std::string>("ui.frontend", "qt");
if (new_frontend != "qt") restart_warning->show();
else restart_warning->hide();
frontend_btn->setText(new_frontend.c_str());
update_label_setting();
}
void PrefsWindow::apply() {
set_options_changed(false);
set_option<std::string>("ui.label_setting", new_label_setting);
set_option<std::string>("ui.frontend", new_frontend);
if (new_frontend != "qt") restart_warning->show();
else restart_warning->hide();
frontend_btn->setText(new_frontend.c_str());
update_label_setting();
}