From 088293b3bb6b91e489bda630e6db500e3fd84234 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 2 Apr 2024 20:32:46 +0300 Subject: [PATCH] UI: RuleEdit: Remove Preset Rule --- src/ui/form/rule/ruleeditdialog.cpp | 28 ++++++++++++++++++++++++---- src/ui/form/rule/ruleeditdialog.h | 3 +++ src/ui/model/rulesetmodel.cpp | 9 +++++++++ src/ui/model/rulesetmodel.h | 2 ++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/ui/form/rule/ruleeditdialog.cpp b/src/ui/form/rule/ruleeditdialog.cpp index adaa66a9..10c56728 100644 --- a/src/ui/form/rule/ruleeditdialog.cpp +++ b/src/ui/form/rule/ruleeditdialog.cpp @@ -174,6 +174,9 @@ QLayout *RuleEditDialog::setupMainLayout() // RuleSet View setupRuleSetView(); + // Actions on rule set view's current changed + setupRuleSetViewChanged(); + // OK/Cancel auto buttonsLayout = setupButtons(); @@ -270,10 +273,10 @@ QLayout *RuleEditDialog::setupRuleSetHeaderLayout() m_btAddPresetRule = ControlUtil::createFlatToolButton(":/icons/add.png", [&] { selectPresetRuleDialog(); - m_ruleSetView->setVisible(true); + // m_ruleSetView->setVisible(false); }); m_btRemovePresetRule = ControlUtil::createFlatToolButton(":/icons/delete.png", [&] { - // TODO + ruleSetModel()->remove(ruleSetCurrentIndex()); }); m_btUpPresetRule = ControlUtil::createIconToolButton(":/icons/bullet_arrow_up.png", [&] { // TODO @@ -298,8 +301,6 @@ void RuleEditDialog::setupRuleSetView() m_ruleSetView->setAlternatingRowColors(true); m_ruleSetView->setModel(ruleSetModel()); - - m_ruleSetView->setVisible(false); } QLayout *RuleEditDialog::setupButtons() @@ -323,6 +324,25 @@ QLayout *RuleEditDialog::setupButtons() return layout; } +void RuleEditDialog::setupRuleSetViewChanged() +{ + const auto refreshRuleSetViewChanged = [&] { + const bool ruleSelected = (ruleSetCurrentIndex() >= 0); + m_btRemovePresetRule->setEnabled(ruleSelected); + m_btUpPresetRule->setEnabled(ruleSelected); + m_btDownPresetRule->setEnabled(ruleSelected); + }; + + refreshRuleSetViewChanged(); + + connect(m_ruleSetView, &ListView::currentIndexChanged, this, refreshRuleSetViewChanged); +} + +int RuleEditDialog::ruleSetCurrentIndex() const +{ + return m_ruleSetView->currentRow(); +} + bool RuleEditDialog::save() { if (!validateFields()) diff --git a/src/ui/form/rule/ruleeditdialog.h b/src/ui/form/rule/ruleeditdialog.h index e4fe3240..fc136ff1 100644 --- a/src/ui/form/rule/ruleeditdialog.h +++ b/src/ui/form/rule/ruleeditdialog.h @@ -54,6 +54,9 @@ private: QLayout *setupRuleSetHeaderLayout(); void setupRuleSetView(); QLayout *setupButtons(); + void setupRuleSetViewChanged(); + + int ruleSetCurrentIndex() const; bool save(); bool saveRule(Rule &rule); diff --git a/src/ui/model/rulesetmodel.cpp b/src/ui/model/rulesetmodel.cpp index 2d86e1e9..3ed873f4 100644 --- a/src/ui/model/rulesetmodel.cpp +++ b/src/ui/model/rulesetmodel.cpp @@ -50,3 +50,12 @@ void RuleSetModel::addRule(const RuleRow &ruleRow) setEdited(true); } + +void RuleSetModel::remove(int row) +{ + m_ruleSet.remove(row); + + StringListModel::remove(row); + + setEdited(true); +} diff --git a/src/ui/model/rulesetmodel.h b/src/ui/model/rulesetmodel.h index d1bf07d3..bffb0c8f 100644 --- a/src/ui/model/rulesetmodel.h +++ b/src/ui/model/rulesetmodel.h @@ -27,6 +27,8 @@ public: public slots: void addRule(const RuleRow &ruleRow); + void remove(int row = -1) override; + private: bool m_edited = false;