1 #include "WordListModel.h"
3 WordListModel::WordListModel(/*QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks, */QObject *parent) :
4 QAbstractListModel(parent)
6 QHash<int, QByteArray> roles;
7 roles[WordRole] = "word";
8 roles[IsBookmarkedRole] = "isBookmarked";
9 roles[NumberRole] = "number";
12 //setTranslations(translations, wordsInBookmarks);
14 // connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
17 int WordListModel::rowCount(const QModelIndex &parent) const
19 return _wordList.count();
22 void WordListModel::addWord(QString word, QList<Translation *> translations, bool isBookmarked)
24 beginInsertRows(QModelIndex(), rowCount(), rowCount());
25 _translations.insert(word, translations);
27 _wordInBookmarks.insert(word, isBookmarked);
31 void WordListModel::clear()
34 if (!_wordList.isEmpty()){
38 _translations.clear();
39 _wordInBookmarks.clear();
46 QVariant WordListModel::data(const QModelIndex &index, int role) const
48 if (index.row() < 0 || index.row() > _translations.count()){
54 return _wordList[index.row()];
56 if (role == NumberRole){
59 if (role == IsBookmarkedRole){
60 return _wordInBookmarks[_wordList[index.row()]];
65 bool WordListModel::setData(const QModelIndex &index, const QVariant &value, int role)
67 int res = setDataPriv(index.row(), value, role);
75 Qt::ItemFlags WordListModel::flags(const QModelIndex &index) const
77 Qt::ItemFlags fl = QAbstractItemModel::flags(index);
78 return (fl | Qt::ItemIsEditable);
81 void WordListModel::setTranslations(QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks)
83 QHashIterator<QString, QList<Translation *> > i(translations);
86 addWord(i.key(), i.value(), wordsInBookmarks[i.key()]);
90 void WordListModel::setModelProperty(int index, const QVariant value, QString role)
92 if (role.contains("isBookmarked"))
94 setDataPriv(index, value, IsBookmarkedRole);
98 int WordListModel::setDataPriv(int index, const QVariant &value, int role)
100 if (index < 0 || index > _translations.count())
103 QString word = _wordList[index];
104 if (role == WordRole)
106 if (role == NumberRole)
108 if (role == IsBookmarkedRole)
110 if (value.type() == QVariant::Bool)
112 _wordInBookmarks[word] = value.toBool();
113 Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1));
114 if (_wordInBookmarks[word] == true){
115 Q_EMIT addToBookmarks(word);
117 Q_EMIT removeFromBookmarks(word);