Skip to content

Commit

Permalink
KeyValue migrated to use UndoSystem signals
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 1, 2017
1 parent 9290ed4 commit 81ce76b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
25 changes: 15 additions & 10 deletions plugins/entity/KeyValue.cpp
@@ -1,8 +1,10 @@
#include "KeyValue.h"

#include <functional>
#include "iundo.h"

namespace entity {
namespace entity
{

KeyValue::KeyValue(const std::string& value, const std::string& empty) :
_value(value),
Expand Down Expand Up @@ -68,25 +70,28 @@ void KeyValue::notify()
}
}

void KeyValue::importState(const std::string& string) {
void KeyValue::importState(const std::string& string)
{
// Add ourselves to the Undo event observers, to get notified after all this has been finished
GlobalUndoSystem().addObserver(this);
_undoHandler = GlobalUndoSystem().signal_postUndo().connect(
sigc::mem_fun(this, &KeyValue::onUndoRedoOperationFinished));
_redoHandler = GlobalUndoSystem().signal_postRedo().connect(
sigc::mem_fun(this, &KeyValue::onUndoRedoOperationFinished));

_value = string;
notify();
}

void KeyValue::postUndo() {
GlobalUndoSystem().removeObserver(this);
notify();
}
void KeyValue::onUndoRedoOperationFinished()
{
_undoHandler.disconnect();
_redoHandler.disconnect();

void KeyValue::postRedo() {
GlobalUndoSystem().removeObserver(this);
notify();
}

void KeyValue::onNameChange(const std::string& oldName, const std::string& newName) {
void KeyValue::onNameChange(const std::string& oldName, const std::string& newName)
{
assert(oldName == _value); // The old name should match

// Just assign the new name to this keyvalue
Expand Down
14 changes: 10 additions & 4 deletions plugins/entity/KeyValue.h
Expand Up @@ -4,23 +4,29 @@
#include "ObservedUndoable.h"
#include "string/string.h"
#include <vector>
#include <sigc++/connection.h>
#include <sigc++/trackable.h>

namespace entity {
namespace entity
{

/// \brief A key/value pair of strings.
///
/// - Notifies observers when value changes - value changes to "" on destruction.
/// - Provides undo support through the global undo system.
class KeyValue :
public EntityKeyValue,
public UndoSystem::Observer
public sigc::trackable
{
private:
typedef std::vector<KeyObserver*> KeyObservers;
KeyObservers _observers;

std::string _value;
std::string _emptyValue;
undo::ObservedUndoable<std::string> _undo;
sigc::connection _undoHandler;
sigc::connection _redoHandler;

public:
KeyValue(const std::string& value, const std::string& empty);
Expand All @@ -45,10 +51,10 @@ class KeyValue :
// NameObserver implementation
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 postUndo();
void postRedo();
void onUndoRedoOperationFinished();
};

} // namespace entity

0 comments on commit 81ce76b

Please sign in to comment.