Added notifications and canceling to xdxf download 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
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
60     treeView = new QTreeView;
61     treeView->setModel(proxyModel);
62     treeView->setRootIsDecorated(false);
63     treeView->setExpandsOnDoubleClick(false);
64
65     treeView->setSortingEnabled(true);
66     treeView->sortByColumn(0, Qt::AscendingOrder);
67
68     treeView->setWordWrap(true);
69
70     #ifndef Q_WS_MAEMO_5
71         treeView->resizeColumnToContents(0);
72         treeView->resizeColumnToContents(1);
73         treeView->setColumnWidth(2, 300);
74         treeView->resizeColumnToContents(3);
75     #else
76         treeView->setColumnWidth(0, 150);
77         treeView->setColumnWidth(1, 150);
78         treeView->setColumnWidth(2, 300);
79         treeView->setColumnWidth(3, 150);
80     #endif
81
82
83     layout->addWidget(treeView);
84
85
86     connect(langFrom, SIGNAL(currentIndexChanged(int)),
87             this, SLOT(refreshDictList()));
88
89     connect(langTo, SIGNAL(currentIndexChanged(int)),
90             this, SLOT(refreshDictList()));
91
92     connect(treeView, SIGNAL(activated(QModelIndex)),
93             this, SLOT(itemClicked(QModelIndex)));
94
95     #ifndef Q_WS_MAEMO_5
96         setMinimumSize(800,500);
97     #else
98         setMinimumHeight(350);
99     #endif
100
101     initializeDicts();
102 }
103
104
105 void XdxfDictSelectDialog::initializeDicts() {
106
107     QSet<QString> languagesFrom;
108     QSet<QString> languagesTo;
109
110     for(int i=0; i < model->rowCount(QModelIndex()); i++) {
111         languagesFrom.insert(
112                 model->data(model->index(i, 0, QModelIndex())).toString());
113         languagesTo.insert(
114                 model->data(model->index(i, 1, QModelIndex())).toString());
115     }
116
117     languagesFrom.remove(QString());
118     languagesTo.remove(QString());
119
120     QList<QString> langFromList = languagesFrom.toList();
121     qSort(langFromList);
122
123     QList<QString> langToList = languagesTo.toList();
124     qSort(langToList);
125
126     langFrom->addItem(tr("Any"));
127     for(int i=0; i < langFromList.count(); i++) {
128          langFrom->addItem(langFromList.at(i));
129     }
130
131     langTo->addItem(tr("Any"));
132     for(int i=0; i < langToList.count(); i++) {
133          langTo->addItem(langToList.at(i));
134     }
135 }
136
137 void XdxfDictSelectDialog::refreshDictList() {
138     if(langTo->currentIndex() == 0)
139         proxyModel->setTo(QString());
140     else
141         proxyModel->setTo(langTo->currentText());
142
143     if(langFrom->currentIndex() == 0)
144         proxyModel->setFrom(QString());
145     else
146         proxyModel->setFrom(langFrom->currentText());
147 }
148
149 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
150     _link = index.model()->data(index, Qt::UserRole).toString();
151     accept();
152 }