UI: RuleEdit: Move Preset Rules

This commit is contained in:
Nodir Temirkhodjaev 2024-04-02 20:56:15 +03:00
parent 088293b3bb
commit 8d7dcb147b
5 changed files with 47 additions and 9 deletions

View File

@ -275,15 +275,12 @@ QLayout *RuleEditDialog::setupRuleSetHeaderLayout()
// m_ruleSetView->setVisible(false); // m_ruleSetView->setVisible(false);
}); });
m_btRemovePresetRule = ControlUtil::createFlatToolButton(":/icons/delete.png", [&] { m_btRemovePresetRule = ControlUtil::createFlatToolButton(
ruleSetModel()->remove(ruleSetCurrentIndex()); ":/icons/delete.png", [&] { ruleSetModel()->remove(ruleSetCurrentIndex()); });
}); m_btUpPresetRule = ControlUtil::createIconToolButton(
m_btUpPresetRule = ControlUtil::createIconToolButton(":/icons/bullet_arrow_up.png", [&] { ":/icons/bullet_arrow_up.png", [&] { ruleSetModel()->moveUp(ruleSetCurrentIndex()); });
// TODO m_btDownPresetRule = ControlUtil::createIconToolButton(":/icons/bullet_arrow_down.png",
}); [&] { ruleSetModel()->moveDown(ruleSetCurrentIndex()); });
m_btDownPresetRule = ControlUtil::createIconToolButton(":/icons/bullet_arrow_down.png", [&] {
// TODO
});
auto layout = ControlUtil::createHLayoutByWidgets( auto layout = ControlUtil::createHLayoutByWidgets(
{ m_btAddPresetRule, m_btRemovePresetRule, ControlUtil::createVSeparator(), { m_btAddPresetRule, m_btRemovePresetRule, ControlUtil::createVSeparator(),

View File

@ -53,9 +53,23 @@ void RuleSetModel::addRule(const RuleRow &ruleRow)
void RuleSetModel::remove(int row) void RuleSetModel::remove(int row)
{ {
row = adjustRow(row);
m_ruleSet.remove(row); m_ruleSet.remove(row);
StringListModel::remove(row); StringListModel::remove(row);
setEdited(true); 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);
}

View File

@ -29,6 +29,10 @@ public slots:
void remove(int row = -1) override; 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: private:
bool m_edited = false; bool m_edited = false;

View File

@ -66,6 +66,26 @@ void StringListModel::replace(const QString &text, int row)
emit dataChanged(modelIndex, modelIndex); 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() void StringListModel::reset()
{ {
if (isChanging()) if (isChanging())

View File

@ -26,6 +26,9 @@ public slots:
virtual void remove(int row = -1); virtual void remove(int row = -1);
virtual void replace(const QString &text, 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 reset();
void refresh(); void refresh();