-
Notifications
You must be signed in to change notification settings - Fork 3
/
tablemodel.cpp
176 lines (150 loc) · 5.61 KB
/
tablemodel.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
#include "mainwindow.h"
#include <QAbstractTableModel>
#include <QFlags>
#include <qabstractitemmodel.h>
#include "tablemodel.h"
#include <iostream>
using std::cout;
using std::endl;
using std::vector;
QModelIndex TableModel::index(int row, int column, const QModelIndex &parent) const {
Q_UNUSED(parent);
return createIndex(row, column);
}
QModelIndex TableModel::parent(const QModelIndex &child) const {
Q_UNUSED(child);
return QModelIndex();
}
QVariant TableModel::data(const QModelIndex &index, int role) const {
if (role == Qt::DisplayRole)
return dataList[index.row()][index.column()];
return QVariant();
}
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role) {
Q_UNUSED(role);
dataList[index.row()][index.column()] = value;
emit dataChanged(index, index);
return true;
}
int TableModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return dataList.size();
}
int TableModel::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return 3;
}
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const {
Q_UNUSED(index);
auto flags = Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
if (itemsAreDropEnabled)
flags |= Qt::ItemIsDropEnabled;
return flags;
}
Qt::DropActions TableModel::supportedDropActions() const {
auto flags = Qt::DropActions();
if (moveActionEnabled)
flags |= Qt::MoveAction;
if (copyActionEnabled)
flags |= Qt::CopyAction;
return flags;
}
bool TableModel::insertRows(int row, int count, const QModelIndex &parent) {
if (row != -1 and count > 0) {
beginInsertRows(parent, row, row + count - 1);
for (int i = 0; i != count; i++)
dataList.emplace(dataList.begin() + row, columnCount());
endInsertRows();
emit dataChanged(index(row, 0), index(row + count - 1, columnCount()));
emit layoutChanged();
return true;
}
else {
return false;
}
}
bool TableModel::removeRows(int row, int count, const QModelIndex &parent) {
if (row != -1 and count > 0) {
beginRemoveRows(parent, row, row + count - 1);
dataList.erase(dataList.begin() + row, dataList.begin() + row + count);
endRemoveRows();
emit dataChanged(index(row, 0), index(row + count - 1, columnCount()));
emit layoutChanged();
return true;
}
else {
return false;
}
}
bool TableModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) {
std::exit(-1);
if (sourceRow != -1 and destinationChild != -1 and count > 0) {
if (destinationChild >= sourceRow and destinationChild < sourceRow + count) // destination is in the middle of origin range, so reject
return false;
beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
dataList.insert(dataList.begin() + destinationChild, dataList.begin() + sourceRow, dataList.begin() + sourceRow + count);
dataList.erase(dataList.begin() + sourceRow, dataList.begin() + sourceRow + count);
endMoveRows();
emit dataChanged(index(sourceRow, 0), index(sourceRow + count - 1, columnCount()));
emit dataChanged(index(destinationChild, 0), index(destinationChild + count - 1, columnCount()));
emit layoutChanged();
return true;
}
else {
return false;
}
}
bool TableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
Q_UNUSED(column)
QModelIndex newParent;
if (parent.isValid()) // row was dropped directly on an item (parent)
{
// If we apply dropMimeData without any modifications,
// the data overwrites the given row.
// However, we would like to insert the data *after* the given row.
// The TableModel inserts a row if the parent is the hidden root parent
// (provided by QModelIndex()), so we use that.
newParent = QModelIndex();
row = parent.row() + 1;
}
else
newParent = parent;
if (row == -1)
row = rowCount();
return QAbstractTableModel::dropMimeData(data, action, row, 0, newParent);
}
// DEBUGGING VERSION
//bool TableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
//{
// cout << "action: " << action << endl;
// cout << "parent: " << parent.row() << endl;
// cout << "row: " << row << endl;
// bool manipulate = true;
// if (manipulate)
// {
// QModelIndex newParent;
// if (parent.isValid()) // row was dropped directly on an item (parent)
// {
// // If we apply dropMimeData without any modifications,
// // the data overwrites the given row.
// // However, we would like to insert the data *after* the given row.
// // The TableModel inserts a row if the parent is the hidden root parent
// // (provided by QModelIndex()), so we use that.
// newParent = QModelIndex();
// row = parent.row() + 1;
// }
// else
// newParent = parent;
// if (row == -1)
// row = rowCount();
// cout << "After manipulation: " << endl;
// cout << "parent: " << newParent.row() << endl;
// cout << "row: " << row << endl;
// return QAbstractTableModel::dropMimeData(data, action, row, 0, newParent);
// }
// else
// {
// return QAbstractTableModel::dropMimeData(data, action, row, column, parent);
// }
//}