-
Notifications
You must be signed in to change notification settings - Fork 41
/
commands.cpp
177 lines (135 loc) · 4.85 KB
/
commands.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
#include "commands.h"
#include "editortab.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
extern QCheckBox* chkBox;
extern bool chk_null;
extern MainWindow* mw_one;
extern EditorTabsWidget* tabWidget;
extern ItemState* AddMoveTemp;
AddCommand::AddCommand(DomModel* model, const QModelIndex& index,
QUndoCommand* parent) {
Q_UNUSED(parent);
m_model = model;
m_index = QModelIndex();
m_parent = index;
// setText(QObject::tr("Add new item"));
mw_one->actionUndo->setToolTip(QObject::tr("Undo") + " " +
QObject::tr("Add new item"));
mw_one->actionRedo->setToolTip(QObject::tr("Redo") + " " +
QObject::tr("Add new item"));
}
void AddCommand::undo() {
// remove item
m_model->removeItem(m_index);
}
void AddCommand::redo() {
/*if (m_index.isValid())
m_model->addItem(m_parent, m_index.row() + 1);
else
m_index = m_model->addItem(m_parent);*/
m_index = m_model->addItem(m_parent);
}
RemoveCommand::RemoveCommand(DomModel* model, const QModelIndex& index,
QUndoCommand* parent) {
Q_UNUSED(parent);
m_model = model;
m_index = index;
m_parent = index.parent();
m_row = index.row();
m_state = model->saveItemState(index);
// get item name (命令列表)
const QModelIndex nameIndex = model->index(m_row, 0, m_parent);
QString name = model->data(nameIndex, Qt::DisplayRole).toString();
setText(QObject::tr("Remove %1").arg(name));
mw_one->actionUndo->setToolTip(QObject::tr("Undo") + " " +
QObject::tr("Remove"));
mw_one->actionRedo->setToolTip(QObject::tr("Redo") + " " +
QObject::tr("Remove"));
}
RemoveCommand::~RemoveCommand() { delete m_state; }
void RemoveCommand::undo() {
// copy to new item
m_model->addItem(m_parent, m_row, m_state);
}
void RemoveCommand::redo() {
// remove item
m_model->removeItem(m_index);
}
EditCommand::EditCommand(QString val, DomModel* model, const QModelIndex& index,
QUndoCommand* parent) {
Q_UNUSED(parent);
m_model = model;
m_index = index;
m_oldVal = model->data(index, Qt::DisplayRole).toString();
m_newVal = val;
// model->setData(index, val);
QString subject;
switch (index.column()) {
case 0:
subject = QObject::tr("key");
break;
case 1:
subject = QObject::tr("type");
break;
case 2:
subject = QObject::tr("value");
};
setText(QObject::tr("Edit item %1").arg(subject));
mw_one->actionUndo->setToolTip(QObject::tr("Undo") + " " +
QObject::tr("Edit item %1").arg(subject));
mw_one->actionRedo->setToolTip(QObject::tr("Redo") + " " +
QObject::tr("Edit item %1").arg(subject));
}
void EditCommand::undo() {
m_model->setData(m_index, m_oldVal.trimmed());
if (!chk_null) {
if (chkBox->isVisible()) chkBox->setVisible(false);
}
tabWidget->getCurentTab()->treeView->clicked(
tabWidget->getCurentTab()->currentIndex());
}
void EditCommand::redo() {
if (!m_index.isValid()) return;
m_model->setData(m_index, m_newVal.trimmed());
if (!chk_null) {
if (chkBox->isVisible()) chkBox->setVisible(false);
}
tabWidget->getCurentTab()->treeView->clicked(
tabWidget->getCurentTab()->currentIndex());
}
PasteCommand::PasteCommand(DomModel* model, const QModelIndex& index,
DomItem* copy_item, QUndoCommand* parent) {
Q_UNUSED(parent);
m_model = model;
m_index = QModelIndex();
m_parent = index;
m_copy_item = copy_item;
setText(QObject::tr("Paste entry"));
mw_one->actionUndo->setToolTip(QObject::tr("Undo") + " " +
QObject::tr("Paste entry"));
mw_one->actionRedo->setToolTip(QObject::tr("Redo") + " " +
QObject::tr("Paste entry"));
}
void PasteCommand::undo() {
// remove item
m_model->removeItem(m_index);
}
void PasteCommand::redo() {
m_index =
m_model->pasteItem(m_parent.parent(), m_parent.row(), NULL, m_copy_item);
}
AddMoveCommand::AddMoveCommand(DomModel* model, const QModelIndex& index,
QUndoCommand* parent) {
Q_UNUSED(parent);
m_model = model;
m_index = QModelIndex();
m_parent = index;
setText(QObject::tr("Add new item"));
mw_one->actionUndo->setToolTip(QObject::tr("Undo") + " " +
QObject::tr("Add new item"));
mw_one->actionRedo->setToolTip(QObject::tr("Redo") + " " +
QObject::tr("Add new item"));
}
void AddMoveCommand::undo() { m_model->removeItem(m_index); }
void AddMoveCommand::redo() { m_index = m_model->addMoveItem(m_parent); }