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"
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 proxyStyle = new WordListProxyStyle();
45 //setting size of star in pixels, on maemo checboxes are much bigger
54 WordListWidget::~WordListWidget() {
59 void WordListWidget::addWord(QString word, int row) {
60 QStandardItem* item = new QStandardItem(word);
62 //we don't want to allow user to edit word
63 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
65 QStandardItem* itemCheckBox = new QStandardItem();
66 //creating checkbox item
67 itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
68 Qt::ItemIsUserCheckable);
70 /*checking if word is already in bookmarks, information about that is
71 stored in its translation object (not all translations have to be in
73 bool bookmark = false;
75 foreach(t, searchResult[word]) {
83 itemCheckBox->setCheckState(Qt::Checked);
85 itemCheckBox->setCheckState(Qt::Unchecked);
88 model->setItem(row,0, item);
89 model->setItem(row,1, itemCheckBox);
93 void WordListWidget::showSearchResults(
94 QHash<QString, QList<Translation *> > result) {
97 searchResult = result;
99 model->setColumnCount(2);
100 model->setRowCount(result.count());
103 QHash<QString, QList<Translation*> >::iterator i;
104 for(i = searchResult.begin(); i != searchResult.end(); i++) {
105 addWord(i.key(), row++);
112 void WordListWidget::wordClicked(QModelIndex index) {
113 //we're getting translation based on data in index
114 Q_EMIT showTranslation(
115 searchResult[index.data().toString()]);
118 void WordListWidget::wordChecked(QModelIndex index) {
120 //save new item state
121 Qt::CheckState state =
122 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 a star and its change
133 //depending on new state emit suitable signal
134 if(state == Qt::Checked) {
135 Q_EMIT addBookmark(searchResult[item.data().toString()]);
138 Q_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 check 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 clicks to stop the scroll,
154 system doesn't select item but emits mouseReleaseEvent*/
155 if(selectedIndexes().count() == 0) return;
157 //if user doesn't click either on a word or on a star, return
158 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
161 int c = index.column();
163 //if column is 0 user clicked on a word
166 //else user clicked on a star
170 void WordListWidget::resizeEvent(QResizeEvent *event) {
172 QTreeView::resizeEvent(event);
175 void WordListWidget::resizeColumns() {
176 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
177 setColumnWidth(1, checkBoxWidth);
180 void WordListWidget::lockList() {
184 void WordListWidget::unlockList() {
188 void WordListWidget::clear() {
191 QHash<QString, QList<Translation*> >::iterator i;
192 for(i = searchResult.begin(); i != searchResult.end(); i++) {
194 foreach(t, i.value()) {
198 searchResult.clear();