1 #include "MoveUndoAction.h"
2
3 #include "control/tools/EditSelection.h"
4 #include "gui/Redrawable.h"
5 #include "model/Element.h"
6 #include "model/Layer.h"
7 #include "model/PageRef.h"
8
9 #include "i18n.h"
10
MoveUndoAction(Layer * sourceLayer,const PageRef & sourcePage,vector<Element * > * selected,double mx,double my,Layer * targetLayer,PageRef targetPage)11 MoveUndoAction::MoveUndoAction(Layer* sourceLayer, const PageRef& sourcePage, vector<Element*>* selected, double mx,
12 double my, Layer* targetLayer, PageRef targetPage):
13 UndoAction("MoveUndoAction") {
14 this->page = sourcePage;
15 this->sourceLayer = sourceLayer;
16 this->text = _("Move");
17
18 this->dx = mx;
19 this->dy = my;
20
21 this->elements = *selected;
22
23 if (this->page != targetPage) {
24 this->targetPage = targetPage;
25 this->targetLayer = targetLayer;
26 }
27 }
28
29 MoveUndoAction::~MoveUndoAction() = default;
30
move()31 void MoveUndoAction::move() {
32 if (this->undone) {
33 for (Element* e: this->elements) {
34 e->move(dx, dy);
35 }
36 } else {
37 for (Element* e: this->elements) {
38 e->move(-dx, -dy);
39 }
40 }
41 }
42
undo(Control * control)43 auto MoveUndoAction::undo(Control* control) -> bool {
44 if (this->sourceLayer != this->targetLayer && this->targetLayer != nullptr) {
45 switchLayer(&this->elements, this->targetLayer, this->sourceLayer);
46 }
47
48 move();
49 repaint();
50 this->undone = true;
51
52 return true;
53 }
54
redo(Control * control)55 auto MoveUndoAction::redo(Control* control) -> bool {
56 if (this->sourceLayer != this->targetLayer && this->targetLayer != nullptr) {
57 switchLayer(&this->elements, this->sourceLayer, this->targetLayer);
58 }
59
60 move();
61 repaint();
62 this->undone = false;
63
64 return true;
65 }
66
switchLayer(vector<Element * > * entries,Layer * oldLayer,Layer * newLayer)67 void MoveUndoAction::switchLayer(vector<Element*>* entries, Layer* oldLayer, Layer* newLayer) {
68 for (Element* e: this->elements) {
69 oldLayer->removeElement(e, false);
70 newLayer->addElement(e);
71 }
72 }
73
repaint()74 void MoveUndoAction::repaint() {
75 if (this->elements.empty()) {
76 return;
77 }
78
79 this->page->firePageChanged();
80
81 if (this->targetPage) {
82 this->targetPage->firePageChanged();
83 }
84 }
85
getPages()86 auto MoveUndoAction::getPages() -> vector<PageRef> {
87 vector<PageRef> pages;
88 pages.push_back(this->page);
89 pages.push_back(this->targetPage);
90 return pages;
91 }
92
getText()93 auto MoveUndoAction::getText() -> string { return text; }
94