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