+#include "DictManagerModel.h"
+
+DictManagerModel::DictManagerModel(QHash<CommonDictInterface *, bool> dictionaries, QObject *parent) :
+ QAbstractListModel(parent)
+{
+ QHash<int, QByteArray> roles;
+ roles[NameRole] = "name";
+ roles[IconPathRole] = "iconPath";
+ roles[IsSelectedRole] = "isSelected";
+ roles[NumberRole] = "number";
+ setRoleNames(roles);
+
+ setDictionaries(dictionaries);
+}
+
+int DictManagerModel::rowCount(const QModelIndex &parent) const
+{
+ return _dictList.count();
+}
+
+void DictManagerModel::setDictionaries(QHash<CommonDictInterface *, bool> dictionaries)
+{
+ QHashIterator<CommonDictInterface *, bool> i(dictionaries);
+ while (i.hasNext()) {
+ i.next();
+ addDictionary(i.key(), i.value());
+ }
+}
+
+void DictManagerModel::clear()
+{
+ beginRemoveRows(QModelIndex(), 0, rowCount());
+ _dictionaries.clear();
+ _dictList.clear();
+ endRemoveRows();
+ emit dataChanged(QModelIndex(), QModelIndex());
+}
+
+QVariant DictManagerModel::data(const QModelIndex & index, int role) const
+{
+ if (index.row() < 0 || index.row() > _dictList.count())
+ return QVariant();
+
+ CommonDictInterface* dictionary = _dictList[index.row()];
+ if (role == NameRole)
+ {
+ QString name;
+ if (dictionary->type() == "stardict") {
+ name = dictionary->name() + " (" + dictionary->type() + ")";
+ }
+ else {
+ name = dictionary->langFrom() + " - " + dictionary->langTo() +
+ " (" + dictionary->type() + " " +
+ dictionary->name() + ")";
+ }
+ return name;
+ }
+ if (role == NumberRole)
+ return index.row();
+ if (role == IconPathRole)
+ return dictionary->iconPath();
+ if (role == IsSelectedRole)
+ return _dictionaries[dictionary];
+ return QVariant();
+}
+
+bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+ if (index.row() < 0 || index.row() > _dictList.count())
+ return false;
+
+ CommonDictInterface* dictionary = _dictList[index.row()];
+ if (role == NameRole)
+ return true;
+ if (role == NumberRole)
+ return true;
+ if (role == IconPathRole)
+ return true;
+ if (role == IsSelectedRole)
+ {
+ if (value.type() == QVariant::Bool)
+ {
+ _dictionaries[dictionary] = value.toBool();
+ emit dataChanged(index, index);
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ return false;
+}
+
+Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
+{
+ Qt::ItemFlags fl = QAbstractItemModel::flags(index);
+ return (fl | Qt::ItemIsEditable);
+}
+
+void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
+{
+ beginInsertRows(QModelIndex(), rowCount(), rowCount());
+ _dictionaries.insert(dictionary, isActive);
+ _dictList << dictionary;
+ endInsertRows();
+}