code clean
[mdictionary] / src / plugins / xdxf / DictsListModel.cpp
1 #include "DictsListModel.h"
2
3
4 DictsListModel::DictsListModel(QList<DownloadDict> dicts, QObject *parent) :
5     QAbstractListModel(parent) {
6
7     _currentIndex = 0;
8     QHash<int, QByteArray> roles;
9     roles[NumberRole] = "number";
10     roles[FromRole] = "from";
11     roles[ToRole] = "to";
12     roles[NameRole] = "name";
13     roles[SizeRole] = "size";
14     roles[LinkRole] = "link";
15     setRoleNames(roles);
16
17     this->dicts=dicts;
18     qSort(this->dicts);
19
20 //    connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
21 }
22
23
24 int DictsListModel::rowCount(const QModelIndex &) const {
25     return dicts.count();
26 }
27
28
29 QVariant DictsListModel::data(const QModelIndex & index, int role) const {
30     if (index.row() < 0 || index.row() > dicts.count())
31         return QVariant();
32
33     DownloadDict dict = dicts[index.row()];
34     if (role == NumberRole){
35         return index.row();
36     }
37     if (role == FromRole){
38         return dict.fromLang();
39     }
40     if (role == ToRole){
41         return dict.toLang();
42     }
43     if (role == NameRole){
44         return dict.title();
45     }
46     if (role == SizeRole){
47         return QString::number(dict.size(),'g', 2) + QString (" MB");
48     }
49     if (role == LinkRole) {
50         return dict.link();
51     }
52     return QVariant();
53 }
54
55
56 QVariant DictsListModel::headerData(int section, Qt::Orientation orientation, int role) const{
57
58     if (role == FromRole){
59         return "From";
60     }
61     if (role == ToRole){
62         return "To";
63     }
64     if (role == NameRole){
65         return "Title";
66     }
67     if (role == SizeRole){
68         return "Size";
69     }
70     return QVariant();
71 }
72
73
74 void DictsListModel::itemSelected(int index) {
75     _currentIndex = index;
76 }
77
78
79 DownloadDict DictsListModel::currentDict() {
80     return dicts[_currentIndex];
81 }
82
83
84 Qt::ItemFlags DictsListModel::flags(const QModelIndex &index) const {
85     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
86     return (fl | Qt::ItemIsEditable);
87 }
88
89
90 QList<DownloadDict> DictsListModel::dictionaries() {
91     return dicts;
92 }