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 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 #include "WordListWidget.h"
27 #include "WordListProxyStyle.h"
28 #include "../../include/translation.h"
32 WordListWidget::WordListWidget(QWidget *parent):
35 //creating new model to store words and stars
36 model = new QStandardItemModel(this);
38 setHeaderHidden(true);
39 setRootIsDecorated(false);
40 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
42 //set our custom style to draw checkboxes as stars
43 proxyStyle = new WordListProxyStyle();
46 //setting size of star in pixels, on maemo checboxes are much bigger
55 WordListWidget::~WordListWidget() {
60 void WordListWidget::addWord(QString word, int row) {
61 QStandardItem* item = new QStandardItem(word);
63 //we don't want to allow user to edit word
64 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
66 QStandardItem* itemCheckBox = new QStandardItem();
67 //creating checkbox item
68 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
69 Qt::ItemIsUserCheckable);
71 /*checking if word is already in bookmarks, information about that is
72 stored in its translation object (not all translations have to be in
74 bool bookmark = false;
76 foreach(t, searchResult[word]) {
84 itemCheckBox->setCheckState(Qt::Checked);
86 itemCheckBox->setCheckState(Qt::Unchecked);
89 model->setItem(row,0, item);
90 model->setItem(row,1, itemCheckBox);
94 void WordListWidget::showSearchResults(
95 QHash<QString, QList<Translation *> > result) {
98 searchResult = result;
100 model->setColumnCount(2);
101 model->setRowCount(result.count());
104 QHash<QString, QList<Translation*> >::iterator i;
105 for(i = searchResult.begin(); i != searchResult.end(); i++) {
106 addWord(i.key(), row++);
115 void WordListWidget::wordClicked(QModelIndex index) {
116 //we're getting translation based on data in index
117 Q_EMIT showTranslation(
118 searchResult[index.data().toString()]);
121 void WordListWidget::wordChecked(QModelIndex index) {
123 //save new item state
124 Qt::CheckState state =
125 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
128 //getting index of item which contains word which should be added/removed
130 QModelIndex item = selectedIndexes().at(0);
131 if(!item.isValid()) return;
133 //to shorten lag between clicking on a star and its change
136 //depending on new state emit suitable signal
137 if(state == Qt::Checked) {
138 Q_EMIT addBookmark(searchResult[item.data().toString()]);
141 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
144 bool onlyBookmarks = true;
145 foreach(t, searchResult[item.data().toString()]) {
146 if(t->isBookmark() == 1 || t->isBookmark()==0) {
148 onlyBookmarks = false;
152 searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
157 searchResult.remove(item.data().toString());
158 model->removeRow(item.row());
164 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
166 //firstly we normally handle this event
167 QTreeView::mouseReleaseEvent(event);
169 //then we check at which item user clicked
170 QModelIndex index = indexAt(event->pos());
171 if(!index.isValid()) return;
173 /*if there are no selected items we return, that occurs sometimes
174 on maemo, when user is scrolling list and clicks to stop the scroll,
175 system doesn't select item but emits mouseReleaseEvent*/
176 if(selectedIndexes().count() == 0) return;
178 //if user doesn't click either on a word or on a star, return
179 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
182 int c = index.column();
184 //if column is 0 user clicked on a word
187 //else user clicked on a star
191 void WordListWidget::resizeEvent(QResizeEvent *event) {
193 QTreeView::resizeEvent(event);
196 void WordListWidget::resizeColumns() {
197 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
198 setColumnWidth(1, checkBoxWidth);
201 void WordListWidget::keyPressEvent(QKeyEvent *event) {
202 QTreeView::keyPressEvent(event);
204 if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
205 if(selectedIndexes().count() == 0) return;
207 wordClicked(selectedIndexes().at(0));
211 void WordListWidget::lockList() {
215 void WordListWidget::unlockList() {
219 void WordListWidget::clear() {
222 QHash<QString, QList<Translation*> >::iterator i;
223 for(i = searchResult.begin(); i != searchResult.end(); i++) {
225 foreach(t, i.value()) {
229 searchResult.clear();