Utils: Replace BaseAspect::pushUndo

Moving the logic into Undoable::set() reduces allocations.

Change-Id: Ib4a10d1e41da8d42d66bfe9ef24a667c5bfd8c0c
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-09-20 09:06:13 +02:00
parent d88c270aaf
commit 84ba3c58c2
2 changed files with 12 additions and 23 deletions

View File

@@ -322,19 +322,6 @@ QUndoStack *BaseAspect::undoStack() const
return d->m_undoStack; return d->m_undoStack;
} }
void BaseAspect::pushUndo(QUndoCommand *cmd)
{
if (!cmd)
return;
if (d->m_undoStack)
d->m_undoStack->push(cmd);
else {
cmd->redo();
delete cmd;
}
}
bool BaseAspect::isEnabled() const bool BaseAspect::isEnabled() const
{ {
return d->m_enabled; return d->m_enabled;
@@ -1739,7 +1726,7 @@ void BoolAspect::addToLayoutHelper(Layouting::LayoutItem &parent, QAbstractButto
} }
connect(button, &QAbstractButton::clicked, this, [button, this] { connect(button, &QAbstractButton::clicked, this, [button, this] {
pushUndo(d->m_undoable.set(button->isChecked())); d->m_undoable.set(undoStack(), button->isChecked());
}); });
connect(&d->m_undoable.m_signal, &UndoSignaller::changed, button, [button, this] { connect(&d->m_undoable.m_signal, &UndoSignaller::changed, button, [button, this] {
@@ -1780,7 +1767,7 @@ std::function<void (QObject *)> BoolAspect::groupChecker()
groupBox->setChecked(value()); groupBox->setChecked(value());
connect(groupBox, &QGroupBox::clicked, this, [groupBox, this] { connect(groupBox, &QGroupBox::clicked, this, [groupBox, this] {
pushUndo(d->m_undoable.set(groupBox->isChecked())); d->m_undoable.set(undoStack(), groupBox->isChecked());
}); });
connect(&d->m_undoable.m_signal, &UndoSignaller::changed, groupBox, [groupBox, this] { connect(&d->m_undoable.m_signal, &UndoSignaller::changed, groupBox, [groupBox, this] {
@@ -2475,7 +2462,7 @@ void FilePathListAspect::addToLayout(LayoutItem &parent)
PathListEditor *editor = new PathListEditor; PathListEditor *editor = new PathListEditor;
editor->setPathList(value()); editor->setPathList(value());
connect(editor, &PathListEditor::changed, this, [this, editor] { connect(editor, &PathListEditor::changed, this, [this, editor] {
pushUndo(d->undoable.set(editor->pathList())); d->undoable.set(undoStack(), editor->pathList());
}); });
connect(&d->undoable.m_signal, &UndoSignaller::changed, this, [this, editor] { connect(&d->undoable.m_signal, &UndoSignaller::changed, this, [this, editor] {
if (editor->pathList() != d->undoable.get()) if (editor->pathList() != d->undoable.get())
@@ -3101,7 +3088,7 @@ QList<std::shared_ptr<BaseAspect>> AspectList::volatileItems() const
std::shared_ptr<BaseAspect> AspectList::addItem(const std::shared_ptr<BaseAspect> &item) std::shared_ptr<BaseAspect> AspectList::addItem(const std::shared_ptr<BaseAspect> &item)
{ {
if (undoStack()) if (undoStack())
pushUndo(new AddItemCommand(this, item)); undoStack()->push(new AddItemCommand(this, item));
else else
return actualAddItem(item); return actualAddItem(item);
@@ -3121,7 +3108,7 @@ void AspectList::actualRemoveItem(const std::shared_ptr<BaseAspect> &item)
void AspectList::removeItem(const std::shared_ptr<BaseAspect> &item) void AspectList::removeItem(const std::shared_ptr<BaseAspect> &item)
{ {
if (undoStack()) if (undoStack())
pushUndo(new RemoveItemCommand(this, item)); undoStack()->push(new RemoveItemCommand(this, item));
else else
actualRemoveItem(item); actualRemoveItem(item);
} }
@@ -3343,7 +3330,7 @@ void StringSelectionAspect::addToLayout(Layouting::LayoutItem &parent)
if (newValue.isEmpty()) if (newValue.isEmpty())
return; return;
pushUndo(m_undoable.set(newValue)); m_undoable.set(undoStack(), newValue);
bufferToGui(); bufferToGui();
}); });

View File

@@ -93,7 +93,6 @@ public:
virtual void setUndoStack(QUndoStack *undoStack); virtual void setUndoStack(QUndoStack *undoStack);
QUndoStack *undoStack() const; QUndoStack *undoStack() const;
void pushUndo(QUndoCommand *cmd);
bool isEnabled() const; bool isEnabled() const;
void setEnabled(bool enabled); void setEnabled(bool enabled);
@@ -978,12 +977,15 @@ public:
T m_newValue; T m_newValue;
}; };
QUndoCommand *set(const T &value) void set(QUndoStack *stack, const T &value)
{ {
if (m_value == value) if (m_value == value)
return nullptr; return;
return new UndoCmd(this, m_value, value); if (stack)
stack->push(new UndoCmd(this, m_value, value));
else
setInternal(value);
} }
void setSilently(const T &value) { m_value = value; } void setSilently(const T &value) { m_value = value; }