2 /*******************************************************************************
4 This file is part of mDictionary.
6 mDictionary is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 mDictionary is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with mDictionary. If not, see <http://www.gnu.org/licenses/>.
19 Copyright 2010 Comarch S.A.
21 *******************************************************************************/
23 /*! \file WordListwidget.cpp
24 \brief Displays list of words found in dictionaries
26 \author Mateusz Półrola <mateusz.polrola@comarch.pl>
29 #include "WordListWidget.h"
30 #include "WordListProxyStyle.h"
31 #include "../../include/translation.h"
35 WordListWidget::WordListWidget(QWidget *parent):
38 //creating new model to store words and stars
40 model = new QStandardItemModel(this);
43 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 //set our custom style to draw checkboxes as stars
46 proxyStyle = new WordListProxyStyle();
49 //for future use, set checkbox icons for maemo
50 // ctxt->setContextProperty("CheckedPath", "qrc:/icons/96x96/staron.png");
51 // ctxt->setContextProperty("UncheckedPath", "qrc:/icons/96x96/staroff.png");
54 listModel = new WordListModel(this);
56 verticalLayout = new QVBoxLayout;
57 setLayout(verticalLayout);
59 qmlView = new QDeclarativeView(this);
61 ctxt = qmlView->rootContext();
63 ctxt->setContextProperty("wordModel", &(*listModel));
64 ctxt->setContextProperty("CheckedPath", "qrc:/icons/16x16/staron.png");
65 ctxt->setContextProperty("UncheckedPath", "qrc:/icons/16x16/staroff.png");
67 qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/WordListWidget.qml"));
69 QGraphicsObject *rootObject = qmlView->rootObject();
71 qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
73 verticalLayout->addWidget(qmlView);
75 connect(rootObject, SIGNAL(wordSelected(QString)), this, SLOT(wordClicked(QString)));
76 connect(listModel, SIGNAL(addToBookmarks(QString)), this, SLOT(addToBookmarks(QString)));
77 connect(listModel, SIGNAL(removeFromBookmarks(QString)), this, SLOT(removeFromBookmarks(QString)));
79 connect(this, SIGNAL(setWordListState(QVariant)), rootObject, SLOT(setEnabled(QVariant)));
83 setHeaderHidden(true);
84 setRootIsDecorated(false);
86 //setting size of star in pixels, on maemo checboxes are much bigger
95 WordListWidget::~WordListWidget() {
100 void WordListWidget::addWord(QString word, int row) {
101 QStandardItem* item = new QStandardItem(word);
103 //we don't want to allow user to edit word
104 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
106 QStandardItem* itemCheckBox = new QStandardItem();
107 //creating checkbox item
108 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
109 Qt::ItemIsUserCheckable);
111 /*checking if word is already in bookmarks, information about that is
112 stored in its translation object (not all translations have to be in
114 bool bookmark = false;
116 foreach(t, searchResult[word]) {
117 if(t->isBookmark()) {
124 itemCheckBox->setCheckState(Qt::Checked);
126 itemCheckBox->setCheckState(Qt::Unchecked);
129 model->setItem(row,0, item);
130 model->setItem(row,1, itemCheckBox);
134 void WordListWidget::showSearchResults(
135 QHash<QString, QList<Translation *> > result) {
138 searchResult = result;
141 if(searchResult.count()>0) {
143 model->setColumnCount(2);
144 model->setRowCount(result.count());
147 QHash<QString, QList<Translation*> >::iterator i;
148 for(i = searchResult.begin(); i != searchResult.end(); i++) {
149 addWord(i.key(), row++);
156 QStandardItem* item = new QStandardItem(tr("Can't find any matching words"));
157 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
158 item->setTextAlignment(Qt::AlignCenter);
161 model->setItem(0,item);
165 QHash<QString, bool> wordsInBookmarks;
166 QHashIterator<QString, QList<Translation *> > i(result);
170 bool bookmark = false;
172 foreach(t, searchResult[i.key()]) {
173 if(t->isBookmark()) {
178 wordsInBookmarks.insert(i.key(), bookmark);
181 if (result.count() == 0){
182 result.insert("!@#$%", QList<Translation*>());
183 wordsInBookmarks.insert("!@#$%", false);
184 Q_EMIT setWordListState(false);
188 listModel = new WordListModel(this);
190 listModel->setTranslations(result, wordsInBookmarks);
198 void WordListWidget::wordClicked(QModelIndex index) {
199 //we're getting translation based on data in index
200 Q_EMIT showTranslation(
201 searchResult[index.data().toString()]);
204 void WordListWidget::wordChecked(QModelIndex index) {
206 //save new item state
207 Qt::CheckState state =
208 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
211 //getting index of item which contains word which should be added/removed
213 QModelIndex item = selectedIndexes().at(0);
214 if(!item.isValid()) return;
216 //to shorten lag between clicking on a star and its change
219 //depending on new state emit suitable signal
220 if(state == Qt::Checked) {
221 Q_EMIT addBookmark(searchResult[item.data().toString()]);
224 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
227 bool onlyBookmarks = true;
228 foreach(t, searchResult[item.data().toString()]) {
229 if(t->isBookmark() == 1 || t->isBookmark()==0) {
231 onlyBookmarks = false;
235 searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
240 searchResult.remove(item.data().toString());
241 model->removeRow(item.row());
246 void WordListWidget::wordClicked(QString word){
247 emit showTranslation(searchResult[word]);
250 void WordListWidget::addToBookmarks(QString word){
251 emit addBookmark(searchResult[word]);
254 void WordListWidget::removeFromBookmarks(QString word){
255 emit removeBookmark(searchResult[word]);
262 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
264 //firstly we normally handle this event
265 QTreeView::mouseReleaseEvent(event);
267 //then we check at which item user clicked
268 QModelIndex index = indexAt(event->pos());
269 if(!index.isValid()) return;
271 /*if there are no selected items we return, that occurs sometimes
272 on maemo, when user is scrolling list and clicks to stop the scroll,
273 system doesn't select item but emits mouseReleaseEvent*/
274 if(selectedIndexes().count() == 0) return;
276 //if user doesn't click either on a word or on a star, return
277 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
280 int c = index.column();
282 //if column is 0 user clicked on a word
285 //else user clicked on a star
289 void WordListWidget::resizeEvent(QResizeEvent *event) {
291 QTreeView::resizeEvent(event);
294 void WordListWidget::keyPressEvent(QKeyEvent *event) {
295 QTreeView::keyPressEvent(event);
297 if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
298 if(selectedIndexes().count() == 0) return;
300 wordClicked(selectedIndexes().at(0));
305 void WordListWidget::resizeColumns() {
306 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
307 setColumnWidth(1, checkBoxWidth);
310 void WordListWidget::lockList() {
314 Q_EMIT setWordListState(false);
318 void WordListWidget::unlockList() {
322 Q_EMIT setWordListState(true);
326 void WordListWidget::clear() {
335 QHash<QString, QList<Translation*> >::iterator i;
336 for(i = searchResult.begin(); i != searchResult.end(); i++) {
338 foreach(t, i.value()) {
342 searchResult.clear();