move samples, getItemAbove() getItemBelow()

This commit is contained in:
mrbesen 2021-12-23 20:07:03 +01:00
parent f28f15e3e0
commit 3db543869c
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
2 changed files with 106 additions and 41 deletions

View File

@ -29,6 +29,12 @@ public:
template<typename T> template<typename T>
T* getSibling(int index) const; T* getSibling(int index) const;
template<typename T>
T* getItemAbove() const;
template<typename T>
T* getItemBelow() const;
void reenumerateAllSiblings(); void reenumerateAllSiblings();
protected: protected:
QTreeWidgetItem* mparent = nullptr; QTreeWidgetItem* mparent = nullptr;
@ -55,9 +61,6 @@ public:
uint8_t getRow() const; // returns the number of this row uint8_t getRow() const; // returns the number of this row
std::vector<Config::ButtonConfig>& getConfig(); std::vector<Config::ButtonConfig>& getConfig();
RowItem* getRowAbove() const;
RowItem* getRowBelow() const;
private: private:
Config::RootConfig& conf; Config::RootConfig& conf;
@ -86,7 +89,6 @@ public:
Config::ButtonConfig& getConfig(); Config::ButtonConfig& getConfig();
private: private:
Config::RootConfig& conf; Config::RootConfig& conf;
RowItem* row = nullptr;
void moveToRowAbove(); void moveToRowAbove();
void moveToRowBelow(); void moveToRowBelow();
@ -100,15 +102,21 @@ public:
virtual bool hasEdit() const override; virtual bool hasEdit() const override;
virtual bool hasRemove() const override; virtual bool hasRemove() const override;
virtual bool hasMoveUp() const override;
virtual bool hasMoveDown() const override;
virtual void edit() override; virtual void edit() override;
virtual void remove() override; virtual void remove() override;
virtual void moveUp() override;
virtual void moveDown() override;
Config::SampleConfig& getConfig(); Config::SampleConfig& getConfig();
private: private:
Config::RootConfig& conf; Config::RootConfig& conf;
ButtonItem* button = nullptr;
//called when a sample was moved //called when a sample was moved
void updatePosition() override; void updatePosition() override;
void moveToButtonAbove();
void moveToButtonBelow();
}; };

View File

@ -48,11 +48,21 @@ T* ButtonManagerItem::getChild(int index) const {
template<typename T> template<typename T>
T* ButtonManagerItem::getSibling(int index) const { T* ButtonManagerItem::getSibling(int index) const {
if(mparent->childCount() <= index) if(mparent->childCount() <= index || index < 0)
return nullptr; return nullptr;
return dynamic_cast<T*>(mparent->child(index)); return dynamic_cast<T*>(mparent->child(index));
} }
template<typename T>
T* ButtonManagerItem::getItemAbove() const {
return getSibling<T>(pos-1);
}
template<typename T>
T* ButtonManagerItem::getItemBelow() const {
return getSibling<T>(pos+1);
}
// enforce existence of required templates // enforce existence of required templates
template RowItem* ButtonManagerItem::getParent() const; template RowItem* ButtonManagerItem::getParent() const;
template ButtonItem* ButtonManagerItem::getParent() const; template ButtonItem* ButtonManagerItem::getParent() const;
@ -166,22 +176,10 @@ std::vector<Config::ButtonConfig>& RowItem::getConfig() {
return conf.buttons.at(pos); return conf.buttons.at(pos);
} }
RowItem* RowItem::getRowAbove() const {
if(pos == 0)
return nullptr;
return dynamic_cast<RowItem*>(mparent->child(pos-1));
}
RowItem* RowItem::getRowBelow() const {
if(pos == mparent->childCount() -1)
return nullptr;
return dynamic_cast<RowItem*>(mparent->child(pos+1));
}
ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, buttonnr), conf(conf) {
ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE, buttonnr), conf(conf), row(parent) {
Config::ButtonConfig& btn = getConfig(); Config::ButtonConfig& btn = getConfig();
setData(1, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(btn.name))); setData(1, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(btn.name)));
setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable); // | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable); // | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled
@ -197,15 +195,15 @@ bool ButtonItem::hasRemove() const {
} }
bool ButtonItem::hasMoveUp() const { bool ButtonItem::hasMoveUp() const {
return pos > 0 || row->hasMoveUp(); return pos > 0 || getParent<RowItem>()->hasMoveUp();
} }
bool ButtonItem::hasMoveDown() const { bool ButtonItem::hasMoveDown() const {
return pos < row->getConfig().size()-1 || row->hasMoveDown(); return pos < getParent<RowItem>()->getConfig().size()-1 || getParent<RowItem>()->hasMoveDown();
} }
void ButtonItem::remove() { void ButtonItem::remove() {
auto& rowconf = row->getConfig(); auto& rowconf = getParent<RowItem>()->getConfig();
rowconf.erase(rowconf.begin() + pos); rowconf.erase(rowconf.begin() + pos);
updateAllPosBellow(-1, rowconf.size()+1); updateAllPosBellow(-1, rowconf.size()+1);
@ -217,7 +215,7 @@ void ButtonItem::moveUp() {
return; return;
} }
//apply change to config //apply change to config
auto& rowconf = row->getConfig(); auto& rowconf = getParent<RowItem>()->getConfig();
std::swap(rowconf.at(pos-1), rowconf.at(pos)); std::swap(rowconf.at(pos-1), rowconf.at(pos));
// apply change in GUI // apply change in GUI
@ -240,15 +238,15 @@ void ButtonItem::moveUp() {
} }
void ButtonItem::moveDown() { void ButtonItem::moveDown() {
if(pos == row->getConfig().size()-1) { if(pos == getParent<RowItem>()->getConfig().size()-1) {
moveToRowBelow(); moveToRowBelow();
return; 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 = getSibling<ButtonItem>(pos+1); ButtonItem* buttonbelow = getSibling<ButtonItem>(pos+1);
if(rowbelow) { if(buttonbelow) {
rowbelow->moveUp(); buttonbelow->moveUp();
} else { } else {
Log::error << "no item below"; Log::error << "no item below";
} }
@ -260,19 +258,19 @@ void ButtonItem::nameWasChanged() {
} }
Config::ButtonConfig& ButtonItem::getConfig() { Config::ButtonConfig& ButtonItem::getConfig() {
return row->getConfig().at(pos); return getParent<RowItem>()->getConfig().at(pos);
} }
void ButtonItem::moveToRowAbove() { void ButtonItem::moveToRowAbove() {
// get row above // get row above
RowItem* rowabove = row->getRowAbove(); RowItem* rowabove = getParent<RowItem>()->getItemAbove<RowItem>();
if(!rowabove) { if(!rowabove) {
Log::error << "no row above!"; Log::error << "no row above!";
return; return;
} }
// apply change in config // apply change in config
auto& rowconf = row->getConfig(); auto& rowconf = getParent<RowItem>()->getConfig();
auto& newrowconf = rowabove->getConfig(); auto& newrowconf = rowabove->getConfig();
newrowconf.push_back(rowconf.front()); newrowconf.push_back(rowconf.front());
@ -283,13 +281,12 @@ void ButtonItem::moveToRowAbove() {
// remove button from this row and attach to other // remove button from this row and attach to other
bool wasExpanded = isExpanded(); bool wasExpanded = isExpanded();
row->removeChild(this); getParent<RowItem>()->removeChild(this);
reenumerateAllSiblings(); // depends on mparent beeing set to the old row reenumerateAllSiblings(); // depends on mparent beeing set to the old row
rowabove->addChild(this); rowabove->addChild(this);
//update local parents //update local parent
row = rowabove;
mparent = rowabove; mparent = rowabove;
//update local pos //update local pos
@ -300,14 +297,14 @@ void ButtonItem::moveToRowAbove() {
void ButtonItem::moveToRowBelow() { void ButtonItem::moveToRowBelow() {
// get row below // get row below
RowItem* rowbelow = row->getRowBelow(); RowItem* rowbelow = getParent<RowItem>()->getItemBelow<RowItem>();
if(!rowbelow) { if(!rowbelow) {
Log::error << "no row below!"; Log::error << "no row below!";
return; return;
} }
// apply change in config // apply change in config
auto& rowconf = row->getConfig(); auto& rowconf = getParent<RowItem>()->getConfig();
auto& newrowconf = rowbelow->getConfig(); auto& newrowconf = rowbelow->getConfig();
newrowconf.insert(newrowconf.begin(), rowconf.back()); newrowconf.insert(newrowconf.begin(), rowconf.back());
@ -315,11 +312,10 @@ void ButtonItem::moveToRowBelow() {
// remove button from this row and attach to other // remove button from this row and attach to other
bool wasExpanded = isExpanded(); bool wasExpanded = isExpanded();
row->removeChild(this); getParent<RowItem>()->removeChild(this);
rowbelow->insertChild(0, this); rowbelow->insertChild(0, this);
//update local parents //update local parent
row = rowbelow;
mparent = rowbelow; mparent = rowbelow;
//update local pos //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(); const Config::SampleConfig& sample = getConfig();
updatePosition(); updatePosition();
setText(3, QString::fromStdString(sample.file)); setText(3, QString::fromStdString(sample.file));
@ -347,6 +345,14 @@ bool SampleItem::hasRemove() const {
return true; return true;
} }
bool SampleItem::hasMoveUp() const {
return pos > 0 || getParent<ButtonItem>()->hasMoveUp();
}
bool SampleItem::hasMoveDown() const {
return pos < getParent<ButtonItem>()->getConfig().samples.size()-1 || getParent<ButtonItem>()->hasMoveDown();
}
void SampleItem::edit() { void SampleItem::edit() {
Config::SampleConfig& sammpleConf = getConfig(); Config::SampleConfig& sammpleConf = getConfig();
EditSample editor(sammpleConf.file); EditSample editor(sammpleConf.file);
@ -364,16 +370,67 @@ void SampleItem::edit() {
} }
void SampleItem::remove() { void SampleItem::remove() {
auto& btnconf = button->getConfig().samples; auto& btnconf = getParent<ButtonItem>()->getConfig().samples;
btnconf.erase(btnconf.begin() + pos); btnconf.erase(btnconf.begin() + pos);
updateAllPosBellow(-1, btnconf.size()); updateAllPosBellow(-1, btnconf.size());
} }
void SampleItem::moveUp() {
if(pos == 0) {
moveToButtonAbove();
return;
}
//apply change to config
auto& rowconf = getParent<ButtonItem>()->getConfig().samples;
std::swap(rowconf.at(pos-1), rowconf.at(pos));
// apply change in GUI
//get Child above
SampleItem* sampleAboveItem = getSibling<SampleItem>(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<ButtonItem>()->getConfig().samples.size()-1) {
moveToButtonBelow();
return;
}
// moving down is the same as moving the item below up
SampleItem* samplebelow = getSibling<SampleItem>(pos+1);
if(samplebelow) {
samplebelow->moveUp();
} else {
Log::error << "no item below";
}
}
Config::SampleConfig& SampleItem::getConfig() { Config::SampleConfig& SampleItem::getConfig() {
return button->getConfig().samples.at(pos); return getParent<ButtonItem>()->getConfig().samples.at(pos);
} }
void SampleItem::updatePosition() { void SampleItem::updatePosition() {
setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1)); setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1));
} }
void SampleItem::moveToButtonAbove() {
}
void SampleItem::moveToButtonBelow() {
}