X-Git-Url: http://git.maemo.org/git/?p=mverbiste;a=blobdiff_plain;f=mainwindow.cpp;h=9ed6d6c030f21ae05620fc7c2dd7839fd7836784;hp=dbba68b724028be67c36b71a7d69fa7561b4a954;hb=24c4175dd69b2b8e87506247a34d4e765d86bb31;hpb=6b71b328a4ed0cc4e8236fc27f4dd0dc392e1c34 diff --git a/mainwindow.cpp b/mainwindow.cpp index dbba68b..9ed6d6c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -3,7 +3,6 @@ #include "gui/conjugation.h" #include -#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) @@ -14,7 +13,6 @@ MainWindow::MainWindow(QWidget *parent) #endif ui->setupUi(this); setupcodedUI(); - initverbiste(); } void MainWindow::setupcodedUI() @@ -30,10 +28,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")); @@ -43,7 +38,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())); @@ -52,6 +47,7 @@ void MainWindow::setupcodedUI() MainWindow::~MainWindow() { delete ui; + delete freVerbDic; } void MainWindow::setOrientation(ScreenOrientation orientation) @@ -106,17 +102,13 @@ void MainWindow::showExpanded() #else show(); #endif + initverbiste(); wordinput->setFocus(); } void MainWindow::initverbiste() { langCode = "fr"; -} - -void MainWindow::startLookup() -{ - QString input = wordinput->text(); FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode); if (lang != FrenchVerbDictionary::FRENCH) @@ -126,6 +118,20 @@ void MainWindow::startLookup() /* Create verb dictionary, accept non-accent input */ freVerbDic = new FrenchVerbDictionary(true); +} + +void MainWindow::startLookup() +{ + QString input = wordinput->text().trimmed(); + if (input.isEmpty()) { + return; + } + + 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); /* Get input word to look up */ const std::string word = input.toLower().toUtf8().constData(); @@ -134,19 +140,53 @@ 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); +#ifndef QT_NO_DEBUG + timer.start(); + qDebug() << "Start " << timer.elapsed(); +#endif + freVerbDic->deconjugate(word, infles); - std::string prevUTF8Infinitive, prevTemplateName; - for (std::vector::const_iterator it = v.begin(); - it != v.end(); it++) + resultPages->setUpdatesEnabled(false); + std::string prevUTF8Infinitive, prevTemplateName; /* Remember found word + to avoid conjugating again */ + + for (std::vector::const_iterator it = infles.begin(); + it != infles.end(); it++) { const InflectionDesc &d = *it; + +#ifndef QT_NO_DEBUG + qDebug() << ">> Infinitive " << d.infinitive.c_str(); + qDebug() << " Template " << d.templateName.c_str(); +#endif + /* If this infinitive has been conjugated, we skip to the next infinitive */ + if (d.infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName) { + continue; + } + /* FIXME: + * In original source (Verbiste), this checking is done later, + * after getConjugation(). I place it here to avoid calling again + * multitimes getConjugation(), which is very slow. + * We need to test more to see which place is more correct. + */ + VVVS conjug; - getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, includePronouns); +#ifndef QT_NO_DEBUG + qDebug() << " START getConjugation " << timer.elapsed(); +#endif + getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, + #ifndef QT_NO_DEBUG + timer, + #endif + includePronouns); + +#ifndef QT_NO_DEBUG + qDebug() << " getConjugation() returns: " << timer.elapsed(); +#endif if (conjug.size() == 0 // if no tenses || conjug[0].size() == 0 // if no infinitive tense @@ -157,20 +197,18 @@ void MainWindow::startLookup() } std::string utf8Infinitive = conjug[0][0][0]; - if (utf8Infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName) - // This result is duplicated - continue; +#ifndef QT_NO_DEBUG + qDebug() << " Infinitive " << utf8Infinitive.c_str(); + qDebug() << " Template " << d.templateName.c_str(); +#endif - /* Show on GUI */ + /* Add result to GUI (not show yet) */ 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) @@ -179,18 +217,25 @@ void MainWindow::startLookup() int row = i / 4; int col = i % 4; - qDebug() << row << col; std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian); if (utf8TenseName.empty()) continue; - qDebug() << utf8TenseName.c_str(); + QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic); + rsp->grid->addLayout(cell, row, col); } + /* Show the result on GUI */ + 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) @@ -203,22 +248,56 @@ 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, + const std::string &tenseName, + 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); + 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; } /**** For ResultPage class ****/ ResultPage::ResultPage() : page(new QScrollArea), - table(new QTableWidget) + grid(new QGridLayout) { } +void ResultPage::packContent() +{ + QWidget *immediate = new QWidget(); + immediate->setLayout(grid); + page->setWidget(immediate); +}