fix bug with empty dictionary list. Add first version of WordListView (qml)
[mdictionary] / src / mdictionary / gui / DictManagerModel.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*! \file DictManagerModel.cpp
23     \brief Contains dictionaries data for qml UI
24
25     \author Marcin Kaźmierczak <marcin.kazmierczak@comarch.pl>
26 */
27
28 #include "DictManagerModel.h"
29
30 DictManagerModel::DictManagerModel(QHash<CommonDictInterface *, bool> dictionaries, QObject *parent) :
31     QAbstractListModel(parent)
32 {
33     _currentIndex = 0;
34     QHash<int, QByteArray> roles;
35     roles[NameRole] = "name";
36     roles[IconPathRole] = "iconPath";
37     roles[IsSelectedRole] = "isSelected";
38     roles[NumberRole] = "number";
39     setRoleNames(roles);
40
41     setDictionaries(dictionaries);
42
43     connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
44 }
45
46 int DictManagerModel::rowCount(const QModelIndex &parent) const
47 {
48     return _dictList.count();
49 }
50
51 void DictManagerModel::setDictionaries(QHash<CommonDictInterface *, bool> dictionaries)
52 {
53     QHashIterator<CommonDictInterface *, bool> i(dictionaries);
54     while (i.hasNext()) {
55         i.next();
56         addDictionary(i.key(), i.value());
57     }
58 }
59
60 void DictManagerModel::clear()
61 {
62     bool empty = true;
63     if (!_dictionaries.isEmpty()){
64 //        beginRemoveRows(QModelIndex(), 0, rowCount());
65         beginResetModel();
66         empty = false;
67     }
68     _dictionaries.clear();
69     _dictList.clear();
70
71     if (!empty){
72         endResetModel();
73     }
74 }
75
76 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
77 {
78     if (index.row() < 0 || index.row() > _dictList.count())
79         return QVariant();
80
81     CommonDictInterface* dictionary = _dictList[index.row()];
82     if (role == NameRole)
83     {
84         QString name;
85         if (dictionary->type() == "stardict") {
86             name = dictionary->name() + " (" + dictionary->type() + ")";
87         }
88         else {
89             name = dictionary->langFrom() + " - " + dictionary->langTo() +
90                    " (" + dictionary->type() + " " +
91                    dictionary->name() + ")";
92         }
93         return name;
94     }
95     if (role == NumberRole){
96         return index.row();
97     }
98     if (role == IconPathRole){
99         return dictionary->iconPath();
100     }
101     if (role == IsSelectedRole){
102         return _dictionaries[dictionary];
103     }
104     return QVariant();
105 }
106
107 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
108 {
109     int res = setDataPriv(index.row(), value, role);
110     if (res == 0)
111         return false;
112     if (res > 0)
113         return true;
114     return true;
115 }
116
117 int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
118 {
119     if (index < 0 || index > _dictList.count())
120         return 0;
121
122     CommonDictInterface* dictionary = _dictList[index];
123     if (role == NameRole)
124         return 1;
125     if (role == NumberRole)
126         return 1;
127     if (role == IconPathRole)
128         return 1;
129     if (role == IsSelectedRole)
130     {
131         if (value.type() == QVariant::Bool)
132         {
133             _dictionaries[dictionary] = value.toBool();
134             Q_EMIT dataChanged(this->index(0), this->index(_dictList.count() - 1));
135             return 2;
136         }
137         else
138         {
139             return 0;
140         }
141     }
142     return 0;
143 }
144
145 void DictManagerModel::setModelProperty(int index, const QVariant value, QString role)
146 {
147     if (role.contains("isSelected"))
148     {
149         setDataPriv(index, value, IsSelectedRole);
150     }
151
152 }
153
154 void DictManagerModel::itemSelected(int index)
155 {
156     _currentIndex = index;
157 }
158
159 CommonDictInterface* DictManagerModel::currentDict()
160 {
161     return _dictList[_currentIndex];
162 }
163
164 bool DictManagerModel::isCurrentDictSelected()
165 {
166     return _dictionaries[_dictList[_currentIndex]];
167 }
168
169 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
170 {
171     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
172     return (fl | Qt::ItemIsEditable);
173 }
174
175 void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
176 {
177     beginInsertRows(QModelIndex(), rowCount(), rowCount());
178     _dictionaries.insert(dictionary, isActive);
179     _dictList << dictionary;
180     endInsertRows();
181 }
182
183 QHash<CommonDictInterface*, bool> DictManagerModel::dictionaries()
184 {
185     return _dictionaries;
186 }
187
188 QList<CommonDictInterface*> DictManagerModel::dictList()
189 {
190     return _dictList;
191 }