From c25153fb17c4f0782b9ffc7206427a63ce096903 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?= Date: Sun, 28 Oct 2012 01:35:23 +0700 Subject: [PATCH] Show conjugated results. --- gui/conjugation.cpp | 41 ++++++++++++++++++++++++++++++++ gui/conjugation.h | 12 +++++++++- mainwindow.cpp | 65 +++++++++++++++++++++++++++------------------------ mainwindow.h | 2 +- 4 files changed, 88 insertions(+), 32 deletions(-) diff --git a/gui/conjugation.cpp b/gui/conjugation.cpp index ab4013a..b37ed8a 100644 --- a/gui/conjugation.cpp +++ b/gui/conjugation.cpp @@ -201,3 +201,44 @@ createTableCellText(verbiste::FrenchVerbDictionary &fvd, } return persons; } + + +/** + * Qt version of createTableCellText() above. + * Return a vertor of QStrings, which are conjugations. + **/ +QVector qgetConjugates(verbiste::FrenchVerbDictionary &fvd, + const VVS &tense, + const string &lowerCaseUTF8UserText, + const string &openMark, + const string &closeMark) +{ + string userTextWOAccents = fvd.removeUTF8Accents(lowerCaseUTF8UserText); + QVector 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; +} diff --git a/gui/conjugation.h b/gui/conjugation.h index f940a3c..e9111de 100644 --- a/gui/conjugation.h +++ b/gui/conjugation.h @@ -27,6 +27,8 @@ #include #include +#include +#include typedef std::vector VS; typedef std::vector 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 qgetConjugates(verbiste::FrenchVerbDictionary &fvd, + const VVS &tense, + const std::string &lowerCaseUTF8UserText, + const std::string &openMark, + const std::string &closeMark); #endif /* _H_conjugation */ diff --git a/mainwindow.cpp b/mainwindow.cpp index 8786183..43f6e29 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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 v; + std::vector 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::const_iterator it = v.begin(); - it != v.end(); it++) + for (std::vector::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 persons = qgetConjugates(*verbDict, tenseIterator,inputWord, + "", ""); + for (int i = 0; i < persons.size(); ++i) { + QLabel *lb = new QLabel(persons.at(i)); + lb->setMargin(4); + vbox->addWidget(lb, 1); + } return vbox; } diff --git a/mainwindow.h b/mainwindow.h index d09491d..b735747 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -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(); }; -- 1.7.9.5