first (buggy) version of keyboard usage in qml list
[mdictionary] / src / mdictionary / gui / HistoryListModel.cpp
1 #include "HistoryListModel.h"
2
3 HistoryListModel::HistoryListModel(QStringList words, QObject *parent) :
4         QAbstractListModel(parent) {
5
6     QHash<int, QByteArray> roles;
7     roles[WordRole] = "word";
8     roles[NumberRole] = "number";
9     setRoleNames(roles);
10     setHistoryTypes(words);
11 }
12
13 int HistoryListModel::rowCount(const QModelIndex &parent) const {
14     return _words.count();
15 }
16
17 void HistoryListModel::setHistoryTypes(QStringList words) {
18     for(int i = 0; i < words.count(); i++) {
19         addType(words[i]);
20     }
21 }
22
23 QVariant HistoryListModel::data(const QModelIndex & index, int role) const
24 {
25     if (index.row() < 0 || index.row() > _words.count())
26         return QVariant();
27
28     const QString word = _words[index.row()];
29     if (role == WordRole)
30         return word;
31     if (role == NumberRole)
32         return index.row();
33     return QVariant();
34 }
35
36 void HistoryListModel::addType(QString word) {
37     beginInsertRows(QModelIndex(), rowCount(), rowCount());
38     _words << word;
39     endInsertRows();
40 }