-
Notifications
You must be signed in to change notification settings - Fork 15
/
desktopundo.cpp
518 lines (393 loc) · 13.5 KB
/
desktopundo.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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
License: GPL-2
An electronic filing cabinet: scan, print, stack, arrange
Copyright (C) 2009 Simon Glass, [email protected]
.
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
X-Comment: On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in the /usr/share/common-licenses/GPL file.
*/
#include <QApplication>
#include <QDebug>
#include <QUndoCommand>
#include "desktopmodel.h"
#include "desktopundo.h"
#include "mainwidget.h"
#include "maxview.h"
Desktopundostack::Desktopundostack (QObject *parent)
: QUndoStack (parent)
{
}
Desktopundostack::~Desktopundostack ()
{
}
Desktopundocmd::Desktopundocmd (Desktopmodel *model)
: QUndoCommand ()
{
_seq = 1; // need to global alloc
_model = model;
_model->resetAdded ();
}
Desktopundocmd::~Desktopundocmd ()
{
}
void Desktopundocmd::aboutToAdd (QModelIndex parent)
{
// we are about to add some items
_model->aboutToAdd (parent);
}
void Desktopundocmd::complain(err_info *err)
{
Mainwidget::singleton()->complain(err);
}
UCTrashStacks::UCTrashStacks (Desktopmodel *model,
QModelIndexList &list, QModelIndex &parent, QString dir, bool copy)
: Desktopundocmd (model)
{
QModelIndex ind;
_dir = model->deskToDirname (parent);
_filenames = model->listToFilenames (list);
_destdir = dir;
_copy = copy;
foreach (ind, list)
if (ind.isValid ())
_files << model->getPosData (ind);
setText (QApplication::translate(dir.isEmpty () ? "UCDeleteStacks" : "UCMoveStacksToDir",
dir.isEmpty () ? "Delete stacks (%1)" : "Move stacks to dir (%1)").arg (list.size ()));
}
void UCTrashStacks::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndexList list = _model->listFromFilenames (_filenames, parent);
_trashlist.clear ();
complain (_model->opTrashStacks (list, parent, _trashlist, _destdir, _copy));
}
void UCTrashStacks::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
complain (_model->opUntrashStacks (_trashlist, parent, _filenames, &_files,
_destdir, _copy));
}
UCMove::UCMove (Desktopmodel *model, QModelIndexList &list, QModelIndex &parent,
QList<QPoint> &oldpos, QList<QPoint> &newpos)
: Desktopundocmd (model)
{
QModelIndex ind;
_dir = model->deskToDirname (parent);
_filenames = model->listToFilenames (list);
_oldpos = oldpos;
_newpos = newpos;
setText (QApplication::translate("UCMove",
"Move stacks (%1)").arg (list.size ()));
}
void UCMove::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndexList list = _model->listFromFilenames (_filenames, parent);
_model->opMoveStacks (list, parent, _newpos);
}
void UCMove::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndexList list = _model->listFromFilenames (_filenames, parent);
_model->opMoveStacks (list, parent, _oldpos);
}
UCDuplicate::UCDuplicate (Desktopmodel *model, QModelIndexList &list,
QModelIndex &parent, File::e_type type, int odd_even)
: Desktopundocmd (model)
{
QModelIndex ind;
_dir = model->deskToDirname (parent);
_filenames = model->listToFilenames (list);
_type = type;
_odd_even = odd_even;
QString str = type == File::Type_other
? QApplication::translate("UCDuplicate1", "Duplicate stacks %1").arg (list.size ())
: QApplication::translate("UCDuplicate2", "Duplicate stacks as %1 (%2)")
.arg (File::typeName (type)).arg (list.size ());
setText (str);
}
void UCDuplicate::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndexList list = _model->listFromFilenames (_filenames, parent);
_newnames.clear ();
aboutToAdd (parent);
complain (_model->opDuplicateStacks (list, parent, _newnames, _type, _odd_even));
}
void UCDuplicate::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndexList list = _model->listFromFilenames (_newnames, parent);
complain (_model->opDeleteStacks (list, parent));
}
UCUnstackStacks::UCUnstackStacks (Desktopmodel *model, QModelIndexList &list,
QModelIndex &parent)
: Desktopundocmd (model)
{
QModelIndex ind;
_dir = model->deskToDirname (parent);
_filenames = model->listToFilenames (list);
setText (QApplication::translate("UCUnstackStacks",
"Unstack stacks (%1)").arg (list.size ()));
}
void UCUnstackStacks::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndexList list = _model->listFromFilenames (_filenames, parent);
_newnames.clear ();
aboutToAdd (parent);
complain (_model->opUnstackStacks (list, parent, _newnames));
}
void UCUnstackStacks::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QStringList fname_list;
QModelIndexList list = _model->listFromFilenames (_filenames, parent);
QList<QModelIndexList> pages;
QString fname;
foreach (fname_list, _newnames)
{
QModelIndexList plist;
foreach (fname, fname_list)
{
QModelIndex ind = _model->index (fname, parent);
plist << ind;
}
pages << plist;
}
/* restack to page 1, since we know that the destination stack has
only 1 page */
int pagenum = 1;
complain (_model->opRestackStacks (list, parent, pages, pagenum));
}
UCUnstackPage::UCUnstackPage (Desktopmodel *model, QModelIndex &ind,
int pagenum, bool remove)
: Desktopundocmd (model)
{
_dir = model->deskToDirname (ind.parent ());
_filename = model->data (ind, Desktopmodel::Role_filename).toString ();
_pagenum = pagenum;
_remove = remove;
setText (QApplication::translate("UCUnstackPage",
"Unstack page"));
}
void UCUnstackPage::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex index = _model->index (_filename, parent);
aboutToAdd (parent);
complain (_model->opUnstackPage (index, _pagenum, _remove, _newname));
}
void UCUnstackPage::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex index = _model->index (_filename, parent);
QModelIndex page = _model->index (_newname, parent);
QModelIndexList list;
list << page;
// if we can't find something, skip
if (index == QModelIndex () || page == QModelIndex ())
return;
// if we removed the page, then to undo we must stack it back
if (_remove)
complain (_model->opStackStacks (index, list, _pagenum));
/* but if we just copied the page, then the stack can be left alone, and
we just need to delete the page */
else
complain (_model->opDeleteStacks (list, parent));
}
UCDeletePages::UCDeletePages (Desktopmodel *model, QModelIndex &ind, QBitArray &pages)
: Desktopundocmd (model)
{
_dir = model->deskToDirname (ind.parent ());
_filename = model->data (ind, Desktopmodel::Role_filename).toString ();
int pagecount = model->data (ind, Desktopmodel::Role_pagecount).toInt ();
_pages = pages;
_count = 0;
for (int i = 0; i < pagecount; i++)
if (pages.testBit (i))
_count++;
setText (QApplication::translate("UCDeletePages",
"Delete pages"));
}
void UCDeletePages::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex index = _model->index (_filename, parent);
complain (_model->opDeletePages (index, _pages, _del_info, _count));
}
void UCDeletePages::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex index = _model->index (_filename, parent);
complain (_model->opUndeletePages (index, _pages, _del_info, _count));
}
UCStackStacks::UCStackStacks (Desktopmodel *model, QModelIndex &dest,
QModelIndexList &list, int pagenum, QList<QPoint> *poslist)
: Desktopundocmd (model)
{
QModelIndex ind;
_dir = model->deskToDirname (dest.parent ());
_destname = model->data (dest, Desktopmodel::Role_filename).toString ();
_filenames = model->listToFilenames (list);
_pagenum = pagenum;
int upto = 0;
foreach (ind, list)
{
pagepos_info pp = model->getPosData (ind);
// override the position if required
if (poslist)
pp.pos = (*poslist) [upto++];
_pagepos << pp;
// qDebug () << "position" << pp.pos;
}
setText (QApplication::translate("UCStackStacks",
"Stack pages (%1)").arg (list.size ()));
}
void UCStackStacks::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex dest = _model->index (_destname, parent);
QModelIndexList list = _model->listFromFilenames (_filenames, parent);
complain (_model->opStackStacks (dest, list, _pagenum));
}
void UCStackStacks::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex dest = _model->index (_destname, parent);
complain (_model->opUnstackFromStack (dest, _filenames, _pagenum, _pagepos));
}
UCChangeDir::UCChangeDir (Desktopmodel *model, QString dirPath, QString rootPath,
QString oldPath, QString oldRoot)
: Desktopundocmd (model)
{
_oldpath = oldPath;
_newpath = dirPath;
_oldroot = oldRoot;
_newroot = rootPath;
setText (QApplication::translate("UCChangeDir",
"Change Directory"));
}
void UCChangeDir::redo (void)
{
_model->opChangeDir (_newpath, _newroot);
}
void UCChangeDir::undo (void)
{
_model->opChangeDir (_oldpath, _oldroot);
}
UCRenameStack::UCRenameStack (Desktopmodel *model, const QModelIndex &ind, QString newname)
: Desktopundocmd (model)
{
_dir = model->deskToDirname (ind.parent ());
_oldname = model->data (ind, Desktopmodel::Role_filename).toString ();
_newname = newname;
setText (QApplication::translate("UCRenameStack",
"Rename stack"));
}
void UCRenameStack::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex ind = _model->index (_oldname, parent);
_model->opRenameStack (ind, _newname);
}
void UCRenameStack::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex ind = _model->index (_newname, parent);
_model->opRenameStack (ind, _oldname);
}
UCRenamePage::UCRenamePage (Desktopmodel *model, const QModelIndex &ind,
QString newname)
: Desktopundocmd (model)
{
_dir = model->deskToDirname (ind.parent ());
_fname = model->data (ind, Desktopmodel::Role_filename).toString ();
_pagenum = model->data (ind, Desktopmodel::Role_pagenum).toInt ();
_oldname = model->data (ind, Desktopmodel::Role_pagename).toString ();
_newname = newname;
setText (QApplication::translate("UCRenamePage",
"Rename page"));
}
void UCRenamePage::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex ind = _model->index (_fname, parent);
_model->opRenamePage (ind, _pagenum, _newname);
}
void UCRenamePage::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex ind = _model->index (_fname, parent);
_model->opRenamePage (ind, _pagenum, _oldname);
}
UCUpdateAnnot::UCUpdateAnnot (Desktopmodel *model, const QModelIndex &ind,
QHash<int, QString> &updates)
: Desktopundocmd (model)
{
_dir = model->deskToDirname (ind.parent ());
_fname = model->data (ind, Desktopmodel::Role_filename).toString ();
_new = updates;
QHashIterator<int, QString> i (updates);
while (i.hasNext())
{
i.next();
_old [i.key ()] = model->getAnnot (ind, (File::e_annot)i.key ());
// qDebug () << "old" << i.key () << model->getAnnot (ind, i.key ());
}
setText (QApplication::translate("UCUpdateAnnot",
"Update stack summary"));
}
void UCUpdateAnnot::redo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex ind = _model->index (_fname, parent);
complain(_model->opUpdateAnnot(ind, _new));
}
void UCUpdateAnnot::undo (void)
{
QModelIndex parent = _model->deskFromDirname (_dir);
QModelIndex ind = _model->index (_fname, parent);
complain(_model->opUpdateAnnot(ind, _old));
}
UCAddRepository::UCAddRepository (Desktopmodel *model, QString dirPath)
: Desktopundocmd (model)
{
_dirpath = dirPath;
setText (QApplication::translate("UCAddRepository", "Add Repository"));
}
void UCAddRepository::redo (void)
{
_model->opUpdateRepositoryList (_dirpath, true);
}
void UCAddRepository::undo (void)
{
_model->opUpdateRepositoryList (_dirpath, false);
}
UCRemoveRepository::UCRemoveRepository (Desktopmodel *model, QString dirPath)
: Desktopundocmd (model)
{
_dirpath = dirPath;
setText (QApplication::translate("UCRemoveRepository", "Remove Repository"));
}
void UCRemoveRepository::redo (void)
{
_model->opUpdateRepositoryList (_dirpath, false);
}
void UCRemoveRepository::undo (void)
{
_model->opUpdateRepositoryList (_dirpath, true);
}