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)));
85 setHeaderHidden(true);
86 setRootIsDecorated(false);
88 //setting size of star in pixels, on maemo checboxes are much bigger
97 WordListWidget::~WordListWidget() {
102 void WordListWidget::addWord(QString word, int row) {
103 QStandardItem* item = new QStandardItem(word);
105 //we don't want to allow user to edit word
106 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
108 QStandardItem* itemCheckBox = new QStandardItem();
109 //creating checkbox item
110 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
111 Qt::ItemIsUserCheckable);
113 /*checking if word is already in bookmarks, information about that is
114 stored in its translation object (not all translations have to be in
116 bool bookmark = false;
118 foreach(t, searchResult[word]) {
119 if(t->isBookmark()) {
126 itemCheckBox->setCheckState(Qt::Checked);
128 itemCheckBox->setCheckState(Qt::Unchecked);
131 model->setItem(row,0, item);
132 model->setItem(row,1, itemCheckBox);
136 void WordListWidget::showSearchResults(
137 QHash<QString, QList<Translation *> > result) {
140 searchResult = result;
143 if(searchResult.count()>0) {
145 model->setColumnCount(2);
146 model->setRowCount(result.count());
149 QHash<QString, QList<Translation*> >::iterator i;
150 for(i = searchResult.begin(); i != searchResult.end(); i++) {
151 addWord(i.key(), row++);
158 QStandardItem* item = new QStandardItem(tr("Can't find any matching words"));
159 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
160 item->setTextAlignment(Qt::AlignCenter);
163 model->setItem(0,item);
167 QHash<QString, bool> wordsInBookmarks;
168 QHashIterator<QString, QList<Translation *> > i(result);
172 bool bookmark = false;
174 foreach(t, searchResult[i.key()]) {
175 if(t->isBookmark()) {
180 wordsInBookmarks.insert(i.key(), bookmark);
183 if (result.count() == 0){
184 result.insert("!@#$%", QList<Translation*>());
185 wordsInBookmarks.insert("!@#$%", false);
186 Q_EMIT setWordListState(false);
190 listModel = new WordListModel(this);
193 listModel->setBookmarkModeActive(isBookmarkModeActive);
194 isBookmarkModeActive = false;
195 listModel->setTranslations(result, wordsInBookmarks);
203 void WordListWidget::wordClicked(QModelIndex index) {
204 //we're getting translation based on data in index
205 Q_EMIT showTranslation(
206 searchResult[index.data().toString()]);
209 void WordListWidget::wordChecked(QModelIndex index) {
211 //save new item state
212 Qt::CheckState state =
213 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
216 //getting index of item which contains word which should be added/removed
218 QModelIndex item = selectedIndexes().at(0);
219 if(!item.isValid()) return;
221 //to shorten lag between clicking on a star and its change
224 //depending on new state emit suitable signal
225 if(state == Qt::Checked) {
226 Q_EMIT addBookmark(searchResult[item.data().toString()]);
229 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
232 bool onlyBookmarks = true;
233 foreach(t, searchResult[item.data().toString()]) {
234 if(t->isBookmark() == 1 || t->isBookmark()==0) {
236 onlyBookmarks = false;
240 searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
245 searchResult.remove(item.data().toString());
246 model->removeRow(item.row());
251 void WordListWidget::wordClicked(QString word){
252 emit showTranslation(searchResult[word]);
255 void WordListWidget::addToBookmarks(QString word){
256 emit addBookmark(searchResult[word]);
259 void WordListWidget::removeFromBookmarks(QString word){
260 emit removeBookmark(searchResult[word]);
267 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
269 //firstly we normally handle this event
270 QTreeView::mouseReleaseEvent(event);
272 //then we check at which item user clicked
273 QModelIndex index = indexAt(event->pos());
274 if(!index.isValid()) return;
276 /*if there are no selected items we return, that occurs sometimes
277 on maemo, when user is scrolling list and clicks to stop the scroll,
278 system doesn't select item but emits mouseReleaseEvent*/
279 if(selectedIndexes().count() == 0) return;
281 //if user doesn't click either on a word or on a star, return
282 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
285 int c = index.column();
287 //if column is 0 user clicked on a word
290 //else user clicked on a star
294 void WordListWidget::resizeEvent(QResizeEvent *event) {
296 QTreeView::resizeEvent(event);
299 void WordListWidget::keyPressEvent(QKeyEvent *event) {
300 QTreeView::keyPressEvent(event);
302 if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
303 if(selectedIndexes().count() == 0) return;
305 wordClicked(selectedIndexes().at(0));
310 void WordListWidget::resizeColumns() {
311 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
312 setColumnWidth(1, checkBoxWidth);
315 void WordListWidget::lockList() {
319 Q_EMIT setWordListState(false);
323 void WordListWidget::unlockList() {
327 Q_EMIT setWordListState(true);
331 void WordListWidget::clear() {
340 QHash<QString, QList<Translation*> >::iterator i;
341 for(i = searchResult.begin(); i != searchResult.end(); i++) {
343 foreach(t, i.value()) {
347 searchResult.clear();
350 void WordListWidget::bookmarkModeActive(){
351 isBookmarkModeActive = true;