looper/backends/ui/qt/preferences.cpp

180 lines
7.5 KiB
C++
Raw Normal View History

2024-11-20 08:15:45 -08:00
#include "preferences.h"
#include <QMenu>
#include <QBoxLayout>
2024-12-21 14:23:00 -08:00
#include <QBuffer>
#include <QImage>
#include <QDataStream>
2024-12-23 14:06:11 -08:00
#include <QButtonGroup>
#include <QGroupBox>
2024-11-20 08:15:45 -08:00
#include <backend.hpp>
#include <options.hpp>
2024-12-21 14:23:00 -08:00
#include <cats.hpp>
#include <log.hpp>
2024-11-20 08:15:45 -08:00
using namespace Looper::Options;
PrefsWindow::PrefsWindow() {
2024-12-21 14:23:00 -08:00
for (auto &cat : get_cat_data()) {
switch (cat.get_type()) {
case CatDataType::Memory: {
auto mem_cat = cat.get_memory_cat();
2024-12-23 14:06:11 -08:00
QPixmap pixmap;
pixmap.loadFromData((const uchar*)mem_cat.get_ptr(), mem_cat.get_len());
cats[cat.get_name()] = pixmap;
2024-12-21 14:23:00 -08:00
} break;
case CatDataType::File: {
try {
2024-12-23 14:06:11 -08:00
cats[cat.get_name()] = QPixmap(cat.get_path().c_str());
2024-12-21 14:23:00 -08:00
} 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);
2024-11-20 08:15:45 -08:00
this->setLayout(root_layout);
restart_warning = new QLabel("A restart is needed to apply some changes.", this);
2024-11-20 08:15:45 -08:00
restart_warning->hide();
root_layout->addWidget(restart_warning);
frontend_btn = new QPushButton(this);
2024-11-20 08:15:45 -08:00
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);
2024-12-23 14:06:11 -08:00
QGroupBox *frame = new QGroupBox("Labels and Icons", this);
auto *label_settings_group = new QBoxLayout(QBoxLayout::TopToBottom, this);
2024-11-20 08:15:45 -08:00
frame->setLayout(label_settings_group);
labels_only = new QRadioButton("Labels Only", frame);
2024-11-20 08:15:45 -08:00
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);
2024-11-20 08:15:45 -08:00
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);
2024-11-20 08:15:45 -08:00
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);
2024-12-21 14:23:00 -08:00
cat_enable = new QCheckBox("Enable Cat", this);
2024-12-23 14:06:11 -08:00
QObject::connect(cat_enable, &QCheckBox::toggled, [=,this]() {
2024-12-21 14:23:00 -08:00
this->enable_cat = cat_enable->isChecked();
this->set_options_changed(true);
});
root_layout->addWidget(cat_enable);
2024-12-23 14:06:11 -08:00
QGroupBox *catFrame = new QGroupBox("Cat Selection", this);
2024-12-21 14:23:00 -08:00
auto *cat_btns_layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
catFrame->setLayout(cat_btns_layout);
2024-12-23 14:06:11 -08:00
QButtonGroup *cat_btn_group = new QButtonGroup(catFrame);
cat_btn_group->setExclusive(true);
2024-12-21 14:23:00 -08:00
for (auto &kv : cats) {
auto id = kv.first;
auto pixmap = kv.second;
2024-12-23 14:06:11 -08:00
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);
2024-12-21 14:23:00 -08:00
QLabel *cat_img = new QLabel(cat_radio);
2024-12-23 14:06:11 -08:00
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));
2024-12-21 14:23:00 -08:00
cat_img->setAlignment(Qt::Alignment::enum_type::AlignRight);
2024-12-23 14:06:11 -08:00
cat_box->addWidget(cat_img);
2024-12-21 14:23:00 -08:00
QObject::connect(cat_radio, &QRadioButton::pressed, [=,this]() {
this->cat_setting = id;
this->set_options_changed(true);
});
2024-12-23 14:06:11 -08:00
cat_btns_layout->addWidget(cat_view);
2024-12-21 14:23:00 -08:00
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);
2024-11-20 08:15:45 -08:00
QObject::connect(revert_btn, &QPushButton::pressed, this, &PrefsWindow::revert);
apply_btn = new QPushButton("Apply", btn_view);
2024-11-20 08:15:45 -08:00
QObject::connect(apply_btn, &QPushButton::pressed, this, &PrefsWindow::apply);
btn_view->setLayout(btn_box);
btn_box->addWidget(revert_btn);
btn_box->addWidget(apply_btn);
2024-12-23 14:06:11 -08:00
btn_view->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
2024-11-20 08:15:45 -08:00
root_layout->addWidget(btn_view);
revert();
setWindowTitle("Looper Preferences");
2024-11-20 08:15:45 -08:00
}
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);
2024-12-23 14:06:11 -08:00
load_options();
2024-11-20 08:15:45 -08:00
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());
2024-12-21 14:23:00 -08:00
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) {
2024-12-23 14:06:11 -08:00
kv.first->setChecked(kv.second);
2024-12-21 14:23:00 -08:00
}
2024-12-23 14:06:11 -08:00
if (cat_btns.contains(cat_setting)) cat_btns[cat_setting]->setChecked(true);
send_cat_signal();
2024-11-20 08:15:45 -08:00
update_label_setting();
}
2024-12-23 14:06:11 -08:00
void PrefsWindow::send_cat_signal() {
if (enable_cat && cats.contains(cat_setting)) emit(cat_set(cats[cat_setting]));
else emit(cat_unset());
}
2024-11-20 08:15:45 -08:00
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();
2024-12-21 14:23:00 -08:00
set_option<bool>("ui.enable_cat", enable_cat);
set_option<std::string>("ui.cat", cat_setting);
2024-12-23 14:06:11 -08:00
send_cat_signal();
save_options();
}