File: undo_delete_datum.cpp

package info (click to toggle)
antimony 0.9.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,476 kB
  • sloc: cpp: 42,596; ansic: 28,661; python: 1,093; yacc: 128; lex: 114; sh: 90; makefile: 10
file content (74 lines) | stat: -rw-r--r-- 1,789 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <Python.h>

#include "undo/undo_delete_datum.h"
#include "undo/undo_delete_multi.h"
#include "undo/undo_stack.h"

#include "app/app.h"
#include "graph/serialize/serializer.h"
#include "graph/serialize/deserializer.h"
#include "graph/proxy/graph.h"

#include "graph/datum.h"
#include "graph/node.h"
#include "graph/graph_node.h"

UndoDeleteDatum::UndoDeleteDatum(Datum* d, UndoDeleteMulti* parent)
    : UndoCommand(parent), d(d), n(static_cast<GraphNode*>(d->parentNode()))
{
    Q_ASSERT(dynamic_cast<GraphNode*>(d->parentNode()));
    setText("'delete datum'");
}

UndoDeleteDatum::UndoDeleteDatum(Datum* d)
    : UndoDeleteDatum(d, NULL)
{
    // Nothing to do here
}

////////////////////////////////////////////////////////////////////////////////

void UndoDeleteDatum::swapPointer(Datum* a, Datum* b) const
{
    if (d == a)
        d = b;
}

void UndoDeleteDatum::swapPointer(Node* a, Node* b) const
{
    if (n == a)
    {
        Q_ASSERT(dynamic_cast<GraphNode*>(b));
        n = static_cast<GraphNode*>(b);
    }
}

////////////////////////////////////////////////////////////////////////////////

void UndoDeleteDatum::redo()
{
    // Serialize d into data byte array
    auto i = App::instance()->getProxy()->canvasInfo();
    data = SceneSerializer::serializeDatum(d, &i);

    // Tell the graph engine to delete the datum
    n->removeDatum(d);
}

void UndoDeleteDatum::undo()
{
    // Deserialize the target datum into its parent node
    SceneDeserializer::Info ds;
    SceneDeserializer::deserializeDatum(data, n, &ds);

    // Extract the new datum pointer
    {
        auto d_new = n->childDatums().back();
        stack->swapPointer(d, d_new);
        d = d_new;
    }

    n->triggerWatchers();
    App::instance()->getProxy()->setPositions(ds.frames);
}