Clean and order documentation in source files. Source ready to beta 2 release
[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     \brief Shows dialog with list of dictionaries allowing user
24   to select one of them.
25
26     \author Mateusz Półrola <mateusz.polrola@comarch.com>
27 */
28
29 #include "XdxfDictSelectDialog.h"
30
31 XdxfDictSelectDialog::XdxfDictSelectDialog(QList<DownloadDict> dicts,
32                                            QWidget *parent) :
33                     QDialog(parent) {
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     //here removing already added dictionary
57
58     for (int i = 0; i < dicts.size(); i++){
59         if(QFile::exists(QDir::homePath() + "/.mdictionary/" + dicts[i].title() + ".xdxf")){
60             dicts.removeAt(i);
61             i--;
62         }
63     }
64
65     model = new DictsModel(dicts, this);
66
67     proxyModel = new DictsProxyModel;
68     proxyModel->setDynamicSortFilter(true);
69     proxyModel->setSourceModel(model);
70
71     treeView = new QTreeView;
72     treeView->setModel(proxyModel);
73     treeView->setRootIsDecorated(false);
74     treeView->setExpandsOnDoubleClick(false);
75
76     treeView->setSortingEnabled(true);
77     treeView->sortByColumn(0, Qt::AscendingOrder);
78
79     treeView->setWordWrap(true);
80
81     #ifndef Q_WS_MAEMO_5
82         treeView->resizeColumnToContents(0);
83         treeView->resizeColumnToContents(1);
84         treeView->setColumnWidth(2, 300);
85         treeView->resizeColumnToContents(3);
86     #else
87         treeView->setColumnWidth(0, 150);
88         treeView->setColumnWidth(1, 150);
89         treeView->setColumnWidth(2, 300);
90         treeView->setColumnWidth(3, 150);
91     #endif
92
93     layout->addWidget(treeView);
94
95     connect(langFrom, SIGNAL(currentIndexChanged(int)),
96             this, SLOT(refreshDictList()));
97
98     connect(langTo, SIGNAL(currentIndexChanged(int)),
99             this, SLOT(refreshDictList()));
100
101     connect(treeView, SIGNAL(activated(QModelIndex)),
102             this, SLOT(itemClicked(QModelIndex)));
103
104     #ifndef Q_WS_MAEMO_5
105         setMinimumSize(800,500);
106     #else
107         setMinimumHeight(350);
108     #endif
109
110     initializeDicts();
111 }
112
113
114 void XdxfDictSelectDialog::initializeDicts() {
115     //scan of all languages of dictionaries, using QSet to get only distinct languages
116     QSet<QString> languagesFrom;
117     QSet<QString> languagesTo;
118
119     for(int i=0; i < model->rowCount(QModelIndex()); i++) {
120         languagesFrom.insert(
121                 model->data(model->index(i, 0, QModelIndex())).toString());
122         languagesTo.insert(
123                 model->data(model->index(i, 1, QModelIndex())).toString());
124     }
125
126     //removes one dictionary which from and to languages are empty....
127     //bug in site with dictionaries
128     languagesFrom.remove(QString());
129     languagesTo.remove(QString());
130
131     //sorting of found languages
132     QList<QString> langFromList = languagesFrom.toList();
133     qSort(langFromList);
134
135     QList<QString> langToList = languagesTo.toList();
136     qSort(langToList);
137
138     //and adding them to combobox, first item in each combobox is "Any"
139     langFrom->addItem(tr("Any"));
140     for(int i=0; i < langFromList.count(); i++) {
141          langFrom->addItem(langFromList.at(i));
142     }
143
144     langTo->addItem(tr("Any"));
145     for(int i=0; i < langToList.count(); i++) {
146          langTo->addItem(langToList.at(i));
147     }
148 }
149
150
151 void XdxfDictSelectDialog::refreshDictList() {
152     //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.
153     if(langTo->currentIndex() == 0)
154         proxyModel->setTo(QString());
155     else
156         proxyModel->setTo(langTo->currentText());
157
158     if(langFrom->currentIndex() == 0)
159         proxyModel->setFrom(QString());
160     else
161         proxyModel->setFrom(langFrom->currentText());
162 }
163
164
165 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
166     _link = index.model()->data(index, Qt::UserRole).toString();
167     accept();
168 }