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
39 model = new QStandardItemModel(this);
41 setHeaderHidden(true);
42 setRootIsDecorated(false);
43 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 //set our custom style to draw checkboxes as stars
46 proxyStyle = new WordListProxyStyle();
49 //setting size of star in pixels, on maemo checboxes are much bigger
58 WordListWidget::~WordListWidget() {
63 void WordListWidget::addWord(QString word, int row) {
64 QStandardItem* item = new QStandardItem(word);
66 //we don't want to allow user to edit word
67 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
69 QStandardItem* itemCheckBox = new QStandardItem();
70 //creating checkbox item
71 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
72 Qt::ItemIsUserCheckable);
74 /*checking if word is already in bookmarks, information about that is
75 stored in its translation object (not all translations have to be in
77 bool bookmark = false;
79 foreach(t, searchResult[word]) {
87 itemCheckBox->setCheckState(Qt::Checked);
89 itemCheckBox->setCheckState(Qt::Unchecked);
92 model->setItem(row,0, item);
93 model->setItem(row,1, itemCheckBox);
97 void WordListWidget::showSearchResults(
98 QHash<QString, QList<Translation *> > result) {
101 searchResult = result;
103 if(searchResult.count()>0) {
105 model->setColumnCount(2);
106 model->setRowCount(result.count());
109 QHash<QString, QList<Translation*> >::iterator i;
110 for(i = searchResult.begin(); i != searchResult.end(); i++) {
111 addWord(i.key(), row++);
118 QStandardItem* item = new QStandardItem(tr("Can't find any matching words"));
119 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
120 item->setTextAlignment(Qt::AlignCenter);
123 model->setItem(0,item);
129 void WordListWidget::wordClicked(QModelIndex index) {
130 //we're getting translation based on data in index
131 Q_EMIT showTranslation(
132 searchResult[index.data().toString()]);
135 void WordListWidget::wordChecked(QModelIndex index) {
137 //save new item state
138 Qt::CheckState state =
139 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
142 //getting index of item which contains word which should be added/removed
144 QModelIndex item = selectedIndexes().at(0);
145 if(!item.isValid()) return;
147 //to shorten lag between clicking on a star and its change
150 //depending on new state emit suitable signal
151 if(state == Qt::Checked) {
152 Q_EMIT addBookmark(searchResult[item.data().toString()]);
155 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
158 bool onlyBookmarks = true;
159 foreach(t, searchResult[item.data().toString()]) {
160 if(t->isBookmark() == 1 || t->isBookmark()==0) {
162 onlyBookmarks = false;
166 searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
171 searchResult.remove(item.data().toString());
172 model->removeRow(item.row());
178 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
180 //firstly we normally handle this event
181 QTreeView::mouseReleaseEvent(event);
183 //then we check at which item user clicked
184 QModelIndex index = indexAt(event->pos());
185 if(!index.isValid()) return;
187 /*if there are no selected items we return, that occurs sometimes
188 on maemo, when user is scrolling list and clicks to stop the scroll,
189 system doesn't select item but emits mouseReleaseEvent*/
190 if(selectedIndexes().count() == 0) return;
192 //if user doesn't click either on a word or on a star, return
193 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
196 int c = index.column();
198 //if column is 0 user clicked on a word
201 //else user clicked on a star
205 void WordListWidget::resizeEvent(QResizeEvent *event) {
207 QTreeView::resizeEvent(event);
210 void WordListWidget::resizeColumns() {
211 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
212 setColumnWidth(1, checkBoxWidth);
215 void WordListWidget::keyPressEvent(QKeyEvent *event) {
216 QTreeView::keyPressEvent(event);
218 if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
219 if(selectedIndexes().count() == 0) return;
221 wordClicked(selectedIndexes().at(0));
225 void WordListWidget::lockList() {
229 void WordListWidget::unlockList() {
233 void WordListWidget::clear() {
236 QHash<QString, QList<Translation*> >::iterator i;
237 for(i = searchResult.begin(); i != searchResult.end(); i++) {
239 foreach(t, i.value()) {
243 searchResult.clear();