-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTableModel.h
136 lines (124 loc) · 3.91 KB
/
TableModel.h
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
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
#include <functional>
#include "ModelDataAdapter.h"
template<class Container,
class Value = typename Container::value_type>
class TableModel: public QAbstractTableModel,
public ModelDataAdapter<Value>
{
public:
using value_type = Value;
explicit TableModel(QObject *parent = 0):
QAbstractTableModel(parent) {}
QVariant headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal &&
headers.contains(section) &&
headers.value(section).contains(role))
return headers[section][role];
return QVariant();
}
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if(orientation != Qt::Horizontal)
return false;
headers[section][role] = value;
return true;
}
int rowCount(const QModelIndex &parent = QModelIndex()) const
{
if(parent.isValid())
return 0;
return container.size();
}
int columnCount(const QModelIndex &parent = QModelIndex()) const
{
if(parent.isValid())
return 0;
return m_column_count;
}
QVariant data(const QModelIndex &index, int role) const
{
return ModelDataAdapter<Value>::data(*(container.begin() + index.row()),
index.column(),
role);
}
bool setData(const QModelIndex &index, const QVariant &value, int role)
{
bool result = ModelDataAdapter<Value>::setData(&*(container.begin() + index.row()),
index.column(),
value,
role);
dataChanged(index.sibling(index.row(), 0),
index.sibling(index.row(), m_column_count - 1));
return result;
}
void addValue(const value_type &value)
{
insertValue(container.size(), value);
}
void insertValue(int position, const value_type &value)
{
beginInsertRows(QModelIndex(), position, position);
container.insert(container.begin() + position, value);
endInsertRows();
}
void setValues(const Container &values)
{
beginResetModel();
container = values;
endResetModel();
}
void remove(int row, int count = 1)
{
beginRemoveRows(QModelIndex(), row, row + count - 1);
container.erase(container.begin() + row, container.begin() + row + count - 1);
endRemoveRows();
}
void update(int row, int column)
{
dataChanged(index(row, column), index(row, column));
}
void update(int row)
{
dataChanged(index(row, 0), index(row, m_column_count - 1));
}
const Container &getContainer() const
{
return container;
}
const value_type &getValue(int pos) const
{
return container.at(pos);
}
void setValue(int pos, const value_type &value)
{
container[pos] = value;
emit dataChanged(index(pos, 0), index(pos, m_column_count - 1));
}
private:
void columnManipulatorAdded(int column) override
{
int count = std::max(column + 1, m_column_count);
if(count == m_column_count)
return;
if(count > m_column_count)
{
beginInsertColumns(QModelIndex(), m_column_count, count - 1);
m_column_count = count;
endInsertColumns();
}
else
{
beginRemoveColumns(QModelIndex(), count, m_column_count - 1);
m_column_count = count;
endRemoveColumns();
}
}
int m_column_count = 0;
Container container;
QMap<int, QMap<int, QVariant>> headers;
};
#endif // TABLEMODEL_H