File: undo_change_script.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 (55 lines) | stat: -rw-r--r-- 1,350 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
#include <Python.h>

#include <QTextCursor>
#include <QPlainTextEdit>
#include <QGraphicsTextItem>

#include "undo/undo_change_script.h"

#include "graph/script_node.h"

UndoChangeScript::UndoChangeScript(ScriptNode* n, QString before,
                                   QString after, QPlainTextEdit* editor,
                                   int cursor_before, int cursor_after)
    : node(n), before(before), after(after), editor(editor),
      cursor_before(cursor_before), cursor_after(cursor_after)
{
    setText("'change script'");
}

void UndoChangeScript::setCursor(int pos)
{
    if (editor)
    {
        QTextCursor c = editor->textCursor();
        c.setPosition(pos);
        editor->setTextCursor(c);
    }
}

void UndoChangeScript::redo()
{
    node->setScript(after.toStdString());
    setCursor(cursor_after);
}

void UndoChangeScript::undo()
{
    // Save text value and cursor position
    after = QString::fromStdString(node->getScript());
    if (editor)
        cursor_after = editor->textCursor().position();

    // Update the script's text and restore the cursor
    node->setScript(before.toStdString());
    setCursor(cursor_before);
}

void UndoChangeScript::swapPointer(Node* a, Node* b) const
{
    if (node == a)
    {
        assert(dynamic_cast<ScriptNode*>(b));
        node = static_cast<ScriptNode*>(b);
    }
}