-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutpasteitemcommand.cpp
More file actions
87 lines (75 loc) · 1.9 KB
/
cutpasteitemcommand.cpp
File metadata and controls
87 lines (75 loc) · 1.9 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
#include "cutpasteitemcommand.h"
#include "dictionarymodel.h"
#include <QAbstractItemView>
#include <QSortFilterProxyModel>
CutItemCommand::CutItemCommand(QAbstractItemView *view, const QModelIndex &index) :
ItemCommand(view, index), pCutItem(0)
{
}
CutItemCommand::~CutItemCommand()
{
delete pCutItem;
pCutItem = 0;
}
void CutItemCommand::undo()
{
reinitializeIndexes();
if (mIndex.isValid())
{
mIndex = sourceModel->insertItem(new DictionaryItem(pCutItem), mIndex);
mIndex = sourceModel->upItem(mIndex.row(), mParentIndex);
pView->scrollTo(proxyModel->mapFromSource(mIndex));
}
else
{
mIndex = sourceModel->insertItem(new DictionaryItem(pCutItem), mIndex);
pView->scrollTo(mIndex);
}
}
void CutItemCommand::redo()
{
reinitializeIndexes();
sourceModel->cutItem(mIndex.row(), mParentIndex);
if (!pCutItem)
{
pCutItem = new DictionaryItem(sourceModel->cuttedItem());
}
}
PasteItemCommand::PasteItemCommand(QAbstractItemView *view, const QModelIndex &index) :
ItemCommand(view, index), pItem(0)
{
if (sourceModel->cuttedItem())
{
pItem = new DictionaryItem(sourceModel->cuttedItem());
}
}
PasteItemCommand::~PasteItemCommand()
{
}
void PasteItemCommand::undo()
{
reinitializeIndexes();
insertedIndex = sourceModel->index(insertedIndex.row(), insertedIndex.column(), mParentIndex);
sourceModel->removeRow(insertedIndex.row(), mParentIndex);
}
void PasteItemCommand::redo()
{
reinitializeIndexes();
insertedIndex = sourceModel->insertItem(new DictionaryItem(pItem), mIndex);
if (insertedIndex.parent() == mIndex)
{
mParentIndex = insertedIndex.parent();
mGrandParentIndex = mParentIndex.parent();
mRootParentIndex = mGrandParentIndex.parent();
}
pView->scrollTo(proxyModel->mapFromSource(insertedIndex));
}
void PasteItemCommand::reinitializeIndexes()
{
bool indexIsParent = (mParentIndex == mIndex);
ItemCommand::reinitializeIndexes();
if (indexIsParent)
{
mIndex = mParentIndex;
}
}