1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "gui/conjugation.h"
5 #include <QtCore/QCoreApplication>
10 MainWindow::MainWindow(QWidget *parent)
11 : QMainWindow(parent), ui(new Ui::MainWindow)
14 this->setAttribute(Qt::WA_Maemo5StackedWindow);
15 this->setWindowFlags(Qt::Window);
22 void MainWindow::setupcodedUI()
24 cent = centralWidget();
25 mlayout = new QVBoxLayout;
26 btlayout = new QHBoxLayout;
28 resultPages = new QTabWidget;
29 resultPages->setTabPosition(QTabWidget::West);
30 mlayout->addWidget(resultPages);
32 btnClear = new QPushButton;
33 btnClear->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_delete.png"));
34 wordinput = new QLineEdit;
35 btlayout->addWidget(btnClear);
36 btlayout->addWidget(wordinput);
37 btnLookup = new QPushButton; // Lookup button
38 btnLookup->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_search.png"));
39 btlayout->addWidget(btnLookup);
41 mlayout->addLayout(btlayout);
42 cent->setLayout(mlayout);
44 // Clear the word input when Clear button is tapped
45 connect(btnClear, SIGNAL(clicked()), this, SLOT(startAgain()));
47 connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
48 connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
51 MainWindow::~MainWindow()
57 void MainWindow::setOrientation(ScreenOrientation orientation)
59 #if defined(Q_OS_SYMBIAN)
60 // If the version of Qt on the device is < 4.7.2, that attribute won't work
61 if (orientation != ScreenOrientationAuto) {
62 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
63 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
64 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
68 #endif // Q_OS_SYMBIAN
70 Qt::WidgetAttribute attribute;
71 switch (orientation) {
72 #if QT_VERSION < 0x040702
73 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
74 case ScreenOrientationLockPortrait:
75 attribute = static_cast<Qt::WidgetAttribute>(128);
77 case ScreenOrientationLockLandscape:
78 attribute = static_cast<Qt::WidgetAttribute>(129);
81 case ScreenOrientationAuto:
82 attribute = static_cast<Qt::WidgetAttribute>(130);
84 #else // QT_VERSION < 0x040702
85 case ScreenOrientationLockPortrait:
86 attribute = Qt::WA_LockPortraitOrientation;
88 case ScreenOrientationLockLandscape:
89 attribute = Qt::WA_LockLandscapeOrientation;
92 case ScreenOrientationAuto:
93 attribute = Qt::WA_AutoOrientation;
95 #endif // QT_VERSION < 0x040702
97 setAttribute(attribute, true);
100 void MainWindow::showExpanded()
102 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
104 #elif defined(Q_WS_MAEMO_5)
109 wordinput->setFocus();
112 void MainWindow::initverbiste()
116 FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
117 if (lang != FrenchVerbDictionary::FRENCH)
119 // TODO: If lang code is not supported?
122 /* Create verb dictionary, accept non-accent input */
123 freVerbDic = new FrenchVerbDictionary(true);
126 void MainWindow::startLookup()
128 btnLookup->setText(tr("Please wait..."));
129 btnLookup->setEnabled(false);
131 /* Pending the lookup job to the next event loop (redraw the button right now) */
132 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
133 QString input = wordinput->text();
135 /* Get input word to look up */
136 const std::string word = input.toLower().toUtf8().constData();
139 * For each possible deconjugation, take the infinitive form and
140 * obtain its complete conjugation.
142 std::vector<InflectionDesc> infles;
143 bool includePronouns = FALSE; // TODO: Will get this value from external
144 bool isItalian = FALSE; // TODO: Will get this value from external
146 freVerbDic->deconjugate(word, infles);
148 resultPages->setUpdatesEnabled(false);
149 std::string prevUTF8Infinitive, prevTemplateName;
150 for (std::vector<InflectionDesc>::const_iterator it = infles.begin();
151 it != infles.end(); it++)
153 const InflectionDesc &d = *it;
155 getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
157 if (conjug.size() == 0 // if no tenses
158 || conjug[0].size() == 0 // if no infinitive tense
159 || conjug[0][0].size() == 0 // if no person in inf. tense
160 || conjug[0][0][0].empty()) // if infinitive string empty
165 std::string utf8Infinitive = conjug[0][0][0];
166 if (utf8Infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName)
167 // This result is duplicated
171 ResultPage *rsp = addResultPage(utf8Infinitive);
173 /* Get modes and tenses of the verb */
175 for (VVVS::const_iterator t = conjug.begin();
176 t != conjug.end(); t++, i++) {
181 assert(i >= 0 && i < 16);
186 std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian);
187 if (utf8TenseName.empty())
190 QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic);
191 rsp->grid->addLayout(cell, row, col);
194 prevUTF8Infinitive = utf8Infinitive;
195 prevTemplateName = d.templateName;
197 /* Enable the button again */
198 btnLookup->setEnabled(true);
199 btnLookup->setText("");
200 resultPages->setUpdatesEnabled(true);
203 ResultPage* MainWindow::addResultPage(const std::string &labelText)
205 ResultPage *rp = new ResultPage();
206 QString label = QString::fromUtf8(labelText.c_str());
207 resultPages->addTab(rp->page, label);
211 void MainWindow::clearResults()
213 while (resultPages->count()) {
214 int lastIndex = resultPages->count() - 1;
215 resultPages->widget(lastIndex)->deleteLater();
216 resultPages->removeTab(lastIndex);
220 void MainWindow::startAgain()
224 wordinput->setFocus();
225 btnLookup->setEnabled(true);
228 QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
229 const std::string &tenseName,
230 const std::string &inputWord,
231 FrenchVerbDictionary *verbDict)
233 /* Mode & Tense name */
234 QLabel *tenseLabel = new QLabel();
235 tenseLabel->setText(QString::fromUtf8(tenseName.c_str()));
236 tenseLabel->setStyleSheet("QLabel {background-color: #44A51C; "
237 "padding-left: 10px; padding-right: 10px}");
240 QVBoxLayout *vbox = new QVBoxLayout();
241 vbox->addWidget(tenseLabel);
242 QVector<QString> persons = qgetConjugates(*verbDict, tenseIterator,inputWord,
243 "<font color='#D20020'>", "</font>");
244 for (int i = 0; i < persons.size(); ++i) {
245 QLabel *lb = new QLabel(persons.at(i));
247 vbox->addWidget(lb, 1);
252 /**** For ResultPage class ****/
253 ResultPage::ResultPage()
254 : page(new QScrollArea),
255 grid(new QGridLayout)
259 void ResultPage::packContent()
261 QWidget *immediate = new QWidget();
262 immediate->setLayout(grid);
263 page->setWidget(immediate);