features of DictManagerWidget restored in qml version.
[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     beginRemoveRows(QModelIndex(), 0, rowCount());
63     _dictionaries.clear();
64     _dictList.clear();
65     endRemoveRows();
66     Q_EMIT dataChanged(QModelIndex(), QModelIndex());
67 }
68
69 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
70 {
71     if (index.row() < 0 || index.row() > _dictList.count())
72         return QVariant();
73
74     CommonDictInterface* dictionary = _dictList[index.row()];
75     if (role == NameRole)
76     {
77         QString name;
78         if (dictionary->type() == "stardict") {
79             name = dictionary->name() + " (" + dictionary->type() + ")";
80         }
81         else {
82             name = dictionary->langFrom() + " - " + dictionary->langTo() +
83                    " (" + dictionary->type() + " " +
84                    dictionary->name() + ")";
85         }
86         return name;
87     }
88     if (role == NumberRole){
89         return index.row();
90     }
91     if (role == IconPathRole){
92         return dictionary->iconPath();
93     }
94     if (role == IsSelectedRole){
95         return _dictionaries[dictionary];
96     }
97     return QVariant();
98 }
99
100 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
101 {
102     int res = setDataPriv(index.row(), value, role);
103     if (res == 0)
104         return false;
105     if (res > 0)
106         return true;
107     return true;
108 }
109
110 int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
111 {
112     if (index < 0 || index > _dictList.count())
113         return 0;
114
115     CommonDictInterface* dictionary = _dictList[index];
116     if (role == NameRole)
117         return 1;
118     if (role == NumberRole)
119         return 1;
120     if (role == IconPathRole)
121         return 1;
122     if (role == IsSelectedRole)
123     {
124         if (value.type() == QVariant::Bool)
125         {
126             _dictionaries[dictionary] = value.toBool();
127 //            if (index == _dictList.count())
128 //                emit dataChanged(this->index(index-1), this->index(index));
129             Q_EMIT dataChanged(this->index(0), this->index(_dictList.count() - 1));
130             return 2;
131         }
132         else
133         {
134             return 0;
135         }
136     }
137     return 0;
138 }
139
140 void DictManagerModel::setModelProperty(int index, const QVariant value, QString role)
141 {
142     if (role.contains("isSelected"))
143     {
144         setDataPriv(index, value, IsSelectedRole);
145     }
146
147 }
148
149 void DictManagerModel::itemSelected(int index)
150 {
151     _currentIndex = index;
152 }
153
154 CommonDictInterface* DictManagerModel::currentDict()
155 {
156     return _dictList[_currentIndex];
157 }
158
159 bool DictManagerModel::isCurrentDictSelected()
160 {
161     return _dictionaries[_dictList[_currentIndex]];
162 }
163
164 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
165 {
166     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
167     qDebug("lol1");
168     return (fl | Qt::ItemIsEditable);
169 }
170
171 void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
172 {
173     beginInsertRows(QModelIndex(), rowCount(), rowCount());
174     _dictionaries.insert(dictionary, isActive);
175     _dictList << dictionary;
176     endInsertRows();
177 }
178
179 QHash<CommonDictInterface*, bool> DictManagerModel::dictionaries()
180 {
181     return _dictionaries;
182 }
183
184 QList<CommonDictInterface*> DictManagerModel::dictList()
185 {
186     return _dictList;
187 }