-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodels.cpp
More file actions
169 lines (134 loc) · 3.48 KB
/
models.cpp
File metadata and controls
169 lines (134 loc) · 3.48 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
#include <QFontComboBox>
#include <QLineEdit>
#include <QMetaEnum>
#include "models.h"
#include "Alphanum.h"
SpellListSortedModel::SpellListSortedModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}
bool SpellListSortedModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QVariant leftData = sourceModel()->data(left);
QVariant rightData = sourceModel()->data(right);
if (compare(leftData.toString(), rightData.toString()) < 0)
return true;
return false;
}
SpellListModel::SpellListModel(QObject *parent)
: QAbstractTableModel(parent)
{
m_spellList.clear();
}
void SpellListModel::clear()
{
beginResetModel();
m_spellList.clear();
endResetModel();
}
int SpellListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_spellList.size();
}
int SpellListModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant SpellListModel::data(const QModelIndex &index, int role) const
{
if (m_spellList.isEmpty())
return QVariant();
if (!index.isValid())
return QVariant();
if (index.row() >= m_spellList.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole)
return m_spellList.at(index.row()).at(index.column());
return QVariant();
}
QVariant SpellListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
{
switch (section)
{
case 0: return QString("ID");
case 1: return QString("Name");
}
}
return QVariant();
}
Qt::ItemFlags SpellListModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index);
}
ComboBoxModel::ComboBoxModel(QObject *parent)
: QAbstractItemModel(parent)
{
m_items.clear();
}
void ComboBoxModel::clear()
{
beginResetModel();
m_items.clear();
endResetModel();
}
QModelIndex ComboBoxModel::parent(const QModelIndex &index) const
{
Q_UNUSED(index);
return QModelIndex();
}
QModelIndex ComboBoxModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(column);
Q_UNUSED(parent);
return createIndex(row, 0);
}
int ComboBoxModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_items.size();
}
int ComboBoxModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
QVariant ComboBoxModel::data(const QModelIndex &index, int role) const
{
if (m_items.isEmpty())
return QVariant();
if (!index.isValid())
return QVariant();
if (index.row() >= m_items.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole)
return m_items[index.row()].second;
if (role == Qt::UserRole)
return m_items[index.row()].first;
return QVariant();
}
void ComboBoxModel::setItems(ComboBoxHash items)
{
m_items = items;
emit dataChanged(createIndex(0, 0), createIndex(items.size(), 0));
}
QVariant ComboBoxModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(section);
Q_UNUSED(orientation);
Q_UNUSED(role);
return QVariant();
}
Qt::ItemFlags ComboBoxModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(index);
}