From 9fc7a939d19d60a489cb8bb866d538cf5b454935 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Wed, 22 Dec 2021 14:58:08 +0100 Subject: [PATCH] button move up --- include/buttonmanageritems.h | 36 +++++---- src/buttonmanageritems.cpp | 145 ++++++++++++++++++++++++++++------- 2 files changed, 139 insertions(+), 42 deletions(-) diff --git a/include/buttonmanageritems.h b/include/buttonmanageritems.h index d10f28f..644ca7d 100644 --- a/include/buttonmanageritems.h +++ b/include/buttonmanageritems.h @@ -6,7 +6,7 @@ // interface class ButtonManagerItem : public QTreeWidgetItem { protected: - ButtonManagerItem(QTreeWidgetItem* parent, int type); + ButtonManagerItem(QTreeWidgetItem* parent, int type, uint8_t pos); public: virtual ~ButtonManagerItem(); @@ -21,7 +21,11 @@ public: virtual void moveDown(); protected: - QTreeWidgetItem* mparent; + QTreeWidgetItem* mparent = nullptr; + uint8_t pos = 0; + + void updateAllPosBellow(int8_t diff, uint8_t size); + virtual void updatePosition(); }; class RowItem : public ButtonManagerItem { @@ -30,23 +34,25 @@ public: const static int TYPE = 1000; - virtual bool hasRemove() const; - virtual bool hasMoveUp() const; - virtual bool hasMoveDown() const; + virtual bool hasRemove() const override; + virtual bool hasMoveUp() const override; + virtual bool hasMoveDown() const override; - virtual void remove(); - virtual void moveUp(); - virtual void moveDown(); + virtual void remove() override; + virtual void moveUp() override; + virtual void moveDown() override; uint8_t getRow() const; // returns the number of this row std::vector& getConfig(); + RowItem* getRowAbove() const; + RowItem* getRowBelow() const; + private: Config::RootConfig& conf; - uint8_t pos = 0; //called when the row was moved - void updatePosition(); + void updatePosition() override; }; class ButtonItem : public ButtonManagerItem { @@ -56,8 +62,12 @@ public: const static int TYPE = 1001; virtual bool hasRemove() const override; + virtual bool hasMoveUp() const override; + virtual bool hasMoveDown() const override; virtual void remove() override; + virtual void moveUp() override; + virtual void moveDown() override; // triggered from the view when the name was changed @@ -66,8 +76,9 @@ public: Config::ButtonConfig& getConfig(); private: Config::RootConfig& conf; - uint8_t buttonnr = 0; RowItem* row = nullptr; + + void moveToRowAbove(); }; class SampleItem : public ButtonManagerItem { @@ -85,9 +96,8 @@ public: Config::SampleConfig& getConfig(); private: Config::RootConfig& conf; - uint8_t samplenr = 0; ButtonItem* button = nullptr; //called when a sample was moved - void updatePosition(); + void updatePosition() override; }; \ No newline at end of file diff --git a/src/buttonmanageritems.cpp b/src/buttonmanageritems.cpp index 9d2fc05..388be45 100644 --- a/src/buttonmanageritems.cpp +++ b/src/buttonmanageritems.cpp @@ -1,10 +1,11 @@ #include "buttonmanageritems.h" +#include #include #include "editsample.h" -ButtonManagerItem::ButtonManagerItem(QTreeWidgetItem* parent, int type) : QTreeWidgetItem(parent, type), mparent(parent) { +ButtonManagerItem::ButtonManagerItem(QTreeWidgetItem* parent, int type, uint8_t pos) : QTreeWidgetItem(parent, type), mparent(parent), pos(pos) { } ButtonManagerItem::~ButtonManagerItem() {} @@ -33,8 +34,20 @@ void ButtonManagerItem::moveUp() {} void ButtonManagerItem::moveDown() {} +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)); + if(item) { + item->pos += diff; + item->updatePosition(); + } + } +} -RowItem::RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE), conf(conf), pos(rownr) { +void ButtonManagerItem::updatePosition() {} + + +RowItem::RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, rownr), conf(conf) { const std::vector& btnrow = conf.buttons.at(rownr); updatePosition(); @@ -61,13 +74,7 @@ bool RowItem::hasMoveDown() const { 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(mparent->child(i)); - if(item) { - item->pos--; - item->updatePosition(); - } - } + updateAllPosBellow(-1, conf.buttons.size()); } void RowItem::moveUp() { @@ -117,10 +124,22 @@ std::vector& RowItem::getConfig() { return conf.buttons.at(pos); } +RowItem* RowItem::getRowAbove() const { + if(pos == 0) + return nullptr; + return dynamic_cast(mparent->child(pos-1)); +} + +RowItem* RowItem::getRowBelow() const { + if(pos == mparent->childCount() -1) + return nullptr; + return dynamic_cast(mparent->child(pos+1)); +} -ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE), conf(conf), buttonnr(buttonnr), row(parent) { + +ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, buttonnr), conf(conf), 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 @@ -135,30 +154,104 @@ bool ButtonItem::hasRemove() const { return true; } +bool ButtonItem::hasMoveUp() const { + return pos > 0 || row->hasMoveUp(); +} + +bool ButtonItem::hasMoveDown() const { + return pos < row->getConfig().size()-1 || row->hasMoveDown(); +} + void ButtonItem::remove() { auto& rowconf = row->getConfig(); - rowconf.erase(rowconf.begin() + buttonnr); + rowconf.erase(rowconf.begin() + pos); - for(uint8_t i = buttonnr+1; i <= rowconf.size(); ++i) { - ButtonItem* item = dynamic_cast(mparent->child(i)); - if(item) { - item->buttonnr--; - } + updateAllPosBellow(-1, rowconf.size()); +} + +void ButtonItem::moveUp() { + if(pos == 0) { + moveToRowAbove(); + return; + } + //apply change to config + auto& rowconf = row->getConfig(); + std::swap(rowconf.at(pos-1), rowconf.at(pos)); + + // apply change in GUI + + //get Child above + ButtonItem* buttonaboveItem = dynamic_cast(mparent->child(pos-1)); + if(buttonaboveItem) { + buttonaboveItem->pos++; + } else { + Log::error << "button above could not be updated"; + } + + bool wasexpanded = isExpanded(); + mparent->removeChild(this); + + mparent->insertChild(pos-1, this); + setExpanded(wasexpanded); + + pos--; +} + +void ButtonItem::moveDown() { + // moving down is the same as moving the item below up + ButtonItem* rowbelow = dynamic_cast(mparent->child(pos+1)); + if(rowbelow) { + rowbelow->moveUp(); + } else { + Log::error << "no item below"; } } + 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); + return row->getConfig().at(pos); +} + +void ButtonItem::moveToRowAbove() { + // get row above + RowItem* rowabove = row->getRowAbove(); + if(!rowabove) { + Log::error << "no row above!"; + return; + } + + // apply change in config + auto& rowconf = row->getConfig(); + auto& newrowconf = rowabove->getConfig(); + + newrowconf.push_back(rowconf.front()); + rowconf.erase(rowconf.begin()); + + // update buttons in old row + updateAllPosBellow(-1, rowconf.size()); + + // remove button from this row and attach to other + bool wasExpanded = isExpanded(); + row->removeChild(this); + + rowabove->addChild(this); + //update local parents + row = rowabove; + mparent = rowabove; + + //update local pos + pos = newrowconf.size()-1; + + setExpanded(wasExpanded); } - -SampleItem::SampleItem(ButtonItem* parent, uint8_t samplenr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE), conf(conf), samplenr(samplenr), 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(); updatePosition(); setText(3, QString::fromStdString(sample.file)); @@ -191,21 +284,15 @@ void SampleItem::edit() { void SampleItem::remove() { auto& btnconf = button->getConfig().samples; - btnconf.erase(btnconf.begin() + samplenr); + btnconf.erase(btnconf.begin() + pos); - for(uint8_t i = samplenr+1; i <= btnconf.size(); ++i) { - SampleItem* item = dynamic_cast(mparent->child(i)); - if(item) { - item->samplenr--; - item->updatePosition(); - } - } + updateAllPosBellow(-1, btnconf.size()); } Config::SampleConfig& SampleItem::getConfig() { - return button->getConfig().samples.at(samplenr); + return button->getConfig().samples.at(pos); } void SampleItem::updatePosition() { - setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) samplenr+1)); + setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1)); }