Merged and fixed backbone settings
[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 }
97
98 void WordListWidget::wordClicked(QModelIndex index) {
99     emit showTranslation(
100             searchResult[index.data().toString()]);
101 }
102
103 void WordListWidget::wordChecked(QModelIndex index) {
104     Qt::CheckState state =
105             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
106
107     if(selectedIndexes().count()==0) return;
108     QModelIndex item = selectedIndexes().at(0);
109     if(!item.isValid()) return;
110
111     if(state == Qt::Checked) {
112         emit addBookmark(searchResult[item.data().toString()]);
113     }
114     else {
115         emit removeBookmark(searchResult[item.data().toString()]);
116     }
117 }
118
119
120 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
121     QTreeView::mouseReleaseEvent(event);
122
123
124     QModelIndex index = indexAt(event->pos());
125     if(!index.isValid()) return;
126     int c = index.column();
127     if(c==0)
128         wordClicked(index);
129     else
130         wordChecked(index);
131 }
132
133 void WordListWidget::resizeEvent(QResizeEvent *event) {
134     resizeColumns();
135     QTreeView::resizeEvent(event);
136 }
137
138 void WordListWidget::resizeColumns() {
139     setColumnWidth(0, viewport()->width() -checkBoxWidth - 20);
140     setColumnWidth(1, checkBoxWidth);
141 }
142
143 void WordListWidget::lockList() {
144     setEnabled(false);
145 }
146
147 void WordListWidget::unlockList() {
148     setEnabled(true);
149 }