looper/backends/ui/qt/preferences.cpp

166 lines
6.9 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-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: {
QImage image;
QBuffer buffer;
QDataStream in(&buffer);
auto mem_cat = cat.get_memory_cat();
buffer.setData(const_cast<char*>((const char*)mem_cat.get_ptr()), mem_cat.get_len());
in >> image;
cats[cat.get_name()] = QPixmap::fromImage(image);
} 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);
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);
QFrame *frame = new QFrame(this);
2024-11-20 08:15:45 -08:00
frame->setWindowTitle("Labels and Icons");
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);
QObject::connect(cat_enable, &QCheckBox::pressed, [=,this]() {
this->enable_cat = cat_enable->isChecked();
this->set_options_changed(true);
});
root_layout->addWidget(cat_enable);
QFrame *catFrame = new QFrame(this);
catFrame->setWindowTitle("Cat Selection");
auto *cat_btns_layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
catFrame->setLayout(cat_btns_layout);
for (auto &kv : cats) {
auto id = kv.first;
auto pixmap = kv.second;
QRadioButton *cat_radio = new QRadioButton(id.c_str(), catFrame);
QLabel *cat_img = new QLabel(cat_radio);
cat_img->setPixmap(pixmap);
cat_img->setAlignment(Qt::Alignment::enum_type::AlignRight);
QObject::connect(cat_radio, &QRadioButton::pressed, [=,this]() {
this->cat_setting = id;
this->set_options_changed(true);
});
cat_btns_layout->addWidget(cat_radio);
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);
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);
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) {
kv.first->setDown(kv.second);
}
if (cat_btns.contains(cat_setting)) cat_btns[cat_setting]->setDown(true);
2024-11-20 08:15:45 -08:00
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();
2024-12-21 14:23:00 -08:00
set_option<bool>("ui.enable_cat", enable_cat);
set_option<std::string>("ui.cat", cat_setting);
if (enable_cat && cats.contains(cat_setting)) emit(cat_set(cats[cat_setting]));
else emit(cat_unset());
}