buttonManager Up button implemented

This commit is contained in:
mrbesen 2021-12-21 15:44:51 +01:00
parent baa5ddb98b
commit c6f825d887
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
9 changed files with 495 additions and 62 deletions

27
.vscode/launch.json vendored Normal file
View File

@ -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
}
]
}
]
}

View File

@ -1,9 +1,12 @@
#ifndef BUTTONMANAGER_H
#define BUTTONMANAGER_H
#pragma once
#include <QAbstractButton>
#include <QDialog>
#include <QTreeWidgetItem>
#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 (ButtonManagerItem::*T)()>
void perform();
};
#endif // BUTTONMANAGER_H

View File

@ -0,0 +1,49 @@
#pragma once
#include "config.h"
#include <QTreeWidgetItem>
// 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();
};

View File

@ -32,6 +32,8 @@ public slots:
void moveRight();
void playCurrent();
void reloadButtonConfig();
private slots:
void alwaysOnTopSettingChange(int status);
void addSample();

View File

@ -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

View File

@ -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 <Log.h>
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<QTreeWidgetItem*> items;
for(uint8_t rownr = 0; rownr < conf.rootConfig.buttons.size(); ++rownr) {
const std::vector<Config::ButtonConfig>& 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<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::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<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);
}
template<void (ButtonManagerItem::*T)()>
void ButtonManager::perform() {
ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(getSelectedItem());
if(item) {
(item->*T)();
} else {
Log::info << "no valid item selected";
}
}

118
src/buttonmanageritems.cpp Normal file
View File

@ -0,0 +1,118 @@
#include "buttonmanageritems.h"
#include <Log.h>
// 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<Config::ButtonConfig>& 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<RowItem*>(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));
}

View File

@ -5,6 +5,7 @@
#include "editsample.h"
#include <QFileDialog>
#include <QMessageBox>
#include <iostream>
#include <string>
@ -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() {

View File

@ -27,7 +27,7 @@
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Reset|QDialogButtonBox::Save</set>
</property>
</widget>
</item>
@ -55,23 +55,8 @@
<item row="0" column="0" rowspan="6">
<widget class="QTreeWidget" name="buttonTreeWidget">
<property name="columnCount">
<number>3</number>
<number>0</number>
</property>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
<column>
<property name="text">
<string notr="true">2</string>
</property>
</column>
<column>
<property name="text">
<string notr="true">3</string>
</property>
</column>
</widget>
</item>
<item row="1" column="2">
@ -92,12 +77,12 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>227</x>
<y>278</y>
<x>236</x>
<y>734</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
<x>179</x>
<y>705</y>
</hint>
</hints>
</connection>
@ -108,14 +93,135 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>295</x>
<y>284</y>
<x>304</x>
<y>734</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
<x>391</x>
<y>704</y>
</hint>
</hints>
</connection>
<connection>
<sender>addButton</sender>
<signal>clicked()</signal>
<receiver>ButtonManager</receiver>
<slot>addButton()</slot>
<hints>
<hint type="sourcelabel">
<x>848</x>
<y>24</y>
</hint>
<hint type="destinationlabel">
<x>801</x>
<y>320</y>
</hint>
</hints>
</connection>
<connection>
<sender>editButton</sender>
<signal>clicked()</signal>
<receiver>ButtonManager</receiver>
<slot>editButton()</slot>
<hints>
<hint type="sourcelabel">
<x>818</x>
<y>52</y>
</hint>
<hint type="destinationlabel">
<x>801</x>
<y>362</y>
</hint>
</hints>
</connection>
<connection>
<sender>deleteButton</sender>
<signal>clicked()</signal>
<receiver>ButtonManager</receiver>
<slot>deleteButton()</slot>
<hints>
<hint type="sourcelabel">
<x>854</x>
<y>82</y>
</hint>
<hint type="destinationlabel">
<x>803</x>
<y>403</y>
</hint>
</hints>
</connection>
<connection>
<sender>moveUpButton</sender>
<signal>clicked()</signal>
<receiver>ButtonManager</receiver>
<slot>upButton()</slot>
<hints>
<hint type="sourcelabel">
<x>863</x>
<y>125</y>
</hint>
<hint type="destinationlabel">
<x>805</x>
<y>443</y>
</hint>
</hints>
</connection>
<connection>
<sender>moveDownButton</sender>
<signal>clicked()</signal>
<receiver>ButtonManager</receiver>
<slot>deleteButton()</slot>
<hints>
<hint type="sourcelabel">
<x>867</x>
<y>155</y>
</hint>
<hint type="destinationlabel">
<x>804</x>
<y>488</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonTreeWidget</sender>
<signal>itemSelectionChanged()</signal>
<receiver>ButtonManager</receiver>
<slot>itemSelected()</slot>
<hints>
<hint type="sourcelabel">
<x>427</x>
<y>393</y>
</hint>
<hint type="destinationlabel">
<x>847</x>
<y>625</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>clicked(QAbstractButton*)</signal>
<receiver>ButtonManager</receiver>
<slot>dialogButtonPressed(QAbstractButton*)</slot>
<hints>
<hint type="sourcelabel">
<x>512</x>
<y>708</y>
</hint>
<hint type="destinationlabel">
<x>530</x>
<y>683</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>addButton()</slot>
<slot>editButton()</slot>
<slot>deleteButton()</slot>
<slot>upButton()</slot>
<slot>downButton()</slot>
<slot>itemSelected()</slot>
<slot>dialogButtonPressed(QAbstractButton*)</slot>
</slots>
</ui>