soundboard/src/buttonmanageritems.cpp

548 lines
13 KiB
C++
Raw Normal View History

2021-12-21 15:44:51 +01:00
#include "buttonmanageritems.h"
2021-12-22 14:58:08 +01:00
#include <algorithm>
2021-12-21 15:44:51 +01:00
#include <Log.h>
#include "editbutton.h"
2021-12-22 13:39:19 +01:00
#include "editsample.h"
2021-12-21 15:44:51 +01:00
2022-04-02 13:46:11 +02:00
#include "keysequencevalidator.h"
2021-12-22 14:58:08 +01:00
ButtonManagerItem::ButtonManagerItem(QTreeWidgetItem* parent, int type, uint8_t pos) : QTreeWidgetItem(parent, type), mparent(parent), pos(pos) {
2021-12-21 15:44:51 +01:00
}
ButtonManagerItem::~ButtonManagerItem() {}
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::edit() {}
void ButtonManagerItem::remove() {}
void ButtonManagerItem::moveUp() {}
void ButtonManagerItem::moveDown() {}
template<typename T>
T* ButtonManagerItem::getParent() const {
return dynamic_cast<T*>(mparent);
}
template<typename T>
T* ButtonManagerItem::getChild(int index) const {
if(childCount() <= index)
return nullptr;
return dynamic_cast<T*>(child(index));
}
2021-12-25 14:01:15 +01:00
template<typename T>
T* ButtonManagerItem::getLastChild() const {
return getChild<T>(childCount()-1);
}
template<typename T>
T* ButtonManagerItem::getSibling(int index) const {
if(mparent->childCount() <= index || index < 0)
return nullptr;
return dynamic_cast<T*>(mparent->child(index));
}
template<typename T>
T* ButtonManagerItem::getItemAbove() const {
return getSibling<T>(pos-1);
}
template<typename T>
T* ButtonManagerItem::getItemBelow() const {
return getSibling<T>(pos+1);
}
// enforce existence of required templates
template RowItem* ButtonManagerItem::getParent() const;
template ButtonItem* ButtonManagerItem::getParent() const;
template SampleItem* ButtonManagerItem::getParent() const;
template RowItem* ButtonManagerItem::getChild(int) const;
template ButtonItem* ButtonManagerItem::getChild(int) const;
template SampleItem* ButtonManagerItem::getChild(int) const;
template RowItem* ButtonManagerItem::getSibling(int) const;
template ButtonItem* ButtonManagerItem::getSibling(int) const;
template SampleItem* ButtonManagerItem::getSibling(int) const;
void ButtonManagerItem::reenumerateAllSiblings() {
for(uint8_t i = 0; i < mparent->childCount(); ++i) {
ButtonManagerItem* item = getSibling<ButtonManagerItem>(i);
if(item) {
item->pos = i;
item->updatePosition();
}
}
}
2021-12-25 14:37:34 +01:00
void ButtonManagerItem::expandTree() {
ButtonManagerItem* c = this;
while(c) {
c->setExpanded(true);
c = c->getParent<ButtonManagerItem>();
}
}
2021-12-22 14:58:08 +01:00
void ButtonManagerItem::updateAllPosBellow(int8_t diff, uint8_t size) {
2021-12-22 19:00:43 +01:00
for(uint8_t i = pos+1; i < size; ++i) {
ButtonManagerItem* item = getSibling<ButtonManagerItem>(i);
2021-12-22 14:58:08 +01:00
if(item) {
item->pos += diff;
item->updatePosition();
}
}
}
void ButtonManagerItem::updatePosition() {}
2021-12-21 15:44:51 +01:00
2021-12-22 14:58:08 +01:00
RowItem::RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, rownr), conf(conf) {
2021-12-21 15:44:51 +01:00
const std::vector<Config::ButtonConfig>& btnrow = conf.buttons.at(rownr);
updatePosition();
2021-12-21 20:19:24 +01:00
setExpanded(true);
2021-12-21 15:44:51 +01:00
// iterate buttons in a row
2021-12-21 20:19:24 +01:00
for(uint8_t btnnr = 0; btnnr < btnrow.size(); btnnr++) {
ButtonItem* btnItem = new ButtonItem(this, btnnr, conf);
// no need to use this var, they add themself to their parent (this)
(void) btnItem;
2021-12-21 15:44:51 +01:00
}
}
2021-12-21 16:21:34 +01:00
bool RowItem::hasRemove() const {
return true;
}
2021-12-21 15:44:51 +01:00
bool RowItem::hasMoveUp() const {
return pos > 0;
}
bool RowItem::hasMoveDown() const {
return pos < conf.buttons.size()-1;
}
2021-12-21 16:21:34 +01:00
void RowItem::remove() {
conf.buttons.erase(conf.buttons.begin() + pos);
2021-12-21 18:18:45 +01:00
2021-12-22 19:00:43 +01:00
updateAllPosBellow(-1, conf.buttons.size()+1);
2021-12-21 16:21:34 +01:00
}
2021-12-21 15:44:51 +01:00
void RowItem::moveUp() {
//apply change to config
conf.buttons.at(pos-1).swap(conf.buttons.at(pos));
// apply change in GUI
//get Child above
RowItem* rowaboveItem = getSibling<RowItem>(pos-1);
2021-12-21 15:44:51 +01:00
if(rowaboveItem) {
rowaboveItem->pos++;
rowaboveItem->updatePosition();
} else {
Log::error << "row above could not be updated";
}
2021-12-21 16:02:04 +01:00
mparent->removeChild(this);
2021-12-21 15:44:51 +01:00
2021-12-21 16:02:04 +01:00
mparent->insertChild(pos-1, this);
2021-12-25 14:37:34 +01:00
expandTree();
2021-12-21 15:44:51 +01:00
pos--;
updatePosition();
}
void RowItem::moveDown() {
2021-12-21 16:02:04 +01:00
// moving down is the same as moving the item below up
RowItem* rowbelow = getSibling<RowItem>(pos+1);
2021-12-21 16:02:04 +01:00
if(rowbelow) {
rowbelow->moveUp();
} else {
Log::error << "no item below";
}
2021-12-21 15:44:51 +01:00
}
2021-12-21 20:19:24 +01:00
uint8_t RowItem::getRow() const {
return pos;
}
2021-12-21 15:44:51 +01:00
void RowItem::updatePosition() {
2022-04-02 13:46:11 +02:00
setData(0, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString("Row " + std::to_string(pos+1))));
2021-12-21 15:44:51 +01:00
}
2021-12-21 20:19:24 +01:00
std::vector<Config::ButtonConfig>& RowItem::getConfig() {
return conf.buttons.at(pos);
}
2021-12-22 14:58:08 +01:00
2021-12-21 20:19:24 +01:00
ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, buttonnr), conf(conf) {
2021-12-21 20:19:24 +01:00
Config::ButtonConfig& btn = getConfig();
2022-04-02 13:46:11 +02:00
setData(0, Qt::ItemDataRole::DisplayRole, QString::fromStdString(btn.name));
setData(1, Qt::ItemDataRole::DisplayRole, (int) btn.samples.size());
setData(2, Qt::ItemDataRole::DisplayRole, QString::fromStdString(btn.key));
2021-12-21 20:19:24 +01:00
setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable); // | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled
// iterate samples in a button
for(uint8_t samplenr = 0; samplenr < btn.samples.size(); ++samplenr) {
2021-12-22 02:08:11 +01:00
SampleItem* sample = new SampleItem(this, samplenr, conf);
// no need to use this var, they add themself to their parent (this)
(void) sample;
2021-12-21 20:19:24 +01:00
}
}
bool ButtonItem::hasEdit() const {
return true;
}
2021-12-22 12:52:25 +01:00
bool ButtonItem::hasRemove() const {
return true;
}
2021-12-22 14:58:08 +01:00
bool ButtonItem::hasMoveUp() const {
return pos > 0 || getParent<RowItem>()->hasMoveUp();
2021-12-22 14:58:08 +01:00
}
bool ButtonItem::hasMoveDown() const {
return pos < getParent<RowItem>()->getConfig().size()-1 || getParent<RowItem>()->hasMoveDown();
2021-12-22 14:58:08 +01:00
}
void ButtonItem::edit() {
Config::ButtonConfig& buttonConf = getConfig();
EditButton editor;
editor.setName(buttonConf.name);
editor.setKey(buttonConf.key);
2022-04-02 13:10:30 +02:00
editor.setWidth(buttonConf.width);
editor.exec();
if(editor.result() == 0) {
Log::info << "Dialog failed";
return;
}
buttonConf.name = editor.getName();
buttonConf.key = editor.getKey();
2022-04-02 13:10:30 +02:00
buttonConf.width = editor.getWidth();
2022-04-02 13:46:11 +02:00
setData(0, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(buttonConf.name)));
}
2021-12-22 12:52:25 +01:00
void ButtonItem::remove() {
auto& rowconf = getParent<RowItem>()->getConfig();
2021-12-22 14:58:08 +01:00
rowconf.erase(rowconf.begin() + pos);
2021-12-22 12:52:25 +01:00
2021-12-22 19:00:43 +01:00
updateAllPosBellow(-1, rowconf.size()+1);
2021-12-22 14:58:08 +01:00
}
void ButtonItem::moveUp() {
if(pos == 0) {
moveToRowAbove();
return;
}
//apply change to config
auto& rowconf = getParent<RowItem>()->getConfig();
2021-12-22 14:58:08 +01:00
std::swap(rowconf.at(pos-1), rowconf.at(pos));
// apply change in GUI
//get Child above
ButtonItem* buttonaboveItem = getSibling<ButtonItem>(pos-1);
2021-12-22 14:58:08 +01:00
if(buttonaboveItem) {
buttonaboveItem->pos++;
} else {
Log::error << "button above could not be updated";
}
mparent->removeChild(this);
mparent->insertChild(pos-1, this);
2021-12-25 14:37:34 +01:00
expandTree();
2021-12-22 14:58:08 +01:00
pos--;
}
void ButtonItem::moveDown() {
if(pos == getParent<RowItem>()->getConfig().size()-1) {
2021-12-22 19:00:43 +01:00
moveToRowBelow();
return;
}
2021-12-22 14:58:08 +01:00
// moving down is the same as moving the item below up
ButtonItem* buttonbelow = getSibling<ButtonItem>(pos+1);
if(buttonbelow) {
buttonbelow->moveUp();
2021-12-22 14:58:08 +01:00
} else {
Log::error << "no item below";
2021-12-22 12:52:25 +01:00
}
}
2021-12-21 20:19:24 +01:00
void ButtonItem::nameWasChanged() {
2022-04-02 13:46:11 +02:00
std::string newName = data(0, Qt::ItemDataRole::DisplayRole).toString().toStdString();
2021-12-21 20:19:24 +01:00
getConfig().name = newName;
}
2022-04-02 13:46:11 +02:00
void ButtonItem::keyWasChanged() {
QString newShortcut = data(2, Qt::ItemDataRole::DisplayRole).toString();
KeysequenceValidator validator;
int pos;
Config::ButtonConfig& btn = getConfig();
if(validator.validate(newShortcut, pos) == QValidator::Acceptable) {
btn.key = newShortcut.toStdString();
} else {
//reset to old
setData(2, Qt::ItemDataRole::DisplayRole, QString::fromStdString(btn.key));
}
}
2021-12-21 20:19:24 +01:00
Config::ButtonConfig& ButtonItem::getConfig() {
return getParent<RowItem>()->getConfig().at(pos);
2021-12-21 20:19:24 +01:00
}
2021-12-22 02:08:11 +01:00
2021-12-22 14:58:08 +01:00
void ButtonItem::moveToRowAbove() {
// get row above
RowItem* rowabove = getParent<RowItem>()->getItemAbove<RowItem>();
2021-12-22 14:58:08 +01:00
if(!rowabove) {
Log::error << "no row above!";
return;
}
// apply change in config
auto& rowconf = getParent<RowItem>()->getConfig();
2021-12-22 14:58:08 +01:00
auto& newrowconf = rowabove->getConfig();
newrowconf.push_back(rowconf.front());
rowconf.erase(rowconf.begin());
// update buttons in old row
// updateAllPosBellow(-1, rowconf.size()+1);
2021-12-22 14:58:08 +01:00
// remove button from this row and attach to other
getParent<RowItem>()->removeChild(this);
2021-12-22 02:08:11 +01:00
reenumerateAllSiblings(); // depends on mparent beeing set to the old row
2021-12-22 14:58:08 +01:00
rowabove->addChild(this);
//update local parent
2021-12-22 14:58:08 +01:00
mparent = rowabove;
2021-12-22 02:08:11 +01:00
2021-12-22 14:58:08 +01:00
//update local pos
pos = newrowconf.size()-1;
2021-12-25 14:37:34 +01:00
expandTree();
2021-12-22 14:58:08 +01:00
}
2021-12-22 19:00:43 +01:00
void ButtonItem::moveToRowBelow() {
// get row below
RowItem* rowbelow = getParent<RowItem>()->getItemBelow<RowItem>();
2021-12-22 19:00:43 +01:00
if(!rowbelow) {
Log::error << "no row below!";
return;
}
// apply change in config
auto& rowconf = getParent<RowItem>()->getConfig();
2021-12-22 19:00:43 +01:00
auto& newrowconf = rowbelow->getConfig();
newrowconf.insert(newrowconf.begin(), rowconf.back());
rowconf.erase(rowconf.end());
// remove button from this row and attach to other
getParent<RowItem>()->removeChild(this);
2021-12-22 19:00:43 +01:00
rowbelow->insertChild(0, this);
//update local parent
2021-12-22 19:00:43 +01:00
mparent = rowbelow;
//update local pos
pos = 0;
//update new buttons below
updateAllPosBellow(1, newrowconf.size());
2021-12-25 14:37:34 +01:00
expandTree();
2021-12-22 19:00:43 +01:00
}
2021-12-22 14:58:08 +01:00
SampleItem::SampleItem(ButtonItem* parent, uint8_t samplenr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, samplenr), conf(conf) {
2021-12-22 02:08:11 +01:00
const Config::SampleConfig& sample = getConfig();
2021-12-22 12:52:25 +01:00
updatePosition();
2022-04-02 13:46:11 +02:00
setText(1, QString::fromStdString(sample.file));
setToolTip(0, "Doubleclick to play");
setToolTip(1, "Doubleclick to play");
2021-12-22 13:39:19 +01:00
}
bool SampleItem::hasEdit() const {
return true;
2021-12-22 02:08:11 +01:00
}
2021-12-22 12:52:25 +01:00
bool SampleItem::hasRemove() const {
return true;
}
bool SampleItem::hasMoveUp() const {
return pos > 0 || getParent<ButtonItem>()->hasMoveUp();
}
bool SampleItem::hasMoveDown() const {
return pos < getParent<ButtonItem>()->getConfig().samples.size()-1 || getParent<ButtonItem>()->hasMoveDown();
}
2021-12-22 13:39:19 +01:00
void SampleItem::edit() {
Config::SampleConfig& sammpleConf = getConfig();
EditSample editor(sammpleConf.file);
editor.setStartTime(sammpleConf.offset);
editor.setLength(sammpleConf.length);
editor.exec();
if(editor.result() == 0) {
Log::info << "Dialog failed";
return;
}
sammpleConf.offset = editor.getStartTime();
sammpleConf.length = editor.getLength();
}
2021-12-22 12:52:25 +01:00
void SampleItem::remove() {
auto& btnconf = getParent<ButtonItem>()->getConfig().samples;
2021-12-22 14:58:08 +01:00
btnconf.erase(btnconf.begin() + pos);
2021-12-22 12:52:25 +01:00
2021-12-22 14:58:08 +01:00
updateAllPosBellow(-1, btnconf.size());
2021-12-22 12:52:25 +01:00
}
void SampleItem::moveUp() {
if(pos == 0) {
moveToButtonAbove();
return;
}
//apply change to config
auto& rowconf = getParent<ButtonItem>()->getConfig().samples;
std::swap(rowconf.at(pos-1), rowconf.at(pos));
// apply change in GUI
//get Child above
SampleItem* sampleAboveItem = getSibling<SampleItem>(pos-1);
if(sampleAboveItem) {
sampleAboveItem->pos++;
} else {
Log::error << "button above could not be updated";
}
mparent->removeChild(this);
mparent->insertChild(pos-1, this);
2021-12-25 14:37:34 +01:00
expandTree();
pos--;
2021-12-25 14:26:19 +01:00
updatePosition();
}
void SampleItem::moveDown() {
if(pos == getParent<ButtonItem>()->getConfig().samples.size()-1) {
moveToButtonBelow();
return;
}
// moving down is the same as moving the item below up
SampleItem* samplebelow = getSibling<SampleItem>(pos+1);
if(samplebelow) {
samplebelow->moveUp();
} else {
Log::error << "no item below";
}
}
2021-12-22 02:08:11 +01:00
Config::SampleConfig& SampleItem::getConfig() {
return getParent<ButtonItem>()->getConfig().samples.at(pos);
2021-12-22 12:52:25 +01:00
}
void SampleItem::updatePosition() {
2022-04-02 13:46:11 +02:00
setData(0, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1));
2021-12-22 13:39:19 +01:00
}
void SampleItem::moveToButtonAbove() {
2021-12-25 14:01:15 +01:00
ButtonItem* buttonabove = getParent<ButtonItem>()->getItemAbove<ButtonItem>();
if(!buttonabove) {
buttonabove = getParent<ButtonItem>()->getParent<RowItem>()->getItemAbove<RowItem>()->getLastChild<ButtonItem>();
}
// apply change in config
auto& buttonconf = getParent<ButtonItem>()->getConfig().samples;
auto& newbuttonconf = buttonabove->getConfig().samples;
newbuttonconf.push_back(buttonconf.front());
buttonconf.erase(buttonconf.begin());
// update buttons in old row
// updateAllPosBellow(-1, rowconf.size()+1);
// remove button from this row and attach to other
getParent<ButtonItem>()->removeChild(this);
reenumerateAllSiblings(); // depends on mparent beeing set to the old row
2021-12-25 14:01:15 +01:00
buttonabove->addChild(this);
//update local parent
mparent = buttonabove;
//update local pos
pos = newbuttonconf.size()-1;
2021-12-25 14:37:34 +01:00
expandTree();
2021-12-25 14:26:19 +01:00
updatePosition();
}
void SampleItem::moveToButtonBelow() {
2021-12-25 14:26:19 +01:00
ButtonItem* buttonbelow = getParent<ButtonItem>()->getItemBelow<ButtonItem>();
if(!buttonbelow) {
buttonbelow = getParent<ButtonItem>()->getParent<RowItem>()->getItemBelow<RowItem>()->getChild<ButtonItem>(0);
}
// apply change in config
auto& buttonConf = getParent<ButtonItem>()->getConfig().samples;
auto& newButtonconf = buttonbelow->getConfig().samples;
newButtonconf.insert(newButtonconf.begin(), buttonConf.back());
buttonConf.erase(buttonConf.end());
2021-12-25 14:26:19 +01:00
// remove button from this row and attach to other
getParent<ButtonItem>()->removeChild(this);
buttonbelow->insertChild(0, this);
//update local parent
mparent = buttonbelow;
//update local pos
pos = 0;
//update new buttons below
reenumerateAllSiblings();
2021-12-25 14:37:34 +01:00
expandTree();
2022-04-02 13:46:11 +02:00
}