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
|
|
|
|
|
|
|
DiffManager::DiffManager(DefinitionNode *tree):
|
|
|
|
tree(tree)
|
|
|
|
{
|
|
|
|
undone = 0;
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:55:30 +00:00
|
|
|
DiffManager::~DiffManager()
|
|
|
|
{
|
|
|
|
for (auto diff: diffs)
|
|
|
|
{
|
|
|
|
delete diff;
|
|
|
|
}
|
|
|
|
diffs.clear();
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:29:54 +00:00
|
|
|
void DiffManager::addWatcher(const DefinitionNode *node, DefinitionWatcher *watcher)
|
|
|
|
{
|
2015-09-21 21:10:43 +00:00
|
|
|
if (std::find(watchers[node].begin(), watchers[node].end(), watcher) == watchers[node].end())
|
|
|
|
{
|
|
|
|
watchers[node].push_back(watcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int DiffManager::getWatcherCount(const DefinitionNode *node)
|
|
|
|
{
|
|
|
|
return watchers[node].size();
|
2015-08-16 22:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff)
|
|
|
|
{
|
2015-08-17 16:18:31 +00:00
|
|
|
while (undone > 0)
|
|
|
|
{
|
|
|
|
// 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-08-17 20:55:30 +00:00
|
|
|
for (auto watcher: watchers[node])
|
2015-08-16 22:29:54 +00:00
|
|
|
{
|
2015-08-17 20:55:30 +00:00
|
|
|
watcher->nodeChanged(node, diff);
|
2015-08-16 22:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiffManager::undo()
|
|
|
|
{
|
2015-08-17 16:18:31 +00:00
|
|
|
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
|
|
|
|
|
|
|
for (auto watcher: watchers[node])
|
|
|
|
{
|
|
|
|
// FIXME Reverse diff
|
|
|
|
watcher->nodeChanged(node, diff);
|
|
|
|
}
|
2015-08-16 22:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiffManager::redo()
|
|
|
|
{
|
|
|
|
if (undone > 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
for (auto watcher: watchers[node])
|
|
|
|
{
|
|
|
|
watcher->nodeChanged(node, diff);
|
|
|
|
}
|
2015-08-16 22:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|