Added settings widget
[mdictionary] / trunk / src / base / gui / WordListWidget.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //Created by Mateusz Półrola
23
24 #include "WordListWidget.h"
25 #include <QDebug>
26 #include "../../includes/translation.h"
27 #include <QMultiHash>
28 #include "WordListProxyStyle.h"
29
30
31 #ifdef Q_WS_MAEMO_5
32     #include <QMaemo5InformationBox>
33 #endif
34
35 WordListWidget::WordListWidget(QWidget *parent):
36     QTreeView(parent) {
37
38     model = new QStandardItemModel(this);
39     setModel(model);
40     setHeaderHidden(true);
41     setRootIsDecorated(false);
42     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43
44     setStyle(new WordListProxyStyle);
45
46     #ifdef Q_WS_MAEMO_5
47         checkBoxWidth = 70;
48     #else
49         checkBoxWidth = 25;
50     #endif
51 }
52
53 void WordListWidget::addWord(QString word, int row) {
54     QStandardItem* item = new QStandardItem(word);
55     item->setFlags(item->flags() ^ Qt::ItemIsEditable);
56     QStandardItem* itemCheckBox = new QStandardItem();
57     itemCheckBox->setFlags(itemCheckBox->flags() ^ Qt::ItemIsEditable |
58                            Qt::ItemIsUserCheckable);
59
60     bool bookmark = false;
61     Translation* t;
62     foreach(t, searchResult[word]) {
63         if(t->isBookmark()) {
64             bookmark = true;
65             break;
66         }
67     }
68
69     if(bookmark)
70         itemCheckBox->setCheckState(Qt::Checked);
71     else
72         itemCheckBox->setCheckState(Qt::Unchecked);
73
74     model->setItem(row,0, item);
75     model->setItem(row,1, itemCheckBox);
76 }
77
78
79 void WordListWidget::showSearchResults(
80         QHash<QString, QList<Translation *> > result) {
81     model->clear();
82     searchResult.clear();
83
84     model->setColumnCount(2);
85     model->setRowCount(result.count());
86
87     searchResult = result;
88     int row=0;
89     QHash<QString, QList<Translation*> >::iterator i;
90     for(i = result.begin(); i != result.end(); i++) {
91            addWord(i.key(), row++);
92     }
93
94     resizeColumns();
95 }
96
97 void WordListWidget::wordClicked(QModelIndex index) {
98     emit showTranslation(
99             searchResult[index.data().toString()]);
100 }
101
102 void WordListWidget::wordChecked(QModelIndex index) {
103     Qt::CheckState state =
104             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
105
106     QModelIndex item = selectedIndexes().at(0);
107     if(!item.isValid()) return;
108
109     if(state == Qt::Checked) {
110         emit addBookmark(searchResult[item.data().toString()]);
111     }
112     else {
113         emit removeBookmark(searchResult[item.data().toString()]);
114     }
115 }
116
117
118 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
119     QTreeView::mouseReleaseEvent(event);
120
121
122     QModelIndex index = indexAt(event->pos());
123     if(!index.isValid()) return;
124     int c = index.column();
125     if(c==0)
126         wordClicked(index);
127     else
128         wordChecked(index);
129 }
130
131 void WordListWidget::resizeEvent(QResizeEvent *event) {
132     resizeColumns();
133     QTreeView::resizeEvent(event);
134 }
135
136 void WordListWidget::resizeColumns() {
137     setColumnWidth(0, viewport()->width() -checkBoxWidth - 20);
138     setColumnWidth(1, checkBoxWidth);
139 }
140
141 void WordListWidget::lockList() {
142     setEnabled(false);
143 }
144
145 void WordListWidget::unlockList() {
146     setEnabled(true);
147 }