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>
T* getSibling(int index) const;
template<typename T>
T* getItemAbove() const;
template<typename T>
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<Config::ButtonConfig>& 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();
};

View File

@ -48,11 +48,21 @@ T* ButtonManagerItem::getChild(int index) const {
template<typename T>
T* ButtonManagerItem::getSibling(int index) const {
if(mparent->childCount() <= index)
if(mparent->childCount() <= index || index < 0)
return nullptr;
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
template RowItem* ButtonManagerItem::getParent() const;
template ButtonItem* ButtonManagerItem::getParent() const;
@ -166,22 +176,10 @@ std::vector<Config::ButtonConfig>& RowItem::getConfig() {
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), 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<RowItem>()->hasMoveUp();
}
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() {
auto& rowconf = row->getConfig();
auto& rowconf = getParent<RowItem>()->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<RowItem>()->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<RowItem>()->getConfig().size()-1) {
moveToRowBelow();
return;
}
// moving down is the same as moving the item below up
ButtonItem* rowbelow = getSibling<ButtonItem>(pos+1);
if(rowbelow) {
rowbelow->moveUp();
ButtonItem* buttonbelow = getSibling<ButtonItem>(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<RowItem>()->getConfig().at(pos);
}
void ButtonItem::moveToRowAbove() {
// get row above
RowItem* rowabove = row->getRowAbove();
RowItem* rowabove = getParent<RowItem>()->getItemAbove<RowItem>();
if(!rowabove) {
Log::error << "no row above!";
return;
}
// apply change in config
auto& rowconf = row->getConfig();
auto& rowconf = getParent<RowItem>()->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<RowItem>()->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<RowItem>()->getItemBelow<RowItem>();
if(!rowbelow) {
Log::error << "no row below!";
return;
}
// apply change in config
auto& rowconf = row->getConfig();
auto& rowconf = getParent<RowItem>()->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<RowItem>()->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<ButtonItem>()->hasMoveUp();
}
bool SampleItem::hasMoveDown() const {
return pos < getParent<ButtonItem>()->getConfig().samples.size()-1 || getParent<ButtonItem>()->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<ButtonItem>()->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<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() {
return button->getConfig().samples.at(pos);
return getParent<ButtonItem>()->getConfig().samples.at(pos);
}
void SampleItem::updatePosition() {
setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1));
}
void SampleItem::moveToButtonAbove() {
}
void SampleItem::moveToButtonBelow() {
}