21990b6d75d59f489583b4326026497f5367a08b
[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     _isBookmarkModeActive = false;
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     _isBookmarkModeActive = false;
43 }
44
45 QVariant WordListModel::data(const QModelIndex &index, int role) const
46 {
47     if (index.row() < 0 || index.row() > _translations.count()){
48         return QVariant();
49     }
50
51     if (role == WordRole)
52     {
53         return _wordList[index.row()];
54     }
55     if (role == NumberRole){
56         return index.row();
57     }
58     if (role == IsBookmarkedRole){
59         return _wordInBookmarks[_wordList[index.row()]];
60     }
61     return QVariant();
62 }
63
64 bool WordListModel::setData(const QModelIndex &index, const QVariant &value, int role)
65 {
66     int res = setDataPriv(index.row(), value, role);
67     if (res == 0)
68         return false;
69     if (res > 0)
70         return true;
71     return true;
72 }
73
74 Qt::ItemFlags WordListModel::flags(const QModelIndex &index) const
75 {
76     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
77     return (fl | Qt::ItemIsEditable);
78 }
79
80 void WordListModel::setTranslations(QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks)
81 {
82     QHashIterator<QString, QList<Translation *> > i(translations);
83     while (i.hasNext()) {
84         i.next();
85         addWord(i.key(), i.value(), wordsInBookmarks[i.key()]);
86     }
87
88     sort(0);
89
90 }
91
92 void WordListModel::sort(int column, Qt::SortOrder order)
93 {
94     if (column != 0)
95         return;
96
97     int left = 0;
98     int right = _wordList.count() - 1;
99
100     if (left < right){
101         if (order == Qt::AscendingOrder){
102             qSort(_wordList.begin(), _wordList.end());
103         } else if (order == Qt::DescendingOrder) {
104             qSort(_wordList.begin(), _wordList.end(), qGreater<QString>());
105         }
106     }
107 }
108
109 void WordListModel::setModelProperty(int index, const QVariant value, QString role)
110 {
111     if (role.contains("isBookmarked"))
112     {
113         setDataPriv(index, value, IsBookmarkedRole);
114     }
115 }
116
117 int WordListModel::setDataPriv(int index, const QVariant &value, int role)
118 {
119     if (index < 0 || index > _translations.count() - 1)
120         return 0;
121
122     QString word = _wordList[index];
123     if (role == WordRole)
124         return 1;
125     if (role == NumberRole)
126         return 1;
127     if (role == IsBookmarkedRole)
128     {
129         if (value.type() == QVariant::Bool)
130         {
131             _wordInBookmarks[word] = value.toBool();
132             if (_wordInBookmarks[word] == true){
133                 Q_EMIT addToBookmarks(word);
134             } else {                
135                 Q_EMIT removeFromBookmarks(word);
136                 if (_isBookmarkModeActive == true){
137                     beginRemoveRows(QModelIndex(), index, index + 1);
138                     this->_translations.remove(_wordList[index]);
139                     this->_wordInBookmarks.remove(_wordList[index]);
140                     this->_wordList.removeAt(index);
141                     endRemoveRows();
142                 }
143             }
144             Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1));
145             return 2;
146         }
147         else
148         {
149             return 0;
150         }
151     }
152     return 0;
153 }
154
155 void WordListModel::setBookmarkModeActive(bool mode)
156 {
157     _isBookmarkModeActive = mode;
158 }