fix bug with empty dictionary list. Add first version of WordListView (qml)
[mdictionary] / src / mdictionary / gui / WordListModel.cpp
1 #include "WordListModel.h"
2
3 WordListModel::WordListModel(/*QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks, */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     //setTranslations(translations, wordsInBookmarks);
13
14 //    connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
15 }
16
17 int WordListModel::rowCount(const QModelIndex &parent) const
18 {
19     return _wordList.count();
20 }
21
22 void WordListModel::addWord(QString word, QList<Translation *> translations, bool isBookmarked)
23 {
24     beginInsertRows(QModelIndex(), rowCount(), rowCount());
25     _translations.insert(word, translations);
26     _wordList << word;
27     _wordInBookmarks.insert(word, isBookmarked);
28     endInsertRows();
29 }
30
31 void WordListModel::clear()
32 {
33     bool empty = true;
34     if (!_wordList.isEmpty()){
35         beginResetModel();
36         empty = false;
37     }
38     _translations.clear();
39     _wordInBookmarks.clear();
40     _wordList.clear();
41     if (!empty){
42         endResetModel();
43     }
44 }
45
46 QVariant WordListModel::data(const QModelIndex &index, int role) const
47 {
48     if (index.row() < 0 || index.row() > _translations.count()){
49         return QVariant();
50     }
51
52     if (role == WordRole)
53     {
54         return _wordList[index.row()];
55     }
56     if (role == NumberRole){
57         return index.row();
58     }
59     if (role == IsBookmarkedRole){
60         return _wordInBookmarks[_wordList[index.row()]];
61     }
62     return QVariant();
63 }
64
65 bool WordListModel::setData(const QModelIndex &index, const QVariant &value, int role)
66 {
67     int res = setDataPriv(index.row(), value, role);
68     if (res == 0)
69         return false;
70     if (res > 0)
71         return true;
72     return true;
73 }
74
75 Qt::ItemFlags WordListModel::flags(const QModelIndex &index) const
76 {
77     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
78     return (fl | Qt::ItemIsEditable);
79 }
80
81 void WordListModel::setTranslations(QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks)
82 {
83     QHashIterator<QString, QList<Translation *> > i(translations);
84     while (i.hasNext()) {
85         i.next();
86         addWord(i.key(), i.value(), wordsInBookmarks[i.key()]);
87     }
88 }
89
90 void WordListModel::setModelProperty(int index, const QVariant value, QString role)
91 {
92     if (role.contains("isBookmarked"))
93     {
94         setDataPriv(index, value, IsBookmarkedRole);
95     }
96 }
97
98 int WordListModel::setDataPriv(int index, const QVariant &value, int role)
99 {
100     if (index < 0 || index > _translations.count())
101         return 0;
102
103     QString word = _wordList[index];
104     if (role == WordRole)
105         return 1;
106     if (role == NumberRole)
107         return 1;
108     if (role == IsBookmarkedRole)
109     {
110         if (value.type() == QVariant::Bool)
111         {
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);
116             } else {
117                 Q_EMIT removeFromBookmarks(word);
118             }
119             return 2;
120         }
121         else
122         {
123             return 0;
124         }
125     }
126     return 0;
127 }