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