/
LayerController.cpp
415 lines (332 loc) · 11.4 KB
/
LayerController.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "LayerController.h"
#include <memory> // for __shared_ptr_access, make...
#include <utility> // for move
#include <vector> // for vector
#include "control/Control.h" // for Control
#include "gui/MainWindow.h" // for MainWindow
#include "gui/XournalView.h" // for XournalView
#include "gui/dialog/RenameLayerDialog.h" // for RenameLayerDialog
#include "model/Document.h" // for Document
#include "model/XojPage.h" // for XojPage
#include "undo/InsertLayerUndoAction.h" // for InsertLayerUndoAction
#include "undo/MergeLayerDownUndoAction.h" // for MergeLayerDownUndoAction
#include "undo/MoveLayerUndoAction.h" // for MoveLayerUndoAction
#include "undo/RemoveLayerUndoAction.h" // for RemoveLayerUndoAction
#include "undo/UndoAction.h" // for UndoActionPtr, UndoAction
#include "undo/UndoRedoHandler.h" // for UndoRedoHandler
#include "util/Util.h" // for npos
#include "util/i18n.h" // for FS, _F
#include "LayerCtrlListener.h" // for LayerCtrlListener
LayerController::LayerController(Control* control): control(control), selectedPage(npos) {}
void LayerController::documentChanged(DocumentChangeType type) {
if (type == DOCUMENT_CHANGE_CLEARED || type == DOCUMENT_CHANGE_COMPLETE) {
fireRebuildLayerMenu();
}
}
void LayerController::pageSelected(size_t page) {
if (selectedPage == page) {
return;
}
selectedPage = page;
fireRebuildLayerMenu();
}
void LayerController::insertLayer(PageRef page, Layer* layer, Layer::Index layerPos) {
page->insertLayer(layer, layerPos);
fireRebuildLayerMenu();
}
void LayerController::removeLayer(PageRef page, Layer* layer) {
page->removeLayer(layer);
fireRebuildLayerMenu();
}
void LayerController::addListener(LayerCtrlListener* listener) { this->listener.push_back(listener); }
void LayerController::removeListener(LayerCtrlListener* listener) { this->listener.remove(listener); }
void LayerController::fireRebuildLayerMenu() {
for (LayerCtrlListener* l: this->listener) { l->rebuildLayerMenu(); }
}
void LayerController::fireLayerVisibilityChanged() {
for (LayerCtrlListener* l: this->listener) { l->layerVisibilityChanged(); }
}
auto LayerController::actionPerformed(ActionType type) -> bool {
switch (type) {
case ACTION_NEW_LAYER:
addNewLayer();
return true;
case ACTION_DELETE_LAYER:
deleteCurrentLayer();
return true;
case ACTION_MERGE_LAYER_DOWN:
mergeCurrentLayerDown();
return true;
case ACTION_FOOTER_LAYER:
// This event is not fired anymore
// This controller is called directly
return true;
case ACTION_GOTO_NEXT_LAYER: {
PageRef p = getCurrentPage();
auto layer = p->getSelectedLayerId();
if (layer < p->getLayerCount()) {
switchToLay(layer + 1, true);
}
}
return true;
case ACTION_GOTO_PREVIOUS_LAYER: {
PageRef p = getCurrentPage();
auto layer = p->getSelectedLayerId();
if (layer > 0) {
switchToLay(layer - 1, true);
}
}
return true;
case ACTION_GOTO_TOP_LAYER: {
PageRef p = getCurrentPage();
switchToLay(p->getLayerCount(), true);
}
return true;
case ACTION_RENAME_LAYER: {
RenameLayerDialog dialog(control->getGladeSearchPath(), control->getUndoRedoHandler(), this,
getCurrentPage()->getSelectedLayer());
dialog.show(control->getGtkWindow());
}
return true;
default:
return false;
}
}
/**
* Show all layer on the current page
*/
void LayerController::showAllLayer() { hideOrHideAllLayer(true); }
/**
* Hide all layer on the current page
*/
void LayerController::hideAllLayer() { hideOrHideAllLayer(false); }
/**
* Show / Hide all layer on the current page
*/
void LayerController::hideOrHideAllLayer(bool show) {
PageRef page = getCurrentPage();
for (Layer::Index i = 1; i <= page->getLayerCount(); i++) { page->setLayerVisible(i, show); }
fireLayerVisibilityChanged();
control->getWindow()->getXournal()->layerChanged(selectedPage);
}
void LayerController::addNewLayer() {
control->clearSelectionEndText();
PageRef p = getCurrentPage();
if (!p) {
return;
}
auto* l = new Layer();
auto layerPos = p->getSelectedLayerId();
p->insertLayer(l, layerPos);
control->getUndoRedoHandler()->addUndoAction(std::make_unique<InsertLayerUndoAction>(this, p, l, layerPos));
fireRebuildLayerMenu();
// Repaint is not needed here - the new layer is empty
}
void LayerController::deleteCurrentLayer() {
control->clearSelectionEndText();
PageRef p = getCurrentPage();
auto pId = selectedPage;
if (!p) {
return;
}
auto lId = p->getSelectedLayerId();
if (lId == 0) {
return;
}
Layer* l = p->getSelectedLayer();
p->removeLayer(l);
MainWindow* win = control->getWindow();
if (win) {
win->getXournal()->layerChanged(pId);
}
control->getUndoRedoHandler()->addUndoAction(std::make_unique<RemoveLayerUndoAction>(this, p, l, lId - 1));
fireRebuildLayerMenu();
}
void LayerController::moveCurrentLayer(bool up) {
control->clearSelectionEndText();
PageRef p = getCurrentPage();
auto pId = selectedPage;
if (!p) {
return;
}
auto lId = p->getSelectedLayerId();
Layer* currentLayer = p->getSelectedLayer();
if (lId == 0) {
// Background cannot be moved
return;
}
if (lId == 1 && !up) {
// bottom layer cannot be moved down
return;
}
if (lId == p->getLayerCount() && up) {
// top layer cannot be moved up
return;
}
p->removeLayer(currentLayer);
// Layer IDs are a bit strange, because background is 0
// so the first layer is 1, technical the first layer is still
// index 0 in the vector... confusing...
auto newIndex = up ? lId : lId - 2;
p->insertLayer(currentLayer, newIndex);
MainWindow* win = control->getWindow();
if (win) {
win->getXournal()->layerChanged(pId);
}
control->getUndoRedoHandler()->addUndoAction(
std::make_unique<MoveLayerUndoAction>(this, p, currentLayer, lId - 1, newIndex));
fireRebuildLayerMenu();
}
void LayerController::mergeCurrentLayerDown() {
control->clearSelectionEndText();
PageRef page = getCurrentPage();
auto pageID = selectedPage;
if (page == nullptr) {
return;
}
/*
* layerID value:
* ...
* 2: layer 2
* 1: layer 1
* 0: background
*/
const auto layerID = page->getSelectedLayerId();
Layer* currentLayer = page->getSelectedLayer();
if (layerID < 2) {
/*
* lowest (non-background) layer cannot be merged into background
* and the background itself obviously also cannot be merged down
*/
return;
}
/*
* We know this cannot be the background (or even an underflow) because
* we checked for !(layerID < 2) before.
*/
const auto layerBelowID = layerID - 1;
/*
* Layer indices in the vector are off by one from the layer IDs because
* the background is not in the vector, so layer 1 has index 0 and so on.
*/
const Layer::Index layerBelowIndex = layerBelowID - 1;
Layer* layerBelow = (*page->getLayers())[layerBelowIndex];
UndoActionPtr undo_redo_action =
std::make_unique<MergeLayerDownUndoAction>(this, page, currentLayer, layerBelow, layerID - 1, pageID);
undo_redo_action->redo(this->control);
control->getUndoRedoHandler()->addUndoAction(std::move(undo_redo_action));
}
void LayerController::copyCurrentLayer() {
control->clearSelectionEndText();
PageRef p = getCurrentPage();
auto pId = selectedPage;
if (!p) {
return;
}
auto lId = p->getSelectedLayerId();
if (lId == 0) {
return;
}
Layer* l = p->getSelectedLayer();
Layer* cloned = l->clone();
p->insertLayer(cloned, lId);
MainWindow* win = control->getWindow();
if (win) {
win->getXournal()->layerChanged(pId);
}
control->getUndoRedoHandler()->addUndoAction(std::make_unique<InsertLayerUndoAction>(this, p, cloned, lId));
fireRebuildLayerMenu();
}
auto LayerController::getCurrentPage() const -> PageRef { return control->getDocument()->getPage(selectedPage); }
auto LayerController::getCurrentPageId() const -> size_t { return selectedPage; }
void LayerController::setLayerVisible(Layer::Index layerId, bool visible) {
getCurrentPage()->setLayerVisible(layerId, visible);
fireLayerVisibilityChanged();
control->getWindow()->getXournal()->layerChanged(selectedPage);
}
/**
* Switch to a layer
*
* @param hideShow Auto hide / show other layers,
* as it was before the advance layer menu
* @param clearSelection Clear / keep current selection before switching layers.
*/
void LayerController::switchToLay(Layer::Index layerId, bool hideShow, bool clearSelection) {
if (clearSelection) {
control->clearSelectionEndText();
}
PageRef p = getCurrentPage();
if (!p) {
return;
}
p->setSelectedLayerId(layerId);
if (hideShow) {
for (Layer::Index i = 1; i <= p->getLayerCount(); i++) { p->setLayerVisible(i, i <= layerId); }
}
// Repaint page
control->getWindow()->getXournal()->layerChanged(selectedPage);
fireLayerVisibilityChanged();
}
/**
* @return Layer count of the current page
*/
auto LayerController::getLayerCount() const -> Layer::Index {
PageRef page = getCurrentPage();
if (!page) {
return 0;
}
return page->getLayerCount();
}
/**
* @return Current layer ID
*/
auto LayerController::getCurrentLayerId() const -> Layer::Index {
PageRef page = getCurrentPage();
if (!page) {
return 0;
}
return page->getSelectedLayerId();
}
auto LayerController::getCurrentLayerName() const -> std::string {
PageRef page = getCurrentPage();
if (page == nullptr) {
return "Unknown layer name";
}
auto currentID = getCurrentLayerId();
if (currentID == 0) { // If is background
return page->getBackgroundName();
} else if (auto layer = page->getSelectedLayer(); layer->hasName()) {
return layer->getName();
} else {
return FS(_F("Layer {1}") % static_cast<long>(currentID));
}
}
void LayerController::setCurrentLayerName(const std::string& newName) {
PageRef page = getCurrentPage();
if (page == nullptr) {
return;
}
if (getCurrentLayerId() == 0) { // Background
page->setBackgroundName(newName);
} else { // Any other layer
page->getSelectedLayer()->setName(newName);
}
fireRebuildLayerMenu();
}
std::string LayerController::getLayerNameById(Layer::Index id) const {
PageRef page = getCurrentPage();
if (page == nullptr) {
return "Unknown layer name";
}
if (id == 0) {
return page->getBackgroundName();
}
auto previousId = page->getSelectedLayerId();
if (previousId == id) {
return getCurrentLayerName();
}
page->setSelectedLayerId(id);
std::string name = getCurrentLayerName();
page->setSelectedLayerId(previousId);
return name;
}