Auto focus to word input.
[mverbiste] / mainwindow.cpp
index e82ebff..35db3e5 100644 (file)
@@ -1,5 +1,6 @@
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
+#include "gui/conjugation.h"
 
 #include <QtCore/QCoreApplication>
 
@@ -12,6 +13,7 @@ MainWindow::MainWindow(QWidget *parent)
 #endif
     ui->setupUi(this);
     setupcodedUI();
+    initverbiste();
 }
 
 void MainWindow::setupcodedUI()
@@ -43,6 +45,7 @@ void MainWindow::setupcodedUI()
     // Clear the word input when Clear button is tapped
     QObject::connect(btnClear, SIGNAL(clicked()), wordinput, SLOT(clear()));
     QObject::connect(btnClear, SIGNAL(clicked()), labVerb, SLOT(clear()));
+    QObject::connect(btnClear, SIGNAL(clicked()), wordinput, SLOT(setFocus()));
 
     QObject::connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
     QObject::connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
@@ -105,10 +108,61 @@ void MainWindow::showExpanded()
 #else
     show();
 #endif
+    wordinput->setFocus();
 }
 
 void MainWindow::startLookup()
 {
-    QString text = wordinput->text();
-    labVerb->setText(text);
+    QString input = wordinput->text();
+
+    FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
+    if (lang != FrenchVerbDictionary::FRENCH)
+    {
+        // TODO: If lang code is not supported?
+    }
+
+    /* Create verb dictionary, accept non-accent input */
+    freVerbDic = new FrenchVerbDictionary(true);
+
+    /* Get input word to look up */
+    const std::string word = input.toLower().toUtf8().constData();
+
+    /*
+     *  For each possible deconjugation, take the infinitive form and
+     *  obtain its complete conjugation.
+     */
+    std::vector<InflectionDesc> v;
+    bool includePronouns = FALSE;    // TODO: Will get this value from external
+
+    freVerbDic->deconjugate(word, v);
+
+    std::string prevUTF8Infinitive, prevTemplateName;
+    for (std::vector<InflectionDesc>::const_iterator it = v.begin();
+         it != v.end(); it++)
+    {
+        const InflectionDesc &d = *it;
+        VVVS conjug;
+        getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
+
+        if (conjug.size() == 0           // if no tenses
+            || conjug[0].size() == 0     // if no infinitive tense
+            || conjug[0][0].size() == 0  // if no person in inf. tense
+            || conjug[0][0][0].empty())  // if infinitive string empty
+        {
+            continue;
+        }
+
+        std::string utf8Infinitive = conjug[0][0][0];
+        if (utf8Infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName)
+            // This result is duplicated
+            continue;
+
+        /* Show on GUI */
+        labVerb->setText(QString::fromUtf8(utf8Infinitive.c_str()));
+    }
+}
+
+void  MainWindow::initverbiste()
+{
+    langCode = "fr";
 }