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);
15 connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
18 int DictManagerModel::rowCount(const QModelIndex &parent) const
20 return _dictList.count();
23 void DictManagerModel::setDictionaries(QHash<CommonDictInterface *, bool> dictionaries)
25 QHashIterator<CommonDictInterface *, bool> i(dictionaries);
28 addDictionary(i.key(), i.value());
32 void DictManagerModel::clear()
34 beginRemoveRows(QModelIndex(), 0, rowCount());
35 _dictionaries.clear();
38 emit dataChanged(QModelIndex(), QModelIndex());
41 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
43 if (index.row() < 0 || index.row() > _dictList.count())
46 CommonDictInterface* dictionary = _dictList[index.row()];
50 if (dictionary->type() == "stardict") {
51 name = dictionary->name() + " (" + dictionary->type() + ")";
54 name = dictionary->langFrom() + " - " + dictionary->langTo() +
55 " (" + dictionary->type() + " " +
56 dictionary->name() + ")";
60 if (role == NumberRole){
63 if (role == IconPathRole){
64 return dictionary->iconPath();
66 if (role == IsSelectedRole){
67 return _dictionaries[dictionary];
72 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
74 int res = setDataPriv(index.row(), value, role);
82 int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
84 if (index < 0 || index > _dictList.count())
87 CommonDictInterface* dictionary = _dictList[index];
90 if (role == NumberRole)
92 if (role == IconPathRole)
94 if (role == IsSelectedRole)
96 if (value.type() == QVariant::Bool)
98 _dictionaries[dictionary] = value.toBool();
99 // if (index == _dictList.count())
100 // emit dataChanged(this->index(index-1), this->index(index));
101 emit dataChanged(this->index(0), this->index(_dictList.count() - 1));
112 void DictManagerModel::setModelProperty(int index, const QVariant value, QString role)
114 if (role.contains("isSelected"))
116 setDataPriv(index, value, IsSelectedRole);
121 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
123 Qt::ItemFlags fl = QAbstractItemModel::flags(index);
125 return (fl | Qt::ItemIsEditable);
128 void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
130 beginInsertRows(QModelIndex(), rowCount(), rowCount());
131 _dictionaries.insert(dictionary, isActive);
132 _dictList << dictionary;
136 QHash<CommonDictInterface*, bool> DictManagerModel::dictionaries()
138 return _dictionaries;
141 QList<CommonDictInterface*> DictManagerModel::dictList()