forked from MrBesen/soundboard
156 lines
3.8 KiB
C++
156 lines
3.8 KiB
C++
#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::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() {}
|
|
|
|
|
|
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();
|
|
setExpanded(true);
|
|
|
|
// iterate buttons in a row
|
|
for(uint8_t btnnr = 0; btnnr < btnrow.size(); btnnr++) {
|
|
ButtonItem* btnItem = new ButtonItem(this, btnnr, conf);
|
|
}
|
|
}
|
|
|
|
bool RowItem::hasRemove() const {
|
|
return true;
|
|
}
|
|
|
|
bool RowItem::hasMoveUp() const {
|
|
return pos > 0;
|
|
}
|
|
|
|
bool RowItem::hasMoveDown() const {
|
|
return pos < conf.buttons.size()-1;
|
|
}
|
|
|
|
void RowItem::remove() {
|
|
conf.buttons.erase(conf.buttons.begin() + pos);
|
|
|
|
for(uint8_t i = pos+1; i <= conf.buttons.size(); ++i) {
|
|
RowItem* item = dynamic_cast<RowItem*>(mparent->child(i));
|
|
if(item) {
|
|
item->pos--;
|
|
item->updatePosition();
|
|
}
|
|
}
|
|
}
|
|
|
|
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 = dynamic_cast<RowItem*>(mparent->child(pos-1));
|
|
if(rowaboveItem) {
|
|
rowaboveItem->pos++;
|
|
rowaboveItem->updatePosition();
|
|
} else {
|
|
Log::error << "row above could not be updated";
|
|
}
|
|
|
|
bool wasexpanded = isExpanded();
|
|
mparent->removeChild(this);
|
|
|
|
mparent->insertChild(pos-1, this);
|
|
setExpanded(wasexpanded);
|
|
|
|
pos--;
|
|
updatePosition();
|
|
}
|
|
|
|
void RowItem::moveDown() {
|
|
// moving down is the same as moving the item below up
|
|
RowItem* rowbelow = dynamic_cast<RowItem*>(mparent->child(pos+1));
|
|
if(rowbelow) {
|
|
rowbelow->moveUp();
|
|
} else {
|
|
Log::error << "no item below";
|
|
}
|
|
}
|
|
|
|
uint8_t RowItem::getRow() const {
|
|
return pos;
|
|
}
|
|
|
|
void RowItem::updatePosition() {
|
|
setData(0, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1));
|
|
}
|
|
|
|
std::vector<Config::ButtonConfig>& RowItem::getConfig() {
|
|
return conf.buttons.at(pos);
|
|
}
|
|
|
|
|
|
|
|
|
|
ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE), conf(conf), buttonnr(buttonnr), row(parent) {
|
|
Config::ButtonConfig& btn = getConfig();
|
|
setData(1, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(btn.name)));
|
|
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) {
|
|
SampleItem* sample = new SampleItem(this, samplenr, conf);
|
|
}
|
|
}
|
|
|
|
void ButtonItem::nameWasChanged() {
|
|
std::string newName = data(1, Qt::ItemDataRole::DisplayRole).toString().toStdString();
|
|
getConfig().name = newName;
|
|
}
|
|
|
|
Config::ButtonConfig& ButtonItem::getConfig() {
|
|
return row->getConfig().at(buttonnr);
|
|
}
|
|
|
|
|
|
|
|
SampleItem::SampleItem(ButtonItem* parent, uint8_t samplenr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE), conf(conf), samplenr(samplenr), button(parent) {
|
|
const Config::SampleConfig& sample = getConfig();
|
|
setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) samplenr+1));
|
|
setData(3, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(sample.file)));
|
|
}
|
|
|
|
Config::SampleConfig& SampleItem::getConfig() {
|
|
return button->getConfig().samples.at(samplenr);
|
|
} |