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(listModel, SIGNAL(addToBookmarks(QString)), this, SLOT(addToBookmarks(QString)));
79 connect(listModel, SIGNAL(removeFromBookmarks(QString)), this, SLOT(removeFromBookmarks(QString)));
81 connect(this, SIGNAL(setWordListState(QVariant)), rootObject, SLOT(setEnabled(QVariant)));
82 connect(this, SIGNAL(setWordListEmpty(QVariant)), rootObject, SLOT(setWordListEmpty(QVariant)));
86 setHeaderHidden(true);
87 setRootIsDecorated(false);
89 //setting size of star in pixels, on maemo checboxes are much bigger
98 WordListWidget::~WordListWidget() {
103 void WordListWidget::addWord(QString word, int row) {
104 QStandardItem* item = new QStandardItem(word);
106 //we don't want to allow user to edit word
107 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
109 QStandardItem* itemCheckBox = new QStandardItem();
110 //creating checkbox item
111 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
112 Qt::ItemIsUserCheckable);
114 /*checking if word is already in bookmarks, information about that is
115 stored in its translation object (not all translations have to be in
117 bool bookmark = false;
119 foreach(t, searchResult[word]) {
120 if(t->isBookmark()) {
127 itemCheckBox->setCheckState(Qt::Checked);
129 itemCheckBox->setCheckState(Qt::Unchecked);
132 model->setItem(row,0, item);
133 model->setItem(row,1, itemCheckBox);
137 void WordListWidget::showSearchResults(
138 QHash<QString, QList<Translation *> > result) {
141 searchResult = result;
144 if(searchResult.count()>0) {
146 model->setColumnCount(2);
147 model->setRowCount(result.count());
150 QHash<QString, QList<Translation*> >::iterator i;
151 for(i = searchResult.begin(); i != searchResult.end(); i++) {
152 addWord(i.key(), row++);
159 QStandardItem* item = new QStandardItem(tr("Can't find any matching words"));
160 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
161 item->setTextAlignment(Qt::AlignCenter);
164 model->setItem(0,item);
168 QHash<QString, bool> wordsInBookmarks;
169 QHashIterator<QString, QList<Translation *> > i(result);
173 bool bookmark = false;
175 foreach(t, searchResult[i.key()]) {
176 if(t->isBookmark()) {
181 wordsInBookmarks.insert(i.key(), bookmark);
184 if (result.count() == 0){
185 //result.insert("!@#$%", QList<Translation*>());
186 // wordsInBookmarks.insert("!@#$%", false);
187 Q_EMIT setWordListState(false);
188 Q_EMIT setWordListEmpty(true);
190 Q_EMIT setWordListEmpty(false);
194 listModel = new WordListModel(this);
197 listModel->setBookmarkModeActive(_isBookmarkModeActive);
198 _isBookmarkModeActive = false;
199 listModel->setTranslations(result, wordsInBookmarks);
207 void WordListWidget::wordClicked(QModelIndex index) {
208 //we're getting translation based on data in index
209 Q_EMIT showTranslation(
210 searchResult[index.data().toString()]);
213 void WordListWidget::wordChecked(QModelIndex index) {
215 //save new item state
216 Qt::CheckState state =
217 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
220 //getting index of item which contains word which should be added/removed
222 QModelIndex item = selectedIndexes().at(0);
223 if(!item.isValid()) return;
225 //to shorten lag between clicking on a star and its change
228 //depending on new state emit suitable signal
229 if(state == Qt::Checked) {
230 Q_EMIT addBookmark(searchResult[item.data().toString()]);
233 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
236 bool onlyBookmarks = true;
237 foreach(t, searchResult[item.data().toString()]) {
238 if(t->isBookmark() == 1 || t->isBookmark()==0) {
240 onlyBookmarks = false;
244 searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
249 searchResult.remove(item.data().toString());
250 model->removeRow(item.row());
255 void WordListWidget::wordClicked(QString word){
256 emit showTranslation(searchResult[word]);
259 void WordListWidget::addToBookmarks(QString word){
260 emit addBookmark(searchResult[word]);
263 void WordListWidget::removeFromBookmarks(QString word){
264 emit removeBookmark(searchResult[word]);
271 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
273 //firstly we normally handle this event
274 QTreeView::mouseReleaseEvent(event);
276 //then we check at which item user clicked
277 QModelIndex index = indexAt(event->pos());
278 if(!index.isValid()) return;
280 /*if there are no selected items we return, that occurs sometimes
281 on maemo, when user is scrolling list and clicks to stop the scroll,
282 system doesn't select item but emits mouseReleaseEvent*/
283 if(selectedIndexes().count() == 0) return;
285 //if user doesn't click either on a word or on a star, return
286 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
289 int c = index.column();
291 //if column is 0 user clicked on a word
294 //else user clicked on a star
298 void WordListWidget::resizeEvent(QResizeEvent *event) {
300 QTreeView::resizeEvent(event);
303 void WordListWidget::keyPressEvent(QKeyEvent *event) {
304 QTreeView::keyPressEvent(event);
306 if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
307 if(selectedIndexes().count() == 0) return;
309 wordClicked(selectedIndexes().at(0));
314 void WordListWidget::resizeColumns() {
315 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
316 setColumnWidth(1, checkBoxWidth);
319 void WordListWidget::lockList() {
323 Q_EMIT setWordListState(false);
327 void WordListWidget::unlockList() {
331 Q_EMIT setWordListState(true);
335 void WordListWidget::clear() {
344 QHash<QString, QList<Translation*> >::iterator i;
345 for(i = searchResult.begin(); i != searchResult.end(); i++) {
347 foreach(t, i.value()) {
351 searchResult.clear();
354 void WordListWidget::bookmarkModeActive(){
355 _isBookmarkModeActive = true;