Added welcome screen
[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     model = new QStandardItemModel(this);
40     setModel(model);
41     setHeaderHidden(true);
42     setRootIsDecorated(false);
43     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44
45     setStyle(new WordListProxyStyle);
46
47     #ifdef Q_WS_MAEMO_5
48         checkBoxWidth = 70;
49     #else
50         checkBoxWidth = 25;
51     #endif
52 }
53
54 void WordListWidget::addWord(QString word, int row) {
55     QStandardItem* item = new QStandardItem(word);
56     item->setFlags(item->flags() ^ Qt::ItemIsEditable);
57     QStandardItem* itemCheckBox = new QStandardItem();
58     itemCheckBox->setFlags(itemCheckBox->flags() ^ Qt::ItemIsEditable |
59                            Qt::ItemIsUserCheckable);
60
61     bool bookmark = false;
62     Translation* t;
63     foreach(t, searchResult[word]) {
64         if(t->isBookmark()) {
65             bookmark = true;
66             break;
67         }
68     }
69
70     if(bookmark)
71         itemCheckBox->setCheckState(Qt::Checked);
72     else
73         itemCheckBox->setCheckState(Qt::Unchecked);
74
75     model->setItem(row,0, item);
76     model->setItem(row,1, itemCheckBox);
77 }
78
79
80 void WordListWidget::showSearchResults(
81         QHash<QString, QList<Translation *> > result) {
82     model->clear();
83     searchResult.clear();
84
85     model->setColumnCount(2);
86     model->setRowCount(result.count());
87
88     searchResult = result;
89     int row=0;
90     QHash<QString, QList<Translation*> >::iterator i;
91     for(i = result.begin(); i != result.end(); i++) {
92            addWord(i.key(), row++);
93     }
94
95     resizeColumns();
96     model->sort(0);
97 }
98
99 void WordListWidget::wordClicked(QModelIndex index) {
100     emit showTranslation(
101             searchResult[index.data().toString()]);
102 }
103
104 void WordListWidget::wordChecked(QModelIndex index) {
105     Qt::CheckState state =
106             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
107
108     if(selectedIndexes().count()==0) return;
109     QModelIndex item = selectedIndexes().at(0);
110     if(!item.isValid()) return;
111
112     if(state == Qt::Checked) {
113         emit addBookmark(searchResult[item.data().toString()]);
114     }
115     else {
116         emit removeBookmark(searchResult[item.data().toString()]);
117     }
118 }
119
120
121 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
122     QTreeView::mouseReleaseEvent(event);
123
124
125     QModelIndex index = indexAt(event->pos());
126     if(!index.isValid()) return;
127     if(selectedIndexes().count() == 0) return;
128
129     if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
130         return;
131
132     int c = index.column();
133     if(c==0)
134         wordClicked(index);
135     else
136         wordChecked(index);
137 }
138
139 void WordListWidget::resizeEvent(QResizeEvent *event) {
140     resizeColumns();
141     QTreeView::resizeEvent(event);
142 }
143
144 void WordListWidget::resizeColumns() {
145     setColumnWidth(0, viewport()->width() -checkBoxWidth - 20);
146     setColumnWidth(1, checkBoxWidth);
147 }
148
149 void WordListWidget::lockList() {
150     setEnabled(false);
151 }
152
153 void WordListWidget::unlockList() {
154     setEnabled(true);
155 }