looper/backends/ui/qt/preferences.cpp
Zachary Hall 791c5103ea
Some checks failed
Build / download-system-deps (push) Successful in 42s
Build / build-gentoo (push) Failing after 1m5s
Build / get-source-code (push) Successful in 3m50s
Build / build-deb (push) Successful in 5m27s
Build / build-appimage (push) Successful in 1m9s
Build / build-android (push) Failing after 4s
Build / build-windows (push) Failing after 3m45s
Finish cat support
2024-12-23 14:06:11 -08:00

179 lines
7.5 KiB
C++

#include "preferences.h"
#include <QMenu>
#include <QBoxLayout>
#include <QBuffer>
#include <QImage>
#include <QDataStream>
#include <QButtonGroup>
#include <QGroupBox>
#include <backend.hpp>
#include <options.hpp>
#include <cats.hpp>
#include <log.hpp>
using namespace Looper::Options;
PrefsWindow::PrefsWindow() {
for (auto &cat : get_cat_data()) {
switch (cat.get_type()) {
case CatDataType::Memory: {
auto mem_cat = cat.get_memory_cat();
QPixmap pixmap;
pixmap.loadFromData((const uchar*)mem_cat.get_ptr(), mem_cat.get_len());
cats[cat.get_name()] = pixmap;
} break;
case CatDataType::File: {
try {
cats[cat.get_name()] = QPixmap(cat.get_path().c_str());
} catch (std::exception e) {
WARNING.writefln("Failed to load cat %s at path %s: %s", cat.get_name().c_str(), cat.get_path().c_str(), e.what());
}
} break;
}
}
auto *root_layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
this->setLayout(root_layout);
restart_warning = new QLabel("A restart is needed to apply some changes.", this);
restart_warning->hide();
root_layout->addWidget(restart_warning);
frontend_btn = new QPushButton(this);
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);
QGroupBox *frame = new QGroupBox("Labels and Icons", this);
auto *label_settings_group = new QBoxLayout(QBoxLayout::TopToBottom, this);
frame->setLayout(label_settings_group);
labels_only = new QRadioButton("Labels Only", frame);
labels_only->connect(labels_only, &QRadioButton::pressed, [=,this]() {
this->new_label_setting = "labels";
this->set_options_changed(true);
});
icons_only = new QRadioButton("Icons Only", frame);
icons_only->connect(icons_only, &QRadioButton::pressed, [=,this]() {
this->new_label_setting = "icons";
this->set_options_changed(true);
});
both_labels_icons = new QRadioButton("Both", frame);
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);
cat_enable = new QCheckBox("Enable Cat", this);
QObject::connect(cat_enable, &QCheckBox::toggled, [=,this]() {
this->enable_cat = cat_enable->isChecked();
this->set_options_changed(true);
});
root_layout->addWidget(cat_enable);
QGroupBox *catFrame = new QGroupBox("Cat Selection", this);
auto *cat_btns_layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
catFrame->setLayout(cat_btns_layout);
QButtonGroup *cat_btn_group = new QButtonGroup(catFrame);
cat_btn_group->setExclusive(true);
for (auto &kv : cats) {
auto id = kv.first;
auto pixmap = kv.second;
QWidget *cat_view = new QWidget(this);
QBoxLayout *cat_box = new QBoxLayout(QBoxLayout::LeftToRight, this);
QRadioButton *cat_radio = new QRadioButton(id.c_str(), cat_view);
cat_btn_group->addButton(cat_radio);
cat_view->setLayout(cat_box);
cat_box->addWidget(cat_radio);
QLabel *cat_img = new QLabel(cat_radio);
cat_img->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
float w = cat_img->width(), h = cat_img->height();
cat_img->setPixmap(pixmap.scaled(w, h, Qt::KeepAspectRatio));
cat_img->setAlignment(Qt::Alignment::enum_type::AlignRight);
cat_box->addWidget(cat_img);
QObject::connect(cat_radio, &QRadioButton::pressed, [=,this]() {
this->cat_setting = id;
this->set_options_changed(true);
});
cat_btns_layout->addWidget(cat_view);
cat_btns[id] = cat_radio;
}
root_layout->addWidget(catFrame);
QWidget *btn_view = new QWidget(this);
QBoxLayout *btn_box = new QBoxLayout(QBoxLayout::LeftToRight, this);
revert_btn = new QPushButton("Revert", btn_view);
QObject::connect(revert_btn, &QPushButton::pressed, this, &PrefsWindow::revert);
apply_btn = new QPushButton("Apply", btn_view);
QObject::connect(apply_btn, &QPushButton::pressed, this, &PrefsWindow::apply);
btn_view->setLayout(btn_box);
btn_box->addWidget(revert_btn);
btn_box->addWidget(apply_btn);
btn_view->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
root_layout->addWidget(btn_view);
revert();
setWindowTitle("Looper Preferences");
}
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);
load_options();
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());
enable_cat = get_option<bool>("ui.enable_cat");
cat_enable->setCheckState(enable_cat ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
cat_setting = get_option<std::string>("ui.cat", cats.empty() ? "" : cats.begin()->first);
std::map<QRadioButton*, bool> radio_btn_values = {{labels_only, false}, {icons_only, false}, {both_labels_icons, false}};
if (new_label_setting == "labels") {
radio_btn_values[labels_only] = true;
} else if (new_label_setting == "icons") {
radio_btn_values[icons_only] = true;
} else if (new_label_setting == "both") {
radio_btn_values[both_labels_icons] = true;
}
for (auto &kv : radio_btn_values) {
kv.first->setChecked(kv.second);
}
if (cat_btns.contains(cat_setting)) cat_btns[cat_setting]->setChecked(true);
send_cat_signal();
update_label_setting();
}
void PrefsWindow::send_cat_signal() {
if (enable_cat && cats.contains(cat_setting)) emit(cat_set(cats[cat_setting]));
else emit(cat_unset());
}
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();
set_option<bool>("ui.enable_cat", enable_cat);
set_option<std::string>("ui.cat", cat_setting);
send_cat_signal();
save_options();
}