bi change + completer in line edit
[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
35     setWindowTitle(tr("Select dictionary"));
36     //here removing already added dictionary
37     for (int i = 0; i < dicts.size(); i++){
38         if(QFile::exists(QDir::homePath() + "/.mdictionary/" + dicts[i].title() + ".xdxf")){
39             dicts.removeAt(i);
40             i--;
41         }
42     }
43
44 #ifndef Q_WS_MAEMO_5
45     model2 = new DictsListModel(dicts, this);
46     proxyModel2 = new DictsProxyListModel;
47     proxyModel2->setSourceModel(model2);;
48     view= new QDeclarativeView();
49     QDeclarativeContext* ctxt=view->rootContext();
50     ctxt->setContextProperty("dictModel", proxyModel2);
51
52     QSet<QString> languagesFrom;
53     for(int i=0; i < model2->rowCount(QModelIndex()); i++)
54         languagesFrom.insert(model2->data(model2->index(i, 0),DictsListModel::FromRole).toString());
55     languagesFrom.remove(QString());
56     QStringList langFromList = languagesFrom.toList();
57     qSort(langFromList);
58     QList<QString> langListFrom;
59     langListFrom.append(tr("Any"));
60     foreach(QString lang,langFromList)
61         langListFrom.append(lang);
62     fromModel = new ComboBoxModel(langListFrom);
63     ctxt->setContextProperty("fromModell", fromModel);
64
65     QSet<QString> languagesTo;
66     for(int i=0; i < model2->rowCount(QModelIndex()); i++)
67         languagesTo.insert(model2->data(model2->index(i, 0),DictsListModel::ToRole).toString());
68     languagesTo.remove(QString());
69     QStringList langToList = languagesTo.toList();
70     qSort(langToList);
71     QList<QString> langListTo;
72     langListTo.append(tr("Any"));
73     foreach(QString lang,langToList)
74         langListTo.append(lang);
75     toModel = new ComboBoxModel(langListTo);
76     ctxt->setContextProperty("toModell", toModel);
77
78     view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/TableWidget.qml"));
79     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
80     view->setAlignment(Qt::AlignCenter);
81
82     mainLayout = new QVBoxLayout;
83     mainLayout->addWidget(view);
84     this->setLayout(mainLayout);
85
86     QGraphicsObject *rootObject = view->rootObject();
87
88     connect(rootObject,SIGNAL(dictLink(QString)),
89             this,SLOT(itemClicked(QString)));
90     connect(rootObject,SIGNAL(fromChange(QString)),
91             this,SLOT(fromChange(QString)));
92     connect(rootObject,SIGNAL(toChange(QString)),
93             this,SLOT(toChange(QString)));
94
95 #else
96
97     layout = new QVBoxLayout;
98     setLayout(layout);
99
100     checkBoxLayout = new QHBoxLayout;
101     layout->addLayout(checkBoxLayout);
102
103     langFrom = new QComboBox;
104     langTo = new QComboBox;
105
106     langFrom->setInsertPolicy(QComboBox::InsertAlphabetically);
107     langTo->setInsertPolicy(QComboBox::InsertAlphabetically);
108
109     langFromLabel = new QLabel(tr("From "));
110     langToLabel = new QLabel(tr("To "));
111
112     checkBoxLayout->addWidget(langFromLabel);
113     checkBoxLayout->addWidget(langFrom, 10);
114     checkBoxLayout->addWidget(langToLabel);
115     checkBoxLayout->addWidget(langTo, 10);
116
117     model = new DictsModel(dicts, this);
118
119     proxyModel = new DictsProxyModel;
120     proxyModel->setDynamicSortFilter(true);
121     proxyModel->setSourceModel(model);
122
123     treeView = new QTreeView;
124     treeView->setModel(proxyModel);
125     treeView->setRootIsDecorated(false);
126     treeView->setExpandsOnDoubleClick(false);
127
128     treeView->setSortingEnabled(true);
129     treeView->sortByColumn(0, Qt::AscendingOrder);
130
131     treeView->setWordWrap(true);
132
133     #ifndef Q_WS_MAEMO_5
134         treeView->resizeColumnToContents(0);
135         treeView->resizeColumnToContents(1);
136         treeView->setColumnWidth(2, 300);
137         treeView->resizeColumnToContents(3);
138     #else
139         treeView->setColumnWidth(0, 150);
140         treeView->setColumnWidth(1, 150);
141         treeView->setColumnWidth(2, 300);
142         treeView->setColumnWidth(3, 150);
143     #endif
144
145     layout->addWidget(treeView);
146
147     connect(langFrom, SIGNAL(currentIndexChanged(int)),
148             this, SLOT(refreshDictList()));
149
150     connect(langTo, SIGNAL(currentIndexChanged(int)),
151             this, SLOT(refreshDictList()));
152
153     connect(treeView, SIGNAL(activated(QModelIndex)),
154             this, SLOT(itemClicked(QModelIndex)));
155
156     #ifndef Q_WS_MAEMO_5
157         setMinimumSize(800,500);
158     #else
159         setMinimumHeight(350);
160     #endif
161
162     initializeDicts();
163     #endif
164 }
165
166 void XdxfDictSelectDialog::toChange(QString lang){
167     if(lang!=tr("Any"))
168         proxyModel2->setTo(lang);
169     else
170         proxyModel2->setTo("");
171 }
172
173 void XdxfDictSelectDialog::fromChange(QString lang){
174     if(lang!=tr("Any"))
175         proxyModel2->setFrom(lang);
176     else
177         proxyModel2->setFrom("");
178 }
179
180 void XdxfDictSelectDialog::initializeDicts() {
181     //scan of all languages of dictionaries, using QSet to get only distinct languages
182     QSet<QString> languagesFrom;
183     QSet<QString> languagesTo;
184
185     for(int i=0; i < model->rowCount(QModelIndex()); i++) {
186         languagesFrom.insert(
187                 model->data(model->index(i, 0, QModelIndex())).toString());
188         languagesTo.insert(
189                 model->data(model->index(i, 1, QModelIndex())).toString());
190     }
191
192     //removes one dictionary which from and to languages are empty....
193     //bug in site with dictionaries
194     languagesFrom.remove(QString());
195     languagesTo.remove(QString());
196
197     //sorting of found languages
198     QList<QString> langFromList = languagesFrom.toList();
199     qSort(langFromList);
200
201     QList<QString> langToList = languagesTo.toList();
202     qSort(langToList);
203
204     //and adding them to combobox, first item in each combobox is "Any"
205     langFrom->addItem(tr("Any"));
206     for(int i=0; i < langFromList.count(); i++) {
207          langFrom->addItem(langFromList.at(i));
208     }
209
210     langTo->addItem(tr("Any"));
211     for(int i=0; i < langToList.count(); i++) {
212          langTo->addItem(langToList.at(i));
213     }
214 }
215
216
217 void XdxfDictSelectDialog::refreshDictList() {
218 #ifndef Q_WS_MAEMO_5
219
220
221 #else
222
223     //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.
224     if(langTo->currentIndex() == 0)
225         proxyModel->setTo(QString());
226     else
227         proxyModel->setTo(langTo->currentText());
228
229     if(langFrom->currentIndex() == 0)
230         proxyModel->setFrom(QString());
231     else
232         proxyModel->setFrom(langFrom->currentText());
233
234 #endif
235 }
236
237
238 void XdxfDictSelectDialog::itemClicked(QString link) {
239 #ifndef Q_WS_MAEMO_5
240     _link = link;
241     accept();
242 #endif
243 }
244
245 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
246 #ifndef Q_WS_MAEMO_5
247     index;
248 #else
249     _link = index.model()->data(index, Qt::UserRole).toString();
250     accept();
251 #endif
252 }