From d1353545bda0275a066b3843cd070645a4b4844e Mon Sep 17 00:00:00 2001 From: MrBesen Date: Tue, 21 Dec 2021 20:19:24 +0100 Subject: [PATCH] adding and renaming buttons --- include/buttonmanager.h | 2 ++ include/buttonmanageritems.h | 20 +++++++++++++- src/buttonmanager.cpp | 52 +++++++++++++++++++++++++++++++++++- src/buttonmanageritems.cpp | 50 ++++++++++++++++++++++++---------- src/config.cpp | 2 ++ ui/buttonmanager.ui | 25 ++++++++++++++--- 6 files changed, 131 insertions(+), 20 deletions(-) diff --git a/include/buttonmanager.h b/include/buttonmanager.h index cee5d00..019334d 100644 --- a/include/buttonmanager.h +++ b/include/buttonmanager.h @@ -33,6 +33,7 @@ private slots: void downButton(); void itemSelected(); + void itemChanged(QTreeWidgetItem* item, int column); void dialogButtonPressed(QAbstractButton* btn); private: @@ -48,6 +49,7 @@ private: void select(QTreeWidgetItem* item); QTreeWidgetItem* getSelectedItem() const; + RowItem* getCurrentRow() const; template void perform(); diff --git a/include/buttonmanageritems.h b/include/buttonmanageritems.h index cf328a1..6eaab00 100644 --- a/include/buttonmanageritems.h +++ b/include/buttonmanageritems.h @@ -28,7 +28,6 @@ class RowItem : public ButtonManagerItem { public: RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& conf); - const static int TYPE = 1000; virtual bool hasRemove() const; @@ -39,6 +38,9 @@ public: virtual void moveUp(); virtual void moveDown(); + uint8_t getRow() const; // returns the number of this row + std::vector& getConfig(); + private: Config::RootConfig& conf; uint8_t pos = 0; @@ -46,3 +48,19 @@ private: //called when the row was moved void updatePosition(); }; + +class ButtonItem : public ButtonManagerItem { +public: + ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf); + + const static int TYPE = 1001; + + // triggered from the view when the name was changed + void nameWasChanged(); + + Config::ButtonConfig& getConfig(); +private: + Config::RootConfig& conf; + uint8_t buttonnr = 0; + RowItem* row = nullptr; +}; \ No newline at end of file diff --git a/src/buttonmanager.cpp b/src/buttonmanager.cpp index ff5134d..30900ee 100644 --- a/src/buttonmanager.cpp +++ b/src/buttonmanager.cpp @@ -43,11 +43,28 @@ void ButtonManager::addButton() { //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); + + //select the new row + select(row); return; } if(type == AddNewWhat::ReturnCode::Button) { - // create new button + //get row to create button in + RowItem* row = getCurrentRow(); + + if(row) { + //create new button in config + row->getConfig().push_back({}); + + // create new button GUI + ButtonItem* newButton = new ButtonItem(row, row->getConfig().size()-1, workingConfig); + + //select the new button + select(newButton); + } else { + Log::error << "no row selected"; + } return; } if(type == AddNewWhat::ReturnCode::Sample) { @@ -100,6 +117,22 @@ void ButtonManager::itemSelected() { } } +void ButtonManager::itemChanged(QTreeWidgetItem* item, int column) { + // only handle own items (should never trigger anyways) + if(!dynamic_cast(item)) return; + + if(RowItem* row = dynamic_cast(item)) { + + } else if(ButtonItem* button = dynamic_cast(item)) { + if(column != 1) { + Log::warn << "column: " << column << " of button was edited"; + return; + } + + button->nameWasChanged(); + } +} + void ButtonManager::dialogButtonPressed(QAbstractButton* btn) { QDialogButtonBox::ButtonRole role = ui->buttonBox->buttonRole(btn); if(role == QDialogButtonBox::ButtonRole::ResetRole) { @@ -150,6 +183,23 @@ QTreeWidgetItem* ButtonManager::getSelectedItem() const { return items.at(0); } +RowItem* ButtonManager::getCurrentRow() const { + QTreeWidgetItem* selectedItem = getSelectedItem(); + if(!selectedItem) { + // no item selected + //TODO: search for first empty row, last row, create a row + return nullptr; + } + + // is current selected item a row? + if(RowItem* row = dynamic_cast(selectedItem)) { + return row; + } + + //TODO: scan for parents + return nullptr; +} + template void ButtonManager::perform() { ButtonManagerItem* item = dynamic_cast(getSelectedItem()); diff --git a/src/buttonmanageritems.cpp b/src/buttonmanageritems.cpp index e321c2f..7176784 100644 --- a/src/buttonmanageritems.cpp +++ b/src/buttonmanageritems.cpp @@ -41,22 +41,11 @@ RowItem::RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& con const std::vector& btnrow = conf.buttons.at(rownr); updatePosition(); + setExpanded(true); // iterate buttons in a row - for(const Config::ButtonConfig& btn : btnrow) { - QTreeWidgetItem* qbtn = new QTreeWidgetItem(BUTTONTYPE); - qbtn->setData(1, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(btn.name))); - qbtn->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable); - addChild(qbtn); - - // iterate samples in a button - for(uint8_t samplenr = 0; samplenr < btn.samples.size(); ++samplenr) { - const Config::SampleConfig& sample = btn.samples.at(samplenr); - QTreeWidgetItem* qsample = new QTreeWidgetItem(SAMPLETYPE); - qsample->setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) samplenr+1)); - qsample->setData(3, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(sample.file))); - qbtn->addChild(qsample); - } + for(uint8_t btnnr = 0; btnnr < btnrow.size(); btnnr++) { + ButtonItem* btnItem = new ButtonItem(this, btnnr, conf); } } @@ -119,7 +108,40 @@ void RowItem::moveDown() { } } +uint8_t RowItem::getRow() const { + return pos; +} + void RowItem::updatePosition() { setData(0, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1)); } +std::vector& RowItem::getConfig() { + return conf.buttons.at(pos); +} + + + + +ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE), conf(conf), buttonnr(buttonnr), 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 + + // iterate samples in a button + for(uint8_t samplenr = 0; samplenr < btn.samples.size(); ++samplenr) { + const Config::SampleConfig& sample = btn.samples.at(samplenr); + QTreeWidgetItem* qsample = new QTreeWidgetItem(this, SAMPLETYPE); + qsample->setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) samplenr+1)); + qsample->setData(3, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(sample.file))); + } +} + +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); +} diff --git a/src/config.cpp b/src/config.cpp index 64283f6..57fc4a8 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -55,6 +55,8 @@ void Config::load() { } } else { Log::error << "config File not found"; + // just save the current config to create a file + save(); } } diff --git a/ui/buttonmanager.ui b/ui/buttonmanager.ui index 5289212..0c8487b 100644 --- a/ui/buttonmanager.ui +++ b/ui/buttonmanager.ui @@ -141,8 +141,8 @@ deleteButton() - 854 - 82 + 878 + 113 803 @@ -173,8 +173,8 @@ downButton() - 867 - 155 + 878 + 187 804 @@ -214,6 +214,22 @@ + + buttonTreeWidget + itemChanged(QTreeWidgetItem*,int) + ButtonManager + itemChanged(QTreeWidgetItem*,int) + + + 312 + 33 + + + 4 + 65 + + + addButton() @@ -223,5 +239,6 @@ downButton() itemSelected() dialogButtonPressed(QAbstractButton*) + itemChanged(QTreeWidgetItem*,int)