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 // refreshDictsList();
64 ctxt->setContextProperty("wordModel", &(*listModel));
65 ctxt->setContextProperty("CheckedPath", "qrc:/icons/16x16/staron.png");
66 ctxt->setContextProperty("UncheckedPath", "qrc:/icons/16x16/staroff.png");
68 qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/WordListWidget.qml"));
70 QGraphicsObject *rootObject = qmlView->rootObject();
72 qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
74 verticalLayout->addWidget(qmlView);
76 connect(rootObject, SIGNAL(wordSelected(QString)), this, SLOT(wordClicked(QString)));
77 connect(listModel, SIGNAL(addToBookmarks(QString)), this, SLOT(addToBookmarks(QString)));
78 connect(listModel, SIGNAL(removeFromBookmarks(QString)), this, SLOT(removeFromBookmarks(QString)));
80 connect(this, SIGNAL(setWordListState(QVariant)), rootObject, SLOT(setEnabled(QVariant)));
84 setHeaderHidden(true);
85 setRootIsDecorated(false);
87 //setting size of star in pixels, on maemo checboxes are much bigger
96 WordListWidget::~WordListWidget() {
101 void WordListWidget::addWord(QString word, int row) {
102 QStandardItem* item = new QStandardItem(word);
104 //we don't want to allow user to edit word
105 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
107 QStandardItem* itemCheckBox = new QStandardItem();
108 //creating checkbox item
109 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
110 Qt::ItemIsUserCheckable);
112 /*checking if word is already in bookmarks, information about that is
113 stored in its translation object (not all translations have to be in
115 bool bookmark = false;
117 foreach(t, searchResult[word]) {
118 if(t->isBookmark()) {
125 itemCheckBox->setCheckState(Qt::Checked);
127 itemCheckBox->setCheckState(Qt::Unchecked);
130 model->setItem(row,0, item);
131 model->setItem(row,1, itemCheckBox);
135 void WordListWidget::showSearchResults(
136 QHash<QString, QList<Translation *> > result) {
139 searchResult = result;
142 if(searchResult.count()>0) {
144 model->setColumnCount(2);
145 model->setRowCount(result.count());
148 QHash<QString, QList<Translation*> >::iterator i;
149 for(i = searchResult.begin(); i != searchResult.end(); i++) {
150 addWord(i.key(), row++);
157 QStandardItem* item = new QStandardItem(tr("Can't find any matching words"));
158 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
159 item->setTextAlignment(Qt::AlignCenter);
162 model->setItem(0,item);
166 QHash<QString, bool> wordsInBookmarks;
167 QHashIterator<QString, QList<Translation *> > i(result);
171 bool bookmark = false;
173 foreach(t, searchResult[i.key()]) {
174 if(t->isBookmark()) {
179 wordsInBookmarks.insert(i.key(), bookmark);
182 if (result.count() == 0){
183 result.insert("!@#$%", QList<Translation*>());
184 wordsInBookmarks.insert("!@#$%", false);
185 Q_EMIT setWordListState(false);
189 listModel = new WordListModel(this);
191 listModel->setTranslations(result, wordsInBookmarks);
200 void WordListWidget::wordClicked(QModelIndex index) {
201 //we're getting translation based on data in index
202 Q_EMIT showTranslation(
203 searchResult[index.data().toString()]);
206 void WordListWidget::wordChecked(QModelIndex index) {
208 //save new item state
209 Qt::CheckState state =
210 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
213 //getting index of item which contains word which should be added/removed
215 QModelIndex item = selectedIndexes().at(0);
216 if(!item.isValid()) return;
218 //to shorten lag between clicking on a star and its change
221 //depending on new state emit suitable signal
222 if(state == Qt::Checked) {
223 Q_EMIT addBookmark(searchResult[item.data().toString()]);
226 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
229 bool onlyBookmarks = true;
230 foreach(t, searchResult[item.data().toString()]) {
231 if(t->isBookmark() == 1 || t->isBookmark()==0) {
233 onlyBookmarks = false;
237 searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
242 searchResult.remove(item.data().toString());
243 model->removeRow(item.row());
248 void WordListWidget::wordClicked(QString word){
249 emit showTranslation(searchResult[word]);
252 void WordListWidget::addToBookmarks(QString word){
253 emit addBookmark(searchResult[word]);
256 void WordListWidget::removeFromBookmarks(QString word){
257 emit removeBookmark(searchResult[word]);
264 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
266 //firstly we normally handle this event
267 QTreeView::mouseReleaseEvent(event);
269 //then we check at which item user clicked
270 QModelIndex index = indexAt(event->pos());
271 if(!index.isValid()) return;
273 /*if there are no selected items we return, that occurs sometimes
274 on maemo, when user is scrolling list and clicks to stop the scroll,
275 system doesn't select item but emits mouseReleaseEvent*/
276 if(selectedIndexes().count() == 0) return;
278 //if user doesn't click either on a word or on a star, return
279 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
282 int c = index.column();
284 //if column is 0 user clicked on a word
287 //else user clicked on a star
291 void WordListWidget::resizeEvent(QResizeEvent *event) {
293 QTreeView::resizeEvent(event);
296 void WordListWidget::keyPressEvent(QKeyEvent *event) {
297 QTreeView::keyPressEvent(event);
299 if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
300 if(selectedIndexes().count() == 0) return;
302 wordClicked(selectedIndexes().at(0));
307 void WordListWidget::resizeColumns() {
308 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
309 setColumnWidth(1, checkBoxWidth);
312 void WordListWidget::lockList() {
316 Q_EMIT setWordListState(false);
320 void WordListWidget::unlockList() {
324 Q_EMIT setWordListState(false);
328 void WordListWidget::clear() {
337 QHash<QString, QList<Translation*> >::iterator i;
338 for(i = searchResult.begin(); i != searchResult.end(); i++) {
340 foreach(t, i.value()) {
344 searchResult.clear();