-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathCommands.cpp
More file actions
459 lines (375 loc) · 12.8 KB
/
Copy pathCommands.cpp
File metadata and controls
459 lines (375 loc) · 12.8 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Commands.cpp
*
* (c) 2014 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2014 Alexandre Quessy -- alexandre(@)quessy(.)net
* (c) 2014 Dame Diongue -- baydamd(@)gmail(.)com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MainWindow.h"
#include "MapperGLCanvas.h"
#include "Commands.h"
namespace mmp {
AddSourceCommand::AddSourceCommand(MainWindow *mainWindow, uid sourceId, const QIcon &icon, const QString &name, QUndoCommand *parent) :
QUndoCommand(parent),
_mainWindow(mainWindow),
_sourceId(sourceId),
_icon(icon),
_name(name)
{
setText(QObject::tr("Add source"));
}
void AddSourceCommand::undo()
{
_source = _mainWindow->getMappingManager().getSourceById(_sourceId);
_mainWindow->removeSourceItem(_sourceId);
}
void AddSourceCommand::redo()
{
if (!_source.isNull())
{
uid lastId = _mainWindow->getMappingManager().addSource(_source);
_mainWindow->addSourceItem(lastId, _icon, _name);
}
else {
_mainWindow->addSourceItem(_sourceId, _icon, _name);
}
}
AddLayerCommand::AddLayerCommand(MainWindow *mainWindow, uid layerId, QUndoCommand *parent):
QUndoCommand(parent),
_mainWindow(mainWindow),
_layerId(layerId)
{
setText(QObject::tr("Add layer"));
}
void AddLayerCommand::undo()
{
_layer = _mainWindow->getMappingManager().getLayerById(_layerId);
_mainWindow->deleteLayer(_layerId);
}
void AddLayerCommand::redo()
{
if (!_layer.isNull())
{
uid storedId = _mainWindow->getMappingManager().addLayer(_layer);
_mainWindow->addLayerItem(storedId);
}
else
{
_mainWindow->addLayerItem(_layerId);
}
}
DuplicateLayerCommand::DuplicateLayerCommand(MainWindow *mainWindow, uid cloneId, QUndoCommand *parent):
AddLayerCommand(mainWindow, cloneId, parent)
{
setText(QObject::tr("Duplicate layer"));
}
TransformShapeCommand::TransformShapeCommand(MapperGLCanvas* canvas, TransformShapeOption option, QUndoCommand* parent)
: QUndoCommand(parent),
_canvas(canvas), _option(option) {
// Copy shape.
_shape = canvas->getCurrentShape();
_option = option;
// Clone shape before applying transform.
auto strong = _shape.toStrongRef();
if (strong)
_originalShape.reset(strong->clone());
}
void TransformShapeCommand::undo() {
// Copy back shape.
auto strong = _shape.toStrongRef();
if (!strong) return;
strong->copyFrom(*_originalShape);
// Update everything.
_canvas->currentShapeWasChanged();
_canvas->update();
}
void TransformShapeCommand::redo() {
// Call transformation.
_doTransform(_shape);
// Update everything.
_canvas->currentShapeWasChanged();
_canvas->update();
}
bool TransformShapeCommand::mergeWith(const QUndoCommand* other) {
// Make sure other is of the same type (id).
if (other->id() != id())
return false;
const TransformShapeCommand* cmd = static_cast<const TransformShapeCommand*>(other);
// Don't merge a new transform with a dropped tranform move (ie. each drag'n'drop is considered
// as a single separate command).
if (_option == RELEASE && cmd->_option == FREE)
return false;
// Don't merge transforms
if (cmd->_canvas != _canvas ||
cmd->_shape != _shape)
return false;
return true;
}
MoveVertexCommand::MoveVertexCommand(MapperGLCanvas* canvas, TransformShapeOption option, int activeVertex, const QPointF &point, QUndoCommand *parent)
: TransformShapeCommand(canvas, option, parent),
_movedVertex(activeVertex),
_vertexPosition(point)
{
setText(QObject::tr("Move vertex"));
}
int MoveVertexCommand::id() const { return (_option == STEP ? CMD_KEY_MOVE_VERTEX : CMD_MOUSE_MOVE_VERTEX); }
void MoveVertexCommand::_doTransform(MShape::ptr shape)
{
shape->setVertex(_movedVertex, _vertexPosition);
}
bool MoveVertexCommand::mergeWith(const QUndoCommand* other)
{
if (!TransformShapeCommand::mergeWith(other))
return false;
const MoveVertexCommand* cmd = static_cast<const MoveVertexCommand*>(other);
// Needs to be the same vertex.
if (cmd->_movedVertex != _movedVertex)
return false;
_vertexPosition = cmd->_vertexPosition;
_option = cmd->_option;
return true;
}
ScaleRotateShapeCommand::ScaleRotateShapeCommand(MapperGLCanvas* canvas, TransformShapeOption option, int activeVertex, const QPointF &point, const QPointF& initialPositionPoint, const MShape::ptr& initialShape, MShape::ShapeMode mode, QUndoCommand *parent)
: TransformShapeCommand(canvas, option, parent),
_movedVertex(activeVertex),
_initialShape(initialShape)
{
// Initial vector from center.
QPointF center = initialShape->getCenter();
QLineF initialVector(center, initialPositionPoint);
QLineF currentVector(center, point);
// Compute scale.
qreal scale = currentVector.length() / initialVector.length();
// Compute rotation.
qreal rotation = currentVector.angleTo(initialVector);
// Create transform object.
_transform.translate(+center.x(), +center.y());
if (mode == MShape::RotateMode) {
setText(QObject::tr("Rotate shape"));
_transform.rotate(rotation);
}
if (mode == MShape::ScaleMode) {
setText(QObject::tr("Scale shape"));
_transform.scale(scale, scale);
}
_transform.translate(-center.x(), -center.y());
}
int ScaleRotateShapeCommand::id() const { return (_option == STEP ? CMD_KEY_SCALE_ROTATE_SHAPE : CMD_MOUSE_SCALE_ROTATE_SHAPE); }
void ScaleRotateShapeCommand::_doTransform(MShape::ptr shape)
{
// Apply to shape.
shape->copyFrom(*_initialShape);
shape->applyTransform(_transform);
}
bool ScaleRotateShapeCommand::mergeWith(const QUndoCommand* other)
{
if (!TransformShapeCommand::mergeWith(other))
return false;
const ScaleRotateShapeCommand* cmd = static_cast<const ScaleRotateShapeCommand*>(other);
// Needs to be the same vertex.
if (cmd->_movedVertex != _movedVertex)
return false;
_option = cmd->_option;
_transform = cmd->_transform;
return true;
}
TranslateShapeCommand::TranslateShapeCommand(MapperGLCanvas *canvas, TransformShapeOption option, const QPointF &translation, QUndoCommand *parent)
: TransformShapeCommand(canvas, option, parent),
_translation(translation)
{
setText(QObject::tr("Move shape"));
}
int TranslateShapeCommand::id() const { return (_option == STEP ? CMD_KEY_TRANSLATE_SHAPE : CMD_MOUSE_TRANSLATE_SHAPE); }
bool TranslateShapeCommand::mergeWith(const QUndoCommand* other)
{
if (!TransformShapeCommand::mergeWith(other))
return false;
const TranslateShapeCommand* cmd = static_cast<const TranslateShapeCommand*>(other);
// Update translation.
_translation += cmd->_translation;
_option = cmd->_option;
return true;
}
void TranslateShapeCommand::_doTransform(MShape::ptr shape)
{
// Apply translation.
shape->translate(_translation);
}
RemoveSourceCommand::RemoveSourceCommand(MainWindow *mainWindow, uid sourceId, QUndoCommand *parent):
QUndoCommand(parent),
_mainWindow(mainWindow),
_sourceId(sourceId),
_sourceLayers()
{
setText(QObject::tr("Remove media"));
}
void RemoveSourceCommand::undo()
{
if (!_source.isNull())
{
MappingManager& manager = _mainWindow->getMappingManager();
uid lastId = manager.addSource(_source);
_mainWindow->addSourceItem(lastId, _source->getIcon(), _source->getName());
// Add all mappings associated with source.
QMap<uid, Layer::ptr> sourceLayers = manager.getSourceLayers(_source);
for (QMap<uid, Layer::ptr>::const_iterator it = _sourceLayers.constBegin();
it != _sourceLayers.constEnd(); ++it) {
uid mid = manager.addLayer( it.value() );
Q_ASSERT( mid == it.key() );
_mainWindow->addLayerItem(mid);
}
}
}
void RemoveSourceCommand::redo()
{
MappingManager& manager = _mainWindow->getMappingManager();
bool deleteWithoutPrompt = (!_source.isNull()); // to avoid the pop-up window when redoing after undoing
_source = manager.getSourceById(_sourceId);
_sourceLayers = manager.getSourceLayers(_source);
_mainWindow->deleteSource(_sourceId, deleteWithoutPrompt);
}
DeleteLayerCommand::DeleteLayerCommand(MainWindow *mainWindow, uid layerId, QUndoCommand *parent) :
QUndoCommand(parent),
_mainWindow(mainWindow),
_layerId(layerId)
{
setText(QObject::tr("Delete layer"));
}
void DeleteLayerCommand::undo()
{
if (!_layer.isNull())
{
uid storedId = _mainWindow->getMappingManager().addLayer(_layer);
_mainWindow->addLayerItem(storedId);
}
}
void DeleteLayerCommand::redo()
{
// Store mapping pointer before delete it
_layer = _mainWindow->getMappingManager().getLayerById(_layerId);
_mainWindow->deleteLayer(_layerId);
}
MoveLayerCommand::MoveLayerCommand(MainWindow *mainWindow, uid layerId, MM::MoveElement moveType, QUndoCommand *parent) :
QUndoCommand(parent),
_mainWindow(mainWindow),
_layerId(layerId),
_moveType(moveType)
{
switch (moveType) {
case MM::Raise: setText(QObject::tr("Raise layer")); break;
case MM::Lower: setText(QObject::tr("Lower layer")); break;
case MM::Top: setText(QObject::tr("Raise layer to top")); break;
case MM::Bottom: setText(QObject::tr("Lower layer to bottom")); break;
default:; // should not happen
}
}
void MoveLayerCommand::undo()
{
if (!_layer.isNull())
{
// Do the inverse move.
_mainWindow->moveLayer(_layerId, _fromIdx);
}
}
void MoveLayerCommand::redo()
{
// Store mapping pointer before delete it
_layer = _mainWindow->getMappingManager().getLayerById(_layerId);
_fromIdx = _mainWindow->getMappingManager().getLayerIndex(_layer);
int maxLayerIdx = _mainWindow->getMappingManager().nLayers()-1;
switch (_moveType) {
case MM::Raise: _toIdx = qMax(_fromIdx - 1, 0); break;
case MM::Lower: _toIdx = qMin(_fromIdx + 1, maxLayerIdx); break;
case MM::Top: _toIdx = 0; break;
case MM::Bottom: _toIdx = maxLayerIdx; break;
default:; // should not happen
}
// Do the move.
_mainWindow->moveLayer(_layerId, _toIdx);
}
FlipShapeCommand::FlipShapeCommand(MapperGLCanvas *canvas, TransformShapeCommand::TransformShapeOption option, const MShape::ptr &initialShape, MShape::FlipDirection direction, QUndoCommand *parent)
: TransformShapeCommand (canvas, option, parent),
_initialShape(initialShape)
{
// Initial vector from center.
QPointF center = initialShape->getCenter();
// Create transform object.
_transform.translate(+center.x(), +center.y());
if (direction == MShape::Horizontal) {
setText(QObject::tr("Flip Horizontally"));
_transform.scale(-1, 1);
}
else if (direction == MShape::Vertical) {
setText(QObject::tr("Flip Vertically"));
_transform.scale(1, -1);
}
_transform.translate(-center.x(), -center.y());
}
int FlipShapeCommand::id() const { return (_option == STEP ? CMD_KEY_FLIP_SHAPE : CMD_MOUSE_FLIP_SHAPE); }
void FlipShapeCommand::_doTransform(MShape::ptr shape)
{
// Apply to shape.
shape->copyFrom(*_initialShape);
shape->applyTransform(_transform);
}
bool FlipShapeCommand::mergeWith(const QUndoCommand* other)
{
if (!TransformShapeCommand::mergeWith(other))
return false;
const FlipShapeCommand* cmd = static_cast<const FlipShapeCommand*>(other);
_option = cmd->_option;
_transform = cmd->_transform;
return true;
}
RotateShapeCommand::RotateShapeCommand(MapperGLCanvas *canvas, TransformShapeCommand::TransformShapeOption option, const MShape::ptr &initialShape, MShape::Rotation rotation, QUndoCommand *parent)
: TransformShapeCommand (canvas, option, parent),
_initialShape(initialShape)
{
// Initial vector from center.
QPointF center = initialShape->getCenter();
// Create transform object.
_transform.translate(+center.x(), +center.y());
if (rotation == MShape::Rotate90CW) {
setText(QObject::tr("Rotate 90° CW"));
_transform.rotate(90);
}
else if (rotation == MShape::Rotate90CCW) {
setText(QObject::tr("Rotate 90° CCW"));
_transform.rotate(-90);
}
else if (rotation == MShape::Rotate180) {
setText(QObject::tr("Rotate 180°"));
_transform.rotate(180);
}
_transform.translate(-center.x(), -center.y());
}
int RotateShapeCommand::id() const { return (_option == STEP ? CMD_KEY_ROTATE_SHAPE : CMD_MOUSE_ROTATE_SHAPE); }
void RotateShapeCommand::_doTransform(MShape::ptr shape)
{
// Apply to shape.
shape->copyFrom(*_initialShape);
shape->applyTransform(_transform);
}
bool RotateShapeCommand::mergeWith(const QUndoCommand* other)
{
if (!TransformShapeCommand::mergeWith(other))
return false;
const RotateShapeCommand* cmd = static_cast<const RotateShapeCommand*>(other);
_option = cmd->_option;
_transform = cmd->_transform;
return true;
}
}