table view
[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
36     setWindowTitle(tr("Select dictionary"));
37
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")){
41             dicts.removeAt(i);
42             i--;
43         }
44     }
45
46 #ifndef Q_WS_MAEMO_5
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);
57
58     mainLayout = new QVBoxLayout;
59     mainLayout->addWidget(view);
60     this->setLayout(mainLayout);
61     proxyModel2->setTo("English");
62
63     QGraphicsObject *rootObject = view->rootObject();
64
65     connect(rootObject,SIGNAL(dictLink(QString)),
66             this,SLOT(itemClicked(QString)));
67 #else
68
69     layout = new QVBoxLayout;
70     setLayout(layout);
71
72     checkBoxLayout = new QHBoxLayout;
73     layout->addLayout(checkBoxLayout);
74
75     langFrom = new QComboBox;
76     langTo = new QComboBox;
77
78     langFrom->setInsertPolicy(QComboBox::InsertAlphabetically);
79     langTo->setInsertPolicy(QComboBox::InsertAlphabetically);
80
81     langFromLabel = new QLabel(tr("From "));
82     langToLabel = new QLabel(tr("To "));
83
84     checkBoxLayout->addWidget(langFromLabel);
85     checkBoxLayout->addWidget(langFrom, 10);
86     checkBoxLayout->addWidget(langToLabel);
87     checkBoxLayout->addWidget(langTo, 10);
88
89     model = new DictsModel(dicts, this);
90
91     proxyModel = new DictsProxyModel;
92     proxyModel->setDynamicSortFilter(true);
93     proxyModel->setSourceModel(model);
94
95     treeView = new QTreeView;
96     treeView->setModel(proxyModel);
97     treeView->setRootIsDecorated(false);
98     treeView->setExpandsOnDoubleClick(false);
99
100     treeView->setSortingEnabled(true);
101     treeView->sortByColumn(0, Qt::AscendingOrder);
102
103     treeView->setWordWrap(true);
104
105     #ifndef Q_WS_MAEMO_5
106         treeView->resizeColumnToContents(0);
107         treeView->resizeColumnToContents(1);
108         treeView->setColumnWidth(2, 300);
109         treeView->resizeColumnToContents(3);
110     #else
111         treeView->setColumnWidth(0, 150);
112         treeView->setColumnWidth(1, 150);
113         treeView->setColumnWidth(2, 300);
114         treeView->setColumnWidth(3, 150);
115     #endif
116
117     layout->addWidget(treeView);
118
119     connect(langFrom, SIGNAL(currentIndexChanged(int)),
120             this, SLOT(refreshDictList()));
121
122     connect(langTo, SIGNAL(currentIndexChanged(int)),
123             this, SLOT(refreshDictList()));
124
125     connect(treeView, SIGNAL(activated(QModelIndex)),
126             this, SLOT(itemClicked(QModelIndex)));
127
128     #ifndef Q_WS_MAEMO_5
129         setMinimumSize(800,500);
130     #else
131         setMinimumHeight(350);
132     #endif
133
134     initializeDicts();
135     #endif
136 }
137
138
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;
143
144     for(int i=0; i < model->rowCount(QModelIndex()); i++) {
145         languagesFrom.insert(
146                 model->data(model->index(i, 0, QModelIndex())).toString());
147         languagesTo.insert(
148                 model->data(model->index(i, 1, QModelIndex())).toString());
149     }
150
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());
155
156     //sorting of found languages
157     QList<QString> langFromList = languagesFrom.toList();
158     qSort(langFromList);
159
160     QList<QString> langToList = languagesTo.toList();
161     qSort(langToList);
162
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));
167     }
168
169     langTo->addItem(tr("Any"));
170     for(int i=0; i < langToList.count(); i++) {
171          langTo->addItem(langToList.at(i));
172     }
173 }
174
175
176 void XdxfDictSelectDialog::refreshDictList() {
177 #ifndef Q_WS_MAEMO_5
178
179
180 #else
181
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());
185     else
186         proxyModel->setTo(langTo->currentText());
187
188     if(langFrom->currentIndex() == 0)
189         proxyModel->setFrom(QString());
190     else
191         proxyModel->setFrom(langFrom->currentText());
192
193 #endif
194 }
195
196
197 void XdxfDictSelectDialog::itemClicked(QString link) {
198 #ifndef Q_WS_MAEMO_5
199     _link = link;
200     accept();
201 #endif
202 }
203
204 void XdxfDictSelectDialog::itemClicked(QModelIndex index) {
205 #ifdef Q_WS_MAEMO_5
206     _link = index.model()->data(index, Qt::UserRole).toString();
207     accept();
208 #endif
209 }