definition: Fixed undo not generating reverse diffs

This commit is contained in:
Michaël Lemaire 2015-12-13 20:32:26 +01:00
parent e0ba87e4e5
commit 84ecb99b12
11 changed files with 61 additions and 4 deletions

View file

@ -5,5 +5,8 @@
DefinitionDiff::DefinitionDiff(const DefinitionNode *node) : type_name(node->getTypeName()), path(node->getPath()) {
}
DefinitionDiff::DefinitionDiff(const DefinitionDiff *other) : type_name(other->type_name), path(other->path) {
}
DefinitionDiff::~DefinitionDiff() {
}

View file

@ -14,6 +14,7 @@ namespace definition {
class DEFINITIONSHARED_EXPORT DefinitionDiff {
public:
DefinitionDiff(const DefinitionNode *node);
DefinitionDiff(const DefinitionDiff *other);
virtual ~DefinitionDiff();
inline const string &getTypeName() const {
@ -23,6 +24,13 @@ class DEFINITIONSHARED_EXPORT DefinitionDiff {
return path;
}
/**
* Abstract method to build a reversed diff (used for undoing).
*
* This diff is allocated and should be freed by the caller.
*/
virtual DefinitionDiff *newReversed() const = 0;
private:
string type_name;
string path;

View file

@ -1,6 +1,7 @@
#include "DiffManager.h"
#include <algorithm>
#include <memory>
#include "DefinitionNode.h"
#include "DefinitionDiff.h"
#include "DefinitionWatcher.h"
@ -57,8 +58,8 @@ void DiffManager::undo() {
node->applyDiff(diff, true);
for (auto watcher : watchers[node]) {
// FIXME Reverse diff
watcher->nodeChanged(node, diff);
unique_ptr<DefinitionDiff> reversed(diff->newReversed());
watcher->nodeChanged(node, reversed.get());
}
} else {
Logs::error("Definition") << "Can't find node to undo diff : " << diff->getPath() << endl;

View file

@ -3,3 +3,11 @@
FloatDiff::FloatDiff(const DefinitionNode *node, double oldvalue, double newvalue)
: DefinitionDiff(node), oldvalue(oldvalue), newvalue(newvalue) {
}
FloatDiff::FloatDiff(const FloatDiff *other, double oldvalue, double newvalue)
: DefinitionDiff(other), oldvalue(oldvalue), newvalue(newvalue) {
}
DefinitionDiff *FloatDiff::newReversed() const {
return new FloatDiff(this, newvalue, oldvalue);
}

View file

@ -14,6 +14,7 @@ namespace definition {
class DEFINITIONSHARED_EXPORT FloatDiff : public DefinitionDiff {
public:
FloatDiff(const DefinitionNode *node, double oldvalue, double newvalue);
FloatDiff(const FloatDiff *other, double oldvalue, double newvalue);
inline double getOldValue() const {
return oldvalue;
@ -22,6 +23,8 @@ class DEFINITIONSHARED_EXPORT FloatDiff : public DefinitionDiff {
return newvalue;
}
virtual DefinitionDiff *newReversed() const override;
private:
double oldvalue;
double newvalue;

View file

@ -3,3 +3,11 @@
IntDiff::IntDiff(const DefinitionNode *node, int oldvalue, int newvalue)
: DefinitionDiff(node), oldvalue(oldvalue), newvalue(newvalue) {
}
IntDiff::IntDiff(const IntDiff *other, int oldvalue, int newvalue)
: DefinitionDiff(other), oldvalue(oldvalue), newvalue(newvalue) {
}
DefinitionDiff *IntDiff::newReversed() const {
return new IntDiff(this, newvalue, oldvalue);
}

View file

@ -14,6 +14,7 @@ namespace definition {
class DEFINITIONSHARED_EXPORT IntDiff : public DefinitionDiff {
public:
IntDiff(const DefinitionNode *node, int oldvalue, int newvalue);
IntDiff(const IntDiff *other, int oldvalue, int newvalue);
inline int getOldValue() const {
return oldvalue;
@ -22,6 +23,8 @@ class DEFINITIONSHARED_EXPORT IntDiff : public DefinitionDiff {
return newvalue;
}
virtual DefinitionDiff *newReversed() const override;
private:
int oldvalue;
int newvalue;

View file

@ -8,6 +8,10 @@ LayersDiff::LayersDiff(const Layers *layers, LayersDiffOp op, int layer1)
: DefinitionDiff(layers), op(op), layer1(layer1), saved(NULL) {
}
LayersDiff::LayersDiff(const LayersDiff *other, LayersDiff::LayersDiffOp op, int layer1)
: DefinitionDiff(other), op(op), layer1(layer1), saved(NULL) {
}
LayersDiff::~LayersDiff() {
if (saved) {
delete saved;
@ -30,3 +34,17 @@ void LayersDiff::restoreSavedLayer(DefinitionNode *dest) const {
dest->load(&reader);
}
}
DefinitionDiff *LayersDiff::newReversed() const {
LayersDiff *result;
if (op == LAYER_ADDED) {
result = new LayersDiff(this, LAYER_REMOVED, layer1);
} else {
result = new LayersDiff(this, LAYER_ADDED, layer1);
}
if (saved) {
result->saved = new PackStream();
result->saved->writeFromBuffer(saved);
}
return result;
}

View file

@ -14,6 +14,7 @@ class DEFINITIONSHARED_EXPORT LayersDiff : public DefinitionDiff {
public:
LayersDiff(const Layers *layers, LayersDiffOp op, int layer1);
LayersDiff(const LayersDiff *other, LayersDiffOp op, int layer1);
virtual ~LayersDiff();
inline LayersDiffOp getOp() const {
@ -33,6 +34,8 @@ class DEFINITIONSHARED_EXPORT LayersDiff : public DefinitionDiff {
*/
void restoreSavedLayer(DefinitionNode *dest) const;
virtual DefinitionDiff *newReversed() const override;
private:
// Operation to apply
LayersDiffOp op;

View file

@ -2,6 +2,7 @@
#include "FloatNode.h"
#include "FloatDiff.h"
#include "IntDiff.h"
#include "PackStream.h"
TEST(FloatNode, toString) {
@ -64,7 +65,7 @@ TEST(FloatNode, applyDiff) {
FloatNode node(NULL, "test", 1.2);
FloatDiff diff(&node, 1.2, 2.4);
DefinitionNode onode(NULL, "test", "badtype");
DefinitionDiff odiff(&onode);
IntDiff odiff(&onode, 1, 2);
bool result;
EXPECT_DOUBLE_EQ(1.2, node.getValue());

View file

@ -2,6 +2,7 @@
#include "IntNode.h"
#include "IntDiff.h"
#include "FloatDiff.h"
#include "PackStream.h"
TEST(IntNode, toString) {
@ -64,7 +65,7 @@ TEST(IntNode, applyDiff) {
IntNode node(NULL, "test", 1);
IntDiff diff(&node, 1, 2);
DefinitionNode onode(NULL, "test", "badtype");
DefinitionDiff odiff(&onode);
FloatDiff odiff(&onode, 1.0, 2.0);
bool result;
EXPECT_DOUBLE_EQ(1, node.getValue());