move buttons down

This commit is contained in:
mrbesen 2021-12-22 19:00:43 +01:00
parent 9fc7a939d1
commit d7ba980cfc
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
2 changed files with 43 additions and 5 deletions

View File

@ -79,6 +79,7 @@ private:
RowItem* row = nullptr; RowItem* row = nullptr;
void moveToRowAbove(); void moveToRowAbove();
void moveToRowBelow();
}; };
class SampleItem : public ButtonManagerItem { class SampleItem : public ButtonManagerItem {

View File

@ -35,7 +35,7 @@ void ButtonManagerItem::moveUp() {}
void ButtonManagerItem::moveDown() {} void ButtonManagerItem::moveDown() {}
void ButtonManagerItem::updateAllPosBellow(int8_t diff, uint8_t size) { void ButtonManagerItem::updateAllPosBellow(int8_t diff, uint8_t size) {
for(uint8_t i = pos+1; i <= size; ++i) { for(uint8_t i = pos+1; i < size; ++i) {
ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(mparent->child(i)); ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(mparent->child(i));
if(item) { if(item) {
item->pos += diff; item->pos += diff;
@ -74,7 +74,7 @@ bool RowItem::hasMoveDown() const {
void RowItem::remove() { void RowItem::remove() {
conf.buttons.erase(conf.buttons.begin() + pos); conf.buttons.erase(conf.buttons.begin() + pos);
updateAllPosBellow(-1, conf.buttons.size()); updateAllPosBellow(-1, conf.buttons.size()+1);
} }
void RowItem::moveUp() { void RowItem::moveUp() {
@ -166,7 +166,7 @@ void ButtonItem::remove() {
auto& rowconf = row->getConfig(); auto& rowconf = row->getConfig();
rowconf.erase(rowconf.begin() + pos); rowconf.erase(rowconf.begin() + pos);
updateAllPosBellow(-1, rowconf.size()); updateAllPosBellow(-1, rowconf.size()+1);
} }
void ButtonItem::moveUp() { void ButtonItem::moveUp() {
@ -198,6 +198,11 @@ void ButtonItem::moveUp() {
} }
void ButtonItem::moveDown() { void ButtonItem::moveDown() {
if(pos == row->getConfig().size()-1) {
moveToRowBelow();
return;
}
// moving down is the same as moving the item below up // moving down is the same as moving the item below up
ButtonItem* rowbelow = dynamic_cast<ButtonItem*>(mparent->child(pos+1)); ButtonItem* rowbelow = dynamic_cast<ButtonItem*>(mparent->child(pos+1));
if(rowbelow) { if(rowbelow) {
@ -207,7 +212,6 @@ void ButtonItem::moveDown() {
} }
} }
void ButtonItem::nameWasChanged() { void ButtonItem::nameWasChanged() {
std::string newName = data(1, Qt::ItemDataRole::DisplayRole).toString().toStdString(); std::string newName = data(1, Qt::ItemDataRole::DisplayRole).toString().toStdString();
getConfig().name = newName; getConfig().name = newName;
@ -233,7 +237,7 @@ void ButtonItem::moveToRowAbove() {
rowconf.erase(rowconf.begin()); rowconf.erase(rowconf.begin());
// update buttons in old row // update buttons in old row
updateAllPosBellow(-1, rowconf.size()); updateAllPosBellow(-1, rowconf.size()+1);
// remove button from this row and attach to other // remove button from this row and attach to other
bool wasExpanded = isExpanded(); bool wasExpanded = isExpanded();
@ -250,6 +254,39 @@ void ButtonItem::moveToRowAbove() {
setExpanded(wasExpanded); setExpanded(wasExpanded);
} }
void ButtonItem::moveToRowBelow() {
// get row below
RowItem* rowbelow = row->getRowBelow();
if(!rowbelow) {
Log::error << "no row below!";
return;
}
// apply change in config
auto& rowconf = row->getConfig();
auto& newrowconf = rowbelow->getConfig();
newrowconf.insert(newrowconf.begin(), rowconf.back());
rowconf.erase(rowconf.end());
// remove button from this row and attach to other
bool wasExpanded = isExpanded();
row->removeChild(this);
rowbelow->insertChild(0, this);
//update local parents
row = rowbelow;
mparent = rowbelow;
//update local pos
pos = 0;
//update new buttons below
updateAllPosBellow(1, newrowconf.size());
setExpanded(wasExpanded);
}
SampleItem::SampleItem(ButtonItem* parent, uint8_t samplenr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, samplenr), conf(conf), button(parent) { SampleItem::SampleItem(ButtonItem* parent, uint8_t samplenr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, samplenr), conf(conf), button(parent) {
const Config::SampleConfig& sample = getConfig(); const Config::SampleConfig& sample = getConfig();