1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 \file XdxfDictSelectDialog.cpp
23 \brief Shows dialog with list of dictionaries allowing user
24 to select one of them.
26 \author Mateusz Półrola <mateusz.polrola@comarch.com>
29 #include "XdxfDictSelectDialog.h"
31 XdxfDictSelectDialog::XdxfDictSelectDialog(QList<DownloadDict> dicts,
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")){
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);
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();
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);
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();
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);
78 view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/TableWidget.qml"));
79 view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
80 view->setAlignment(Qt::AlignCenter);
82 mainLayout = new QVBoxLayout;
83 mainLayout->addWidget(view);
84 this->setLayout(mainLayout);
86 QGraphicsObject *rootObject = view->rootObject();
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)));
97 layout = new QVBoxLayout;
100 checkBoxLayout = new QHBoxLayout;
101 layout->addLayout(checkBoxLayout);
103 langFrom = new QComboBox;
104 langTo = new QComboBox;
106 langFrom->setInsertPolicy(QComboBox::InsertAlphabetically);
107 langTo->setInsertPolicy(QComboBox::InsertAlphabetically);
109 langFromLabel = new QLabel(tr("From "));
110 langToLabel = new QLabel(tr("To "));
112 checkBoxLayout->addWidget(langFromLabel);
113 checkBoxLayout->addWidget(langFrom, 10);
114 checkBoxLayout->addWidget(langToLabel);
115 checkBoxLayout->addWidget(langTo, 10);
117 model = new DictsModel(dicts, this);
119 proxyModel = new DictsProxyModel;
120 proxyModel->setDynamicSortFilter(true);
121 proxyModel->setSourceModel(model);
123 treeView = new QTreeView;
124 treeView->setModel(proxyModel);
125 treeView->setRootIsDecorated(false);
126 treeView->setExpandsOnDoubleClick(false);
128 treeView->setSortingEnabled(true);
129 treeView->sortByColumn(0, Qt::AscendingOrder);
131 treeView->setWordWrap(true);
134 treeView->resizeColumnToContents(0);
135 treeView->resizeColumnToContents(1);
136 treeView->setColumnWidth(2, 300);
137 treeView->resizeColumnToContents(3);
139 treeView->setColumnWidth(0, 150);
140 treeView->setColumnWidth(1, 150);
141 treeView->setColumnWidth(2, 300);
142 treeView->setColumnWidth(3, 150);
145 layout->addWidget(treeView);
147 connect(langFrom, SIGNAL(currentIndexChanged(int)),
148 this, SLOT(refreshDictList()));
150 connect(langTo, SIGNAL(currentIndexChanged(int)),
151 this, SLOT(refreshDictList()));
153 connect(treeView, SIGNAL(activated(QModelIndex)),
154 this, SLOT(itemClicked(QModelIndex)));
157 setMinimumSize(800,500);
159 setMinimumHeight(350);
166 void XdxfDictSelectDialog::toChange(QString lang){
168 proxyModel2->setTo(lang);
170 proxyModel2->setTo("");
173 void XdxfDictSelectDialog::fromChange(QString lang){
175 proxyModel2->setFrom(lang);
177 proxyModel2->setFrom("");
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;
185 for(int i=0; i < model->rowCount(QModelIndex()); i++) {
186 languagesFrom.insert(
187 model->data(model->index(i, 0, QModelIndex())).toString());
189 model->data(model->index(i, 1, QModelIndex())).toString());
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());
197 //sorting of found languages
198 QList<QString> langFromList = languagesFrom.toList();
201 QList<QString> langToList = languagesTo.toList();
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));
210 langTo->addItem(tr("Any"));
211 for(int i=0; i < langToList.count(); i++) {
212 langTo->addItem(langToList.at(i));
217 void XdxfDictSelectDialog::refreshDictList() {
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());
227 proxyModel->setTo(langTo->currentText());
229 if(langFrom->currentIndex() == 0)
230 proxyModel->setFrom(QString());
232 proxyModel->setFrom(langFrom->currentText());
238 void XdxfDictSelectDialog::itemClicked(QString link) {
245 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
249 _link = index.model()->data(index, Qt::UserRole).toString();