Skip to content

Commit e2091ff

Browse files
committed
feat(ui): implement adding/removing entries
1 parent 7011faa commit e2091ff

2 files changed

Lines changed: 63 additions & 14 deletions

File tree

include/editor/paramListWidget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <QWidget>
55
#include <QListView>
6+
#include <QPushButton>
67
#include <QStandardItemModel>
78

89
#include "param/param.h"
@@ -22,6 +23,7 @@ private slots:
2223

2324
private:
2425
void populateList();
26+
void appendItem(int index = -1);
2527
void loadFriendlyNames();
2628

2729
private:
@@ -31,6 +33,9 @@ private slots:
3133
QListView mListView;
3234

3335
std::map<int, QString> mFriendlyNames;
36+
37+
QPushButton mInsertButton;
38+
QPushButton mDeleteButton;
3439
};
3540

3641
#endif // PARAMLISTWIDGET_H

src/editor/paramListWidget.cpp

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,45 @@ ParamListWidget::ParamListWidget(QWidget* parent) :
1313
mListView.setModel(&mListModel);
1414
connect(mListView.selectionModel(), &QItemSelectionModel::selectionChanged, this, &ParamListWidget::handleSelectionChanged);
1515

16+
// Insert Button
17+
mInsertButton.setText("Insert");
18+
connect(&mInsertButton, &QPushButton::pressed, this, [=, this]() {
19+
if (!mParamListPtr) { return; }
20+
mParamListPtr->emplace_back();
21+
appendItem();
22+
});
23+
24+
// Delete Button
25+
mDeleteButton.setText("Delete");
26+
connect(&mDeleteButton, &QPushButton::pressed, this, [=, this]() {
27+
if (!mParamListPtr) { return; }
28+
29+
auto selectionModel = mListView.selectionModel();
30+
if (!selectionModel) { return; }
31+
32+
QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
33+
if (selectedIndexes.isEmpty()) { return; }
34+
35+
int row = selectedIndexes.first().row();
36+
37+
if (row >= 0 && row < static_cast<int>(mParamListPtr->size())) {
38+
mParamListPtr->erase(mParamListPtr->begin() + row);
39+
} else {
40+
return;
41+
}
42+
43+
mListModel.removeRow(row);
44+
});
45+
46+
// Button Layout
47+
auto* buttonLayout = new QHBoxLayout;
48+
buttonLayout->addWidget(&mInsertButton);
49+
buttonLayout->addWidget(&mDeleteButton);
50+
1651
// Main Layout
1752
auto* mainLayout = new QVBoxLayout(this);
1853
mainLayout->addWidget(&mListView);
54+
mainLayout->addLayout(buttonLayout);
1955

2056
loadFriendlyNames();
2157
}
@@ -63,23 +99,31 @@ void ParamListWidget::populateList() {
6399
return;
64100
}
65101

66-
int index = 0;
67-
for (const auto& param : *mParamListPtr) {
68-
auto iter = mFriendlyNames.find(index);
69-
QString label;
70-
if (iter != mFriendlyNames.end()) {
71-
label = QString("Param %1 : %2").arg(index).arg(iter->second);
72-
} else {
73-
label = QString("Param %1").arg(index);
74-
}
75-
QStandardItem* item = new QStandardItem(label);
76-
item->setEditable(false);
77-
item->setData(index, Qt::UserRole);
78-
mListModel.appendRow(item);
79-
++index;
102+
int count = static_cast<int>(mParamListPtr->size());
103+
for (int index = 0; index < count; ++index) {
104+
appendItem(index);
80105
}
81106
}
82107

108+
void ParamListWidget::appendItem(int index) {
109+
if (index == -1) {
110+
index = mListModel.rowCount();
111+
}
112+
113+
QString label;
114+
auto iter = mFriendlyNames.find(index);
115+
if (iter != mFriendlyNames.end()) {
116+
label = QString("Param %1 : %2").arg(index).arg(iter->second);
117+
} else {
118+
label = QString("Param %1").arg(index);
119+
}
120+
121+
QStandardItem* item = new QStandardItem(label);
122+
item->setEditable(false);
123+
item->setData(index, Qt::UserRole);
124+
mListModel.appendRow(item);
125+
}
126+
83127
void ParamListWidget::handleSelectionChanged(const QItemSelection& selection) {
84128
if (selection.indexes().isEmpty())
85129
return;

0 commit comments

Comments
 (0)