1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "gui/conjugation.h"
5 #include <QtCore/QCoreApplication>
7 MainWindow::MainWindow(QWidget *parent)
8 : QMainWindow(parent), ui(new Ui::MainWindow)
11 this->setAttribute(Qt::WA_Maemo5StackedWindow);
12 this->setWindowFlags(Qt::Window);
19 void MainWindow::setupcodedUI()
21 cent = centralWidget();
22 mlayout = new QVBoxLayout;
23 btlayout = new QHBoxLayout;
25 resultPages = new QTabWidget;
26 resultPages->setTabPosition(QTabWidget::West);
27 mlayout->addWidget(resultPages);
29 btnClear = new QPushButton;
30 btnClear->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_delete.png"));
31 wordinput = new QLineEdit;
32 btlayout->addWidget(btnClear);
33 btlayout->addWidget(wordinput);
34 btnLookup = new QPushButton; // Lookup button
35 btnLookup->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_search.png"));
36 btlayout->addWidget(btnLookup);
38 mlayout->addLayout(btlayout);
39 cent->setLayout(mlayout);
41 // Clear the word input when Clear button is tapped
42 connect(btnClear, SIGNAL(clicked()), this, SLOT(startAgain()));
44 connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
45 connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
48 MainWindow::~MainWindow()
54 void MainWindow::setOrientation(ScreenOrientation orientation)
56 #if defined(Q_OS_SYMBIAN)
57 // If the version of Qt on the device is < 4.7.2, that attribute won't work
58 if (orientation != ScreenOrientationAuto) {
59 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
60 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
61 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
65 #endif // Q_OS_SYMBIAN
67 Qt::WidgetAttribute attribute;
68 switch (orientation) {
69 #if QT_VERSION < 0x040702
70 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
71 case ScreenOrientationLockPortrait:
72 attribute = static_cast<Qt::WidgetAttribute>(128);
74 case ScreenOrientationLockLandscape:
75 attribute = static_cast<Qt::WidgetAttribute>(129);
78 case ScreenOrientationAuto:
79 attribute = static_cast<Qt::WidgetAttribute>(130);
81 #else // QT_VERSION < 0x040702
82 case ScreenOrientationLockPortrait:
83 attribute = Qt::WA_LockPortraitOrientation;
85 case ScreenOrientationLockLandscape:
86 attribute = Qt::WA_LockLandscapeOrientation;
89 case ScreenOrientationAuto:
90 attribute = Qt::WA_AutoOrientation;
92 #endif // QT_VERSION < 0x040702
94 setAttribute(attribute, true);
97 void MainWindow::showExpanded()
99 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
101 #elif defined(Q_WS_MAEMO_5)
106 wordinput->setFocus();
109 void MainWindow::initverbiste()
113 FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
114 if (lang != FrenchVerbDictionary::FRENCH)
116 // TODO: If lang code is not supported?
119 /* Create verb dictionary, accept non-accent input */
120 freVerbDic = new FrenchVerbDictionary(true);
123 void MainWindow::startLookup()
125 QString input = wordinput->text().trimmed();
126 if (input.isEmpty()) {
130 btnLookup->setText(tr("Please wait..."));
131 btnLookup->setEnabled(false);
133 /* Pending the lookup job to the next event loop (redraw the button right now) */
134 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
136 /* Get input word to look up */
137 const std::string word = input.toLower().toUtf8().constData();
140 * For each possible deconjugation, take the infinitive form and
141 * obtain its complete conjugation.
143 std::vector<InflectionDesc> infles;
144 bool includePronouns = FALSE; // TODO: Will get this value from external
145 bool isItalian = FALSE; // TODO: Will get this value from external
147 freVerbDic->deconjugate(word, infles);
149 resultPages->setUpdatesEnabled(false);
150 std::string prevUTF8Infinitive, prevTemplateName; /* Remember found word
151 to avoid conjugating again */
153 for (std::vector<InflectionDesc>::const_iterator it = infles.begin();
154 it != infles.end(); it++)
156 const InflectionDesc &d = *it;
159 qDebug() << ">> Infinitive " << d.infinitive.c_str();
160 qDebug() << " Template " << d.templateName.c_str();
162 /* If this infinitive has been conjugated, we skip to the next infinitive */
163 if (d.infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName) {
167 * In original source (Verbiste), this checking is done later,
168 * after getConjugation(). I place it here to avoid calling again
169 * multitimes getConjugation(), which is very slow.
170 * We need to test more to see which place is more correct.
174 getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
176 if (conjug.size() == 0 // if no tenses
177 || conjug[0].size() == 0 // if no infinitive tense
178 || conjug[0][0].size() == 0 // if no person in inf. tense
179 || conjug[0][0][0].empty()) // if infinitive string empty
184 std::string utf8Infinitive = conjug[0][0][0];
186 qDebug() << " getConjugation() returns:";
187 qDebug() << " Infinitive " << utf8Infinitive.c_str() << " at " << timer.elapsed();
188 qDebug() << " Template " << d.templateName.c_str();
191 /* Add result to GUI (not show yet) */
192 ResultPage *rsp = addResultPage(utf8Infinitive);
194 /* Get modes and tenses of the verb */
196 for (VVVS::const_iterator t = conjug.begin();
197 t != conjug.end(); t++, i++) {
202 assert(i >= 0 && i < 16);
207 std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian);
208 if (utf8TenseName.empty())
211 QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic);
212 rsp->grid->addLayout(cell, row, col);
214 /* Show the result on GUI */
216 prevUTF8Infinitive = utf8Infinitive;
217 prevTemplateName = d.templateName;
219 /* Enable the button again */
220 btnLookup->setEnabled(true);
221 btnLookup->setText("");
222 resultPages->setUpdatesEnabled(true);
225 ResultPage* MainWindow::addResultPage(const std::string &labelText)
227 ResultPage *rp = new ResultPage();
228 QString label = QString::fromUtf8(labelText.c_str());
229 resultPages->addTab(rp->page, label);
233 void MainWindow::clearResults()
235 while (resultPages->count()) {
236 int lastIndex = resultPages->count() - 1;
237 resultPages->widget(lastIndex)->deleteLater();
238 resultPages->removeTab(lastIndex);
242 void MainWindow::startAgain()
246 wordinput->setFocus();
247 btnLookup->setEnabled(true);
250 QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
251 const std::string &tenseName,
252 const std::string &inputWord,
253 FrenchVerbDictionary *verbDict)
255 /* Mode & Tense name */
256 QLabel *tenseLabel = new QLabel();
257 tenseLabel->setText(QString::fromUtf8(tenseName.c_str()));
258 tenseLabel->setStyleSheet("QLabel {background-color: #44A51C; "
259 "padding-left: 10px; padding-right: 10px}");
262 QVBoxLayout *vbox = new QVBoxLayout();
263 vbox->addWidget(tenseLabel);
264 QVector<QString> persons = qgetConjugates(*verbDict, tenseIterator,inputWord,
265 "<font color='#D20020'>", "</font>");
266 for (int i = 0; i < persons.size(); ++i) {
267 QLabel *lb = new QLabel(persons.at(i));
269 vbox->addWidget(lb, 1);
274 /**** For ResultPage class ****/
275 ResultPage::ResultPage()
276 : page(new QScrollArea),
277 grid(new QGridLayout)
281 void ResultPage::packContent()
283 QWidget *immediate = new QWidget();
284 immediate->setLayout(grid);
285 page->setWidget(immediate);