-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessiblestringlistmodel.cpp
148 lines (124 loc) · 3.89 KB
/
accessiblestringlistmodel.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
#include "accessiblestringlistmodel.h"
AccessibleStringListModel::AccessibleStringListModel()
{
}
void AccessibleStringListModel::clear()
{
modeldata.clear() ;
}
bool AccessibleStringListModel::appendData(QVariant display, QVariant accessibletext, QVariant user)
{
if (insertRows(rowCount(), 1)) {
return setData(index(rowCount()-1, 0), display, accessibletext, user) ;
} else {
return false ;
}
}
bool AccessibleStringListModel::setData(const QModelIndex &index, QVariant display, QVariant accessibletext, QVariant user)
{
if (!setData(index, display, Qt::DisplayRole)) return false ;
if (!setData(index, accessibletext, Qt::AccessibleTextRole)) return false ;
if (!setData(index, user, Qt::UserRole)) return false ;
return true ;
}
bool AccessibleStringListModel::insertRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent) ;
QList<QVariant> newentry ;
newentry.append(QVariant(""));
newentry.append(QVariant(""));
newentry.append(QVariant(""));
beginInsertRows(parent, row, row + count - 1) ;
for (int i=0; i<count; i++) {
modeldata.insert(row, newentry) ;
}
endInsertRows();
return true ;
}
bool AccessibleStringListModel::setData(const QModelIndex &index, QVariant data, int role)
{
int row = index.row() ;
if (row<0 || row>=modeldata.count()) return false ;
const QList<QVariant>& selected = modeldata.at(row) ;
switch (role) {
case Qt::DisplayRole: {
QVariant& item = (QVariant&)selected.at(0) ;
item.setValue(data) ;
emit dataChanged(index, index) ;
return true ;
}
break ;
case Qt::AccessibleTextRole: {
QVariant& item = (QVariant&)selected.at(1) ;
item.setValue(data) ;
emit dataChanged(index, index) ;
return true ;
}
break ;
case Qt::UserRole: {
QVariant& item = (QVariant&)selected.at(2) ;
item.setValue(data) ;
emit dataChanged(index, index) ;
return true ;
}
break ;
default:
return false ;
break ;
}
}
QVariant AccessibleStringListModel::data(const QModelIndex &index, int role) const
{
int row = index.row() ;
if (row<0 || row>=modeldata.count()) return QVariant() ;
const QList<QVariant>& selected = modeldata.at(row) ;
switch (role){
case Qt::DisplayRole:
return selected.at(0) ;
break ;
case Qt::AccessibleTextRole:
return selected.at(1) ;
break ;
case Qt::UserRole:
return selected.at(2) ;
break ;
default: return QVariant() ;
}
}
QModelIndex AccessibleStringListModel::find(QVariant user, int startrow)
{
int result=-1 ;
QString userstring = user.toString() ;
for (int i=startrow; result<0 && i<modeldata.count(); i++) {
const QList<QVariant>& entry = modeldata.at(i) ;
QString srch = entry.toVector().at(2).toString() ;
if (srch.contains(userstring, Qt::CaseInsensitive)) result=i ;
}
if (result<0) return QModelIndex() ;
else return index(result,0) ;
}
QString AccessibleStringListModel::hintAt(int row)
{
const QVariant& result = data(index(row,0), Qt::UserRole) ;
if (!result.isValid()) return QString("") ;
else return result.toString() ;
}
QModelIndex AccessibleStringListModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent) ;
Q_UNUSED(column) ;
return createIndex(row, column) ;
}
int AccessibleStringListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent) ;
return modeldata.count() ;
}
Qt::ItemFlags AccessibleStringListModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
void AccessibleStringListModel::debug()
{
}