1 #include "DictManagerModel.h"
3 DictManagerModel::DictManagerModel(QHash<CommonDictInterface *, bool> dictionaries, QObject *parent) :
4 QAbstractListModel(parent)
6 QHash<int, QByteArray> roles;
7 roles[NameRole] = "name";
8 roles[IconPathRole] = "iconPath";
9 roles[IsSelectedRole] = "isSelected";
10 roles[NumberRole] = "number";
13 setDictionaries(dictionaries);
16 int DictManagerModel::rowCount(const QModelIndex &parent) const
18 return _dictList.count();
21 void DictManagerModel::setDictionaries(QHash<CommonDictInterface *, bool> dictionaries)
23 QHashIterator<CommonDictInterface *, bool> i(dictionaries);
26 addDictionary(i.key(), i.value());
30 void DictManagerModel::clear()
32 beginRemoveRows(QModelIndex(), 0, rowCount());
33 _dictionaries.clear();
36 emit dataChanged(QModelIndex(), QModelIndex());
39 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
41 if (index.row() < 0 || index.row() > _dictList.count())
44 CommonDictInterface* dictionary = _dictList[index.row()];
48 if (dictionary->type() == "stardict") {
49 name = dictionary->name() + " (" + dictionary->type() + ")";
52 name = dictionary->langFrom() + " - " + dictionary->langTo() +
53 " (" + dictionary->type() + " " +
54 dictionary->name() + ")";
58 if (role == NumberRole)
60 if (role == IconPathRole)
61 return dictionary->iconPath();
62 if (role == IsSelectedRole)
63 return _dictionaries[dictionary];
67 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
69 if (index.row() < 0 || index.row() > _dictList.count())
72 CommonDictInterface* dictionary = _dictList[index.row()];
75 if (role == NumberRole)
77 if (role == IconPathRole)
79 if (role == IsSelectedRole)
81 if (value.type() == QVariant::Bool)
83 _dictionaries[dictionary] = value.toBool();
84 emit dataChanged(index, index);
95 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
97 Qt::ItemFlags fl = QAbstractItemModel::flags(index);
98 return (fl | Qt::ItemIsEditable);
101 void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
103 beginInsertRows(QModelIndex(), rowCount(), rowCount());
104 _dictionaries.insert(dictionary, isActive);
105 _dictList << dictionary;