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) {
95 qDebug()<<searchResult.count();
96 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 qDebug()<<searchResult[index.data().toString()].at(0)->dictionaryInfo();
115 emit showTranslation(
116 searchResult[index.data().toString()]);
119 void WordListWidget::wordChecked(QModelIndex index) {
121 //save new item state
122 Qt::CheckState state =
123 Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
127 //getting index of item which contains word which should be added/removed
129 QModelIndex item = selectedIndexes().at(0);
130 if(!item.isValid()) return;
132 //to shorten lag between clicking on star and it's change
135 //depending on new state emit suitable signal
136 if(state == Qt::Checked) {
137 emit addBookmark(searchResult[item.data().toString()]);
140 emit removeBookmark(searchResult[item.data().toString()]);
145 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
147 //firstly we normally handle this event
148 QTreeView::mouseReleaseEvent(event);
150 //then we checking at which item user clicked
151 QModelIndex index = indexAt(event->pos());
152 if(!index.isValid()) return;
154 /*if there are no selected items we return, that occurs sometimes
155 on maemo, when user is scrolling list and click to stop the scroll,
156 system don't select item but emitting mouseReleaseEvent*/
157 if(selectedIndexes().count() == 0) return;
159 //if user don't click on word either on star return
160 if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
163 int c = index.column();
165 //if column is 0 user clicked word
168 //else user clicked star
172 void WordListWidget::resizeEvent(QResizeEvent *event) {
174 QTreeView::resizeEvent(event);
177 void WordListWidget::resizeColumns() {
178 setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
179 setColumnWidth(1, checkBoxWidth);
182 void WordListWidget::lockList() {
186 void WordListWidget::unlockList() {
190 void WordListWidget::clear() {
192 qDebug()<<"asdasdas";
194 /* QHash<QString, QList<Translation*> >::iterator i;
195 for(i = searchResult.begin(); i != searchResult.end(); i++) {
197 foreach(t, i.value()) {
201 searchResult.clear();