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)));
83 connect(this, SIGNAL(setFocusOnQML()), rootObject, SLOT(setFocus()));
87 setHeaderHidden(true);
88 setRootIsDecorated(false);
90 //setting size of star in pixels, on maemo checboxes are much bigger
99 WordListWidget::~WordListWidget() {
104 void WordListWidget::setFocusOnElement(){
107 Q_EMIT setFocusOnQML();
108 qDebug()<<hasFocus()<<" "<<qmlView->hasFocus();
111 void WordListWidget::addWord(QString word, int row) {
112 QStandardItem* item = new QStandardItem(word);
114 //we don't want to allow user to edit word
115 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
117 QStandardItem* itemCheckBox = new QStandardItem();
118 //creating checkbox item
119 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
120 Qt::ItemIsUserCheckable);
122 /*checking if word is already in bookmarks, information about that is
123 stored in its translation object (not all translations have to be in
125 bool bookmark = false;
127 foreach(t, searchResult[word]) {
128 if(t->isBookmark()) {
135 itemCheckBox->setCheckState(Qt::Checked);
137 itemCheckBox->setCheckState(Qt::Unchecked);
140 model->setItem(row,0, item);
141 model->setItem(row,1, itemCheckBox);
145 void WordListWidget::showSearchResults(
146 QHash<QString, QList<Translation *> > result) {
149 searchResult = result;
152 if(searchResult.count()>0) {
154 model->setColumnCount(2);
155 model->setRowCount(result.count());
158 QHash<QString, QList<Translation*> >::iterator i;
159 for(i = searchResult.begin(); i != searchResult.end(); i++) {
160 addWord(i.key(), row++);
167 QStandardItem* item = new QStandardItem(tr("Can't find any matching words"));
168 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
169 item->setTextAlignment(Qt::AlignCenter);
172 model->setItem(0,item);
176 QHash<QString, bool> wordsInBookmarks;
177 QHashIterator<QString, QList<Translation *> > i(result);
181 bool bookmark = false;
183 foreach(t, searchResult[i.key()]) {
184 if(t->isBookmark()) {
189 wordsInBookmarks.insert(i.key(), bookmark);
192 if (result.count() == 0){
193 //result.insert("!@#$%", QList<Translation*>());
194 // wordsInBookmarks.insert("!@#$%", false);
195 Q_EMIT setWordListState(false);
196 Q_EMIT setWordListEmpty(true);
198 Q_EMIT setWordListEmpty(false);
202 listModel = new WordListModel(this);
205 listModel->setBookmarkModeActive(_isBookmarkModeActive);
206 _isBookmarkModeActive = false;
207 listModel->setTranslations(result, wordsInBookmarks);
215 void WordListWidget::wordClicked(QModelIndex index) {
216 //we're getting translation based on data in index
217 Q_EMIT showTranslation(
218 searchResult[index.data().toString()]);
221 void WordListWidget::wordChecked(QModelIndex index) {
223 //save new item state
224 Qt::CheckState state =
225 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
228 //getting index of item which contains word which should be added/removed
230 QModelIndex item = selectedIndexes().at(0);
231 if(!item.isValid()) return;
233 //to shorten lag between clicking on a star and its change
236 //depending on new state emit suitable signal
237 if(state == Qt::Checked) {
238 Q_EMIT addBookmark(searchResult[item.data().toString()]);
241 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
244 bool onlyBookmarks = true;
245 foreach(t, searchResult[item.data().toString()]) {
246 if(t->isBookmark() == 1 || t->isBookmark()==0) {
248 onlyBookmarks = false;
252 searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
257 searchResult.remove(item.data().toString());
258 model->removeRow(item.row());
263 void WordListWidget::wordClicked(QString word){
264 emit showTranslation(searchResult[word]);
267 void WordListWidget::addToBookmarks(QString word){
268 emit addBookmark(searchResult[word]);
271 void WordListWidget::removeFromBookmarks(QString word){
272 emit removeBookmark(searchResult[word]);
279 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
281 //firstly we normally handle this event
282 QTreeView::mouseReleaseEvent(event);
284 //then we check at which item user clicked
285 QModelIndex index = indexAt(event->pos());
286 if(!index.isValid()) return;
288 /*if there are no selected items we return, that occurs sometimes
289 on maemo, when user is scrolling list and clicks to stop the scroll,
290 system doesn't select item but emits mouseReleaseEvent*/
291 if(selectedIndexes().count() == 0) return;
293 //if user doesn't click either on a word or on a star, return
294 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
297 int c = index.column();
299 //if column is 0 user clicked on a word
302 //else user clicked on a star
306 void WordListWidget::resizeEvent(QResizeEvent *event) {
308 QTreeView::resizeEvent(event);
311 void WordListWidget::keyPressEvent(QKeyEvent *event) {
312 QTreeView::keyPressEvent(event);
314 if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
315 if(selectedIndexes().count() == 0) return;
317 wordClicked(selectedIndexes().at(0));
322 void WordListWidget::resizeColumns() {
323 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
324 setColumnWidth(1, checkBoxWidth);
327 void WordListWidget::lockList() {
331 Q_EMIT setWordListState(false);
335 void WordListWidget::unlockList() {
339 Q_EMIT setWordListState(true);
343 void WordListWidget::clear() {
352 QHash<QString, QList<Translation*> >::iterator i;
353 for(i = searchResult.begin(); i != searchResult.end(); i++) {
355 foreach(t, i.value()) {
359 searchResult.clear();
362 void WordListWidget::bookmarkModeActive(){
363 _isBookmarkModeActive = true;