284 lines
7.2 KiB
C++
284 lines
7.2 KiB
C++
#include "buttonmanager.h"
|
|
#include "ui_buttonmanager.h"
|
|
|
|
#include "addnewwhat.h"
|
|
#include "editsample.h"
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <Log.h>
|
|
|
|
ButtonManager::ButtonManager(Config& conf, MainWindow* parent) : QDialog(parent), ui(new Ui::ButtonManager), mainW(parent), mainConfig(conf) {
|
|
ui->setupUi(this);
|
|
|
|
loadConfig();
|
|
|
|
QStringList labels;
|
|
labels.push_back("Row");
|
|
labels.push_back("Button");
|
|
labels.push_back("Sample");
|
|
labels.push_back("File");
|
|
|
|
ui->buttonTreeWidget->setHeaderLabels(labels);
|
|
}
|
|
|
|
ButtonManager::~ButtonManager() {
|
|
delete ui;
|
|
}
|
|
|
|
void ButtonManager::accept() {
|
|
saveChanges();
|
|
|
|
QDialog::accept();
|
|
}
|
|
|
|
void ButtonManager::reject() {
|
|
// nothing?
|
|
QDialog::reject();
|
|
}
|
|
|
|
void ButtonManager::addButton() {
|
|
AddNewWhat whatshouldbeadded(this);
|
|
whatshouldbeadded.exec();
|
|
AddNewWhat::ReturnCode type = whatshouldbeadded.returnCode;
|
|
if(type == AddNewWhat::ReturnCode::Row) {
|
|
// create new row
|
|
//TODO: when a row is selected: add after the selected row
|
|
workingConfig.buttons.push_back({}); // create a empty vector
|
|
RowItem* row = new RowItem(ui->buttonTreeWidget->invisibleRootItem(), workingConfig.buttons.size()-1, workingConfig);
|
|
|
|
//select the new row
|
|
select(row);
|
|
|
|
return;
|
|
}
|
|
if(type == AddNewWhat::ReturnCode::Button) {
|
|
//get row to create button in
|
|
RowItem* row = getCurrentRow();
|
|
|
|
if(row) {
|
|
//create new button in config
|
|
row->getConfig().push_back({});
|
|
|
|
// create new button GUI
|
|
ButtonItem* newButton = new ButtonItem(row, row->getConfig().size()-1, workingConfig);
|
|
|
|
//select the new button
|
|
select(newButton);
|
|
|
|
// start editing
|
|
ui->buttonTreeWidget->editItem(newButton, 1);
|
|
} else {
|
|
Log::error << "no row selected";
|
|
}
|
|
return;
|
|
}
|
|
if(type == AddNewWhat::ReturnCode::Sample) {
|
|
// create new sample
|
|
ButtonItem* btn = getCurrentButton();
|
|
|
|
if(btn) {
|
|
// button to add sample to exists -> start sample editor
|
|
QFileDialog fileselector(this);
|
|
fileselector.setFileMode(QFileDialog::ExistingFile);
|
|
fileselector.setDirectory(QString::fromStdString(Sound::FOLDER));
|
|
fileselector.exec();
|
|
|
|
QStringList filelist = fileselector.selectedFiles();
|
|
if(filelist.size() != 1) {
|
|
Log::error << "no file selected";
|
|
return;
|
|
} else {
|
|
QString qfile = filelist.at(0);
|
|
std::string file = qfile.toStdString();
|
|
if(file.find(Sound::FOLDER) != 0) {
|
|
Log::error << "file outside of " << Sound::FOLDER << " was selected, that is not allowed";
|
|
return;
|
|
}
|
|
file = file.substr(file.rfind('/')+1); // remove path; only keep filename
|
|
Log::info << "selected file: " << file;
|
|
|
|
//spawn edit dialog
|
|
EditSample esample(file, this);
|
|
esample.exec();
|
|
|
|
if(esample.result() == 0) {
|
|
Log::info << "Dialog failed";
|
|
return;
|
|
}
|
|
|
|
// create sample
|
|
Config::SampleConfig sample;
|
|
sample.file = file;
|
|
sample.offset = esample.getStartTime();
|
|
sample.length = esample.getLength();
|
|
sample.volume = 1.0f;
|
|
btn->getConfig().samples.push_back(sample);
|
|
|
|
// create SampleItem in GUI
|
|
SampleItem* item = new SampleItem(btn, btn->getConfig().samples.size()-1, workingConfig);
|
|
|
|
btn->setExpanded(true);
|
|
select(item);
|
|
}
|
|
} else {
|
|
Log::error << "no button selected";
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
void ButtonManager::editButton() {
|
|
perform<&ButtonManagerItem::edit>();
|
|
}
|
|
|
|
void ButtonManager::deleteButton() {
|
|
QTreeWidgetItem* currentSelectedItem = getSelectedItem();
|
|
perform<&ButtonManagerItem::remove>();
|
|
delete currentSelectedItem;
|
|
}
|
|
|
|
void ButtonManager::upButton() {
|
|
QTreeWidgetItem* currentSelectedItem = getSelectedItem();
|
|
perform<&ButtonManagerItem::moveUp>();
|
|
select(currentSelectedItem);
|
|
}
|
|
|
|
void ButtonManager::downButton() {
|
|
QTreeWidgetItem* currentSelectedItem = getSelectedItem();
|
|
perform<&ButtonManagerItem::moveDown>();
|
|
select(currentSelectedItem);
|
|
}
|
|
|
|
void ButtonManager::itemSelected() {
|
|
Log::debug << "item selected";
|
|
|
|
ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(getSelectedItem());
|
|
if(item) {
|
|
// set buttons
|
|
// ui->addButton->setEnabled(item->hasAdd());
|
|
ui->editButton->setEnabled(item->hasEdit());
|
|
ui->deleteButton->setEnabled(item->hasRemove());
|
|
ui->moveUpButton->setEnabled(item->hasMoveUp());
|
|
ui->moveDownButton->setEnabled(item->hasMoveDown());
|
|
} else {
|
|
Log::info << "no valid item selected";
|
|
|
|
// ui->addButton->setEnabled(false);
|
|
ui->editButton->setEnabled(false);
|
|
ui->deleteButton->setEnabled(false);
|
|
ui->moveUpButton->setEnabled(false);
|
|
ui->moveDownButton->setEnabled(false);
|
|
}
|
|
}
|
|
|
|
void ButtonManager::itemChanged(QTreeWidgetItem* item, int column) {
|
|
// only handle own items (should never trigger anyways)
|
|
if(!dynamic_cast<ButtonManagerItem*>(item)) return;
|
|
|
|
if(RowItem* row = dynamic_cast<RowItem*>(item)) {
|
|
|
|
} else if(ButtonItem* button = dynamic_cast<ButtonItem*>(item)) {
|
|
if(column != 1) {
|
|
Log::warn << "column: " << column << " of button was edited";
|
|
return;
|
|
}
|
|
|
|
button->nameWasChanged();
|
|
}
|
|
}
|
|
|
|
void ButtonManager::dialogButtonPressed(QAbstractButton* btn) {
|
|
QDialogButtonBox::ButtonRole role = ui->buttonBox->buttonRole(btn);
|
|
if(role == QDialogButtonBox::ButtonRole::ResetRole) {
|
|
// reset
|
|
loadConfig();
|
|
} else if(role == QDialogButtonBox::ButtonRole::ApplyRole) {
|
|
// apply
|
|
saveChanges();
|
|
}
|
|
}
|
|
|
|
void ButtonManager::loadConfig() {
|
|
workingConfig = mainConfig.rootConfig;
|
|
|
|
while(ui->buttonTreeWidget->invisibleRootItem()->childCount()) {
|
|
auto kid = ui->buttonTreeWidget->invisibleRootItem()->takeChild(0);
|
|
delete kid;
|
|
}
|
|
|
|
QTreeWidgetItem* root = ui->buttonTreeWidget->invisibleRootItem();
|
|
for(uint8_t rownr = 0; rownr < workingConfig.buttons.size(); ++rownr) {
|
|
RowItem* row = new RowItem(root, rownr, workingConfig);
|
|
}
|
|
}
|
|
|
|
void ButtonManager::saveChanges() {
|
|
// store working config to main config and save
|
|
mainConfig.rootConfig = workingConfig;
|
|
mainConfig.save();
|
|
|
|
mainW->reloadButtonConfig();
|
|
}
|
|
|
|
void ButtonManager::select(QTreeWidgetItem* item) {
|
|
QList<QTreeWidgetItem*> selected = ui->buttonTreeWidget->selectedItems();
|
|
for(QTreeWidgetItem* selectedItem : selected) {
|
|
ui->buttonTreeWidget->setItemSelected(selectedItem, false);
|
|
}
|
|
ui->buttonTreeWidget->setItemSelected(item, true);
|
|
}
|
|
|
|
QTreeWidgetItem* ButtonManager::getSelectedItem() const {
|
|
QList items = ui->buttonTreeWidget->selectedItems();
|
|
if(items.size() != 1) {
|
|
return nullptr; // not exectly one selected
|
|
}
|
|
|
|
return items.at(0);
|
|
}
|
|
|
|
RowItem* ButtonManager::getCurrentRow() const {
|
|
QTreeWidgetItem* selectedItem = getSelectedItem();
|
|
if(!selectedItem) {
|
|
// no item selected
|
|
//TODO: search for first empty row, last row, create a row
|
|
return nullptr;
|
|
}
|
|
|
|
// is current selected item a row?
|
|
if(RowItem* row = dynamic_cast<RowItem*>(selectedItem)) {
|
|
return row;
|
|
}
|
|
|
|
//TODO: scan for parents
|
|
return nullptr;
|
|
}
|
|
|
|
ButtonItem* ButtonManager::getCurrentButton() const {
|
|
QTreeWidgetItem* selectedItem = getSelectedItem();
|
|
if(!selectedItem) {
|
|
// no item selected
|
|
//TODO: search for first empty button, create a new button
|
|
return nullptr;
|
|
}
|
|
|
|
// is current selected item a button?
|
|
if(ButtonItem* btn = dynamic_cast<ButtonItem*>(selectedItem)) {
|
|
return btn;
|
|
}
|
|
|
|
//TODO: scan for parents / siblings
|
|
return nullptr;
|
|
}
|
|
|
|
template<void (ButtonManagerItem::*T)()>
|
|
void ButtonManager::perform() {
|
|
ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(getSelectedItem());
|
|
if(item) {
|
|
(item->*T)();
|
|
} else {
|
|
Log::info << "no valid item selected";
|
|
}
|
|
} |