Fixed removing from bookmarks database
[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
43     #ifdef Q_WS_MAEMO_5
44         checkBoxWidth = 70;
45     #else
46         checkBoxWidth = 25;
47     #endif
48
49
50     setStyle(new WordListProxyStyle);
51
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     setColumnWidth(0, width()-checkBoxWidth - 20);
96     setColumnWidth(1, checkBoxWidth);
97     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
98 }
99
100 void WordListWidget::wordClicked(QModelIndex index) {
101     emit showTranslation(
102             searchResult[index.data().toString()]);
103 }
104
105 void WordListWidget::wordChecked(QModelIndex index) {
106     Qt::CheckState state =
107             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
108
109      QModelIndex item = selectedIndexes().at(0);
110
111     if(state == Qt::Checked) {
112         qDebug()<<"Added  "<<item.data().toString();
113         emit addBookmark(searchResult[item.data().toString()]);
114     }
115     else {
116         qDebug()<<"Removed  "<<item.data().toString();
117         emit removeBookmark(searchResult[item.data().toString()]);
118     }
119 }
120
121
122 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
123     QTreeView::mouseReleaseEvent(event);
124
125
126     QModelIndex index = indexAt(event->pos());
127     if(!index.isValid()) return;
128     int c = index.column();
129     if(c==0)
130         wordClicked(index);
131     else
132         wordChecked(index);
133 }
134
135 void WordListWidget::resizeEvent(QResizeEvent *event) {
136     setColumnWidth(0, width()-checkBoxWidth - 20);
137     setColumnWidth(1, checkBoxWidth);
138     QTreeView::resizeEvent(event);
139 }
140
141 void WordListWidget::lockList() {
142     setEnabled(false);
143 }
144
145 void WordListWidget::unlockList() {
146     setEnabled(true);
147 }