@@ -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+
83127void ParamListWidget::handleSelectionChanged (const QItemSelection& selection) {
84128 if (selection.indexes ().isEmpty ())
85129 return ;
0 commit comments