30 , _widget(new QWidget)
32 _ui->setupUi(_widget);
36 _ui->pbRxAdd, &QPushButton::pressed, std::bind(&CanRawFilterGuiImpl::handleListAdd,
this, _ui->rxTv));
38 _ui->pbRxRemove, &QPushButton::pressed, std::bind(&CanRawFilterGuiImpl::handleListRemove,
this, _ui->rxTv));
40 _ui->pbRxUp, &QPushButton::pressed, std::bind(&CanRawFilterGuiImpl::handleListUp,
this, _ui->rxTv));
42 _ui->pbRxDown, &QPushButton::pressed, std::bind(&CanRawFilterGuiImpl::handleListDown,
this, _ui->rxTv));
43 QObject::connect(_ui->rxPolicy, &QComboBox::currentTextChanged,
44 std::bind(&CanRawFilterGuiImpl::listUpdated,
this, _ui->rxTv));
47 _ui->pbTxAdd, &QPushButton::pressed, std::bind(&CanRawFilterGuiImpl::handleListAdd,
this, _ui->txTv));
49 _ui->pbTxRemove, &QPushButton::pressed, std::bind(&CanRawFilterGuiImpl::handleListRemove,
this, _ui->txTv));
51 _ui->pbTxUp, &QPushButton::pressed, std::bind(&CanRawFilterGuiImpl::handleListUp,
this, _ui->txTv));
53 _ui->pbTxDown, &QPushButton::pressed, std::bind(&CanRawFilterGuiImpl::handleListDown,
this, _ui->txTv));
54 QObject::connect(_ui->rxPolicy, &QComboBox::currentTextChanged,
55 std::bind(&CanRawFilterGuiImpl::listUpdated,
this, _ui->txTv));
65 _txListUpdatedCbk = cb;
72 _rxListUpdatedCbk = cb;
79 setList(list, _rxModel,
"RX");
85 setList(list, _txModel,
"TX");
90 void setList(
const AcceptList_t& list, QStandardItemModel& model,
const QString& dir)
92 model.removeRows(0, model.rowCount());
94 if (list.size() >= 1) {
96 for (AcceptList_t::size_type i = 0; i < list.size() - 1; ++i) {
97 const auto& item = list[i];
98 model.appendRow(prepareRow(std::get<0>(item), std::get<1>(item), dir, std::get<2>(item)));
102 const auto& item = list[list.size() - 1];
103 _ui->rxPolicy->setCurrentText(std::get<2>(item) ?
"ACCEPT" :
"DROP");
104 }
else if (dir ==
"TX") {
105 const auto& item = list[list.size() - 1];
106 _ui->txPolicy->setCurrentText(std::get<2>(item) ?
"ACCEPT" :
"DROP");
108 cds_warn(
"Wrong direction ({})", dir.toStdString());
112 cds_warn(
"{} list expected to have at least 1 element", dir.toStdString());
116 QStandardItemModel& getItemModel(QTableView* tv)
118 if (tv == _ui->txTv) {
125 void handleListDown(QTableView* tv)
127 auto& model = getItemModel(tv);
128 int currRow = tv->currentIndex().row();
129 int currCol = tv->currentIndex().column();
131 if (currRow < model.rowCount() - 1) {
132 const auto& row = model.takeRow(currRow);
133 model.insertRow(currRow + 1, row);
134 tv->setCurrentIndex(model.index(currRow + 1, currCol));
140 void handleListUp(QTableView* tv)
142 auto& model = getItemModel(tv);
143 int currRow = tv->currentIndex().row();
144 int currCol = tv->currentIndex().column();
147 const auto& row = model.takeRow(currRow);
148 model.insertRow(currRow - 1, row);
149 tv->setCurrentIndex(model.index(currRow - 1, currCol));
155 void handleListRemove(QTableView* tv)
157 auto& model = getItemModel(tv);
158 model.removeRow(tv->currentIndex().row());
163 void handleListAdd(QTableView* tv)
165 auto& model = getItemModel(tv);
168 if (tv == _ui->txTv) {
172 auto&& list = prepareRow(
".*",
".*", dir,
true);
173 model.insertRow(0, list);
178 QList<QStandardItem*> prepareRow(
const QString&
id,
const QString& payload,
const QString& dir,
bool policy)
180 QList<QStandardItem*> list;
181 list.append(
new QStandardItem(
id));
182 list.append(
new QStandardItem(payload));
183 QStandardItem* item =
new QStandardItem(dir);
184 item->setEditable(
false);
186 list.append(
new QStandardItem(policy ?
"ACCEPT" :
"DROP"));
191 AcceptList_t getAcceptList(
const QStandardItemModel& model,
bool policy)
195 for (
int i = 0; i < model.rowCount(); ++i) {
196 QString
id = model.item(i, 0)->data(Qt::DisplayRole).toString();
197 QString payload = model.item(i, 1)->data(Qt::DisplayRole).toString();
198 bool policy = model.item(i, 3)->data(Qt::DisplayRole).toString() ==
"ACCEPT";
208 void listUpdated(
const QTableView* tv)
210 const QStandardItemModel* model =
nullptr;
214 if (tv == _ui->txTv) {
216 cb = &_txListUpdatedCbk;
217 policy = _ui->txPolicy->currentText() ==
"ACCEPT";
218 }
else if (tv == _ui->rxTv) {
220 cb = &_rxListUpdatedCbk;
221 policy = _ui->rxPolicy->currentText() ==
"ACCEPT";
223 cds_error(
"Failed to recognized which list has been updated");
232 cds_warn(
"List callback not defined");
238 listUpdated(_ui->rxTv);
243 listUpdated(_ui->txTv);
246 template <
typename F>
247 void setRxDelegate(QTableView* tv,
int col, QStyledItemDelegate& del,
const std::function<
void()>& cb)
249 QItemEditorFactory* factory =
new QItemEditorFactory;
250 QItemEditorCreatorBase* editor =
new QStandardItemEditorCreator<F>();
251 factory->registerEditor(QVariant::String, editor);
252 del.setItemEditorFactory(factory);
253 tv->setItemDelegateForColumn(col, &del);
254 QObject::connect(&del, &QAbstractItemDelegate::closeEditor, cb);
259 setRxDelegate<QLineEdit>(_ui->rxTv, 0, _rxIdDelegate, std::bind(&CanRawFilterGuiImpl::listUpdatedRx,
this));
260 setRxDelegate<QLineEdit>(
261 _ui->rxTv, 1, _rxPayloadDelegate, std::bind(&CanRawFilterGuiImpl::listUpdatedRx,
this));
262 setRxDelegate<PolicyCB>(_ui->rxTv, 3, _rxPolicyDelegate, std::bind(&CanRawFilterGuiImpl::listUpdatedRx,
this));
264 _ui->rxTv->setModel(&_rxModel);
265 _rxModel.setHorizontalHeaderLabels(_tabList);
266 _ui->rxTv->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
267 _ui->rxTv->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
269 setRxDelegate<QLineEdit>(_ui->txTv, 0, _txIdDelegate, std::bind(&CanRawFilterGuiImpl::listUpdatedTx,
this));
270 setRxDelegate<QLineEdit>(
271 _ui->txTv, 1, _txPayloadDelegate, std::bind(&CanRawFilterGuiImpl::listUpdatedTx,
this));
272 setRxDelegate<PolicyCB>(_ui->txTv, 3, _txPolicyDelegate, std::bind(&CanRawFilterGuiImpl::listUpdatedTx,
this));
274 _ui->txTv->setModel(&_txModel);
275 _txModel.setHorizontalHeaderLabels(_tabList);
276 _ui->txTv->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
277 _ui->txTv->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
281 QStandardItemModel _rxModel;
282 QStandardItemModel _txModel;
283 const QStringList _tabList = {
"id",
"payload",
"dir",
"policy" };
284 Ui::CanRawFilterPrivate* _ui;
286 QStyledItemDelegate _rxIdDelegate;
287 QStyledItemDelegate _rxPayloadDelegate;
288 QStyledItemDelegate _rxPolicyDelegate;
289 QStyledItemDelegate _txIdDelegate;
290 QStyledItemDelegate _txPayloadDelegate;
291 QStyledItemDelegate _txPolicyDelegate;