Add comments and translations for xdxf downloading dialog
[mdictionary] / src / plugins / xdxf / XdxfDictSelectDialog.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 XdxfDictSelectDialog.cpp
24   \author Mateusz Półrola <mateusz.polrola@comarch.com>
25   */
26
27 #include "XdxfDictSelectDialog.h"
28
29 XdxfDictSelectDialog::XdxfDictSelectDialog(QList<DownloadDict> dicts,
30                                            QWidget *parent) :
31     QDialog(parent) {
32
33
34     setWindowTitle(tr("Select dictionary"));
35
36     layout = new QVBoxLayout;
37     setLayout(layout);
38
39     checkBoxLayout = new QHBoxLayout;
40     layout->addLayout(checkBoxLayout);
41
42     langFrom = new QComboBox;
43     langTo = new QComboBox;
44
45     langFrom->setInsertPolicy(QComboBox::InsertAlphabetically);
46     langTo->setInsertPolicy(QComboBox::InsertAlphabetically);
47
48     langFromLabel = new QLabel(tr("From "));
49     langToLabel = new QLabel(tr("To "));
50
51     checkBoxLayout->addWidget(langFromLabel);
52     checkBoxLayout->addWidget(langFrom, 10);
53     checkBoxLayout->addWidget(langToLabel);
54     checkBoxLayout->addWidget(langTo, 10);
55
56     model = new DictsModel(dicts, this);
57
58     proxyModel = new DictsProxyModel;
59     proxyModel->setDynamicSortFilter(true);
60     proxyModel->setSourceModel(model);
61
62
63     treeView = new QTreeView;
64     treeView->setModel(proxyModel);
65     treeView->setRootIsDecorated(false);
66     treeView->setExpandsOnDoubleClick(false);
67
68     treeView->setSortingEnabled(true);
69     treeView->sortByColumn(0, Qt::AscendingOrder);
70
71     treeView->setWordWrap(true);
72
73     #ifndef Q_WS_MAEMO_5
74         treeView->resizeColumnToContents(0);
75         treeView->resizeColumnToContents(1);
76         treeView->setColumnWidth(2, 300);
77         treeView->resizeColumnToContents(3);
78     #else
79         treeView->setColumnWidth(0, 150);
80         treeView->setColumnWidth(1, 150);
81         treeView->setColumnWidth(2, 300);
82         treeView->setColumnWidth(3, 150);
83     #endif
84
85
86     layout->addWidget(treeView);
87
88
89     connect(langFrom, SIGNAL(currentIndexChanged(int)),
90             this, SLOT(refreshDictList()));
91
92     connect(langTo, SIGNAL(currentIndexChanged(int)),
93             this, SLOT(refreshDictList()));
94
95     connect(treeView, SIGNAL(activated(QModelIndex)),
96             this, SLOT(itemClicked(QModelIndex)));
97
98     #ifndef Q_WS_MAEMO_5
99         setMinimumSize(800,500);
100     #else
101         setMinimumHeight(350);
102     #endif
103
104     initializeDicts();
105 }
106
107
108 void XdxfDictSelectDialog::initializeDicts() {
109
110     //scan of all languages of dictionaries, using QSet to get only distinct languages
111     QSet<QString> languagesFrom;
112     QSet<QString> languagesTo;
113
114     for(int i=0; i < model->rowCount(QModelIndex()); i++) {
115         languagesFrom.insert(
116                 model->data(model->index(i, 0, QModelIndex())).toString());
117         languagesTo.insert(
118                 model->data(model->index(i, 1, QModelIndex())).toString());
119     }
120
121     //removes one dictionary which from and to languages are empty....
122     //bug in site with dictionaries
123     languagesFrom.remove(QString());
124     languagesTo.remove(QString());
125
126     //sorting of found languages
127     QList<QString> langFromList = languagesFrom.toList();
128     qSort(langFromList);
129
130     QList<QString> langToList = languagesTo.toList();
131     qSort(langToList);
132
133     //and adding them to combobox, first item in each combobox is "Any"
134     langFrom->addItem(tr("Any"));
135     for(int i=0; i < langFromList.count(); i++) {
136          langFrom->addItem(langFromList.at(i));
137     }
138
139     langTo->addItem(tr("Any"));
140     for(int i=0; i < langToList.count(); i++) {
141          langTo->addItem(langToList.at(i));
142     }
143 }
144
145 void XdxfDictSelectDialog::refreshDictList() {
146     //if selected language is "Any"(index 0), from filter string is set to empty string, proxy model uses empty string as special case and don't filter by this field.
147     if(langTo->currentIndex() == 0)
148         proxyModel->setTo(QString());
149     else
150         proxyModel->setTo(langTo->currentText());
151
152     if(langFrom->currentIndex() == 0)
153         proxyModel->setFrom(QString());
154     else
155         proxyModel->setFrom(langFrom->currentText());
156 }
157
158 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
159     _link = index.model()->data(index, Qt::UserRole).toString();
160     accept();
161 }