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