47147305867b5c53025892eadeaca1449a942a21
[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     connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
16 }
17
18 int DictManagerModel::rowCount(const QModelIndex &parent) const
19 {
20     return _dictList.count();
21 }
22
23 void DictManagerModel::setDictionaries(QHash<CommonDictInterface *, bool> dictionaries)
24 {
25     QHashIterator<CommonDictInterface *, bool> i(dictionaries);
26     while (i.hasNext()) {
27         i.next();
28         addDictionary(i.key(), i.value());
29     }
30 }
31
32 void DictManagerModel::clear()
33 {
34     beginRemoveRows(QModelIndex(), 0, rowCount());
35     _dictionaries.clear();
36     _dictList.clear();
37     endRemoveRows();
38     emit dataChanged(QModelIndex(), QModelIndex());
39 }
40
41 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
42 {
43     if (index.row() < 0 || index.row() > _dictList.count())
44         return QVariant();
45
46     CommonDictInterface* dictionary = _dictList[index.row()];
47     if (role == NameRole)
48     {
49         QString name;
50         if (dictionary->type() == "stardict") {
51             name = dictionary->name() + " (" + dictionary->type() + ")";
52         }
53         else {
54             name = dictionary->langFrom() + " - " + dictionary->langTo() +
55                    " (" + dictionary->type() + " " +
56                    dictionary->name() + ")";
57         }
58         return name;
59     }
60     if (role == NumberRole){
61         return index.row();
62     }
63     if (role == IconPathRole){
64         return dictionary->iconPath();
65     }
66     if (role == IsSelectedRole){
67         return _dictionaries[dictionary];
68     }
69     return QVariant();
70 }
71
72 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
73 {
74     int res = setDataPriv(index.row(), value, role);
75     if (res == 0)
76         return false;
77     if (res > 0)
78         return true;
79     return true;
80 }
81
82 int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
83 {
84     if (index < 0 || index > _dictList.count())
85         return 0;
86
87     CommonDictInterface* dictionary = _dictList[index];
88     if (role == NameRole)
89         return 1;
90     if (role == NumberRole)
91         return 1;
92     if (role == IconPathRole)
93         return 1;
94     if (role == IsSelectedRole)
95     {
96         if (value.type() == QVariant::Bool)
97         {
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));
102             return 2;
103         }
104         else
105         {
106             return 0;
107         }
108     }
109     return 0;
110 }
111
112 void DictManagerModel::setModelProperty(int index, const QVariant value, QString role)
113 {
114     if (role.contains("isSelected"))
115     {
116         setDataPriv(index, value, IsSelectedRole);
117     }
118
119 }
120
121 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
122 {
123     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
124     qDebug("lol1");
125     return (fl | Qt::ItemIsEditable);
126 }
127
128 void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive)
129 {
130     beginInsertRows(QModelIndex(), rowCount(), rowCount());
131     _dictionaries.insert(dictionary, isActive);
132     _dictList << dictionary;
133     endInsertRows();
134 }
135
136 QHash<CommonDictInterface*, bool> DictManagerModel::dictionaries()
137 {
138     return _dictionaries;
139 }
140
141 QList<CommonDictInterface*> DictManagerModel::dictList()
142 {
143     return _dictList;
144 }