added sorting and filtering in download xdxf 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 //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);
59
60     layout->addWidget(treeView);
61
62
63     connect(langFrom, SIGNAL(currentIndexChanged(int)),
64             this, SLOT(refreshDictList()));
65
66     connect(langTo, SIGNAL(currentIndexChanged(int)),
67             this, SLOT(refreshDictList()));
68
69     connect(treeView, SIGNAL(activated(QModelIndex)),
70             this, SLOT(itemClicked(QModelIndex)));
71
72     #ifndef Q_WS_MAEMO_5
73         setMinimumSize(400,200);
74     #else
75         setMinimumHeight(350);
76     #endif
77
78     initializeDicts();
79 }
80
81
82 void XdxfDictSelectDialog::initializeDicts() {
83
84     QSet<QString> languagesFrom;
85     QSet<QString> languagesTo;
86
87     for(int i=0; i < model->rowCount(QModelIndex()); i++) {
88         languagesFrom.insert(
89                 model->data(model->index(i, 0, QModelIndex())).toString());
90         languagesTo.insert(
91                 model->data(model->index(i, 1, QModelIndex())).toString());
92     }
93
94     languagesFrom.remove(QString());
95     languagesTo.remove(QString());
96
97     QList<QString> langFromList = languagesFrom.toList();
98     qSort(langFromList);
99
100     QList<QString> langToList = languagesTo.toList();
101     qSort(langToList);
102
103     langFrom->addItem(tr("Any"));
104     for(int i=0; i < langFromList.count(); i++) {
105          langFrom->addItem(langFromList.at(i));
106     }
107
108     langTo->addItem(tr("Any"));
109     for(int i=0; i < langToList.count(); i++) {
110          langTo->addItem(langToList.at(i));
111     }
112 }
113
114 void XdxfDictSelectDialog::refreshDictList() {
115     if(langTo->currentIndex() == 0)
116         proxyModel->setTo(QString());
117     else
118         proxyModel->setTo(langTo->currentText());
119
120     if(langFrom->currentIndex() == 0)
121         proxyModel->setFrom(QString());
122     else
123         proxyModel->setFrom(langFrom->currentText());
124 }
125
126 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
127     _link = index.model()->data(index, Qt::UserRole);
128     accept();
129 }