adding and renaming buttons

This commit is contained in:
MrBesen 2021-12-21 20:19:24 +01:00
parent e3407d3053
commit d1353545bd
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
6 changed files with 131 additions and 20 deletions

View File

@ -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 (ButtonManagerItem::*T)()>
void perform();

View File

@ -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<Config::ButtonConfig>& 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;
};

View File

@ -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<ButtonManagerItem*>(item)) return;
if(RowItem* row = dynamic_cast<RowItem*>(item)) {
} else if(ButtonItem* button = dynamic_cast<ButtonItem*>(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<RowItem*>(selectedItem)) {
return row;
}
//TODO: scan for parents
return nullptr;
}
template<void (ButtonManagerItem::*T)()>
void ButtonManager::perform() {
ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(getSelectedItem());

View File

@ -41,22 +41,11 @@ RowItem::RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& con
const std::vector<Config::ButtonConfig>& 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<Config::ButtonConfig>& 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);
}

View File

@ -55,6 +55,8 @@ void Config::load() {
}
} else {
Log::error << "config File not found";
// just save the current config to create a file
save();
}
}

View File

@ -141,8 +141,8 @@
<slot>deleteButton()</slot>
<hints>
<hint type="sourcelabel">
<x>854</x>
<y>82</y>
<x>878</x>
<y>113</y>
</hint>
<hint type="destinationlabel">
<x>803</x>
@ -173,8 +173,8 @@
<slot>downButton()</slot>
<hints>
<hint type="sourcelabel">
<x>867</x>
<y>155</y>
<x>878</x>
<y>187</y>
</hint>
<hint type="destinationlabel">
<x>804</x>
@ -214,6 +214,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>buttonTreeWidget</sender>
<signal>itemChanged(QTreeWidgetItem*,int)</signal>
<receiver>ButtonManager</receiver>
<slot>itemChanged(QTreeWidgetItem*,int)</slot>
<hints>
<hint type="sourcelabel">
<x>312</x>
<y>33</y>
</hint>
<hint type="destinationlabel">
<x>4</x>
<y>65</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>addButton()</slot>
@ -223,5 +239,6 @@
<slot>downButton()</slot>
<slot>itemSelected()</slot>
<slot>dialogButtonPressed(QAbstractButton*)</slot>
<slot>itemChanged(QTreeWidgetItem*,int)</slot>
</slots>
</ui>