Show conjugated results.
authorNguyễn Hồng Quân <ng.hong.quan@gmail.com>
Sat, 27 Oct 2012 18:35:23 +0000 (01:35 +0700)
committerNguyễn Hồng Quân <ng.hong.quan@gmail.com>
Sat, 27 Oct 2012 18:35:23 +0000 (01:35 +0700)
gui/conjugation.cpp
gui/conjugation.h
mainwindow.cpp
mainwindow.h

index ab4013a..b37ed8a 100644 (file)
@@ -201,3 +201,44 @@ createTableCellText(verbiste::FrenchVerbDictionary &fvd,
     }
     return persons;
 }
+
+
+/**
+ * Qt version of createTableCellText() above.
+ * Return a vertor of QStrings, which are conjugations.
+ **/
+QVector<QString> qgetConjugates(verbiste::FrenchVerbDictionary &fvd,
+                                const VVS &tense,
+                                const string &lowerCaseUTF8UserText,
+                                const string &openMark,
+                                const string &closeMark)
+{
+    string userTextWOAccents = fvd.removeUTF8Accents(lowerCaseUTF8UserText);
+    QVector<QString> persons(0);
+    for (VVS::const_iterator it = tense.begin(); it != tense.end(); it++)
+    {
+        const VS &person = *it;
+        QString ver;
+
+//        if (it != tense.begin())
+//            persons += "\n";
+
+        for (VS::const_iterator i = person.begin(); i != person.end(); i++)
+        {
+            if (i != person.begin())
+                ver.append(", ");
+
+            string inflection = fvd.removeUTF8Accents(removePronoun(*i));
+            if (inflection == userTextWOAccents) {
+                const std::string wrapped = openMark + *i + closeMark;
+                ver.append(QString::fromUtf8(wrapped.c_str()));
+            }
+            else {
+                const char *str = (*i).c_str();
+                ver.append(QString::fromUtf8(str));
+            }
+        }
+        persons.append(ver);
+    }
+    return persons;
+}
index f940a3c..e9111de 100644 (file)
@@ -27,6 +27,8 @@
 
 #include <vector>
 #include <string>
+#include <QVector>
+#include <QString>
 
 typedef std::vector<std::string> VS;
 typedef std::vector<VS> VVS;
@@ -94,5 +96,13 @@ std::string createTableCellText(verbiste::FrenchVerbDictionary &fvd,
                                 const std::string &openMark,
                                 const std::string &closeMark);
 
-
+/**
+ * Qt version of createTableCellText() above.
+ * Return a vertor of QStrings, which are conjugations.
+ **/
+QVector<QString> qgetConjugates(verbiste::FrenchVerbDictionary &fvd,
+                                const VVS &tense,
+                                const std::string &lowerCaseUTF8UserText,
+                                const std::string &openMark,
+                                const std::string &closeMark);
 #endif  /* _H_conjugation */
index 8786183..43f6e29 100644 (file)
@@ -32,10 +32,7 @@ void MainWindow::setupcodedUI()
     btnClear = new QPushButton;
     btnClear->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_delete.png"));
     wordinput = new QLineEdit;
-    labVerb = new QLabel();
-    labVerb->setMinimumWidth(250);
     btlayout->addWidget(btnClear);
-    btlayout->addWidget(labVerb);
     btlayout->addWidget(wordinput);
     btnLookup = new QPushButton;  // Lookup button
     btnLookup->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_search.png"));
@@ -45,7 +42,7 @@ void MainWindow::setupcodedUI()
     cent->setLayout(mlayout);
 
     // Clear the word input when Clear button is tapped
-    connect(btnClear, SIGNAL(clicked()), this, SLOT(clearResults()));
+    connect(btnClear, SIGNAL(clicked()), this, SLOT(startAgain()));
 
     connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
     connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
@@ -119,6 +116,11 @@ void  MainWindow::initverbiste()
 
 void MainWindow::startLookup()
 {
+    btnLookup->setText(tr("Please wait..."));
+    btnLookup->setEnabled(false);
+    clearResults();
+    /* Pending the lookup job to the next event loop (redraw the button right now) */
+    QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
     QString input = wordinput->text();
 
     FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
@@ -137,15 +139,16 @@ void MainWindow::startLookup()
      *  For each possible deconjugation, take the infinitive form and
      *  obtain its complete conjugation.
      */
-    std::vector<InflectionDesc> v;
+    std::vector<InflectionDesc> infles;
     bool includePronouns = FALSE;    // TODO: Will get this value from external
     bool isItalian = FALSE;          // TODO: Will get this value from external
 
-    freVerbDic->deconjugate(word, v);
+    freVerbDic->deconjugate(word, infles);
 
+    resultPages->setUpdatesEnabled(false);
     std::string prevUTF8Infinitive, prevTemplateName;
-    for (std::vector<InflectionDesc>::const_iterator it = v.begin();
-         it != v.end(); it++)
+    for (std::vector<InflectionDesc>::const_iterator it = infles.begin();
+         it != infles.end(); it++)
     {
         const InflectionDesc &d = *it;
         VVVS conjug;
@@ -166,14 +169,11 @@ void MainWindow::startLookup()
 
         /* 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++)
-        {
+             t != conjug.end(); t++, i++) {
             if (i == 1)
                 i = 4;
             else if (i == 11)
@@ -187,19 +187,17 @@ void MainWindow::startLookup()
             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;
     }
+    /* Enable the button again */
+    btnLookup->setEnabled(true);
+    btnLookup->setText("");
+    resultPages->setUpdatesEnabled(true);
 }
 
 ResultPage* MainWindow::addResultPage(const std::string &labelText)
@@ -212,15 +210,19 @@ ResultPage* MainWindow::addResultPage(const std::string &labelText)
 
 void MainWindow::clearResults()
 {
-    wordinput->clear();
-    labVerb->clear();
-
     while (resultPages->count()) {
         int lastIndex = resultPages->count() - 1;
         resultPages->widget(lastIndex)->deleteLater();
         resultPages->removeTab(lastIndex);
     }
+}
+
+void MainWindow::startAgain()
+{
+    wordinput->clear();
+    clearResults();
     wordinput->setFocus();
+    btnLookup->setEnabled(true);
 }
 
 QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
@@ -228,19 +230,22 @@ QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
                                         const std::string &inputWord,
                                         FrenchVerbDictionary *verbDict)
 {
+    /* Mode & Tense name */
     QLabel *tenseLabel = new QLabel();
     tenseLabel->setText(QString::fromUtf8(tenseName.c_str()));
+    tenseLabel->setStyleSheet("QLabel {background-color: #44A51C; "
+                              "padding-left: 10px; padding-right: 10px}");
+
+    /* Conjugaison */
     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);
+    QVector<QString> persons = qgetConjugates(*verbDict, tenseIterator,inputWord,
+                                              "<font color='#D20020'>", "</font>");
+    for (int i = 0; i < persons.size(); ++i) {
+        QLabel *lb = new QLabel(persons.at(i));
+        lb->setMargin(4);
+        vbox->addWidget(lb, 1);
+    }
     return vbox;
 }
 
index d09491d..b735747 100644 (file)
@@ -75,7 +75,6 @@ private:
     QPushButton *btnClear;           // Clear button
     QLineEdit   *wordinput;          //  Word input
     QPushButton *btnLookup;          // Lookup button
-    QLabel      *labVerb;
     std::string langCode;
     FrenchVerbDictionary *freVerbDic;
 
@@ -83,6 +82,7 @@ private:
 
 private slots:
     void clearResults();
+    void startAgain();
 };