From 3db543869cfd3a37defba8b38d1a0286471b9ba8 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Thu, 23 Dec 2021 20:07:03 +0100 Subject: [PATCH] move samples, getItemAbove() getItemBelow() --- include/buttonmanageritems.h | 18 +++-- src/buttonmanageritems.cpp | 129 +++++++++++++++++++++++++---------- 2 files changed, 106 insertions(+), 41 deletions(-) diff --git a/include/buttonmanageritems.h b/include/buttonmanageritems.h index 1f24670..a766692 100644 --- a/include/buttonmanageritems.h +++ b/include/buttonmanageritems.h @@ -29,6 +29,12 @@ public: template T* getSibling(int index) const; + template + T* getItemAbove() const; + + template + T* getItemBelow() const; + void reenumerateAllSiblings(); protected: QTreeWidgetItem* mparent = nullptr; @@ -55,9 +61,6 @@ public: uint8_t getRow() const; // returns the number of this row std::vector& getConfig(); - RowItem* getRowAbove() const; - RowItem* getRowBelow() const; - private: Config::RootConfig& conf; @@ -86,7 +89,6 @@ public: Config::ButtonConfig& getConfig(); private: Config::RootConfig& conf; - RowItem* row = nullptr; void moveToRowAbove(); void moveToRowBelow(); @@ -100,15 +102,21 @@ public: virtual bool hasEdit() const override; virtual bool hasRemove() const override; + virtual bool hasMoveUp() const override; + virtual bool hasMoveDown() const override; virtual void edit() override; virtual void remove() override; + virtual void moveUp() override; + virtual void moveDown() override; Config::SampleConfig& getConfig(); private: Config::RootConfig& conf; - ButtonItem* button = nullptr; //called when a sample was moved void updatePosition() override; + + void moveToButtonAbove(); + void moveToButtonBelow(); }; \ No newline at end of file diff --git a/src/buttonmanageritems.cpp b/src/buttonmanageritems.cpp index 3461f26..76388e4 100644 --- a/src/buttonmanageritems.cpp +++ b/src/buttonmanageritems.cpp @@ -48,11 +48,21 @@ T* ButtonManagerItem::getChild(int index) const { template T* ButtonManagerItem::getSibling(int index) const { - if(mparent->childCount() <= index) + if(mparent->childCount() <= index || index < 0) return nullptr; return dynamic_cast(mparent->child(index)); } +template +T* ButtonManagerItem::getItemAbove() const { + return getSibling(pos-1); +} + +template +T* ButtonManagerItem::getItemBelow() const { + return getSibling(pos+1); +} + // enforce existence of required templates template RowItem* ButtonManagerItem::getParent() const; template ButtonItem* ButtonManagerItem::getParent() const; @@ -166,22 +176,10 @@ 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, buttonnr), conf(conf), row(parent) { +ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, buttonnr), conf(conf) { 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 @@ -197,15 +195,15 @@ bool ButtonItem::hasRemove() const { } bool ButtonItem::hasMoveUp() const { - return pos > 0 || row->hasMoveUp(); + return pos > 0 || getParent()->hasMoveUp(); } bool ButtonItem::hasMoveDown() const { - return pos < row->getConfig().size()-1 || row->hasMoveDown(); + return pos < getParent()->getConfig().size()-1 || getParent()->hasMoveDown(); } void ButtonItem::remove() { - auto& rowconf = row->getConfig(); + auto& rowconf = getParent()->getConfig(); rowconf.erase(rowconf.begin() + pos); updateAllPosBellow(-1, rowconf.size()+1); @@ -217,7 +215,7 @@ void ButtonItem::moveUp() { return; } //apply change to config - auto& rowconf = row->getConfig(); + auto& rowconf = getParent()->getConfig(); std::swap(rowconf.at(pos-1), rowconf.at(pos)); // apply change in GUI @@ -240,15 +238,15 @@ void ButtonItem::moveUp() { } void ButtonItem::moveDown() { - if(pos == row->getConfig().size()-1) { + if(pos == getParent()->getConfig().size()-1) { moveToRowBelow(); return; } // moving down is the same as moving the item below up - ButtonItem* rowbelow = getSibling(pos+1); - if(rowbelow) { - rowbelow->moveUp(); + ButtonItem* buttonbelow = getSibling(pos+1); + if(buttonbelow) { + buttonbelow->moveUp(); } else { Log::error << "no item below"; } @@ -260,19 +258,19 @@ void ButtonItem::nameWasChanged() { } Config::ButtonConfig& ButtonItem::getConfig() { - return row->getConfig().at(pos); + return getParent()->getConfig().at(pos); } void ButtonItem::moveToRowAbove() { // get row above - RowItem* rowabove = row->getRowAbove(); + RowItem* rowabove = getParent()->getItemAbove(); if(!rowabove) { Log::error << "no row above!"; return; } // apply change in config - auto& rowconf = row->getConfig(); + auto& rowconf = getParent()->getConfig(); auto& newrowconf = rowabove->getConfig(); newrowconf.push_back(rowconf.front()); @@ -283,13 +281,12 @@ void ButtonItem::moveToRowAbove() { // remove button from this row and attach to other bool wasExpanded = isExpanded(); - row->removeChild(this); + getParent()->removeChild(this); reenumerateAllSiblings(); // depends on mparent beeing set to the old row rowabove->addChild(this); - //update local parents - row = rowabove; + //update local parent mparent = rowabove; //update local pos @@ -300,14 +297,14 @@ void ButtonItem::moveToRowAbove() { void ButtonItem::moveToRowBelow() { // get row below - RowItem* rowbelow = row->getRowBelow(); + RowItem* rowbelow = getParent()->getItemBelow(); if(!rowbelow) { Log::error << "no row below!"; return; } // apply change in config - auto& rowconf = row->getConfig(); + auto& rowconf = getParent()->getConfig(); auto& newrowconf = rowbelow->getConfig(); newrowconf.insert(newrowconf.begin(), rowconf.back()); @@ -315,11 +312,10 @@ void ButtonItem::moveToRowBelow() { // remove button from this row and attach to other bool wasExpanded = isExpanded(); - row->removeChild(this); + getParent()->removeChild(this); rowbelow->insertChild(0, this); - //update local parents - row = rowbelow; + //update local parent mparent = rowbelow; //update local pos @@ -332,7 +328,9 @@ void ButtonItem::moveToRowBelow() { } -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) { const Config::SampleConfig& sample = getConfig(); updatePosition(); setText(3, QString::fromStdString(sample.file)); @@ -347,6 +345,14 @@ bool SampleItem::hasRemove() const { return true; } +bool SampleItem::hasMoveUp() const { + return pos > 0 || getParent()->hasMoveUp(); +} + +bool SampleItem::hasMoveDown() const { + return pos < getParent()->getConfig().samples.size()-1 || getParent()->hasMoveDown(); +} + void SampleItem::edit() { Config::SampleConfig& sammpleConf = getConfig(); EditSample editor(sammpleConf.file); @@ -364,16 +370,67 @@ void SampleItem::edit() { } void SampleItem::remove() { - auto& btnconf = button->getConfig().samples; + auto& btnconf = getParent()->getConfig().samples; btnconf.erase(btnconf.begin() + pos); updateAllPosBellow(-1, btnconf.size()); } +void SampleItem::moveUp() { + if(pos == 0) { + moveToButtonAbove(); + return; + } + //apply change to config + auto& rowconf = getParent()->getConfig().samples; + std::swap(rowconf.at(pos-1), rowconf.at(pos)); + + // apply change in GUI + + //get Child above + SampleItem* sampleAboveItem = getSibling(pos-1); + if(sampleAboveItem) { + sampleAboveItem->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 SampleItem::moveDown() { + if(pos == getParent()->getConfig().samples.size()-1) { + moveToButtonBelow(); + return; + } + + // moving down is the same as moving the item below up + SampleItem* samplebelow = getSibling(pos+1); + if(samplebelow) { + samplebelow->moveUp(); + } else { + Log::error << "no item below"; + } +} + Config::SampleConfig& SampleItem::getConfig() { - return button->getConfig().samples.at(pos); + return getParent()->getConfig().samples.at(pos); } void SampleItem::updatePosition() { setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1)); } + +void SampleItem::moveToButtonAbove() { + +} + +void SampleItem::moveToButtonBelow() { + +} \ No newline at end of file