Further documentation corrections
[mdictionary] / src / mdictionary / 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 //! \file WordListWidget.cpp
24 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
25
26 #include "WordListWidget.h"
27 #include "WordListProxyStyle.h"
28 #include "../../include/translation.h"
29
30
31 WordListWidget::WordListWidget(QWidget *parent):
32     QTreeView(parent) {
33
34     //creating new model to store words and stars
35     model = new QStandardItemModel(this);
36     setModel(model);
37     setHeaderHidden(true);
38     setRootIsDecorated(false);
39     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40
41     //set our custom style to draw checkboxes as stars
42     proxyStyle = new WordListProxyStyle();
43     setStyle(proxyStyle);
44
45     //setting size of star in pixels, on maemo checboxes are much bigger
46     #ifdef Q_WS_MAEMO_5
47         checkBoxWidth = 70;
48     #else
49         checkBoxWidth = 25;
50     #endif
51 }
52
53
54 WordListWidget::~WordListWidget() {
55     if(proxyStyle)
56         delete proxyStyle;
57 }
58
59 void WordListWidget::addWord(QString word, int row) {
60     QStandardItem* item = new QStandardItem(word);
61
62     //we don't want to allow user to edit word
63     item->setFlags(item->flags() ^ Qt::ItemIsEditable);
64
65     QStandardItem* itemCheckBox = new QStandardItem();
66     //creating checkbox item
67     itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
68                            Qt::ItemIsUserCheckable);
69
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
72     bookmarks)*/
73     bool bookmark = false;
74     Translation* t;
75     foreach(t, searchResult[word]) {
76         if(t->isBookmark()) {
77             bookmark = true;
78             break;
79         }
80     }
81
82     if(bookmark)
83         itemCheckBox->setCheckState(Qt::Checked);
84     else
85         itemCheckBox->setCheckState(Qt::Unchecked);
86
87     //add item to model
88     model->setItem(row,0, item);
89     model->setItem(row,1, itemCheckBox);
90 }
91
92
93 void WordListWidget::showSearchResults(
94         QHash<QString, QList<Translation *> > result) {
95
96     clear();
97     searchResult = result;
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     model->sort(0);
109     resizeColumns();    
110 }
111
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()]);
116 }
117
118 void WordListWidget::wordChecked(QModelIndex index) {
119
120     //save new item state
121     Qt::CheckState state =
122             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
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 a star and its change
131     repaint();
132
133     //depending on new state emit suitable signal
134     if(state == Qt::Checked) {
135         Q_EMIT addBookmark(searchResult[item.data().toString()]);
136     }
137     else {
138         Q_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 check 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 clicks to stop the scroll,
154     system doesn't select item but emits mouseReleaseEvent*/
155     if(selectedIndexes().count() == 0) return;
156
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)
159         return;
160
161     int c = index.column();
162     if(c==0)
163         //if column is 0 user clicked on a word
164         wordClicked(index);
165     else
166         //else user clicked on a 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 - 5);
177     setColumnWidth(1, checkBoxWidth);
178 }
179
180 void WordListWidget::lockList() {
181     setEnabled(false);
182 }
183
184 void WordListWidget::unlockList() {
185     setEnabled(true);
186 }
187
188 void WordListWidget::clear() {
189     model->clear();
190
191     QHash<QString, QList<Translation*> >::iterator i;
192     for(i = searchResult.begin(); i != searchResult.end(); i++) {
193            Translation*t;
194            foreach(t, i.value()) {
195                delete t;
196            }
197     }
198     searchResult.clear();
199 }