998cdc008da74a87399adf10fc3c20695b678db3
[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     \file DictsModel.cpp
23     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
24 */
25
26 #include "DictsModel.h"
27
28 DictsModel::DictsModel(QList<DownloadDict> dicts, QObject *parent) :
29                        QAbstractItemModel(parent) {
30     this->dicts = dicts;
31     qSort(this->dicts);
32 }
33
34
35 int DictsModel::rowCount(const QModelIndex &) const {
36     return dicts.count();
37 }
38
39
40 int DictsModel::columnCount(const QModelIndex &) const {
41     return 4;
42 }
43
44
45 QVariant DictsModel::data(const QModelIndex &index, int role) const {
46     if (!index.isValid())
47         return QVariant();
48
49     if (index.row() >= dicts.count())
50         return QVariant();
51
52     if (role == Qt::DisplayRole) {
53         switch(index.column()) {
54         case 0:
55             return dicts.at(index.row()).fromLang();
56         case 1:
57             return dicts.at(index.row()).toLang();
58         case 2:
59             return dicts.at(index.row()).title();
60         case 3:
61             return QString::number(dicts.at(index.row()).size(), 'g', 2) +
62                     QString (" MB");
63         }
64     }
65
66     if (role == Qt::UserRole) {
67         return dicts.at(index.row()).link();
68     }
69     return QVariant();
70 }
71
72
73 QVariant DictsModel::headerData(int section, Qt::Orientation orientation,
74                                       int role) const {
75     if (role != Qt::DisplayRole)
76         return QVariant();
77
78     if (orientation == Qt::Horizontal) {
79         switch(section) {
80         case 0:
81             return tr("From");
82         case 1:
83             return tr("To");
84         case 2:
85             return tr("Name");
86         case 3:
87             return tr("Size");
88         }
89     }
90     return QVariant();
91 }
92
93
94 QModelIndex DictsModel::index(int row, int column, const QModelIndex &) const {
95     return createIndex(row, column, row);
96 }
97
98
99 QModelIndex DictsModel::parent(const QModelIndex &) const {
100     return QModelIndex();
101 }