ba9a977c48f919786cfa6d913724e3ab79bccbe2
[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
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() - 1;
98
99     if (left < right){
100         if (order == Qt::AscendingOrder){
101             qSort(_wordList.begin(), _wordList.end());
102         } else if (order == Qt::DescendingOrder) {
103             qSort(_wordList.begin(), _wordList.end(), qGreater<QString>());
104         }
105     }
106 }
107
108 void WordListModel::setModelProperty(int index, const QVariant value, QString role)
109 {
110     if (role.contains("isBookmarked"))
111     {
112         setDataPriv(index, value, IsBookmarkedRole);
113     }
114 }
115
116 int WordListModel::setDataPriv(int index, const QVariant &value, int role)
117 {
118     bool bookmarksOnly = false;
119     if (_wordInBookmarks.values().count(false) == 0){
120         bookmarksOnly = true;
121     }
122
123     if (index < 0 || index > _translations.count())
124         return 0;
125
126     QString word = _wordList[index];
127     if (role == WordRole)
128         return 1;
129     if (role == NumberRole)
130         return 1;
131     if (role == IsBookmarkedRole)
132     {
133         if (value.type() == QVariant::Bool)
134         {
135             _wordInBookmarks[word] = value.toBool();
136             Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1));
137             if (_wordInBookmarks[word] == true){
138                 Q_EMIT addToBookmarks(word);
139             } else {                
140                 Q_EMIT removeFromBookmarks(word);
141                 if (bookmarksOnly == true){
142                     beginRemoveRows(QModelIndex(), index, index + 1);
143                     this->_translations.remove(_wordList[index]);
144                     this->_wordInBookmarks.remove(_wordList[index]);
145                     this->_wordList.removeAt(index);
146                     endRemoveRows();
147                 }
148             }
149             return 2;
150         }
151         else
152         {
153             return 0;
154         }
155     }
156     return 0;
157 }