1 #include "UndoRedoController.h"
2 
3 #include "gui/XournalView.h"
4 
5 #include "Control.h"
6 
UndoRedoController(Control * control)7 UndoRedoController::UndoRedoController(Control* control): control(control) {}
8 
~UndoRedoController()9 UndoRedoController::~UndoRedoController() {
10     this->control = nullptr;
11     this->layer = nullptr;
12     elements.clear();
13 }
14 
before()15 void UndoRedoController::before() {
16     EditSelection* selection = control->getWindow()->getXournal()->getSelection();
17     if (selection != nullptr) {
18         layer = selection->getSourceLayer();
19         for (Element* e: *selection->getElements()) {
20             elements.push_back(e);
21         }
22     }
23 
24     control->clearSelection();
25 }
26 
after()27 void UndoRedoController::after() {
28     control->resetShapeRecognizer();
29 
30     // Restore selection, if any
31 
32     if (layer == nullptr) {
33         // No layer - no selection
34         return;
35     }
36 
37     Document* doc = control->getDocument();
38 
39     PageRef page = control->getCurrentPage();
40     size_t pageNo = doc->indexOf(page);
41     XojPageView* view = control->getWindow()->getXournal()->getViewFor(pageNo);
42 
43     if (!view || !page) {
44         return;
45     }
46 
47     vector<Element*> visibleElements;
48     for (Element* e: elements) {
49         if (layer->indexOf(e) == -1) {
50             // Element is gone - so it's not selectable
51             continue;
52         }
53 
54         visibleElements.push_back(e);
55     }
56     if (!visibleElements.empty()) {
57         auto* selection = new EditSelection(control->getUndoRedoHandler(), visibleElements, view, page);
58         control->getWindow()->getXournal()->setSelection(selection);
59     }
60 }
61 
undo(Control * control)62 void UndoRedoController::undo(Control* control) {
63     UndoRedoController handler(control);
64     handler.before();
65 
66     // Move out of text mode to allow textboxundo to work
67     control->clearSelectionEndText();
68 
69     control->getUndoRedoHandler()->undo();
70 
71     handler.after();
72 }
73 
redo(Control * control)74 void UndoRedoController::redo(Control* control) {
75     UndoRedoController handler(control);
76     handler.before();
77 
78     control->getUndoRedoHandler()->redo();
79 
80     handler.after();
81 }
82