1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 //Created by Mateusz Półrola
24 #include "WordListWidget.h"
26 #include "../../includes/translation.h"
30 #include <QMaemo5InformationBox>
33 WordListWidget::WordListWidget(Backbone *backbone, QWidget *parent):
36 this->backbone = backbone;
38 _exactMatchString = QString();
40 wordListModel = new QStringListModel();
42 connect(backbone, SIGNAL(ready()),
43 this, SLOT(showSearchResults()));
45 connect(this, SIGNAL(clicked(QModelIndex)),
46 this, SLOT(itemClicked(QModelIndex)));
48 connect(backbone, SIGNAL(htmlReady()),
49 this, SLOT(unlockList()));
51 setModel(wordListModel);
54 void WordListWidget::addWord(QString word) {
55 int wordsCount = wordListModel->rowCount();
57 wordListModel->insertRow(wordsCount);
59 QModelIndex newWordIndex = wordListModel->index(wordsCount);
61 wordListModel->setData(newWordIndex, word);
64 void WordListWidget::clear() {
65 int wordsCount = wordListModel->rowCount();
67 for(int i = 0; i < wordsCount; i++) {
68 wordListModel->removeRow(0);
72 void WordListWidget::showSearchResults() {
75 QMultiHash<QString, Translation*> result = backbone->result();
77 if(result.count() == 0) {
79 QMaemo5InformationBox::information(this,
80 tr("Can't find any matching words"),
81 QMaemo5InformationBox::DefaultTimeout);
86 QMultiHash<QString, Translation*>::iterator i;
87 for(i = result.begin(); i != result.end(); i++) {
89 if(!searchResult.contains(i.key())) {
93 searchResult[i.key()].push_back(i.value());
96 bool foundExactMatch = false;
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;
104 backbone->searchHtml(j.value());
105 emit selectedWord(j.key());
109 if(!foundExactMatch) {
111 QMaemo5InformationBox::information(this,
112 tr("Can't find exactly matching word"),
113 QMaemo5InformationBox::DefaultTimeout);
116 setExactMatchString(QString());
118 wordListModel->sort(0, Qt::AscendingOrder);
120 scrollTo(model()->index(0,0));
126 void WordListWidget::itemClicked(QModelIndex index) {
127 backbone->searchHtml(searchResult[index.model()->data(index).toString()]);
129 emit selectedWord(index.model()->data(index).toString());
132 void WordListWidget::lockList() {
136 void WordListWidget::unlockList() {
140 void WordListWidget::setExactMatchString(QString exactMatchString) {
141 _exactMatchString = exactMatchString;
144 QString WordListWidget::exactMatchString() {
145 return _exactMatchString;