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 _isBookmarkModeActive = false;
40 //creating new model to store words and stars
42 model = new QStandardItemModel(this);
45 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 //set our custom style to draw checkboxes as stars
48 proxyStyle = new WordListProxyStyle();
51 //for future use, set checkbox icons for maemo
52 // ctxt->setContextProperty("CheckedPath", "qrc:/icons/96x96/staron.png");
53 // ctxt->setContextProperty("UncheckedPath", "qrc:/icons/96x96/staroff.png");
56 listModel = new WordListModel(this);
58 verticalLayout = new QVBoxLayout;
59 setLayout(verticalLayout);
61 qmlView = new QDeclarativeView(this);
63 ctxt = qmlView->rootContext();
65 ctxt->setContextProperty("wordModel", &(*listModel));
66 ctxt->setContextProperty("CheckedPath", "qrc:/icons/16x16/staron.png");
67 ctxt->setContextProperty("UncheckedPath", "qrc:/icons/16x16/staroff.png");
69 qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/WordListWidget.qml"));
71 QGraphicsObject *rootObject = qmlView->rootObject();
73 qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
75 verticalLayout->addWidget(qmlView);
77 connect(rootObject, SIGNAL(wordSelected(QString)), this, SLOT(wordClicked(QString)));
78 connect(rootObject, SIGNAL(wordSelectedByIndex(int)), this, SLOT(wordClickedByIndex(int)));
79 connect(listModel, SIGNAL(addToBookmarks(QString)), this, SLOT(addToBookmarks(QString)));
80 connect(listModel, SIGNAL(removeFromBookmarks(QString)), this, SLOT(removeFromBookmarks(QString)));
82 connect(this, SIGNAL(setWordListState(QVariant)), rootObject, SLOT(setEnabled(QVariant)));
83 connect(this, SIGNAL(setWordListEmpty(QVariant)), rootObject, SLOT(setWordListEmpty(QVariant)));
84 connect(this, SIGNAL(setFocusOnQML()), rootObject, SLOT(setFocus()));
88 setHeaderHidden(true);
89 setRootIsDecorated(false);
91 //setting size of star in pixels, on maemo checboxes are much bigger
100 WordListWidget::~WordListWidget() {
107 void WordListWidget::addWord(QString word, int row) {
108 QStandardItem* item = new QStandardItem(word);
110 //we don't want to allow user to edit word
111 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
113 QStandardItem* itemCheckBox = new QStandardItem();
114 //creating checkbox item
115 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
116 Qt::ItemIsUserCheckable);
118 /*checking if word is already in bookmarks, information about that is
119 stored in its translation object (not all translations have to be in
121 bool bookmark = false;
123 foreach(t, searchResult[word]) {
124 if(t->isBookmark()) {
131 itemCheckBox->setCheckState(Qt::Checked);
133 itemCheckBox->setCheckState(Qt::Unchecked);
136 model->setItem(row,0, item);
137 model->setItem(row,1, itemCheckBox);
141 void WordListWidget::showSearchResults(
142 QHash<QString, QList<Translation *> > result) {
145 searchResult = result;
148 if(searchResult.count()>0) {
150 model->setColumnCount(2);
151 model->setRowCount(result.count());
154 QHash<QString, QList<Translation*> >::iterator i;
155 for(i = searchResult.begin(); i != searchResult.end(); i++) {
156 addWord(i.key(), row++);
163 QStandardItem* item = new QStandardItem(tr("Can't find any matching words"));
164 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
165 item->setTextAlignment(Qt::AlignCenter);
168 model->setItem(0,item);
172 QHash<QString, bool> wordsInBookmarks;
173 QHashIterator<QString, QList<Translation *> > i(result);
177 bool bookmark = false;
179 foreach(t, searchResult[i.key()]) {
180 if(t->isBookmark()) {
185 wordsInBookmarks.insert(i.key(), bookmark);
188 if (result.count() == 0){
189 //result.insert("!@#$%", QList<Translation*>());
190 // wordsInBookmarks.insert("!@#$%", false);
191 Q_EMIT setWordListState(false);
192 Q_EMIT setWordListEmpty(true);
194 Q_EMIT setWordListEmpty(false);
198 listModel = new WordListModel(this);
201 listModel->setBookmarkModeActive(_isBookmarkModeActive);
202 _isBookmarkModeActive = false;
203 listModel->setTranslations(result, wordsInBookmarks);
211 void WordListWidget::wordClicked(QModelIndex index) {
212 //we're getting translation based on data in index
213 Q_EMIT showTranslation(
214 searchResult[index.data().toString()]);
217 void WordListWidget::wordChecked(QModelIndex index) {
219 //save new item state
220 Qt::CheckState state =
221 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
224 //getting index of item which contains word which should be added/removed
226 QModelIndex item = selectedIndexes().at(0);
227 if(!item.isValid()) return;
229 //to shorten lag between clicking on a star and its change
232 //depending on new state emit suitable signal
233 if(state == Qt::Checked) {
234 Q_EMIT addBookmark(searchResult[item.data().toString()]);
237 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
240 bool onlyBookmarks = true;
241 foreach(t, searchResult[item.data().toString()]) {
242 if(t->isBookmark() == 1 || t->isBookmark()==0) {
244 onlyBookmarks = false;
248 searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
253 searchResult.remove(item.data().toString());
254 model->removeRow(item.row());
259 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
261 //firstly we normally handle this event
262 QTreeView::mouseReleaseEvent(event);
264 //then we check at which item user clicked
265 QModelIndex index = indexAt(event->pos());
266 if(!index.isValid()) return;
268 /*if there are no selected items we return, that occurs sometimes
269 on maemo, when user is scrolling list and clicks to stop the scroll,
270 system doesn't select item but emits mouseReleaseEvent*/
271 if(selectedIndexes().count() == 0) return;
273 //if user doesn't click either on a word or on a star, return
274 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
277 int c = index.column();
279 //if column is 0 user clicked on a word
282 //else user clicked on a star
286 void WordListWidget::resizeEvent(QResizeEvent *event) {
288 QTreeView::resizeEvent(event);
291 void WordListWidget::keyPressEvent(QKeyEvent *event) {
292 QTreeView::keyPressEvent(event);
294 if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
295 if(selectedIndexes().count() == 0) return;
297 wordClicked(selectedIndexes().at(0));
301 void WordListWidget::wordClicked(QString word){
302 emit showTranslation(searchResult[word]);
305 void WordListWidget::addToBookmarks(QString word){
306 emit addBookmark(searchResult[word]);
309 void WordListWidget::removeFromBookmarks(QString word){
310 emit removeBookmark(searchResult[word]);
313 void WordListWidget::wordClickedByIndex(int index){
314 emit showTranslation(searchResult[listModel->wordOnPosition(index)]);
317 void WordListWidget::setFocusOnElement(){
320 Q_EMIT setFocusOnQML();
325 void WordListWidget::resizeColumns() {
326 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
327 setColumnWidth(1, checkBoxWidth);
330 void WordListWidget::lockList() {
334 Q_EMIT setWordListState(false);
338 void WordListWidget::unlockList() {
342 Q_EMIT setWordListState(true);
346 void WordListWidget::clear() {
355 QHash<QString, QList<Translation*> >::iterator i;
356 for(i = searchResult.begin(); i != searchResult.end(); i++) {
358 foreach(t, i.value()) {
362 searchResult.clear();
365 void WordListWidget::bookmarkModeActive(){
366 _isBookmarkModeActive = true;