582382954e271655cd933ca4fde20b0cf0b598d0
[mdictionary] / src / mdictionary / gui / DictTypeModel.cpp
1 #include "DictTypeModel.h"
2
3 DictTypeModel::DictTypeModel(QList<CommonDictInterface *> plugins, QObject *parent) :
4     QAbstractListModel(parent)
5 {
6     QHash<int, QByteArray> roles;
7     roles[TypeRole] = "type";
8     setRoleNames(roles);
9
10     setDictTypes(plugins);
11 }
12
13 int DictTypeModel::rowCount(const QModelIndex &parent) const
14 {
15     return _plugins.count();
16 }
17
18 void DictTypeModel::setDictTypes(QList<CommonDictInterface *> plugins)
19 {
20     for(int i = 0; i < plugins.count(); i++)
21     {
22         addType(plugins[i]);
23     }
24 }
25
26 QVariant DictTypeModel::data(const QModelIndex & index, int role) const
27 {
28     if (index.row() < 0 || index.row() > _plugins.count())
29         return QVariant();
30
31     const CommonDictInterface* plugin = _plugins[index.row()];
32     if (role == TypeRole)
33         return plugin->type();
34     return QVariant();
35 }
36
37 void DictTypeModel::addType(CommonDictInterface *plugin)
38 {
39     beginInsertRows(QModelIndex(), rowCount(), rowCount());
40     _plugins << plugin;
41     endInsertRows();
42 }