Add model class (DictManagerModel) to manage loaded dictionaries data and settings...
[mdictionary] / src / mdictionary / gui / DictManagerModel.cpp
1 #include "DictManagerModel.h"
2
3 DictManagerModel::DictManagerModel(QHash<CommonDictInterface *, bool> dictionaries, QObject *parent) :
4     QAbstractListModel(parent)
5 {
6     QHash<int, QByteArray> roles;
7     roles[NameRole] = "name";
8     roles[IconPathRole] = "iconPath";
9     roles[IsSelectedRole] = "isSelected";
10     roles[NumberRole] = "number";
11     setRoleNames(roles);
12
13     setDictionaries(dictionaries);
14 }
15
16 int DictManagerModel::rowCount(const QModelIndex &parent) const
17 {
18     return _dictList.count();
19 }
20
21 void DictManagerModel::setDictionaries(QHash<CommonDictInterface *, bool> dictionaries)
22 {
23     QHashIterator<CommonDictInterface *, bool> i(dictionaries);
24     while (i.hasNext()) {
25         i.next();
26         addDictionary(i.key(), i.value());
27     }
28 }
29
30 void DictManagerModel::clear()
31 {
32     beginRemoveRows(QModelIndex(), 0, rowCount());
33     _dictionaries.clear();
34     _dictList.clear();
35     endRemoveRows();
36     emit dataChanged(QModelIndex(), QModelIndex());
37 }
38
39 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
40 {
41     if (index.row() < 0 || index.row() > _dictList.count())
42         return QVariant();
43
44     CommonDictInterface* dictionary = _dictList[index.row()];
45     if (role == NameRole)
46     {
47         QString name;
48         if (dictionary->type() == "stardict") {
49             name = dictionary->name() + " (" + dictionary->type() + ")";
50         }
51         else {
52             name = dictionary->langFrom() + " - " + dictionary->langTo() +
53                    " (" + dictionary->type() + " " +
54                    dictionary->name() + ")";
55         }
56         return name;
57     }
58     if (role == NumberRole)
59         return index.row();
60     if (role == IconPathRole)
61         return dictionary->iconPath();
62     if (role == IsSelectedRole)
63         return _dictionaries[dictionary];
64     return QVariant();
65 }
66
67 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
68 {
69     if (index.row() < 0 || index.row() > _dictList.count())
70         return false;
71
72     CommonDictInterface* dictionary = _dictList[index.row()];
73     if (role == NameRole)
74         return true;
75     if (role == NumberRole)
76         return true;
77     if (role == IconPathRole)
78         return true;
79     if (role == IsSelectedRole)
80     {
81         if (value.type() == QVariant::Bool)
82         {
83             _dictionaries[dictionary] = value.toBool();
84             emit dataChanged(index, index);
85             return true;
86         }
87         else
88         {
89             return false;
90         }
91     }
92     return false;
93 }
94
95 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
96 {
97     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
98     return (fl | Qt::ItemIsEditable);
99 }
100
101 void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
102 {
103     beginInsertRows(QModelIndex(), rowCount(), rowCount());
104     _dictionaries.insert(dictionary, isActive);
105     _dictList << dictionary;
106     endInsertRows();
107 }