Can get inflections of verb.
[mverbiste] / mainwindow.cpp
index 31fed0c..8786183 100644 (file)
@@ -1,7 +1,11 @@
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
+#include "gui/conjugation.h"
 
 #include <QtCore/QCoreApplication>
+#ifdef DEBUG
+#include <QDebug>
+#endif
 
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent), ui(new Ui::MainWindow)
@@ -18,13 +22,12 @@ MainWindow::MainWindow(QWidget *parent)
 void MainWindow::setupcodedUI()
 {
     cent = centralWidget();
-    //mlayout = qobject_cast<QVBoxLayout *>(cent->layout());
     mlayout = new QVBoxLayout;
     btlayout = new QHBoxLayout;
 
-    QScrollArea *scrollArea = new QScrollArea;
-    scrollArea->setBackgroundRole(QPalette::Dark);
-    mlayout->addWidget(scrollArea);
+    resultPages = new QTabWidget;
+    resultPages->setTabPosition(QTabWidget::West);
+    mlayout->addWidget(resultPages);
 
     btnClear = new QPushButton;
     btnClear->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_delete.png"));
@@ -42,16 +45,16 @@ void MainWindow::setupcodedUI()
     cent->setLayout(mlayout);
 
     // 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()));
+    connect(btnClear, SIGNAL(clicked()), this, SLOT(clearResults()));
 
-    QObject::connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
-    QObject::connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
+    connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
+    connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
 }
 
 MainWindow::~MainWindow()
 {
     delete ui;
+    delete freVerbDic;
 }
 
 void MainWindow::setOrientation(ScreenOrientation orientation)
@@ -106,35 +109,152 @@ void MainWindow::showExpanded()
 #else
     show();
 #endif
+    wordinput->setFocus();
+}
+
+void  MainWindow::initverbiste()
+{
+    langCode = "fr";
 }
 
 void MainWindow::startLookup()
 {
-    const std::set<std::string> *templateSet = NULL;
     QString input = wordinput->text();
-    labVerb->setText(input);
+
     FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
     if (lang != FrenchVerbDictionary::FRENCH)
     {
-        //wordinput->setText(QString("Fr"));
+        // 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();
+
     /*
-    std::string conjFileName, verbsFileName;
-    FrenchVerbDictionary::getXMLFilenames(conjFileName, verbsFileName, lang);
-    */
-    freVerbDic = new FrenchVerbDictionary(false);
-    const std::string word = input.toUtf8().constData();
-    templateSet = &freVerbDic->getVerbTemplateSet(word);
-    for (std::set<std::string>::const_iterator it = templateSet->begin();
-         it != templateSet->end(); ++it)
-       {
-           const std::string &tname = *it;
-
-           FrenchVerbDictionary::getRadical(word, tname);
-       }
+     *  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
+    bool isItalian = 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 */
+        ResultPage *rsp = addResultPage(utf8Infinitive);
+        //QString infVerb = QString::fromUtf8(utf8Infinitive.c_str());
+        //labVerb->setText(infVerb);
+
+        /* Get modes and tenses of the verb */
+        int i = 0;
+        for (VVVS::const_iterator t = conjug.begin();
+             t != conjug.end(); t++, i++)
+        {
+            if (i == 1)
+                i = 4;
+            else if (i == 11)
+                i = 12;
+            assert(i >= 0 && i < 16);
+
+            int row = i / 4;
+            int col = i % 4;
+
+            std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian);
+            if (utf8TenseName.empty())
+                continue;
+
+#ifdef DEBUG
+            qDebug() << utf8TenseName.c_str();
+#endif
+            QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic);
+            rsp->grid->addLayout(cell, row, col);
+#ifdef DEBUG
+            qDebug() << "Add cell to " << row << col;
+#endif
+        }
+        rsp->packContent();
+        prevUTF8Infinitive = utf8Infinitive;
+        prevTemplateName = d.templateName;
+    }
 }
 
-void  MainWindow::initverbiste()
+ResultPage* MainWindow::addResultPage(const std::string &labelText)
 {
-    langCode = "fr";
+    ResultPage *rp = new ResultPage();
+    QString label = QString::fromUtf8(labelText.c_str());
+    resultPages->addTab(rp->page, label);
+    return rp;
+}
+
+void MainWindow::clearResults()
+{
+    wordinput->clear();
+    labVerb->clear();
+
+    while (resultPages->count()) {
+        int lastIndex = resultPages->count() - 1;
+        resultPages->widget(lastIndex)->deleteLater();
+        resultPages->removeTab(lastIndex);
+    }
+    wordinput->setFocus();
 }
+
+QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
+                                        const std::string &tenseName,
+                                        const std::string &inputWord,
+                                        FrenchVerbDictionary *verbDict)
+{
+    QLabel *tenseLabel = new QLabel();
+    tenseLabel->setText(QString::fromUtf8(tenseName.c_str()));
+    QVBoxLayout *vbox = new QVBoxLayout();
+    vbox->addWidget(tenseLabel);
+    std::string conjugated = createTableCellText(
+                                    *verbDict,
+                                    tenseIterator,
+                                    inputWord,
+                                    "",
+                                    "");
+    QLabel *conjResult = new QLabel();
+    conjResult->setText(QString::fromUtf8(conjugated.c_str()));
+    vbox->addWidget(conjResult);
+    return vbox;
+}
+
+/**** For ResultPage class ****/
+ResultPage::ResultPage()
+    : page(new QScrollArea),
+      grid(new QGridLayout)
+{
+}
+
+void ResultPage::packContent()
+{
+    QWidget *immediate = new QWidget();
+    immediate->setLayout(grid);
+    page->setWidget(immediate);
+}
+