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 DefinitionNode *node) : type_name(node->getTypeName()), path(node->getPath()) {
} }
DefinitionDiff::DefinitionDiff(const DefinitionDiff *other) : type_name(other->type_name), path(other->path) {
}
DefinitionDiff::~DefinitionDiff() { DefinitionDiff::~DefinitionDiff() {
} }

View file

@ -14,6 +14,7 @@ namespace definition {
class DEFINITIONSHARED_EXPORT DefinitionDiff { class DEFINITIONSHARED_EXPORT DefinitionDiff {
public: public:
DefinitionDiff(const DefinitionNode *node); DefinitionDiff(const DefinitionNode *node);
DefinitionDiff(const DefinitionDiff *other);
virtual ~DefinitionDiff(); virtual ~DefinitionDiff();
inline const string &getTypeName() const { inline const string &getTypeName() const {
@ -23,6 +24,13 @@ class DEFINITIONSHARED_EXPORT DefinitionDiff {
return path; 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: private:
string type_name; string type_name;
string path; string path;

View file

@ -1,6 +1,7 @@
#include "DiffManager.h" #include "DiffManager.h"
#include <algorithm> #include <algorithm>
#include <memory>
#include "DefinitionNode.h" #include "DefinitionNode.h"
#include "DefinitionDiff.h" #include "DefinitionDiff.h"
#include "DefinitionWatcher.h" #include "DefinitionWatcher.h"
@ -57,8 +58,8 @@ void DiffManager::undo() {
node->applyDiff(diff, true); node->applyDiff(diff, true);
for (auto watcher : watchers[node]) { for (auto watcher : watchers[node]) {
// FIXME Reverse diff unique_ptr<DefinitionDiff> reversed(diff->newReversed());
watcher->nodeChanged(node, diff); watcher->nodeChanged(node, reversed.get());
} }
} else { } else {
Logs::error("Definition") << "Can't find node to undo diff : " << diff->getPath() << endl; 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) FloatDiff::FloatDiff(const DefinitionNode *node, double oldvalue, double newvalue)
: DefinitionDiff(node), oldvalue(oldvalue), newvalue(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 { class DEFINITIONSHARED_EXPORT FloatDiff : public DefinitionDiff {
public: public:
FloatDiff(const DefinitionNode *node, double oldvalue, double newvalue); FloatDiff(const DefinitionNode *node, double oldvalue, double newvalue);
FloatDiff(const FloatDiff *other, double oldvalue, double newvalue);
inline double getOldValue() const { inline double getOldValue() const {
return oldvalue; return oldvalue;
@ -22,6 +23,8 @@ class DEFINITIONSHARED_EXPORT FloatDiff : public DefinitionDiff {
return newvalue; return newvalue;
} }
virtual DefinitionDiff *newReversed() const override;
private: private:
double oldvalue; double oldvalue;
double newvalue; double newvalue;

View file

@ -3,3 +3,11 @@
IntDiff::IntDiff(const DefinitionNode *node, int oldvalue, int newvalue) IntDiff::IntDiff(const DefinitionNode *node, int oldvalue, int newvalue)
: DefinitionDiff(node), oldvalue(oldvalue), newvalue(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 { class DEFINITIONSHARED_EXPORT IntDiff : public DefinitionDiff {
public: public:
IntDiff(const DefinitionNode *node, int oldvalue, int newvalue); IntDiff(const DefinitionNode *node, int oldvalue, int newvalue);
IntDiff(const IntDiff *other, int oldvalue, int newvalue);
inline int getOldValue() const { inline int getOldValue() const {
return oldvalue; return oldvalue;
@ -22,6 +23,8 @@ class DEFINITIONSHARED_EXPORT IntDiff : public DefinitionDiff {
return newvalue; return newvalue;
} }
virtual DefinitionDiff *newReversed() const override;
private: private:
int oldvalue; int oldvalue;
int newvalue; 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) { : 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() { LayersDiff::~LayersDiff() {
if (saved) { if (saved) {
delete saved; delete saved;
@ -30,3 +34,17 @@ void LayersDiff::restoreSavedLayer(DefinitionNode *dest) const {
dest->load(&reader); 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: public:
LayersDiff(const Layers *layers, LayersDiffOp op, int layer1); LayersDiff(const Layers *layers, LayersDiffOp op, int layer1);
LayersDiff(const LayersDiff *other, LayersDiffOp op, int layer1);
virtual ~LayersDiff(); virtual ~LayersDiff();
inline LayersDiffOp getOp() const { inline LayersDiffOp getOp() const {
@ -33,6 +34,8 @@ class DEFINITIONSHARED_EXPORT LayersDiff : public DefinitionDiff {
*/ */
void restoreSavedLayer(DefinitionNode *dest) const; void restoreSavedLayer(DefinitionNode *dest) const;
virtual DefinitionDiff *newReversed() const override;
private: private:
// Operation to apply // Operation to apply
LayersDiffOp op; LayersDiffOp op;

View file

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

View file

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