complete WordListWidget with model.
[mdictionary] / src / mdictionary / gui / WordListModel.cpp
1 #include "WordListModel.h"
2
3 WordListModel::WordListModel(QObject *parent) :
4     QAbstractListModel(parent)
5 {
6     QHash<int, QByteArray> roles;
7     roles[WordRole] = "word";
8     roles[IsBookmarkedRole] = "isBookmarked";
9     roles[NumberRole] = "number";
10     setRoleNames(roles);
11
12
13 }
14
15 int WordListModel::rowCount(const QModelIndex &parent) const
16 {
17     return _wordList.count();
18 }
19
20 void WordListModel::addWord(QString word, QList<Translation *> translations, bool isBookmarked)
21 {
22     beginInsertRows(QModelIndex(), rowCount(), rowCount());
23     _translations.insert(word, translations);
24     _wordList << word;
25     _wordInBookmarks.insert(word, isBookmarked);
26     endInsertRows();
27 }
28
29 void WordListModel::clear()
30 {
31     bool empty = true;
32     if (!_wordList.isEmpty()){
33         beginResetModel();
34         empty = false;
35     }
36     _translations.clear();
37     _wordInBookmarks.clear();
38     _wordList.clear();
39     if (!empty){
40         endResetModel();
41     }
42 }
43
44 QVariant WordListModel::data(const QModelIndex &index, int role) const
45 {
46     if (index.row() < 0 || index.row() > _translations.count()){
47         return QVariant();
48     }
49
50     if (role == WordRole)
51     {
52         return _wordList[index.row()];
53     }
54     if (role == NumberRole){
55         return index.row();
56     }
57     if (role == IsBookmarkedRole){
58         return _wordInBookmarks[_wordList[index.row()]];
59     }
60     return QVariant();
61 }
62
63 bool WordListModel::setData(const QModelIndex &index, const QVariant &value, int role)
64 {
65     int res = setDataPriv(index.row(), value, role);
66     if (res == 0)
67         return false;
68     if (res > 0)
69         return true;
70     return true;
71 }
72
73 Qt::ItemFlags WordListModel::flags(const QModelIndex &index) const
74 {
75     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
76     return (fl | Qt::ItemIsEditable);
77 }
78
79 void WordListModel::setTranslations(QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks)
80 {
81     QHashIterator<QString, QList<Translation *> > i(translations);
82     while (i.hasNext()) {
83         i.next();
84         addWord(i.key(), i.value(), wordsInBookmarks[i.key()]);
85     }
86     //todo: repair sorting
87 //    sort(0);
88
89 }
90
91 void WordListModel::sort(int column, Qt::SortOrder order)
92 {
93     if (column != 0)
94         return;
95
96     int left = 0;
97     int right = _wordList.count();
98
99     if (left < right){
100         if (order == Qt::AscendingOrder){
101             ascendingQuickSort(left, right);
102         } else if (order == Qt::DescendingOrder) {
103             descendingQuickSort(left, right);
104         }
105     }
106 }
107
108 void WordListModel::ascendingQuickSort(int left, int right){
109     int m = left;
110     for(int i = left+1; i < right; i++){
111         if(_wordList[i] < _wordList[left]){
112             _wordList.swap(++m, i);
113         }
114     }
115
116     _wordList.swap(left, m);
117     ascendingQuickSort(left, m - 1);
118     ascendingQuickSort(m + 1, right);
119 }
120
121 void WordListModel::descendingQuickSort(int left, int right){
122     int m = left;
123     for(int i = left+1; i < right; i++){
124         if(_wordList[i] > _wordList[left]){
125             _wordList.swap(++m, i);
126         }
127     }
128
129     _wordList.swap(left, m);
130     ascendingQuickSort(left, m - 1);
131     ascendingQuickSort(m + 1, right);
132 }
133
134 void WordListModel::setModelProperty(int index, const QVariant value, QString role)
135 {
136     if (role.contains("isBookmarked"))
137     {
138         setDataPriv(index, value, IsBookmarkedRole);
139     }
140 }
141
142 int WordListModel::setDataPriv(int index, const QVariant &value, int role)
143 {
144     bool bookmarksOnly = false;
145     if (_wordInBookmarks.values().count(false) == 0){
146         bookmarksOnly = true;
147     }
148
149     if (index < 0 || index > _translations.count())
150         return 0;
151
152     QString word = _wordList[index];
153     if (role == WordRole)
154         return 1;
155     if (role == NumberRole)
156         return 1;
157     if (role == IsBookmarkedRole)
158     {
159         if (value.type() == QVariant::Bool)
160         {
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);
165             } else {                
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);
172                     endRemoveRows();
173                 }
174             }
175             return 2;
176         }
177         else
178         {
179             return 0;
180         }
181     }
182     return 0;
183 }