From cbfae9f636549abe61ae719266f069dd2d4ced95 Mon Sep 17 00:00:00 2001 From: codereader Date: Tue, 26 Oct 2021 12:05:26 +0200 Subject: [PATCH] #5408: Replace the second occurrence of GlobalUndoSystem() event subscriptions in the entity KeyValue class. --- libs/ObservedUndoable.h | 24 +++++++++++++++++------- radiantcore/entity/KeyValue.cpp | 11 ++--------- radiantcore/entity/KeyValue.h | 18 +++++++----------- radiantcore/entity/SpawnArgs.cpp | 6 ++++-- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/libs/ObservedUndoable.h b/libs/ObservedUndoable.h index d4e40bf0e5..cade885f47 100644 --- a/libs/ObservedUndoable.h +++ b/libs/ObservedUndoable.h @@ -12,23 +12,25 @@ class ObservedUndoable : public IUndoable { typedef std::function ImportCallback; + typedef std::function RestoreFinishedCallback; Copyable& _object; ImportCallback _importCallback; + RestoreFinishedCallback _finishedCallback; IUndoStateSaver* _undoStateSaver; std::string _debugName; public: - ObservedUndoable(Copyable& object, const ImportCallback& importCallback) : - _object(object), - _importCallback(importCallback), - _undoStateSaver(nullptr) + ObservedUndoable(Copyable& object, const ImportCallback& importCallback) : + ObservedUndoable(object, importCallback, RestoreFinishedCallback(), "") {} - ObservedUndoable(Copyable& object, const ImportCallback& importCallback, const std::string& debugName) : + ObservedUndoable(Copyable& object, const ImportCallback& importCallback, + const RestoreFinishedCallback& finishedCallback, const std::string& debugName) : _object(object), _importCallback(importCallback), + _finishedCallback(finishedCallback), _undoStateSaver(nullptr), _debugName(debugName) {} @@ -65,17 +67,25 @@ class ObservedUndoable : } } - IUndoMementoPtr exportState() const + IUndoMementoPtr exportState() const override { return IUndoMementoPtr(new BasicUndoMemento(_object)); } - void importState(const IUndoMementoPtr& state) + void importState(const IUndoMementoPtr& state) override { save(); _importCallback(std::static_pointer_cast >(state)->data()); } + + void onOperationRestored() override + { + if (_finishedCallback) + { + _finishedCallback(); + } + } }; } // namespace diff --git a/radiantcore/entity/KeyValue.cpp b/radiantcore/entity/KeyValue.cpp index 698d35712e..7369029c99 100644 --- a/radiantcore/entity/KeyValue.cpp +++ b/radiantcore/entity/KeyValue.cpp @@ -10,7 +10,8 @@ KeyValue::KeyValue(const std::string& value, const std::string& empty, const std::function& valueChanged) : _value(value), _emptyValue(empty), - _undo(_value, std::bind(&KeyValue::importState, this, std::placeholders::_1), "KeyValue"), + _undo(_value, std::bind(&KeyValue::importState, this, std::placeholders::_1), + std::bind(&KeyValue::onUndoRedoOperationFinished, this), "KeyValue"), _valueChanged(valueChanged) {} @@ -83,19 +84,11 @@ void KeyValue::notify() void KeyValue::importState(const std::string& string) { // We notify our observers after the entire undo rollback is done - _undoHandler = GlobalUndoSystem().signal_postUndo().connect( - sigc::mem_fun(this, &KeyValue::onUndoRedoOperationFinished)); - _redoHandler = GlobalUndoSystem().signal_postRedo().connect( - sigc::mem_fun(this, &KeyValue::onUndoRedoOperationFinished)); - _value = string; } void KeyValue::onUndoRedoOperationFinished() { - _undoHandler.disconnect(); - _redoHandler.disconnect(); - notify(); } diff --git a/radiantcore/entity/KeyValue.h b/radiantcore/entity/KeyValue.h index 642307e1c0..1fa1f076c1 100644 --- a/radiantcore/entity/KeyValue.h +++ b/radiantcore/entity/KeyValue.h @@ -4,21 +4,19 @@ #include "ObservedUndoable.h" #include "string/string.h" #include -#include -#include namespace entity { class SpawnArgs; -/// \brief A key/value pair of strings. +/// \brief +/// Represents the string value of an entity key. /// /// - Notifies observers when value changes - value changes to "" on destruction. -/// - Provides undo support through the global undo system. +/// - Provides undo support through the map's undo system. class KeyValue final : - public EntityKeyValue, - public sigc::trackable + public EntityKeyValue { private: typedef std::vector KeyObservers; @@ -27,8 +25,6 @@ class KeyValue final : std::string _value; std::string _emptyValue; undo::ObservedUndoable _undo; - sigc::connection _undoHandler; - sigc::connection _redoHandler; // This is a specialised callback pointing to the owning SpawnArgs std::function _valueChanged; @@ -60,9 +56,9 @@ class KeyValue final : void onNameChange(const std::string& oldName, const std::string& newName); private: - // Gets called after a undo/redo operation is fully completed. - // This triggers a keyobserver refresh, to allow for reconnection to Namespaces and such. - void onUndoRedoOperationFinished(); + // Gets called after a undo/redo operation is fully completed. + // This triggers a keyobserver refresh, to allow for reconnection to Namespaces and such. + void onUndoRedoOperationFinished(); }; } // namespace entity diff --git a/radiantcore/entity/SpawnArgs.cpp b/radiantcore/entity/SpawnArgs.cpp index 4903889b6e..da3ad1ef8f 100644 --- a/radiantcore/entity/SpawnArgs.cpp +++ b/radiantcore/entity/SpawnArgs.cpp @@ -10,7 +10,8 @@ namespace entity SpawnArgs::SpawnArgs(const IEntityClassPtr& eclass) : _eclass(eclass), - _undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1), "EntityKeyValues"), + _undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1), + std::function(), "EntityKeyValues"), _observerMutex(false), _isContainer(!eclass->isFixedSize()), _attachments(eclass->getName()) @@ -22,7 +23,8 @@ SpawnArgs::SpawnArgs(const IEntityClassPtr& eclass) : SpawnArgs::SpawnArgs(const SpawnArgs& other) : Entity(other), _eclass(other.getEntityClass()), - _undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1), "EntityKeyValues"), + _undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1), + std::function(), "EntityKeyValues"), _observerMutex(false), _isContainer(other._isContainer), _attachments(other._attachments)