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()
62 beginRemoveRows(QModelIndex(), 0, rowCount());
63 _dictionaries.clear();
66 Q_EMIT dataChanged(QModelIndex(), QModelIndex());
69 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
71 if (index.row() < 0 || index.row() > _dictList.count())
74 CommonDictInterface* dictionary = _dictList[index.row()];
78 if (dictionary->type() == "stardict") {
79 name = dictionary->name() + " (" + dictionary->type() + ")";
82 name = dictionary->langFrom() + " - " + dictionary->langTo() +
83 " (" + dictionary->type() + " " +
84 dictionary->name() + ")";
88 if (role == NumberRole){
91 if (role == IconPathRole){
92 return dictionary->iconPath();
94 if (role == IsSelectedRole){
95 return _dictionaries[dictionary];
100 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
102 int res = setDataPriv(index.row(), value, role);
110 int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
112 if (index < 0 || index > _dictList.count())
115 CommonDictInterface* dictionary = _dictList[index];
116 if (role == NameRole)
118 if (role == NumberRole)
120 if (role == IconPathRole)
122 if (role == IsSelectedRole)
124 if (value.type() == QVariant::Bool)
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));
140 void DictManagerModel::setModelProperty(int index, const QVariant value, QString role)
142 if (role.contains("isSelected"))
144 setDataPriv(index, value, IsSelectedRole);
149 void DictManagerModel::itemSelected(int index)
151 _currentIndex = index;
154 CommonDictInterface* DictManagerModel::currentDict()
156 return _dictList[_currentIndex];
159 bool DictManagerModel::isCurrentDictSelected()
161 return _dictionaries[_dictList[_currentIndex]];
164 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
166 Qt::ItemFlags fl = QAbstractItemModel::flags(index);
168 return (fl | Qt::ItemIsEditable);
171 void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
173 beginInsertRows(QModelIndex(), rowCount(), rowCount());
174 _dictionaries.insert(dictionary, isActive);
175 _dictList << dictionary;
179 QHash<CommonDictInterface*, bool> DictManagerModel::dictionaries()
181 return _dictionaries;
184 QList<CommonDictInterface*> DictManagerModel::dictList()