Changed thread handling for translation fetching
[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
29 #ifdef Q_WS_MAEMO_5
30     #include <QMaemo5InformationBox>
31 #endif
32
33 WordListWidget::WordListWidget(Backbone *backbone, QWidget *parent):
34     QListView(parent) {
35
36     this->backbone = backbone;
37
38     _exactMatchString = QString();
39
40     wordListModel = new QStringListModel();
41
42     connect(backbone, SIGNAL(ready()),
43             this, SLOT(showSearchResults()));
44
45     connect(this, SIGNAL(clicked(QModelIndex)),
46             this, SLOT(itemClicked(QModelIndex)));
47
48     connect(backbone, SIGNAL(htmlReady()),
49             this, SLOT(unlockList()));
50
51     setModel(wordListModel);
52 }
53
54 void WordListWidget::addWord(QString word) {
55     int wordsCount = wordListModel->rowCount();
56
57     wordListModel->insertRow(wordsCount);
58
59     QModelIndex newWordIndex = wordListModel->index(wordsCount);
60
61     wordListModel->setData(newWordIndex, word);
62 }
63
64 void WordListWidget::clear() {
65     int wordsCount = wordListModel->rowCount();
66
67     for(int i = 0; i < wordsCount; i++) {
68         wordListModel->removeRow(0);
69     }
70 }
71
72 void WordListWidget::showSearchResults() {
73     clear();
74     searchResult.clear();
75     QMultiHash<QString, Translation*> result = backbone->result();
76
77     if(result.count() == 0) {
78         #ifdef Q_WS_MAEMO_5
79         QMaemo5InformationBox::information(this,
80                             tr("Can't find any matching words"),
81                             QMaemo5InformationBox::DefaultTimeout);
82         #endif
83     }
84     else
85     {
86         QMultiHash<QString, Translation*>::iterator i;
87         for(i = result.begin(); i != result.end(); i++) {
88
89             if(!searchResult.contains(i.key())) {
90                addWord(i.key());
91             }
92
93             searchResult[i.key()].push_back(i.value());
94         }
95
96         bool foundExactMatch = false;
97
98         if(!_exactMatchString.isEmpty()) {
99             QHash<QString, QList<Translation*> >::iterator j;
100             for(j = searchResult.begin(); j != searchResult.end(); j++) {
101                 if(j.key() == _exactMatchString && !foundExactMatch) {
102                     foundExactMatch = true;
103                     clear();
104                     backbone->searchHtml(j.value());
105                     emit selectedWord(j.key());
106                     break;
107                 }
108             }
109             if(!foundExactMatch) {
110                 #ifdef Q_WS_MAEMO_5
111                 QMaemo5InformationBox::information(this,
112                                     tr("Can't find exactly matching word"),
113                                     QMaemo5InformationBox::DefaultTimeout);
114                 #endif
115             }
116             setExactMatchString(QString());
117         }
118         wordListModel->sort(0, Qt::AscendingOrder);
119
120         scrollTo(model()->index(0,0));
121     }
122
123     unlockList();
124 }
125
126 void WordListWidget::itemClicked(QModelIndex index) {
127     backbone->searchHtml(searchResult[index.model()->data(index).toString()]);
128     lockList();
129     emit selectedWord(index.model()->data(index).toString());
130 }
131
132 void WordListWidget::lockList() {
133     setEnabled(false);
134 }
135
136 void WordListWidget::unlockList() {
137     setEnabled(true);
138 }
139
140 void WordListWidget::setExactMatchString(QString exactMatchString) {
141     _exactMatchString = exactMatchString;
142 }
143
144 QString WordListWidget::exactMatchString() {
145     return _exactMatchString;
146 }