f44af27096c44866cb855f0bd9cb03634bbfa33f
[mdictionary] / trunk / src / base / gui / WordListWidget.cpp
1
2 /*******************************************************************************
3
4     This file is part of mDictionary.
5
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.
10
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.
15
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/>.
18
19     Copyright 2010 Comarch S.A.
20
21 *******************************************************************************/
22
23 //Created by Mateusz Półrola
24
25 #include "WordListWidget.h"
26 #include <QDebug>
27 #include "../../includes/translation.h"
28 #include <QMultiHash>
29 #include "WordListProxyStyle.h"
30
31
32 #ifdef Q_WS_MAEMO_5
33     #include <QMaemo5InformationBox>
34 #endif
35
36 WordListWidget::WordListWidget(QWidget *parent):
37     QTreeView(parent) {
38
39     //creating new model to store words and stars
40     model = new QStandardItemModel(this);
41     setModel(model);
42     setHeaderHidden(true);
43     setRootIsDecorated(false);
44     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45
46     //set our custom style to draw checkboxes as stars
47     setStyle(new WordListProxyStyle);
48
49     //setting size of star in pixels, on maemo checboxes are much bigger
50     #ifdef Q_WS_MAEMO_5
51         checkBoxWidth = 70;
52     #else
53         checkBoxWidth = 25;
54     #endif
55 }
56
57 void WordListWidget::addWord(QString word, int row) {
58     QStandardItem* item = new QStandardItem(word);
59
60     //we don't want to allow user to edit word
61     item->setFlags(item->flags() ^ Qt::ItemIsEditable);
62
63     QStandardItem* itemCheckBox = new QStandardItem();
64     //creating checkbox item
65     itemCheckBox->setFlags(itemCheckBox->flags() ^ Qt::ItemIsEditable |
66                            Qt::ItemIsUserCheckable);
67
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
70     bookmarks)*/
71     bool bookmark = false;
72     Translation* t;
73     foreach(t, searchResult[word]) {
74         if(t->isBookmark()) {
75             bookmark = true;
76             break;
77         }
78     }
79
80     if(bookmark)
81         itemCheckBox->setCheckState(Qt::Checked);
82     else
83         itemCheckBox->setCheckState(Qt::Unchecked);
84
85     //add item to model
86     model->setItem(row,0, item);
87     model->setItem(row,1, itemCheckBox);
88 }
89
90
91 void WordListWidget::showSearchResults(
92         QHash<QString, QList<Translation *> > result) {
93
94     model->clear();
95     searchResult.clear();
96
97     model->setColumnCount(2);
98     model->setRowCount(result.count());
99
100     searchResult = result;
101     int row=0;
102     QHash<QString, QList<Translation*> >::iterator i;
103     for(i = result.begin(); i != result.end(); i++) {
104            addWord(i.key(), row++);
105     }
106
107     resizeColumns();
108     model->sort(0);
109 }
110
111 void WordListWidget::wordClicked(QModelIndex index) {
112     //we're getting translation based on data in index
113     emit showTranslation(
114             searchResult[index.data().toString()]);
115 }
116
117 void WordListWidget::wordChecked(QModelIndex index) {
118
119     //save new item state
120     Qt::CheckState state =
121             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
122
123
124
125     //getting index of item which contains word which should be added/removed
126     //from bookmarks
127     QModelIndex item = selectedIndexes().at(0);
128     if(!item.isValid()) return;
129
130     //to shorten lag between clicking on star and it's change
131     repaint();
132
133     //depending on new state emit suitable signal
134     if(state == Qt::Checked) {
135         emit addBookmark(searchResult[item.data().toString()]);
136     }
137     else {
138         emit removeBookmark(searchResult[item.data().toString()]);
139     }
140 }
141
142
143 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
144
145     //firstly we normally handle this event
146     QTreeView::mouseReleaseEvent(event);
147
148     //then we checking at which item user clicked
149     QModelIndex index = indexAt(event->pos());
150     if(!index.isValid()) return;
151
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;
156
157     //if user don't click on word either on star return
158     if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
159         return;
160
161     int c = index.column();
162     if(c==0)
163         //if column is 0 user clicked word
164         wordClicked(index);
165     else
166         //else user clicked star
167         wordChecked(index);
168 }
169
170 void WordListWidget::resizeEvent(QResizeEvent *event) {
171     resizeColumns();
172     QTreeView::resizeEvent(event);
173 }
174
175 void WordListWidget::resizeColumns() {
176     setColumnWidth(0, viewport()->width() -checkBoxWidth - 20);
177     setColumnWidth(1, checkBoxWidth);
178 }
179
180 void WordListWidget::lockList() {
181     setEnabled(false);
182 }
183
184 void WordListWidget::unlockList() {
185     setEnabled(true);
186 }