Added notifications and canceling to xdxf download dialog
[mdictionary] / src / plugins / xdxf / DictsModel.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*!
23  \file DictsModel.cpp
24  \author Mateusz Półrola <mateusz.polrola@comarch.pl>
25 */
26
27
28 DictsModel::DictsModel(QList<DownloadDict> dicts, QObject *parent) :
29     QAbstractItemModel(parent)
30 {
31     this->dicts = dicts;
32
33     qSort(this->dicts);
34 }
35
36
37 int DictsModel::rowCount(const QModelIndex &) const {
38     return dicts.count();
39 }
40
41 int DictsModel::columnCount(const QModelIndex &) const {
42     return 4;
43 }
44
45 QVariant DictsModel::data(const QModelIndex &index, int role) const
46 {
47     if (!index.isValid())
48         return QVariant();
49
50     if (index.row() >= dicts.count())
51         return QVariant();
52
53     if (role == Qt::DisplayRole) {
54         switch(index.column()) {
55         case 0:
56             return dicts.at(index.row()).fromLang();
57         case 1:
58             return dicts.at(index.row()).toLang();
59         case 2:
60             return dicts.at(index.row()).title();
61         case 3:
62             return QString::number(dicts.at(index.row()).size(), 'g', 2) +
63                     QString (" MB");
64         }
65     }
66
67     if (role == Qt::UserRole) {
68         return dicts.at(index.row()).link();
69     }
70     return QVariant();
71 }
72
73 QVariant DictsModel::headerData(int section, Qt::Orientation orientation,
74                                       int role) const
75 {
76     if (role != Qt::DisplayRole)
77         return QVariant();
78
79     if (orientation == Qt::Horizontal) {
80         switch(section) {
81         case 0:
82             return tr("From");
83         case 1:
84             return tr("To");
85         case 2:
86             return tr("Name");
87         case 3:
88             return tr("Size");
89         }
90     }
91     return QVariant();
92 }
93
94 QModelIndex DictsModel::index(int row, int column, const QModelIndex &) const {
95     return createIndex(row, column, row);
96 }
97
98 QModelIndex DictsModel::parent(const QModelIndex &) const {
99     return QModelIndex();
100 }