diff --git a/src/ui/form/prog/programeditdialog.cpp b/src/ui/form/prog/programeditdialog.cpp index 6d3dc1f3..e0f9b553 100644 --- a/src/ui/form/prog/programeditdialog.cpp +++ b/src/ui/form/prog/programeditdialog.cpp @@ -612,12 +612,10 @@ QLayout *ProgramEditDialog::setupRuleLayout() m_btSelectRule = ControlUtil::createIconToolButton(":/icons/script.png", [&] { const int ruleId = VariantUtil::userData(m_editRuleName).toInt(); if (ruleId != 0) { - // Edit the Rule - return; + editRuleDialog(ruleId); + } else { + selectRuleDialog(); } - - // Select a Rule - selectRuleDialog(); }); auto layout = ControlUtil::createHLayoutByWidgets({ m_editRuleName, m_btSelectRule }); @@ -858,7 +856,7 @@ bool ProgramEditDialog::isWildcard() const void ProgramEditDialog::selectRuleDialog() { - auto rulesDialog = RulesWindow::showRulesDialog(this); + auto rulesDialog = RulesWindow::showRulesDialog(Rule::AppRule, this); connect(rulesDialog, &RulesWindow::ruleSelected, this, [&](const RuleRow &ruleRow) { VariantUtil::setUserData(m_editRuleName, ruleRow.ruleId); @@ -866,6 +864,11 @@ void ProgramEditDialog::selectRuleDialog() }); } +void ProgramEditDialog::editRuleDialog(int ruleId) +{ + RulesWindow::showRuleEditDialog(ruleId, Rule::AppRule, this); +} + void ProgramEditDialog::warnDangerousOption() const { IoC()->showErrorBox( diff --git a/src/ui/form/prog/programeditdialog.h b/src/ui/form/prog/programeditdialog.h index 218f34c7..230dc96d 100644 --- a/src/ui/form/prog/programeditdialog.h +++ b/src/ui/form/prog/programeditdialog.h @@ -102,6 +102,7 @@ private: bool isWildcard() const; void selectRuleDialog(); + void editRuleDialog(int ruleId); void warnDangerousOption() const; diff --git a/src/ui/form/rule/ruleswindow.cpp b/src/ui/form/rule/ruleswindow.cpp index 6a8d694c..34663c35 100644 --- a/src/ui/form/rule/ruleswindow.cpp +++ b/src/ui/form/rule/ruleswindow.cpp @@ -390,7 +390,7 @@ void RulesWindow::openRuleEditForm(const RuleRow &ruleRow) m_formRuleEdit->initialize(ruleRow); - WidgetWindow::showWidget(m_formRuleEdit); + DialogUtil::showDialog(m_formRuleEdit); } void RulesWindow::deleteSelectedRule() @@ -411,7 +411,7 @@ QModelIndex RulesWindow::ruleListCurrentIndex() const return m_ruleListView->currentIndex(); } -RulesWindow *RulesWindow::showRulesDialog(QWidget *parent, Rule::RuleType ruleType) +RulesWindow *RulesWindow::showRulesDialog(Rule::RuleType ruleType, QWidget *parent) { auto w = new RulesWindow(ruleType, parent, Qt::Dialog); w->setAttribute(Qt::WA_DeleteOnClose); @@ -420,3 +420,24 @@ RulesWindow *RulesWindow::showRulesDialog(QWidget *parent, Rule::RuleType ruleTy return w; } + +RuleEditDialog *RulesWindow::showRuleEditDialog( + int ruleId, Rule::RuleType ruleType, QWidget *parent) +{ + auto ctrl = new RulesController(); + + auto w = new RuleEditDialog(ctrl, parent); + w->setAttribute(Qt::WA_DeleteOnClose); + + ctrl->setParent(w); + + // Initialize the Rule edit dialog + { + const auto ruleRow = ctrl->ruleListModel()->ruleRowById(ruleId, ruleType); + w->initialize(ruleRow); + } + + DialogUtil::showDialog(w); + + return w; +} diff --git a/src/ui/form/rule/ruleswindow.h b/src/ui/form/rule/ruleswindow.h index 70727ee4..2a428115 100644 --- a/src/ui/form/rule/ruleswindow.h +++ b/src/ui/form/rule/ruleswindow.h @@ -46,7 +46,8 @@ public: void saveWindowState(bool wasVisible) override; void restoreWindowState() override; - static RulesWindow *showRulesDialog(QWidget *parent, Rule::RuleType ruleType = Rule::AppRule); + static RulesWindow *showRulesDialog(Rule::RuleType ruleType, QWidget *parent); + static RuleEditDialog *showRuleEditDialog(int ruleId, Rule::RuleType ruleType, QWidget *parent); signals: void ruleSelected(const RuleRow &ruleRow); diff --git a/src/ui/model/rulelistmodel.cpp b/src/ui/model/rulelistmodel.cpp index 0cb6d8c5..301be9db 100644 --- a/src/ui/model/rulelistmodel.cpp +++ b/src/ui/model/rulelistmodel.cpp @@ -267,6 +267,19 @@ const RuleRow &RuleListModel::ruleRowAt(const QModelIndex &index) const return m_ruleRow; } +RuleRow RuleListModel::ruleRowById(int ruleId, Rule::RuleType ruleType) const +{ + setSqlRuleType(ruleType); + + QVariantHash vars; + fillQueryVars(vars); + vars.insert(":rule_id", ruleId); + + RuleRow ruleRow; + updateRuleRow(sqlBase() + " AND t.rule_id = :rule_id;", vars, ruleRow); + return ruleRow; +} + bool RuleListModel::updateTableRow(const QVariantHash &vars, int /*row*/) const { return updateRuleRow(sql(), vars, m_ruleRow); @@ -324,7 +337,7 @@ QString RuleListModel::sqlOrderColumn() const return "rule_type, lower(name)"; } -void RuleListModel::setSqlRuleType(quint8 v) const +void RuleListModel::setSqlRuleType(qint8 v) const { if (m_sqlRuleType == v) return; diff --git a/src/ui/model/rulelistmodel.h b/src/ui/model/rulelistmodel.h index 67984b28..5b106b2f 100644 --- a/src/ui/model/rulelistmodel.h +++ b/src/ui/model/rulelistmodel.h @@ -53,6 +53,7 @@ public: Qt::ItemFlags flagHasChildren(const QModelIndex &index) const override; const RuleRow &ruleRowAt(const QModelIndex &index) const; + RuleRow ruleRowById(int ruleId, Rule::RuleType ruleType) const; static QStringList ruleTypeNames(); @@ -69,7 +70,7 @@ protected: QString sqlOrderColumn() const override; quint8 sqlRuleType() const { return m_sqlRuleType; } - void setSqlRuleType(quint8 v) const; + void setSqlRuleType(qint8 v) const; void setSqlRuleType(const QModelIndex &index) const; private: @@ -81,7 +82,7 @@ private: QVariant dataEnabled(const QModelIndex &index) const; private: - mutable quint8 m_sqlRuleType = 0; + mutable qint8 m_sqlRuleType = 0; mutable RuleRow m_ruleRow; };