helper methods to modify the ButtonManagerItem; more advanced getCurrentRow() and getCurrentButtn() methods

This commit is contained in:
mrbesen 2021-12-22 21:07:07 +01:00
parent d7ba980cfc
commit df58358c42
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
4 changed files with 126 additions and 22 deletions

View File

@ -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 (ButtonManagerItem::*T)()>
void perform();

View File

@ -20,6 +20,16 @@ public:
virtual void moveUp();
virtual void moveDown();
template<typename T>
T* getParent() const;
template<typename T>
T* getChild(int index) const;
template<typename T>
T* getSibling(int index) const;
void reenumerateAllSiblings();
protected:
QTreeWidgetItem* mparent = nullptr;
uint8_t pos = 0;

View File

@ -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<ButtonManagerItem*>(item)) return;
if(RowItem* row = dynamic_cast<RowItem*>(item)) {
} else if(ButtonItem* button = dynamic_cast<ButtonItem*>(item)) {
if(ButtonItem* button = dynamic_cast<ButtonItem*>(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<RowItem*>(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<ButtonItem*>(selectedItem)) {
return button->getParent<RowItem>();
}
// selected item is sample -> take grand parent
if(SampleItem* sample = dynamic_cast<SampleItem*>(selectedItem)) {
if(ButtonItem* button = sample->getParent<ButtonItem>())
return button->getParent<RowItem>();
}
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<SampleItem*>(selectedItem)) {
return sample->getParent<ButtonItem>();
}
// a row is selected use the first empty button
if(RowItem* row = dynamic_cast<RowItem*>(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<ButtonItem>(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 (ButtonManagerItem::*T)()>
void ButtonManager::perform() {
ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(getSelectedItem());

View File

@ -34,9 +34,51 @@ 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));
}
template<typename T>
T* ButtonManagerItem::getSibling(int index) const {
if(mparent->childCount() <= index)
return nullptr;
return dynamic_cast<T*>(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<ButtonManagerItem>(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<ButtonManagerItem*>(mparent->child(i));
ButtonManagerItem* item = getSibling<ButtonManagerItem>(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<RowItem*>(mparent->child(pos-1));
RowItem* rowaboveItem = getSibling<RowItem>(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<RowItem*>(mparent->child(pos+1));
RowItem* rowbelow = getSibling<RowItem>(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<ButtonItem*>(mparent->child(pos-1));
ButtonItem* buttonaboveItem = getSibling<ButtonItem>(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<ButtonItem*>(mparent->child(pos+1));
ButtonItem* rowbelow = getSibling<ButtonItem>(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;