forked from MrBesen/soundboard
adding and renaming buttons
This commit is contained in:
parent
e3407d3053
commit
d1353545bd
|
@ -33,6 +33,7 @@ private slots:
|
||||||
void downButton();
|
void downButton();
|
||||||
|
|
||||||
void itemSelected();
|
void itemSelected();
|
||||||
|
void itemChanged(QTreeWidgetItem* item, int column);
|
||||||
void dialogButtonPressed(QAbstractButton* btn);
|
void dialogButtonPressed(QAbstractButton* btn);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -48,6 +49,7 @@ private:
|
||||||
void select(QTreeWidgetItem* item);
|
void select(QTreeWidgetItem* item);
|
||||||
|
|
||||||
QTreeWidgetItem* getSelectedItem() const;
|
QTreeWidgetItem* getSelectedItem() const;
|
||||||
|
RowItem* getCurrentRow() const;
|
||||||
|
|
||||||
template<void (ButtonManagerItem::*T)()>
|
template<void (ButtonManagerItem::*T)()>
|
||||||
void perform();
|
void perform();
|
||||||
|
|
|
@ -28,7 +28,6 @@ class RowItem : public ButtonManagerItem {
|
||||||
public:
|
public:
|
||||||
RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& conf);
|
RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& conf);
|
||||||
|
|
||||||
|
|
||||||
const static int TYPE = 1000;
|
const static int TYPE = 1000;
|
||||||
|
|
||||||
virtual bool hasRemove() const;
|
virtual bool hasRemove() const;
|
||||||
|
@ -39,6 +38,9 @@ public:
|
||||||
virtual void moveUp();
|
virtual void moveUp();
|
||||||
virtual void moveDown();
|
virtual void moveDown();
|
||||||
|
|
||||||
|
uint8_t getRow() const; // returns the number of this row
|
||||||
|
std::vector<Config::ButtonConfig>& getConfig();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Config::RootConfig& conf;
|
Config::RootConfig& conf;
|
||||||
uint8_t pos = 0;
|
uint8_t pos = 0;
|
||||||
|
@ -46,3 +48,19 @@ private:
|
||||||
//called when the row was moved
|
//called when the row was moved
|
||||||
void updatePosition();
|
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;
|
||||||
|
};
|
|
@ -43,11 +43,28 @@ void ButtonManager::addButton() {
|
||||||
//TODO: when a row is selected: add after the selected row
|
//TODO: when a row is selected: add after the selected row
|
||||||
workingConfig.buttons.push_back({}); // create a empty vector
|
workingConfig.buttons.push_back({}); // create a empty vector
|
||||||
RowItem* row = new RowItem(ui->buttonTreeWidget->invisibleRootItem(), workingConfig.buttons.size()-1, workingConfig);
|
RowItem* row = new RowItem(ui->buttonTreeWidget->invisibleRootItem(), workingConfig.buttons.size()-1, workingConfig);
|
||||||
|
|
||||||
|
//select the new row
|
||||||
|
select(row);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(type == AddNewWhat::ReturnCode::Button) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
if(type == AddNewWhat::ReturnCode::Sample) {
|
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) {
|
void ButtonManager::dialogButtonPressed(QAbstractButton* btn) {
|
||||||
QDialogButtonBox::ButtonRole role = ui->buttonBox->buttonRole(btn);
|
QDialogButtonBox::ButtonRole role = ui->buttonBox->buttonRole(btn);
|
||||||
if(role == QDialogButtonBox::ButtonRole::ResetRole) {
|
if(role == QDialogButtonBox::ButtonRole::ResetRole) {
|
||||||
|
@ -150,6 +183,23 @@ QTreeWidgetItem* ButtonManager::getSelectedItem() const {
|
||||||
return items.at(0);
|
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)()>
|
template<void (ButtonManagerItem::*T)()>
|
||||||
void ButtonManager::perform() {
|
void ButtonManager::perform() {
|
||||||
ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(getSelectedItem());
|
ButtonManagerItem* item = dynamic_cast<ButtonManagerItem*>(getSelectedItem());
|
||||||
|
|
|
@ -41,22 +41,11 @@ RowItem::RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& con
|
||||||
const std::vector<Config::ButtonConfig>& btnrow = conf.buttons.at(rownr);
|
const std::vector<Config::ButtonConfig>& btnrow = conf.buttons.at(rownr);
|
||||||
|
|
||||||
updatePosition();
|
updatePosition();
|
||||||
|
setExpanded(true);
|
||||||
|
|
||||||
// iterate buttons in a row
|
// iterate buttons in a row
|
||||||
for(const Config::ButtonConfig& btn : btnrow) {
|
for(uint8_t btnnr = 0; btnnr < btnrow.size(); btnnr++) {
|
||||||
QTreeWidgetItem* qbtn = new QTreeWidgetItem(BUTTONTYPE);
|
ButtonItem* btnItem = new ButtonItem(this, btnnr, conf);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +108,40 @@ void RowItem::moveDown() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t RowItem::getRow() const {
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
void RowItem::updatePosition() {
|
void RowItem::updatePosition() {
|
||||||
setData(0, Qt::ItemDataRole::DisplayRole, QVariant((int) pos+1));
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -55,6 +55,8 @@ void Config::load() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log::error << "config File not found";
|
Log::error << "config File not found";
|
||||||
|
// just save the current config to create a file
|
||||||
|
save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -141,8 +141,8 @@
|
||||||
<slot>deleteButton()</slot>
|
<slot>deleteButton()</slot>
|
||||||
<hints>
|
<hints>
|
||||||
<hint type="sourcelabel">
|
<hint type="sourcelabel">
|
||||||
<x>854</x>
|
<x>878</x>
|
||||||
<y>82</y>
|
<y>113</y>
|
||||||
</hint>
|
</hint>
|
||||||
<hint type="destinationlabel">
|
<hint type="destinationlabel">
|
||||||
<x>803</x>
|
<x>803</x>
|
||||||
|
@ -173,8 +173,8 @@
|
||||||
<slot>downButton()</slot>
|
<slot>downButton()</slot>
|
||||||
<hints>
|
<hints>
|
||||||
<hint type="sourcelabel">
|
<hint type="sourcelabel">
|
||||||
<x>867</x>
|
<x>878</x>
|
||||||
<y>155</y>
|
<y>187</y>
|
||||||
</hint>
|
</hint>
|
||||||
<hint type="destinationlabel">
|
<hint type="destinationlabel">
|
||||||
<x>804</x>
|
<x>804</x>
|
||||||
|
@ -214,6 +214,22 @@
|
||||||
</hint>
|
</hint>
|
||||||
</hints>
|
</hints>
|
||||||
</connection>
|
</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>
|
</connections>
|
||||||
<slots>
|
<slots>
|
||||||
<slot>addButton()</slot>
|
<slot>addButton()</slot>
|
||||||
|
@ -223,5 +239,6 @@
|
||||||
<slot>downButton()</slot>
|
<slot>downButton()</slot>
|
||||||
<slot>itemSelected()</slot>
|
<slot>itemSelected()</slot>
|
||||||
<slot>dialogButtonPressed(QAbstractButton*)</slot>
|
<slot>dialogButtonPressed(QAbstractButton*)</slot>
|
||||||
|
<slot>itemChanged(QTreeWidgetItem*,int)</slot>
|
||||||
</slots>
|
</slots>
|
||||||
</ui>
|
</ui>
|
||||||
|
|
Loading…
Reference in New Issue