1 #include "WordListModel.h"
3 WordListModel::WordListModel(QObject *parent) :
4 QAbstractListModel(parent)
6 QHash<int, QByteArray> roles;
7 roles[WordRole] = "word";
8 roles[IsBookmarkedRole] = "isBookmarked";
9 roles[NumberRole] = "number";
15 int WordListModel::rowCount(const QModelIndex &parent) const
17 return _wordList.count();
20 void WordListModel::addWord(QString word, QList<Translation *> translations, bool isBookmarked)
22 beginInsertRows(QModelIndex(), rowCount(), rowCount());
23 _translations.insert(word, translations);
25 _wordInBookmarks.insert(word, isBookmarked);
29 void WordListModel::clear()
32 if (!_wordList.isEmpty()){
36 _translations.clear();
37 _wordInBookmarks.clear();
44 QVariant WordListModel::data(const QModelIndex &index, int role) const
46 if (index.row() < 0 || index.row() > _translations.count()){
52 return _wordList[index.row()];
54 if (role == NumberRole){
57 if (role == IsBookmarkedRole){
58 return _wordInBookmarks[_wordList[index.row()]];
63 bool WordListModel::setData(const QModelIndex &index, const QVariant &value, int role)
65 int res = setDataPriv(index.row(), value, role);
73 Qt::ItemFlags WordListModel::flags(const QModelIndex &index) const
75 Qt::ItemFlags fl = QAbstractItemModel::flags(index);
76 return (fl | Qt::ItemIsEditable);
79 void WordListModel::setTranslations(QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks)
81 QHashIterator<QString, QList<Translation *> > i(translations);
84 addWord(i.key(), i.value(), wordsInBookmarks[i.key()]);
86 //todo: repair sorting
91 void WordListModel::sort(int column, Qt::SortOrder order)
97 int right = _wordList.count();
100 if (order == Qt::AscendingOrder){
101 ascendingQuickSort(left, right);
102 } else if (order == Qt::DescendingOrder) {
103 descendingQuickSort(left, right);
108 void WordListModel::ascendingQuickSort(int left, int right){
110 for(int i = left+1; i < right; i++){
111 if(_wordList[i] < _wordList[left]){
112 _wordList.swap(++m, i);
116 _wordList.swap(left, m);
117 ascendingQuickSort(left, m - 1);
118 ascendingQuickSort(m + 1, right);
121 void WordListModel::descendingQuickSort(int left, int right){
123 for(int i = left+1; i < right; i++){
124 if(_wordList[i] > _wordList[left]){
125 _wordList.swap(++m, i);
129 _wordList.swap(left, m);
130 ascendingQuickSort(left, m - 1);
131 ascendingQuickSort(m + 1, right);
134 void WordListModel::setModelProperty(int index, const QVariant value, QString role)
136 if (role.contains("isBookmarked"))
138 setDataPriv(index, value, IsBookmarkedRole);
142 int WordListModel::setDataPriv(int index, const QVariant &value, int role)
144 bool bookmarksOnly = false;
145 if (_wordInBookmarks.values().count(false) == 0){
146 bookmarksOnly = true;
149 if (index < 0 || index > _translations.count())
152 QString word = _wordList[index];
153 if (role == WordRole)
155 if (role == NumberRole)
157 if (role == IsBookmarkedRole)
159 if (value.type() == QVariant::Bool)
161 _wordInBookmarks[word] = value.toBool();
162 Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1));
163 if (_wordInBookmarks[word] == true){
164 Q_EMIT addToBookmarks(word);
166 Q_EMIT removeFromBookmarks(word);
167 if (bookmarksOnly == true){
168 beginRemoveRows(QModelIndex(), index, index + 1);
169 this->_translations.remove(_wordList[index]);
170 this->_wordInBookmarks.remove(_wordList[index]);
171 this->_wordList.removeAt(index);