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 "../../common/translation.h"
31 WordListWidget::WordListWidget(QWidget *parent):
34 //creating new model to store words and stars
35 model = new QStandardItemModel(this);
37 setHeaderHidden(true);
38 setRootIsDecorated(false);
39 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41 //set our custom style to draw checkboxes as stars
42 setStyle(new WordListProxyStyle);
44 //setting size of star in pixels, on maemo checboxes are much bigger
52 void WordListWidget::addWord(QString word, int row) {
53 QStandardItem* item = new QStandardItem(word);
55 //we don't want to allow user to edit word
56 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
58 QStandardItem* itemCheckBox = new QStandardItem();
59 //creating checkbox item
60 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
61 Qt::ItemIsUserCheckable);
63 /*checking if word is already in bookmarks, information about that is
64 stored in its translation object (not all translations have to be in
66 bool bookmark = false;
68 foreach(t, searchResult[word]) {
76 itemCheckBox->setCheckState(Qt::Checked);
78 itemCheckBox->setCheckState(Qt::Unchecked);
81 model->setItem(row,0, item);
82 model->setItem(row,1, itemCheckBox);
86 void WordListWidget::showSearchResults(
87 QHash<QString, QList<Translation *> > result) {
90 searchResult = result;
92 model->setColumnCount(2);
93 model->setRowCount(result.count());
96 QHash<QString, QList<Translation*> >::iterator i;
97 for(i = searchResult.begin(); i != searchResult.end(); i++) {
98 addWord(i.key(), row++);
105 void WordListWidget::wordClicked(QModelIndex index) {
106 //we're getting translation based on data in index
107 Q_EMIT showTranslation(
108 searchResult[index.data().toString()]);
111 void WordListWidget::wordChecked(QModelIndex index) {
113 //save new item state
114 Qt::CheckState state =
115 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
118 //getting index of item which contains word which should be added/removed
120 QModelIndex item = selectedIndexes().at(0);
121 if(!item.isValid()) return;
123 //to shorten lag between clicking on star and its change
126 //depending on new state emit suitable signal
127 if(state == Qt::Checked) {
128 Q_EMIT addBookmark(searchResult[item.data().toString()]);
131 Q_EMIT removeBookmark(searchResult[item.data().toString()]);
136 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
138 //firstly we normally handle this event
139 QTreeView::mouseReleaseEvent(event);
141 //then we check at which item user clicked
142 QModelIndex index = indexAt(event->pos());
143 if(!index.isValid()) return;
145 /*if there are no selected items we return, that occurs sometimes
146 on maemo, when user is scrolling list and clicks to stop the scroll,
147 system doesn't select item but emits mouseReleaseEvent*/
148 if(selectedIndexes().count() == 0) return;
150 //if user doesn't click either on word or on star, return
151 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
154 int c = index.column();
156 //if column is 0 user clicked word
159 //else user clicked star
163 void WordListWidget::resizeEvent(QResizeEvent *event) {
165 QTreeView::resizeEvent(event);
168 void WordListWidget::resizeColumns() {
169 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
170 setColumnWidth(1, checkBoxWidth);
173 void WordListWidget::lockList() {
177 void WordListWidget::unlockList() {
181 void WordListWidget::clear() {
184 QHash<QString, QList<Translation*> >::iterator i;
185 for(i = searchResult.begin(); i != searchResult.end(); i++) {
187 foreach(t, i.value()) {
191 searchResult.clear();