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 btnLookup->setText(tr("Please wait..."));
126 btnLookup->setEnabled(false);
128 /* Pending the lookup job to the next event loop (redraw the button right now) */
129 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
130 QString input = wordinput->text();
132 /* Get input word to look up */
133 const std::string word = input.toLower().toUtf8().constData();
136 * For each possible deconjugation, take the infinitive form and
137 * obtain its complete conjugation.
139 std::vector<InflectionDesc> infles;
140 bool includePronouns = FALSE; // TODO: Will get this value from external
141 bool isItalian = FALSE; // TODO: Will get this value from external
143 freVerbDic->deconjugate(word, infles);
145 resultPages->setUpdatesEnabled(false);
146 std::string prevUTF8Infinitive, prevTemplateName;
147 for (std::vector<InflectionDesc>::const_iterator it = infles.begin();
148 it != infles.end(); it++)
150 const InflectionDesc &d = *it;
152 getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
154 if (conjug.size() == 0 // if no tenses
155 || conjug[0].size() == 0 // if no infinitive tense
156 || conjug[0][0].size() == 0 // if no person in inf. tense
157 || conjug[0][0][0].empty()) // if infinitive string empty
162 std::string utf8Infinitive = conjug[0][0][0];
163 if (utf8Infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName)
164 // This result is duplicated
168 ResultPage *rsp = addResultPage(utf8Infinitive);
170 /* Get modes and tenses of the verb */
172 for (VVVS::const_iterator t = conjug.begin();
173 t != conjug.end(); t++, i++) {
178 assert(i >= 0 && i < 16);
183 std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian);
184 if (utf8TenseName.empty())
187 QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic);
188 rsp->grid->addLayout(cell, row, col);
191 prevUTF8Infinitive = utf8Infinitive;
192 prevTemplateName = d.templateName;
194 /* Enable the button again */
195 btnLookup->setEnabled(true);
196 btnLookup->setText("");
197 resultPages->setUpdatesEnabled(true);
200 ResultPage* MainWindow::addResultPage(const std::string &labelText)
202 ResultPage *rp = new ResultPage();
203 QString label = QString::fromUtf8(labelText.c_str());
204 resultPages->addTab(rp->page, label);
208 void MainWindow::clearResults()
210 while (resultPages->count()) {
211 int lastIndex = resultPages->count() - 1;
212 resultPages->widget(lastIndex)->deleteLater();
213 resultPages->removeTab(lastIndex);
217 void MainWindow::startAgain()
221 wordinput->setFocus();
222 btnLookup->setEnabled(true);
225 QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
226 const std::string &tenseName,
227 const std::string &inputWord,
228 FrenchVerbDictionary *verbDict)
230 /* Mode & Tense name */
231 QLabel *tenseLabel = new QLabel();
232 tenseLabel->setText(QString::fromUtf8(tenseName.c_str()));
233 tenseLabel->setStyleSheet("QLabel {background-color: #44A51C; "
234 "padding-left: 10px; padding-right: 10px}");
237 QVBoxLayout *vbox = new QVBoxLayout();
238 vbox->addWidget(tenseLabel);
239 QVector<QString> persons = qgetConjugates(*verbDict, tenseIterator,inputWord,
240 "<font color='#D20020'>", "</font>");
241 for (int i = 0; i < persons.size(); ++i) {
242 QLabel *lb = new QLabel(persons.at(i));
244 vbox->addWidget(lb, 1);
249 /**** For ResultPage class ****/
250 ResultPage::ResultPage()
251 : page(new QScrollArea),
252 grid(new QGridLayout)
256 void ResultPage::packContent()
258 QWidget *immediate = new QWidget();
259 immediate->setLayout(grid);
260 page->setWidget(immediate);