-
Notifications
You must be signed in to change notification settings - Fork 1
/
hivevaluesmodel.cpp
118 lines (104 loc) · 2.59 KB
/
hivevaluesmodel.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
#include "hivevaluesmodel.h"
#include <QVariant>
#include <QFont>
enum ValueColumn
{
Name = 0,
Type = 1,
Data = 2
};
HiveValuesModel::HiveValuesModel(QObject *parent)
: QAbstractTableModel(parent)
{
m_italicFont.setItalic(true);
}
int HiveValuesModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_values.length();
}
int HiveValuesModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QModelIndex HiveValuesModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() || column < 0 || column > columnCount(parent))
return QModelIndex{};
ValueItem *item = m_values.value(row, nullptr);
if (!item)
return QModelIndex{};
return createIndex(row, column, item);
}
QVariant HiveValuesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
ValueItem *item = static_cast<ValueItem *>(index.internalPointer());
if (!item)
return QVariant{};
switch (role)
{
case Qt::DisplayRole:
switch (index.column())
{
case ValueColumn::Name:
{
if (item->isDefault())
return "(Default)";
return item->name();
}
case ValueColumn::Type:
return item->valueTypeDisplay();
case ValueColumn::Data:
return item->dataDisplay().first;
}
break;
case Qt::FontRole:
if (index.column() == ValueColumn::Name && item->isDefault())
{
return m_italicFont;
}
if (index.column() == ValueColumn::Data && item->dataDisplay().second)
{
return m_italicFont;
}
break;
}
return QVariant{};
}
QVariant HiveValuesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
{
return QVariant{};
}
switch (section)
{
case ValueColumn::Name:
return "Name";
case ValueColumn::Type:
return "Type";
case ValueColumn::Data:
return "Data";
default:
return QVariant{};
}
}
void HiveValuesModel::loadNode(HiveItem *item)
{
beginResetModel();
// The HiveItem given is responsible for deleting ValueItems.
m_values.clear();
if (item)
{
for (const auto &val : item->getValues())
{
auto valItem = dynamic_cast<ValueItem *>(val);
if (valItem)
m_values.append(valItem);
}
}
endResetModel();
}