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,
36 setWindowTitle(tr("Select dictionary"));
38 //here removing already added dictionary
39 for (int i = 0; i < dicts.size(); i++){
40 if(QFile::exists(QDir::homePath() + "/.mdictionary/" + dicts[i].title() + ".xdxf")){
47 model2 = new DictsListModel(dicts, this);
48 proxyModel2 = new DictsProxyListModel;
49 proxyModel2->setSourceModel(model2);
50 proxyModel2->setTo("Polish");
51 view= new QDeclarativeView();
52 QDeclarativeContext* ctxt=view->rootContext();
53 ctxt->setContextProperty("dictModel", proxyModel2);
54 view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/TableWidget.qml"));
55 view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
56 view->setAlignment(Qt::AlignCenter);
58 mainLayout = new QVBoxLayout;
59 mainLayout->addWidget(view);
60 this->setLayout(mainLayout);
61 proxyModel2->setTo("English");
63 QGraphicsObject *rootObject = view->rootObject();
65 connect(rootObject,SIGNAL(dictLink(QString)),
66 this,SLOT(itemClicked(QString)));
69 layout = new QVBoxLayout;
72 checkBoxLayout = new QHBoxLayout;
73 layout->addLayout(checkBoxLayout);
75 langFrom = new QComboBox;
76 langTo = new QComboBox;
78 langFrom->setInsertPolicy(QComboBox::InsertAlphabetically);
79 langTo->setInsertPolicy(QComboBox::InsertAlphabetically);
81 langFromLabel = new QLabel(tr("From "));
82 langToLabel = new QLabel(tr("To "));
84 checkBoxLayout->addWidget(langFromLabel);
85 checkBoxLayout->addWidget(langFrom, 10);
86 checkBoxLayout->addWidget(langToLabel);
87 checkBoxLayout->addWidget(langTo, 10);
89 model = new DictsModel(dicts, this);
91 proxyModel = new DictsProxyModel;
92 proxyModel->setDynamicSortFilter(true);
93 proxyModel->setSourceModel(model);
95 treeView = new QTreeView;
96 treeView->setModel(proxyModel);
97 treeView->setRootIsDecorated(false);
98 treeView->setExpandsOnDoubleClick(false);
100 treeView->setSortingEnabled(true);
101 treeView->sortByColumn(0, Qt::AscendingOrder);
103 treeView->setWordWrap(true);
106 treeView->resizeColumnToContents(0);
107 treeView->resizeColumnToContents(1);
108 treeView->setColumnWidth(2, 300);
109 treeView->resizeColumnToContents(3);
111 treeView->setColumnWidth(0, 150);
112 treeView->setColumnWidth(1, 150);
113 treeView->setColumnWidth(2, 300);
114 treeView->setColumnWidth(3, 150);
117 layout->addWidget(treeView);
119 connect(langFrom, SIGNAL(currentIndexChanged(int)),
120 this, SLOT(refreshDictList()));
122 connect(langTo, SIGNAL(currentIndexChanged(int)),
123 this, SLOT(refreshDictList()));
125 connect(treeView, SIGNAL(activated(QModelIndex)),
126 this, SLOT(itemClicked(QModelIndex)));
129 setMinimumSize(800,500);
131 setMinimumHeight(350);
139 void XdxfDictSelectDialog::initializeDicts() {
140 //scan of all languages of dictionaries, using QSet to get only distinct languages
141 QSet<QString> languagesFrom;
142 QSet<QString> languagesTo;
144 for(int i=0; i < model->rowCount(QModelIndex()); i++) {
145 languagesFrom.insert(
146 model->data(model->index(i, 0, QModelIndex())).toString());
148 model->data(model->index(i, 1, QModelIndex())).toString());
151 //removes one dictionary which from and to languages are empty....
152 //bug in site with dictionaries
153 languagesFrom.remove(QString());
154 languagesTo.remove(QString());
156 //sorting of found languages
157 QList<QString> langFromList = languagesFrom.toList();
160 QList<QString> langToList = languagesTo.toList();
163 //and adding them to combobox, first item in each combobox is "Any"
164 langFrom->addItem(tr("Any"));
165 for(int i=0; i < langFromList.count(); i++) {
166 langFrom->addItem(langFromList.at(i));
169 langTo->addItem(tr("Any"));
170 for(int i=0; i < langToList.count(); i++) {
171 langTo->addItem(langToList.at(i));
176 void XdxfDictSelectDialog::refreshDictList() {
182 //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.
183 if(langTo->currentIndex() == 0)
184 proxyModel->setTo(QString());
186 proxyModel->setTo(langTo->currentText());
188 if(langFrom->currentIndex() == 0)
189 proxyModel->setFrom(QString());
191 proxyModel->setFrom(langFrom->currentText());
197 void XdxfDictSelectDialog::itemClicked(QString link) {
204 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
206 _link = index.model()->data(index, Qt::UserRole).toString();