/
KVModel.cpp
159 lines (120 loc) · 3.55 KB
/
KVModel.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
#include <QtWidgets>
#include "KeyValues.h"
#include "KVModel.h"
KVModel::KVModel(QObject *parent /*= nullptr*/) : QAbstractItemModel(parent)
{
rootItem = new KeyValues("Key", "Value");
}
int KVModel::columnCount(const QModelIndex & /* parent */) const
{
return 2;
}
QVariant KVModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::ToolTipRole)
{
return index.child(0, 1).data(Qt::DisplayRole);
}
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
KeyValues *item = getItem(index);
return QString::fromStdString(item->GetData(!!index.column()));
}
Qt::ItemFlags KVModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
if (!getItem(index)->IsValue() && index.column() > 0)
return QAbstractItemModel::flags(index);
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
KeyValues *KVModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
KeyValues *item = static_cast<KeyValues*>(index.internalPointer());
if (item)
return item;
}
return rootItem;
}
QVariant KVModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return QString::fromStdString(rootItem->GetData(!!section));
return QVariant();
}
QModelIndex KVModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0)
return QModelIndex();
KeyValues *parentItem = getItem(parent);
KeyValues *childItem = parentItem->GetChildAt(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
bool KVModel::insertColumns(int position, int columns, const QModelIndex &parent)
{
return false;
}
bool KVModel::insertRows(int position, int rows, const QModelIndex &parent)
{
KeyValues *parentItem = getItem(parent);
bool success;
beginInsertRows(parent, position, position + rows - 1);
success = parentItem->InsertChildren(position, rows);
endInsertRows();
return success;
}
QModelIndex KVModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
KeyValues *childItem = getItem(index);
KeyValues *parentItem = childItem->GetParent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->GetParent()->FindChild(parentItem), 0, parentItem);
}
bool KVModel::removeColumns(int position, int columns, const QModelIndex &parent)
{
return false;
}
bool KVModel::removeRows(int position, int rows, const QModelIndex &parent)
{
KeyValues *parentItem = getItem(parent);
bool success = true;
beginRemoveRows(parent, position, position + rows - 1);
success = parentItem->RemoveChildren(position, rows);
endRemoveRows();
return success;
}
int KVModel::rowCount(const QModelIndex &parent) const
{
KeyValues *parentItem = getItem(parent);
return parentItem->GetChildCount();
}
bool KVModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
return false;
KeyValues *item = getItem(index);
bool result = item->SetData(!!index.column(), value.toString().toStdString());
if (result)
emit dataChanged(index, index);
return result;
}
bool KVModel::setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role)
{
if (role != Qt::EditRole || orientation != Qt::Horizontal)
return false;
bool result = rootItem->SetData(!!section, value.toString().toStdString());
if (result)
emit headerDataChanged(orientation, section, section);
return result;
}