1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 /*! \file DictManagerModel.cpp
23 \brief Contains dictionaries data for qml UI
25 \author Marcin Kaźmierczak <marcin.kazmierczak@comarch.pl>
28 #include "DictManagerModel.h"
30 DictManagerModel::DictManagerModel(QHash<CommonDictInterface *, bool> dictionaries, QObject *parent) :
31 QAbstractListModel(parent)
34 QHash<int, QByteArray> roles;
35 roles[NameRole] = "name";
36 roles[IconPathRole] = "iconPath";
37 roles[IsSelectedRole] = "isSelected";
38 roles[NumberRole] = "number";
41 setDictionaries(dictionaries);
43 connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
46 int DictManagerModel::rowCount(const QModelIndex &parent) const
48 return _dictList.count();
51 void DictManagerModel::setDictionaries(QHash<CommonDictInterface *, bool> dictionaries)
53 QHashIterator<CommonDictInterface *, bool> i(dictionaries);
56 addDictionary(i.key(), i.value());
60 void DictManagerModel::clear()
63 if (!_dictionaries.isEmpty()){
64 // beginRemoveRows(QModelIndex(), 0, rowCount());
68 _dictionaries.clear();
76 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
78 if (index.row() < 0 || index.row() > _dictList.count())
81 CommonDictInterface* dictionary = _dictList[index.row()];
85 if (dictionary->type() == "stardict") {
86 name = dictionary->name() + " (" + dictionary->type() + ")";
89 name = dictionary->langFrom() + " - " + dictionary->langTo() +
90 " (" + dictionary->type() + " " +
91 dictionary->name() + ")";
95 if (role == NumberRole){
98 if (role == IconPathRole){
99 return dictionary->iconPath();
101 if (role == IsSelectedRole){
102 return _dictionaries[dictionary];
107 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
109 int res = setDataPriv(index.row(), value, role);
117 int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
119 if (index < 0 || index > _dictList.count() - 1)
122 CommonDictInterface* dictionary = _dictList[index];
123 if (role == NameRole)
125 if (role == NumberRole)
127 if (role == IconPathRole)
129 if (role == IsSelectedRole)
131 if (value.type() == QVariant::Bool)
133 _dictionaries[dictionary] = value.toBool();
134 Q_EMIT dataChanged(this->index(0), this->index(_dictList.count() - 1));
145 void DictManagerModel::setModelProperty(int index, const QVariant value, QString role)
147 if (role.contains("isSelected"))
149 setDataPriv(index, value, IsSelectedRole);
154 void DictManagerModel::setModelProperty(int index, QString role)
156 if (role.contains("isSelected"))
158 if (index < 0 || index > _dictionaries.count() - 1)
161 setDataPriv(index, !_dictionaries[_dictList[index]], IsSelectedRole);
166 void DictManagerModel::itemSelected(int index)
168 _currentIndex = index;
171 CommonDictInterface* DictManagerModel::currentDict()
173 return _dictList[_currentIndex];
176 bool DictManagerModel::isCurrentDictSelected()
178 return _dictionaries[_dictList[_currentIndex]];
181 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
183 Qt::ItemFlags fl = QAbstractItemModel::flags(index);
184 return (fl | Qt::ItemIsEditable);
187 void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
189 beginInsertRows(QModelIndex(), rowCount(), rowCount());
190 _dictionaries.insert(dictionary, isActive);
191 _dictList << dictionary;
195 QHash<CommonDictInterface*, bool> DictManagerModel::dictionaries()
197 return _dictionaries;
200 QList<CommonDictInterface*> DictManagerModel::dictList()