diff --git a/src/ui/form/rule/ruleeditdialog.cpp b/src/ui/form/rule/ruleeditdialog.cpp index 10c56728..6ad26d44 100644 --- a/src/ui/form/rule/ruleeditdialog.cpp +++ b/src/ui/form/rule/ruleeditdialog.cpp @@ -275,15 +275,12 @@ QLayout *RuleEditDialog::setupRuleSetHeaderLayout() // m_ruleSetView->setVisible(false); }); - m_btRemovePresetRule = ControlUtil::createFlatToolButton(":/icons/delete.png", [&] { - ruleSetModel()->remove(ruleSetCurrentIndex()); - }); - m_btUpPresetRule = ControlUtil::createIconToolButton(":/icons/bullet_arrow_up.png", [&] { - // TODO - }); - m_btDownPresetRule = ControlUtil::createIconToolButton(":/icons/bullet_arrow_down.png", [&] { - // TODO - }); + m_btRemovePresetRule = ControlUtil::createFlatToolButton( + ":/icons/delete.png", [&] { ruleSetModel()->remove(ruleSetCurrentIndex()); }); + m_btUpPresetRule = ControlUtil::createIconToolButton( + ":/icons/bullet_arrow_up.png", [&] { ruleSetModel()->moveUp(ruleSetCurrentIndex()); }); + m_btDownPresetRule = ControlUtil::createIconToolButton(":/icons/bullet_arrow_down.png", + [&] { ruleSetModel()->moveDown(ruleSetCurrentIndex()); }); auto layout = ControlUtil::createHLayoutByWidgets( { m_btAddPresetRule, m_btRemovePresetRule, ControlUtil::createVSeparator(), diff --git a/src/ui/model/rulesetmodel.cpp b/src/ui/model/rulesetmodel.cpp index 3ed873f4..cf7ed0f8 100644 --- a/src/ui/model/rulesetmodel.cpp +++ b/src/ui/model/rulesetmodel.cpp @@ -53,9 +53,23 @@ void RuleSetModel::addRule(const RuleRow &ruleRow) void RuleSetModel::remove(int row) { + row = adjustRow(row); + m_ruleSet.remove(row); StringListModel::remove(row); setEdited(true); } + +void RuleSetModel::move(int fromRow, int toRow) +{ + if (!StringListModel::canMove(fromRow, toRow)) + return; + + m_ruleSet.move(fromRow, toRow); + + StringListModel::move(fromRow, toRow); + + setEdited(true); +} diff --git a/src/ui/model/rulesetmodel.h b/src/ui/model/rulesetmodel.h index bffb0c8f..f2c486f9 100644 --- a/src/ui/model/rulesetmodel.h +++ b/src/ui/model/rulesetmodel.h @@ -29,6 +29,10 @@ public slots: void remove(int row = -1) override; + void move(int fromRow, int toRow); + inline void moveUp(int row) { move(row, row - 1); } + inline void moveDown(int row) { move(row, row + 1); } + private: bool m_edited = false; diff --git a/src/ui/util/model/stringlistmodel.cpp b/src/ui/util/model/stringlistmodel.cpp index 0c8f30fa..a24aa2f0 100644 --- a/src/ui/util/model/stringlistmodel.cpp +++ b/src/ui/util/model/stringlistmodel.cpp @@ -66,6 +66,26 @@ void StringListModel::replace(const QString &text, int row) emit dataChanged(modelIndex, modelIndex); } +bool StringListModel::canMove(int fromRow, int toRow) +{ + if (fromRow == toRow || fromRow < 0 || toRow < 0) + return false; + + const int listSize = list().size(); + if (fromRow >= listSize || toRow >= listSize) + return false; + + return true; +} + +void StringListModel::move(int fromRow, int toRow) +{ + beginMoveRows( + QModelIndex(), fromRow, fromRow, QModelIndex(), toRow + (toRow > fromRow ? 1 : 0)); + m_list.move(fromRow, toRow); + endMoveRows(); +} + void StringListModel::reset() { if (isChanging()) diff --git a/src/ui/util/model/stringlistmodel.h b/src/ui/util/model/stringlistmodel.h index 33455d23..5341d8b2 100644 --- a/src/ui/util/model/stringlistmodel.h +++ b/src/ui/util/model/stringlistmodel.h @@ -26,6 +26,9 @@ public slots: virtual void remove(int row = -1); virtual void replace(const QString &text, int row = -1); + virtual bool canMove(int fromRow, int toRow); + virtual void move(int fromRow, int toRow); + void reset(); void refresh();