From df58358c42f8e8bfea78b24e68cc5304564dbd06 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Wed, 22 Dec 2021 21:07:07 +0100 Subject: [PATCH] helper methods to modify the ButtonManagerItem; more advanced getCurrentRow() and getCurrentButtn() methods --- include/buttonmanager.h | 8 +++- include/buttonmanageritems.h | 10 +++++ src/buttonmanager.cpp | 72 +++++++++++++++++++++++++++++------- src/buttonmanageritems.cpp | 58 +++++++++++++++++++++++++---- 4 files changed, 126 insertions(+), 22 deletions(-) diff --git a/include/buttonmanager.h b/include/buttonmanager.h index 6b89356..5b00544 100644 --- a/include/buttonmanager.h +++ b/include/buttonmanager.h @@ -50,8 +50,12 @@ private: void select(QTreeWidgetItem* item); QTreeWidgetItem* getSelectedItem() const; - RowItem* getCurrentRow() const; - ButtonItem* getCurrentButton() const; + // create: allow this method to create a new Row, if nothing else is found + // onlyIfSelected: only return a row if there is a selection + RowItem* getCurrentRow(bool create = true, bool onlyIfSelected = false); + ButtonItem* getCurrentButton(bool create = true) const; + + RowItem* addNewRow(); template void perform(); diff --git a/include/buttonmanageritems.h b/include/buttonmanageritems.h index 83f395e..1f24670 100644 --- a/include/buttonmanageritems.h +++ b/include/buttonmanageritems.h @@ -20,6 +20,16 @@ public: virtual void moveUp(); virtual void moveDown(); + template + T* getParent() const; + + template + T* getChild(int index) const; + + template + T* getSibling(int index) const; + + void reenumerateAllSiblings(); protected: QTreeWidgetItem* mparent = nullptr; uint8_t pos = 0; diff --git a/src/buttonmanager.cpp b/src/buttonmanager.cpp index fcb391c..2c7b99c 100644 --- a/src/buttonmanager.cpp +++ b/src/buttonmanager.cpp @@ -44,10 +44,8 @@ void ButtonManager::addButton() { AddNewWhat::ReturnCode type = whatshouldbeadded.returnCode; if(type == AddNewWhat::ReturnCode::Row) { // create new row - //TODO: when a row is selected: add after the selected row - workingConfig.buttons.push_back({}); // create a empty vector - RowItem* row = new RowItem(ui->buttonTreeWidget->invisibleRootItem(), workingConfig.buttons.size()-1, workingConfig); - + RowItem* row = addNewRow(); + //select the new row select(row); @@ -176,9 +174,7 @@ void ButtonManager::itemChanged(QTreeWidgetItem* item, int column) { // only handle own items (should never trigger anyways) if(!dynamic_cast(item)) return; - if(RowItem* row = dynamic_cast(item)) { - - } else if(ButtonItem* button = dynamic_cast(item)) { + if(ButtonItem* button = dynamic_cast(item)) { if(column != 1) { Log::warn << "column: " << column << " of button was edited"; return; @@ -218,6 +214,7 @@ void ButtonManager::loadConfig() { QTreeWidgetItem* root = ui->buttonTreeWidget->invisibleRootItem(); for(uint8_t rownr = 0; rownr < workingConfig.buttons.size(); ++rownr) { RowItem* row = new RowItem(root, rownr, workingConfig); + (void) row; } } @@ -246,11 +243,27 @@ QTreeWidgetItem* ButtonManager::getSelectedItem() const { return items.at(0); } -RowItem* ButtonManager::getCurrentRow() const { +RowItem* ButtonManager::getCurrentRow(bool create, bool onlyIfSelected) { QTreeWidgetItem* selectedItem = getSelectedItem(); - if(!selectedItem) { + // no item selected and its allowed to use alternative messures + if(!selectedItem && !onlyIfSelected) { // no item selected - //TODO: search for first empty row, last row, create a row + + // search for a empty row + for(uint8_t rownr = 0; rownr < workingConfig.buttons.size(); ++rownr) { + if(workingConfig.buttons.at(rownr).empty()) { + //first empty row + RowItem* row = dynamic_cast(ui->buttonTreeWidget->invisibleRootItem()->child(rownr)); + if(row) + return row; + } + } + + // no empty row -> create new row + if(create) { + return addNewRow(); + } + return nullptr; } @@ -259,11 +272,20 @@ RowItem* ButtonManager::getCurrentRow() const { return row; } - //TODO: scan for parents + // selected item is button -> take parent + if(ButtonItem* button = dynamic_cast(selectedItem)) { + return button->getParent(); + } + + // selected item is sample -> take grand parent + if(SampleItem* sample = dynamic_cast(selectedItem)) { + if(ButtonItem* button = sample->getParent()) + return button->getParent(); + } return nullptr; } -ButtonItem* ButtonManager::getCurrentButton() const { +ButtonItem* ButtonManager::getCurrentButton(bool create) const { QTreeWidgetItem* selectedItem = getSelectedItem(); if(!selectedItem) { // no item selected @@ -276,10 +298,34 @@ ButtonItem* ButtonManager::getCurrentButton() const { return btn; } - //TODO: scan for parents / siblings + // a sample is selected -> use its parent + if(SampleItem* sample = dynamic_cast(selectedItem)) { + return sample->getParent(); + } + + // a row is selected use the first empty button + if(RowItem* row = dynamic_cast(selectedItem)) { + // search for empty button in current row + auto& rowConf = row->getConfig(); + for(uint8_t buttonnr = 0; buttonnr < rowConf.size(); ++buttonnr) { + if(rowConf.at(buttonnr).samples.empty()) { + return row->getChild(buttonnr); + } + } + } + return nullptr; } +RowItem* ButtonManager::addNewRow() { + //TODO: when a row is selected: add after the selected row + + workingConfig.buttons.push_back({}); // create a empty vector + RowItem* row = new RowItem(ui->buttonTreeWidget->invisibleRootItem(), workingConfig.buttons.size()-1, workingConfig); + + return row; +} + template void ButtonManager::perform() { ButtonManagerItem* item = dynamic_cast(getSelectedItem()); diff --git a/src/buttonmanageritems.cpp b/src/buttonmanageritems.cpp index 22f342d..3461f26 100644 --- a/src/buttonmanageritems.cpp +++ b/src/buttonmanageritems.cpp @@ -34,9 +34,51 @@ void ButtonManagerItem::moveUp() {} void ButtonManagerItem::moveDown() {} +template +T* ButtonManagerItem::getParent() const { + return dynamic_cast(mparent); +} + +template +T* ButtonManagerItem::getChild(int index) const { + if(childCount() <= index) + return nullptr; + return dynamic_cast(child(index)); +} + +template +T* ButtonManagerItem::getSibling(int index) const { + if(mparent->childCount() <= index) + return nullptr; + return dynamic_cast(mparent->child(index)); +} + +// 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(i); + if(item) { + item->pos = i; + item->updatePosition(); + } + } +} + void ButtonManagerItem::updateAllPosBellow(int8_t diff, uint8_t size) { for(uint8_t i = pos+1; i < size; ++i) { - ButtonManagerItem* item = dynamic_cast(mparent->child(i)); + ButtonManagerItem* item = getSibling(i); if(item) { item->pos += diff; item->updatePosition(); @@ -84,7 +126,7 @@ void RowItem::moveUp() { // apply change in GUI //get Child above - RowItem* rowaboveItem = dynamic_cast(mparent->child(pos-1)); + RowItem* rowaboveItem = getSibling(pos-1); if(rowaboveItem) { rowaboveItem->pos++; rowaboveItem->updatePosition(); @@ -104,7 +146,7 @@ void RowItem::moveUp() { void RowItem::moveDown() { // moving down is the same as moving the item below up - RowItem* rowbelow = dynamic_cast(mparent->child(pos+1)); + RowItem* rowbelow = getSibling(pos+1); if(rowbelow) { rowbelow->moveUp(); } else { @@ -181,7 +223,7 @@ void ButtonItem::moveUp() { // apply change in GUI //get Child above - ButtonItem* buttonaboveItem = dynamic_cast(mparent->child(pos-1)); + ButtonItem* buttonaboveItem = getSibling(pos-1); if(buttonaboveItem) { buttonaboveItem->pos++; } else { @@ -204,7 +246,7 @@ void ButtonItem::moveDown() { } // moving down is the same as moving the item below up - ButtonItem* rowbelow = dynamic_cast(mparent->child(pos+1)); + ButtonItem* rowbelow = getSibling(pos+1); if(rowbelow) { rowbelow->moveUp(); } else { @@ -237,12 +279,14 @@ void ButtonItem::moveToRowAbove() { rowconf.erase(rowconf.begin()); // update buttons in old row - updateAllPosBellow(-1, rowconf.size()+1); - + // updateAllPosBellow(-1, rowconf.size()+1); + // remove button from this row and attach to other bool wasExpanded = isExpanded(); row->removeChild(this); + reenumerateAllSiblings(); // depends on mparent beeing set to the old row + rowabove->addChild(this); //update local parents row = rowabove;