Added downloads of xdxf dicts
[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 //Created by Mateusz Półrola
23
24 #include "XdxfDictSelectDialog.h"
25
26 XdxfDictSelectDialog::XdxfDictSelectDialog(QList<DownloadDict> dicts,
27                                            QWidget *parent) :
28     QDialog(parent) {
29
30     layout = new QVBoxLayout;
31     setLayout(layout);
32
33     checkBoxLayout = new QHBoxLayout;
34     layout->addLayout(checkBoxLayout);
35
36     langFrom = new QComboBox;
37     langTo = new QComboBox;
38
39     langFrom->setInsertPolicy(QComboBox::InsertAlphabetically);
40     langTo->setInsertPolicy(QComboBox::InsertAlphabetically);
41
42     checkBoxLayout->addWidget(langFrom);
43     checkBoxLayout->addWidget(langTo);
44
45     model = new DictsModel(dicts, this);
46
47     proxyModel = new DictsProxyModel;
48     proxyModel->setDynamicSortFilter(true);
49     proxyModel->setSourceModel(model);
50
51
52     treeView = new QTreeView;
53     treeView->setModel(proxyModel);
54     treeView->setRootIsDecorated(false);
55     treeView->setExpandsOnDoubleClick(false);
56
57     treeView->setSortingEnabled(true);
58     treeView->sortByColumn(0, Qt::AscendingOrder);
59
60     treeView->resizeColumnToContents(0);
61     treeView->resizeColumnToContents(1);
62     treeView->resizeColumnToContents(2);
63     treeView->resizeColumnToContents(3);
64
65   //  treeView->sortByColumn(0);
66
67     layout->addWidget(treeView);
68
69
70     connect(langFrom, SIGNAL(currentIndexChanged(int)),
71             this, SLOT(refreshDictList()));
72
73     connect(langTo, SIGNAL(currentIndexChanged(int)),
74             this, SLOT(refreshDictList()));
75
76     connect(treeView, SIGNAL(activated(QModelIndex)),
77             this, SLOT(itemClicked(QModelIndex)));
78
79     #ifndef Q_WS_MAEMO_5
80         setMinimumSize(400,200);
81     #else
82         setMinimumHeight(350);
83     #endif
84
85     initializeDicts();
86 }
87
88
89 void XdxfDictSelectDialog::initializeDicts() {
90
91     QSet<QString> languagesFrom;
92     QSet<QString> languagesTo;
93
94     for(int i=0; i < model->rowCount(QModelIndex()); i++) {
95         languagesFrom.insert(
96                 model->data(model->index(i, 0, QModelIndex())).toString());
97         languagesTo.insert(
98                 model->data(model->index(i, 1, QModelIndex())).toString());
99     }
100
101     languagesFrom.remove(QString());
102     languagesTo.remove(QString());
103
104     QList<QString> langFromList = languagesFrom.toList();
105     qSort(langFromList);
106
107     QList<QString> langToList = languagesTo.toList();
108     qSort(langToList);
109
110     langFrom->addItem(tr("Any"));
111     for(int i=0; i < langFromList.count(); i++) {
112          langFrom->addItem(langFromList.at(i));
113     }
114
115     langTo->addItem(tr("Any"));
116     for(int i=0; i < langToList.count(); i++) {
117          langTo->addItem(langToList.at(i));
118     }
119 }
120
121 void XdxfDictSelectDialog::refreshDictList() {
122     if(langTo->currentIndex() == 0)
123         proxyModel->setTo(QString());
124     else
125         proxyModel->setTo(langTo->currentText());
126
127     if(langFrom->currentIndex() == 0)
128         proxyModel->setFrom(QString());
129     else
130         proxyModel->setFrom(langFrom->currentText());
131 }
132
133 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
134     _link = index.model()->data(index, Qt::UserRole).toString();
135     accept();
136 }