further code cleanup
[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 its translation object (not all translations 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     clear();
95     qDebug()<<searchResult.count();
96     searchResult = result;
97
98
99     model->setColumnCount(2);
100     model->setRowCount(result.count());
101
102     int row=0;
103     QHash<QString, QList<Translation*> >::iterator i;
104     for(i = searchResult.begin(); i != searchResult.end(); i++) {
105            addWord(i.key(), row++);
106     }
107
108     resizeColumns();
109     model->sort(0);
110 }
111
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     Q_EMIT showTranslation(
116             searchResult[index.data().toString()]);
117 }
118
119 void WordListWidget::wordChecked(QModelIndex index) {
120
121     //save new item state
122     Qt::CheckState state =
123             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
124
125
126
127     //getting index of item which contains word which should be added/removed
128     //from bookmarks
129     QModelIndex item = selectedIndexes().at(0);
130     if(!item.isValid()) return;
131
132     //to shorten lag between clicking on star and its change
133     repaint();
134
135     //depending on new state emit suitable signal
136     if(state == Qt::Checked) {
137         Q_EMIT addBookmark(searchResult[item.data().toString()]);
138     }
139     else {
140         Q_EMIT removeBookmark(searchResult[item.data().toString()]);
141     }
142 }
143
144
145 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
146
147     //firstly we normally handle this event
148     QTreeView::mouseReleaseEvent(event);
149
150     //then we check at which item user clicked
151     QModelIndex index = indexAt(event->pos());
152     if(!index.isValid()) return;
153
154     /*if there are no selected items we return, that occurs sometimes
155     on maemo, when user is scrolling list and clicks to stop the scroll,
156     system doesn't select item but emits mouseReleaseEvent*/
157     if(selectedIndexes().count() == 0) return;
158
159     //if user doesn't click either on word or on star, return
160     if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
161         return;
162
163     int c = index.column();
164     if(c==0)
165         //if column is 0 user clicked word
166         wordClicked(index);
167     else
168         //else user clicked star
169         wordChecked(index);
170 }
171
172 void WordListWidget::resizeEvent(QResizeEvent *event) {
173     resizeColumns();
174     QTreeView::resizeEvent(event);
175 }
176
177 void WordListWidget::resizeColumns() {
178     setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
179     setColumnWidth(1, checkBoxWidth);
180 }
181
182 void WordListWidget::lockList() {
183     setEnabled(false);
184 }
185
186 void WordListWidget::unlockList() {
187     setEnabled(true);
188 }
189
190 void WordListWidget::clear() {
191     model->clear();
192
193     QHash<QString, QList<Translation*> >::iterator i;
194     for(i = searchResult.begin(); i != searchResult.end(); i++) {
195            Translation*t;
196            foreach(t, i.value()) {
197                delete t;
198            }
199     }
200     searchResult.clear();
201 }