Added direct search of translation, changed searching for translation of words list...
[mdictionary] / trunk / src / base / gui / WordListWidget.cpp
index 6536d6e..ddb8f3f 100644 (file)
 #include "../../includes/translation.h"
 #include <QMultiHash>
 
+#ifdef Q_WS_MAEMO_5
+    #include <QMaemo5InformationBox>
+#endif
 
 WordListWidget::WordListWidget(Backbone *backbone, QWidget *parent):
     QListView(parent) {
 
     this->backbone = backbone;
 
+    _exactMatchString = QString();
+
     wordListModel = new QStringListModel();
 
     connect(backbone, SIGNAL(ready()),
@@ -69,17 +74,51 @@ void WordListWidget::showSearchResults() {
     searchResult.clear();
     QMultiHash<QString, Translation*> result = backbone->result();
 
-    QMultiHash<QString, Translation*>::iterator i;
-    for(i = result.begin(); i != result.end(); i++) {
-        if(!searchResult.contains(i.key())) {
-           addWord(i.key());
-        }
-        searchResult[i.key()].push_back(i.value());
+    if(result.count() == 0) {
+        #ifdef Q_WS_MAEMO_5
+        QMaemo5InformationBox::information(this,
+                            tr("Can't find any matching words"),
+                            QMaemo5InformationBox::DefaultTimeout);
+        #endif
     }
+    else
+    {
+        QMultiHash<QString, Translation*>::iterator i;
+        for(i = result.begin(); i != result.end(); i++) {
+
+            if(!searchResult.contains(i.key())) {
+               addWord(i.key());
+            }
 
-    wordListModel->sort(0, Qt::AscendingOrder);
+            searchResult[i.key()].push_back(i.value());
+        }
 
-    scrollTo(model()->index(0,0));
+        bool foundExactMatch = false;
+
+        if(!_exactMatchString.isEmpty()) {
+            QHash<QString, QList<Translation*> >::iterator j;
+            for(j = searchResult.begin(); j != searchResult.end(); j++) {
+                if(j.key() == _exactMatchString && !foundExactMatch) {
+                    foundExactMatch = true;
+                    clear();
+                    backbone->searchHtml(j.value());
+                    emit selectedWord(j.key());
+                    break;
+                }
+            }
+            if(!foundExactMatch) {
+                #ifdef Q_WS_MAEMO_5
+                QMaemo5InformationBox::information(this,
+                                    tr("Can't find exactly matching word"),
+                                    QMaemo5InformationBox::DefaultTimeout);
+                #endif
+            }
+            setExactMatchString(QString());
+        }
+        wordListModel->sort(0, Qt::AscendingOrder);
+
+        scrollTo(model()->index(0,0));
+    }
 
     unlockList();
 }
@@ -97,3 +136,11 @@ void WordListWidget::lockList() {
 void WordListWidget::unlockList() {
     setEnabled(true);
 }
+
+void WordListWidget::setExactMatchString(QString exactMatchString) {
+    _exactMatchString = exactMatchString;
+}
+
+QString WordListWidget::exactMatchString() {
+    return _exactMatchString;
+}