Changed repo structure
[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 "../../common/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     setStyle(new WordListProxyStyle);
43
44     //setting size of star in pixels, on maemo checboxes are much bigger
45     #ifdef Q_WS_MAEMO_5
46         checkBoxWidth = 70;
47     #else
48         checkBoxWidth = 25;
49     #endif
50 }
51
52 void WordListWidget::addWord(QString word, int row) {
53     QStandardItem* item = new QStandardItem(word);
54
55     //we don't want to allow user to edit word
56     item->setFlags(item->flags() ^ Qt::ItemIsEditable);
57
58     QStandardItem* itemCheckBox = new QStandardItem();
59     //creating checkbox item
60     itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
61                            Qt::ItemIsUserCheckable);
62
63     /*checking if word is already in bookmarks, information about that is
64     stored in its translation object (not all translations have to be in
65     bookmarks)*/
66     bool bookmark = false;
67     Translation* t;
68     foreach(t, searchResult[word]) {
69         if(t->isBookmark()) {
70             bookmark = true;
71             break;
72         }
73     }
74
75     if(bookmark)
76         itemCheckBox->setCheckState(Qt::Checked);
77     else
78         itemCheckBox->setCheckState(Qt::Unchecked);
79
80     //add item to model
81     model->setItem(row,0, item);
82     model->setItem(row,1, itemCheckBox);
83 }
84
85
86 void WordListWidget::showSearchResults(
87         QHash<QString, QList<Translation *> > result) {
88
89     clear();
90     searchResult = result;
91
92     model->setColumnCount(2);
93     model->setRowCount(result.count());
94
95     int row=0;
96     QHash<QString, QList<Translation*> >::iterator i;
97     for(i = searchResult.begin(); i != searchResult.end(); i++) {
98            addWord(i.key(), row++);
99     }
100
101     model->sort(0);
102     resizeColumns();    
103 }
104
105 void WordListWidget::wordClicked(QModelIndex index) {
106     //we're getting translation based on data in index
107     Q_EMIT showTranslation(
108             searchResult[index.data().toString()]);
109 }
110
111 void WordListWidget::wordChecked(QModelIndex index) {
112
113     //save new item state
114     Qt::CheckState state =
115             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
116
117
118     //getting index of item which contains word which should be added/removed
119     //from bookmarks
120     QModelIndex item = selectedIndexes().at(0);
121     if(!item.isValid()) return;
122
123     //to shorten lag between clicking on star and its change
124     repaint();
125
126     //depending on new state emit suitable signal
127     if(state == Qt::Checked) {
128         Q_EMIT addBookmark(searchResult[item.data().toString()]);
129     }
130     else {
131         Q_EMIT removeBookmark(searchResult[item.data().toString()]);
132     }
133 }
134
135
136 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
137
138     //firstly we normally handle this event
139     QTreeView::mouseReleaseEvent(event);
140
141     //then we check at which item user clicked
142     QModelIndex index = indexAt(event->pos());
143     if(!index.isValid()) return;
144
145     /*if there are no selected items we return, that occurs sometimes
146     on maemo, when user is scrolling list and clicks to stop the scroll,
147     system doesn't select item but emits mouseReleaseEvent*/
148     if(selectedIndexes().count() == 0) return;
149
150     //if user doesn't click either on word or on star, return
151     if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
152         return;
153
154     int c = index.column();
155     if(c==0)
156         //if column is 0 user clicked word
157         wordClicked(index);
158     else
159         //else user clicked star
160         wordChecked(index);
161 }
162
163 void WordListWidget::resizeEvent(QResizeEvent *event) {
164     resizeColumns();
165     QTreeView::resizeEvent(event);
166 }
167
168 void WordListWidget::resizeColumns() {
169     setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
170     setColumnWidth(1, checkBoxWidth);
171 }
172
173 void WordListWidget::lockList() {
174     setEnabled(false);
175 }
176
177 void WordListWidget::unlockList() {
178     setEnabled(true);
179 }
180
181 void WordListWidget::clear() {
182     model->clear();
183
184     QHash<QString, QList<Translation*> >::iterator i;
185     for(i = searchResult.begin(); i != searchResult.end(); i++) {
186            Translation*t;
187            foreach(t, i.value()) {
188                delete t;
189            }
190     }
191     searchResult.clear();
192 }