128 lines
3.1 KiB
C++
128 lines
3.1 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();
|
|
|
|
// 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::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));
|
|
|
|
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";
|
|
}
|
|
|
|
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";
|
|
}
|
|
}
|
|
|
|
void RowItem::updatePosition() {
|
|
setData(0, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1));
|
|
}
|
|
|