-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifcommand.cpp
More file actions
175 lines (157 loc) · 5.27 KB
/
modifcommand.cpp
File metadata and controls
175 lines (157 loc) · 5.27 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
#include "modifcommand.h"
#include <QDebug>
ModifCommand::ModifCommand(QStandardItemModel *model, QModelIndex index,
QVariant oldData, QVariant data, ModifCommandType type, Qt::ItemDataRole role /*= Qt::EditRole*/, QUndoCommand *parent) : QUndoCommand(parent)
{
this->model = model;
this->index = index;
this->oldData = oldData;
this->data = data;
this->m_type = type;
this->m_role = role;
}
ModifCommand::ModifCommand(QStandardItemModel *model, ModifCommandList list, ModifCommandType type, Qt::ItemDataRole role /*= Qt::EditRole*/, QUndoCommand *parent) : QUndoCommand(parent)
{
this->model = model;
m_commandList = list;
this->m_type = type;
this->m_role = role;
}
ModifCommand::ModifCommand(QStandardItemModel *model, QList<int> nRows, QTableView* tableView, ModifCommandType type, QUndoCommand *parent/* = nullptr*/) : QUndoCommand(parent)
{
this->model = model;
this->m_setRows = nRows;
this->m_type = type;
this->m_tableView = tableView;
}
ModifCommand::ModifCommand(QStandardItemModel *model, QMap<int, int> colWidth, QTableView* tableView, ModifCommandType type, QUndoCommand *parent/* = nullptr*/) : QUndoCommand(parent)
{
this->model = model;
this->m_colWidth = colWidth;
this->m_type = type;
this->m_tableView = tableView;
}
void ModifCommand::undo()
{
switch (m_type) {
case ModifCommandType::singleModelIndex :
{
model->setData(index, oldData, m_role);
break;
}
case ModifCommandType::ListModelIndex :
{
for (auto command : m_commandList)
{
model->setData(command.index, command.oldData, m_role);
}
break;
}
case ModifCommandType::hiddenRow :
{
if (m_tableView)
{
for (auto row : m_setRows)
{
m_tableView->setRowHidden(row, false);
}
}
break;
}
case ModifCommandType::hiddenCol :
{
if (m_tableView)
{
for (auto iter = m_colWidth.begin(); iter != m_colWidth.end(); ++iter)
{
m_tableView->setColumnWidth(iter.key(), iter.value());
}
}
break;
}
case ModifCommandType::expandCol :
{
if (m_tableView)
{
for (auto iter = m_colWidth.begin(); iter != m_colWidth.end(); ++iter)
{
m_tableView->setColumnWidth(iter.key(), iter.value());
}
}
break;
}
}
}
void ModifCommand::redo()
{
switch (m_type) {
case ModifCommandType::singleModelIndex :
{
model->setData(index, data, m_role);
QString str = "row:" + QString::number(index.row() + 1) + " col:" + QString::number(index.column() + 1) + " value:" + data.toString();
setText(str);
break;
}
case ModifCommandType::ListModelIndex :
{
QString str;
for (int i = 0; i < m_commandList.size(); ++i)
{
auto command = m_commandList[i];
model->setData(command.index, command.data, m_role);
str = str + "row:" + QString::number(command.index.row() + 1) + " col:" + QString::number(command.index.column() + 1) + " value:" + command.data.toString();
if (i < m_commandList.size() - 1)
{
str = str + " && ";
}
}
setText(str);
break;
}
case ModifCommandType::hiddenRow :
{
if (m_tableView)
{
QString str;
str = str + "hidden row : ";
for (auto row : m_setRows)
{
str = str + row + " ";
m_tableView->setRowHidden(row, true);
}
setText(str);
}
break;
}
case ModifCommandType::hiddenCol :
{
if (m_tableView)
{
QString str;
str = str + "hidden col ";
for (auto iter = m_colWidth.begin(); iter != m_colWidth.end(); ++iter)
{
str = str + QString::number(iter.key()) + ":" + QString::number(iter.value()) + " ";
m_tableView->setColumnWidth(iter.key(), COLHIDDENWIDTH);
}
setText(str);
}
break;
}
case ModifCommandType::expandCol :
{
if (m_tableView)
{
QString str;
str = str + "expand col ";
for (auto iter = m_colWidth.begin(); iter != m_colWidth.end(); ++iter)
{
str = str + QString::number(iter.key()) + " ";
m_tableView->resizeColumnToContents(iter.key());
}
setText(str);
}
break;
}
}
}