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 //Created by Mateusz Półrola
25 #include "WordListWidget.h"
27 #include "../../includes/translation.h"
29 #include "WordListProxyStyle.h"
33 #include <QMaemo5InformationBox>
36 WordListWidget::WordListWidget(QWidget *parent):
39 //creating new model to store words and stars
40 model = new QStandardItemModel(this);
42 setHeaderHidden(true);
43 setRootIsDecorated(false);
44 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46 //set our custom style to draw checkboxes as stars
47 setStyle(new WordListProxyStyle);
49 //setting size of star in pixels, on maemo checboxes are much bigger
57 void WordListWidget::addWord(QString word, int row) {
58 QStandardItem* item = new QStandardItem(word);
60 //we don't want to allow user to edit word
61 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
63 QStandardItem* itemCheckBox = new QStandardItem();
64 //creating checkbox item
65 itemCheckBox->setFlags(itemCheckBox->flags() ^ Qt::ItemIsEditable |
66 Qt::ItemIsUserCheckable);
68 /*checking if word is already in bookmarks, information about that is
69 stored in it's translation object (not all translation have to be in
71 bool bookmark = false;
73 foreach(t, searchResult[word]) {
81 itemCheckBox->setCheckState(Qt::Checked);
83 itemCheckBox->setCheckState(Qt::Unchecked);
86 model->setItem(row,0, item);
87 model->setItem(row,1, itemCheckBox);
91 void WordListWidget::showSearchResults(
92 QHash<QString, QList<Translation *> > result) {
97 model->setColumnCount(2);
98 model->setRowCount(result.count());
100 searchResult = result;
102 QHash<QString, QList<Translation*> >::iterator i;
103 for(i = result.begin(); i != result.end(); i++) {
104 addWord(i.key(), row++);
111 void WordListWidget::wordClicked(QModelIndex index) {
112 //we're getting translation based on data in index
113 emit showTranslation(
114 searchResult[index.data().toString()]);
117 void WordListWidget::wordChecked(QModelIndex index) {
119 //save new item state
120 Qt::CheckState state =
121 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
125 //getting index of item which contains word which should be added/removed
127 QModelIndex item = selectedIndexes().at(0);
128 if(!item.isValid()) return;
130 //to shorten lag between clicking on star and it's change
133 //depending on new state emit suitable signal
134 if(state == Qt::Checked) {
135 emit addBookmark(searchResult[item.data().toString()]);
138 emit removeBookmark(searchResult[item.data().toString()]);
143 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
145 //firstly we normally handle this event
146 QTreeView::mouseReleaseEvent(event);
148 //then we checking at which item user clicked
149 QModelIndex index = indexAt(event->pos());
150 if(!index.isValid()) return;
152 /*if there are no selected items we return, that occurs sometimes
153 on maemo, when user is scrolling list and click to stop the scroll,
154 system don't select item but emitting mouseReleaseEvent*/
155 if(selectedIndexes().count() == 0) return;
157 //if user don't click on word either on star return
158 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
161 int c = index.column();
163 //if column is 0 user clicked word
166 //else user clicked star
170 void WordListWidget::resizeEvent(QResizeEvent *event) {
172 QTreeView::resizeEvent(event);
175 void WordListWidget::resizeColumns() {
176 setColumnWidth(0, viewport()->width() -checkBoxWidth - 20);
177 setColumnWidth(1, checkBoxWidth);
180 void WordListWidget::lockList() {
184 void WordListWidget::unlockList() {