add some file
[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     qDebug()<<"tu jestem"<< role;
58
59     if (role == FromRole){
60         return "From";
61     }
62     if (role == ToRole){
63         return "To";
64     }
65     if (role == NameRole){
66         return "Title";
67     }
68     if (role == SizeRole){
69         return "Size";
70     }
71     return QVariant();
72 }
73
74
75 void DictsListModel::itemSelected(int index) {
76     _currentIndex = index;
77 }
78
79
80 DownloadDict DictsListModel::currentDict() {
81     return dicts[_currentIndex];
82 }
83
84
85 Qt::ItemFlags DictsListModel::flags(const QModelIndex &index) const {
86     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
87     return (fl | Qt::ItemIsEditable);
88 }
89
90
91 QList<DownloadDict> DictsListModel::dictionaries() {
92     return dicts;
93 }