From c6f825d8875bb7b3173b9fe80017f0b38f3b659d Mon Sep 17 00:00:00 2001 From: mrbesen Date: Tue, 21 Dec 2021 15:44:51 +0100 Subject: [PATCH] buttonManager Up button implemented --- .vscode/launch.json | 27 ++++++ include/buttonmanager.h | 40 +++++++-- include/buttonmanageritems.h | 49 +++++++++++ include/mainwindow.h | 2 + soundboard.pro | 3 + src/buttonmanager.cpp | 150 ++++++++++++++++++++++++++------- src/buttonmanageritems.cpp | 118 ++++++++++++++++++++++++++ src/mainwindow.cpp | 12 +++ ui/buttonmanager.ui | 156 +++++++++++++++++++++++++++++------ 9 files changed, 495 insertions(+), 62 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 include/buttonmanageritems.h create mode 100644 src/buttonmanageritems.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..71c9a46 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Starten", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/soundboard", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/include/buttonmanager.h b/include/buttonmanager.h index 6c256ed..cee5d00 100644 --- a/include/buttonmanager.h +++ b/include/buttonmanager.h @@ -1,9 +1,12 @@ -#ifndef BUTTONMANAGER_H -#define BUTTONMANAGER_H +#pragma once +#include #include +#include #include "config.h" +#include "buttonmanageritems.h" +#include "mainwindow.h" namespace Ui { class ButtonManager; @@ -14,13 +17,38 @@ class ButtonManager : public QDialog Q_OBJECT public: - explicit ButtonManager(const Config& conf, QWidget *parent = nullptr); + explicit ButtonManager(Config& conf, MainWindow *parent = nullptr); ~ButtonManager(); +public slots: + virtual void accept(); + virtual void reject(); + +private slots: + //buttons: + void addButton(); + void editButton(); + void deleteButton(); + void upButton(); + void downButton(); + + void itemSelected(); + void dialogButtonPressed(QAbstractButton* btn); + private: Ui::ButtonManager *ui; + MainWindow* mainW = nullptr; - const Config& mainConfig; + Config& mainConfig; + Config::RootConfig workingConfig; + + void loadConfig(); + void saveChanges(); + + void select(QTreeWidgetItem* item); + + QTreeWidgetItem* getSelectedItem() const; + + template + void perform(); }; - -#endif // BUTTONMANAGER_H diff --git a/include/buttonmanageritems.h b/include/buttonmanageritems.h new file mode 100644 index 0000000..002a58c --- /dev/null +++ b/include/buttonmanageritems.h @@ -0,0 +1,49 @@ +#pragma once + +#include "config.h" +#include + +// interface +class ButtonManagerItem : public QTreeWidgetItem { +protected: + ButtonManagerItem(QTreeWidgetItem* parent, int type); +public: + virtual ~ButtonManagerItem(); + + virtual bool hasAdd() const; + virtual bool hasEdit() const; + virtual bool hasRemove() const; + virtual bool hasMoveUp() const; + virtual bool hasMoveDown() const; + + virtual void add(); + virtual void edit(); + virtual void remove(); + virtual void moveUp(); + virtual void moveDown(); + +protected: + QTreeWidgetItem* mparent; +}; + +class RowItem : public ButtonManagerItem { +public: + RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& conf); + + + const static int TYPE = 1000; + + virtual bool hasMoveUp() const; + virtual bool hasMoveDown() const; + + + virtual void moveUp(); + virtual void moveDown(); + +private: + Config::RootConfig& conf; + uint8_t pos = 0; + + //called when the row was moved + void updatePosition(); +}; diff --git a/include/mainwindow.h b/include/mainwindow.h index abad3e7..be65bca 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -32,6 +32,8 @@ public slots: void moveRight(); void playCurrent(); + void reloadButtonConfig(); + private slots: void alwaysOnTopSettingChange(int status); void addSample(); diff --git a/soundboard.pro b/soundboard.pro index 8ef5f61..8b3f744 100644 --- a/soundboard.pro +++ b/soundboard.pro @@ -4,6 +4,7 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 CONFIG += debug_info +CONFIG += force_debug_info LIBS += -lX11 -ldl # The following define makes your compiler emit warnings if you use @@ -28,6 +29,7 @@ SOURCES += \ src/soundview.cpp \ src/samplereader.cpp \ src/buttonmanager.cpp \ + src/buttonmanageritems.cpp \ Log/Log.cpp HEADERS += \ @@ -40,6 +42,7 @@ HEADERS += \ include/soundview.h \ include/samplereader.h \ include/buttonmanager.h \ + include/buttonmanageritems.h \ miniaudio/miniaudio.h \ Log/Log.h diff --git a/src/buttonmanager.cpp b/src/buttonmanager.cpp index 47f63cb..83d3d5a 100644 --- a/src/buttonmanager.cpp +++ b/src/buttonmanager.cpp @@ -1,39 +1,12 @@ #include "buttonmanager.h" #include "ui_buttonmanager.h" -// itemtypes -const static int ROWTYPE = 1000; -const static int BUTTONTYPE = 1001; -const static int SAMPLETYPE = 1002; +#include -ButtonManager::ButtonManager(const Config& conf, QWidget *parent) : QDialog(parent), ui(new Ui::ButtonManager), mainConfig(conf) { +ButtonManager::ButtonManager(Config& conf, MainWindow* parent) : QDialog(parent), ui(new Ui::ButtonManager), mainW(parent), mainConfig(conf) { ui->setupUi(this); - - QList items; - for(uint8_t rownr = 0; rownr < conf.rootConfig.buttons.size(); ++rownr) { - const std::vector& btnrow = conf.rootConfig.buttons.at(rownr); - QTreeWidgetItem* row = new QTreeWidgetItem(ROWTYPE); - row->setData(0, 0, QVariant((int) rownr+1)); - items.push_back(row); - - // iterate buttons in a row - for(const Config::ButtonConfig& btn : btnrow) { - QTreeWidgetItem* qbtn = new QTreeWidgetItem(BUTTONTYPE); - qbtn->setData(1, 0, QVariant(QString::fromStdString(btn.name))); - qbtn->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable); - row->addChild(qbtn); - - // iterate samples in a button - for(uint8_t samplenr = 0; samplenr < btn.samples.size(); ++samplenr) { - const Config::SampleConfig& sample = btn.samples.at(samplenr); - QTreeWidgetItem* qsample = new QTreeWidgetItem(SAMPLETYPE); - qsample->setData(2, 0, QVariant((int) samplenr+1)); - qsample->setData(3, 0, QVariant(QString::fromStdString(sample.file))); - qbtn->addChild(qsample); - } - } - } + loadConfig(); QStringList labels; labels.push_back("Row"); @@ -41,10 +14,125 @@ ButtonManager::ButtonManager(const Config& conf, QWidget *parent) : QDialog(pare labels.push_back("Sample"); labels.push_back("File"); - ui->buttonTreeWidget->addTopLevelItems(items); ui->buttonTreeWidget->setHeaderLabels(labels); } ButtonManager::~ButtonManager() { delete ui; } + +void ButtonManager::accept() { + saveChanges(); + + QDialog::accept(); +} + +void ButtonManager::reject() { + // nothing? + QDialog::reject(); +} + +void ButtonManager::addButton() { + perform<&ButtonManagerItem::add>(); +} + +void ButtonManager::editButton() { + perform<&ButtonManagerItem::edit>(); +} + +void ButtonManager::deleteButton() { + perform<&ButtonManagerItem::remove>(); +} + +void ButtonManager::upButton() { + QTreeWidgetItem* currentSelectedItem = getSelectedItem(); + perform<&ButtonManagerItem::moveUp>(); + select(currentSelectedItem); +} + +void ButtonManager::downButton() { + perform<&ButtonManagerItem::moveDown>(); +} + +void ButtonManager::itemSelected() { + Log::debug << "item selected"; + + ButtonManagerItem* item = dynamic_cast(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::dialogButtonPressed(QAbstractButton* btn) { + QDialogButtonBox::ButtonRole role = ui->buttonBox->buttonRole(btn); + Log::trace << "btn: " << (int) role; + 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 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); +} + +template +void ButtonManager::perform() { + ButtonManagerItem* item = dynamic_cast(getSelectedItem()); + if(item) { + (item->*T)(); + } else { + Log::info << "no valid item selected"; + } +} \ No newline at end of file diff --git a/src/buttonmanageritems.cpp b/src/buttonmanageritems.cpp new file mode 100644 index 0000000..25d0666 --- /dev/null +++ b/src/buttonmanageritems.cpp @@ -0,0 +1,118 @@ +#include "buttonmanageritems.h" + +#include + +// itemtypes +const static int ROWTYPE = 1000; +const static int BUTTONTYPE = 1001; +const static int SAMPLETYPE = 1002; + +ButtonManagerItem::ButtonManagerItem(QTreeWidgetItem* parent, int type) : QTreeWidgetItem(parent, type), mparent(parent) { +} + +ButtonManagerItem::~ButtonManagerItem() {} + +bool ButtonManagerItem::hasAdd() const { + return false; +} + +bool ButtonManagerItem::hasEdit() const { + return false; +} + +bool ButtonManagerItem::hasRemove() const { + return false; +} + +bool ButtonManagerItem::hasMoveUp() const { + return false; +} + +bool ButtonManagerItem::hasMoveDown() const { + return false; +} + + +void ButtonManagerItem::add() {} + +void ButtonManagerItem::edit() {} + +void ButtonManagerItem::remove() {} + +void ButtonManagerItem::moveUp() {} + +void ButtonManagerItem::moveDown() {} + + +RowItem::RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE), conf(conf), pos(rownr) { + const std::vector& btnrow = conf.buttons.at(rownr); + + updatePosition(); + + // iterate buttons in a row + for(const Config::ButtonConfig& btn : btnrow) { + QTreeWidgetItem* qbtn = new QTreeWidgetItem(BUTTONTYPE); + qbtn->setData(1, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(btn.name))); + qbtn->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable); + addChild(qbtn); + + // iterate samples in a button + for(uint8_t samplenr = 0; samplenr < btn.samples.size(); ++samplenr) { + const Config::SampleConfig& sample = btn.samples.at(samplenr); + QTreeWidgetItem* qsample = new QTreeWidgetItem(SAMPLETYPE); + qsample->setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) samplenr+1)); + qsample->setData(3, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(sample.file))); + qbtn->addChild(qsample); + } + } +} + +bool RowItem::hasMoveUp() const { + return pos > 0; +} + +bool RowItem::hasMoveDown() const { + return pos < conf.buttons.size()-1; +} + +void RowItem::moveUp() { + //apply change to config + conf.buttons.at(pos-1).swap(conf.buttons.at(pos)); + + Log::trace << "rows swapped in config"; + + // apply change in GUI + + //get Child above + RowItem* rowaboveItem = dynamic_cast(mparent->child(pos-1)); + if(rowaboveItem) { + rowaboveItem->pos++; + rowaboveItem->updatePosition(); + } else { + Log::error << "row above could not be updated"; + } + + QTreeWidgetItem* myparent = mparent; + Log::debug << "parent: " << myparent; + + int index = myparent->indexOfChild(this); + Log::debug << "index: " << index; + + bool wasexpanded = isExpanded(); + myparent->removeChild(this); + + myparent->insertChild(index-1, this); + setExpanded(wasexpanded); + + pos--; + updatePosition(); +} + +void RowItem::moveDown() { + +} + +void RowItem::updatePosition() { + setData(0, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1)); +} + diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d6a2d8d..cf7841b 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -5,6 +5,7 @@ #include "editsample.h" #include +#include #include #include @@ -124,6 +125,11 @@ void MainWindow::playCurrent() { } } +void MainWindow::reloadButtonConfig() { + removeAllButtons(); + loadButtonsFromConfig(); +} + void MainWindow::alwaysOnTopSettingChange(int status) { Qt::WindowFlags eFlags = windowFlags(); Log::debug << "status: " << status; @@ -170,6 +176,12 @@ void MainWindow::openButtonManager() { ButtonManager buttonManager(config, this); buttonManager.exec(); + + // reload button config + if(buttonManager.result() == QMessageBox::ButtonRole::AcceptRole) { + removeAllButtons(); + loadButtonsFromConfig(); + } } void MainWindow::loadSoundFromConfig() { diff --git a/ui/buttonmanager.ui b/ui/buttonmanager.ui index 978846f..a4104de 100644 --- a/ui/buttonmanager.ui +++ b/ui/buttonmanager.ui @@ -27,7 +27,7 @@ Qt::Horizontal - QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Save + QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Reset|QDialogButtonBox::Save @@ -55,23 +55,8 @@ - 3 + 0 - - - 1 - - - - - 2 - - - - - 3 - - @@ -92,12 +77,12 @@ accept() - 227 - 278 + 236 + 734 - 157 - 274 + 179 + 705 @@ -108,14 +93,135 @@ reject() - 295 - 284 + 304 + 734 - 286 - 274 + 391 + 704 + + + + + addButton + clicked() + ButtonManager + addButton() + + + 848 + 24 + + + 801 + 320 + + + + + editButton + clicked() + ButtonManager + editButton() + + + 818 + 52 + + + 801 + 362 + + + + + deleteButton + clicked() + ButtonManager + deleteButton() + + + 854 + 82 + + + 803 + 403 + + + + + moveUpButton + clicked() + ButtonManager + upButton() + + + 863 + 125 + + + 805 + 443 + + + + + moveDownButton + clicked() + ButtonManager + deleteButton() + + + 867 + 155 + + + 804 + 488 + + + + + buttonTreeWidget + itemSelectionChanged() + ButtonManager + itemSelected() + + + 427 + 393 + + + 847 + 625 + + + + + buttonBox + clicked(QAbstractButton*) + ButtonManager + dialogButtonPressed(QAbstractButton*) + + + 512 + 708 + + + 530 + 683 + + addButton() + editButton() + deleteButton() + upButton() + downButton() + itemSelected() + dialogButtonPressed(QAbstractButton*) +