2015-08-16 22:29:54 +00:00
|
|
|
#include "DiffManager.h"
|
|
|
|
|
|
|
|
#include "DefinitionNode.h"
|
|
|
|
#include "DefinitionDiff.h"
|
2015-08-17 20:55:30 +00:00
|
|
|
#include "DefinitionWatcher.h"
|
2015-08-16 22:29:54 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
DiffManager::DiffManager(DefinitionNode *tree) : tree(tree) {
|
2015-08-16 22:29:54 +00:00
|
|
|
undone = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
DiffManager::~DiffManager() {
|
|
|
|
for (auto diff : diffs) {
|
2015-08-17 20:55:30 +00:00
|
|
|
delete diff;
|
|
|
|
}
|
|
|
|
diffs.clear();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void DiffManager::addWatcher(const DefinitionNode *node, DefinitionWatcher *watcher) {
|
|
|
|
if (std::find(watchers[node].begin(), watchers[node].end(), watcher) == watchers[node].end()) {
|
2015-09-21 21:10:43 +00:00
|
|
|
watchers[node].push_back(watcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
int DiffManager::getWatcherCount(const DefinitionNode *node) {
|
2015-09-21 21:10:43 +00:00
|
|
|
return watchers[node].size();
|
2015-08-16 22:29:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff) {
|
|
|
|
while (undone > 0) {
|
2015-08-17 16:18:31 +00:00
|
|
|
// truncate diffs ahead
|
2015-08-17 20:55:30 +00:00
|
|
|
delete diffs.back();
|
2015-08-17 16:18:31 +00:00
|
|
|
diffs.pop_back();
|
|
|
|
undone--;
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:29:54 +00:00
|
|
|
diffs.push_back(diff);
|
|
|
|
|
|
|
|
// TODO Delayed commit (with merge of consecutive diffs)
|
|
|
|
node->applyDiff(diff);
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
for (auto watcher : watchers[node]) {
|
2015-08-17 20:55:30 +00:00
|
|
|
watcher->nodeChanged(node, diff);
|
2015-08-16 22:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void DiffManager::undo() {
|
|
|
|
if (undone < (int)diffs.size()) {
|
2015-08-16 22:29:54 +00:00
|
|
|
undone++;
|
|
|
|
const DefinitionDiff *diff = diffs[diffs.size() - undone];
|
|
|
|
|
|
|
|
// Obtain the node by path and reverse apply diff on it
|
|
|
|
DefinitionNode *node = tree->findByPath(diff->getPath());
|
|
|
|
node->applyDiff(diff, true);
|
2015-08-18 23:17:49 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
for (auto watcher : watchers[node]) {
|
2015-08-18 23:17:49 +00:00
|
|
|
// FIXME Reverse diff
|
|
|
|
watcher->nodeChanged(node, diff);
|
|
|
|
}
|
2015-08-16 22:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void DiffManager::redo() {
|
|
|
|
if (undone > 0) {
|
2015-08-16 22:29:54 +00:00
|
|
|
const DefinitionDiff *diff = diffs[diffs.size() - undone];
|
|
|
|
undone--;
|
|
|
|
|
|
|
|
// Obtain the node by path and re-apply diff on it
|
|
|
|
DefinitionNode *node = tree->findByPath(diff->getPath());
|
|
|
|
node->applyDiff(diff);
|
2015-08-18 23:17:49 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
for (auto watcher : watchers[node]) {
|
2015-08-18 23:17:49 +00:00
|
|
|
watcher->nodeChanged(node, diff);
|
|
|
|
}
|
2015-08-16 22:29:54 +00:00
|
|
|
}
|
|
|
|
}
|