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);
18 void MainWindow::setupcodedUI()
20 cent = centralWidget();
21 mlayout = new QVBoxLayout;
22 btlayout = new QHBoxLayout;
24 resultPages = new QTabWidget;
25 resultPages->setStyleSheet("QTabBar::tab { height: 50px }");
26 mlayout->addWidget(resultPages);
28 btnPron = new QCheckBox();
29 btnPron->setIcon(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_conference_avatar.png"));
30 btnClear = new QPushButton; /* Clearbutton */
31 btnClear->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_delete.png"));
32 wordinput = new QLineEdit; /* Word input */
33 btlayout->addWidget(btnPron);
34 btlayout->addWidget(btnClear);
35 btlayout->addWidget(wordinput);
36 btnLookup = new QPushButton; /* Lookup button */
37 btnLookup->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_search.png"));
38 btlayout->addWidget(btnLookup);
40 mlayout->addLayout(btlayout);
41 cent->setLayout(mlayout);
43 // Clear the word input when Clear button is tapped
44 connect(btnClear, SIGNAL(clicked()), this, SLOT(startAgain()));
46 connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
47 connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
48 connect(btnPron, SIGNAL(clicked()), this, SLOT(startLookup()));
51 QIcon *icon = new QIcon();
52 icon->addFile(ICONFILE);
56 aboutDialog = new AboutDialog(ICONFILE, QString("MVerbiste v%1").arg(VERSTR));
57 aboutDialog->setIntro(trUtf8("A French conjugation utility for Maemo and MeeGo"));
58 aboutDialog->addAuthor(QString::fromUtf8("Nguyễn Hồng Quân <ng.hong.quan@gmail.com>\nPierre Sarrazin <sarrazip@sarrazip.com>"));
61 QMenu *menu = ui->menuBar->addMenu(tr("Top menu"));
62 QAction *act_about = menu->addAction(tr("About"));
63 connect(act_about, SIGNAL(triggered()), aboutDialog, SLOT(show()));
65 QActionGroup *filterGroup = new QActionGroup(this);
66 filterGroup->setExclusive(true);
67 filFrench = new QAction(tr("Search French"), filterGroup);
68 filFrench->setCheckable(true);
69 filFrench->setChecked(true);
70 filItalian = new QAction(tr("Search Italian"), filterGroup);
71 filItalian->setCheckable(true);
72 menu->addActions(filterGroup->actions());
73 connect(filItalian, SIGNAL(changed()), this, SLOT(switchLang()));
74 connect(filFrench, SIGNAL(changed()), this, SLOT(switchLang()));
77 MainWindow::~MainWindow()
84 void MainWindow::setOrientation(ScreenOrientation orientation)
86 #if defined(Q_OS_SYMBIAN)
87 // If the version of Qt on the device is < 4.7.2, that attribute won't work
88 if (orientation != ScreenOrientationAuto) {
89 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
90 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
91 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
95 #endif // Q_OS_SYMBIAN
97 Qt::WidgetAttribute attribute;
98 switch (orientation) {
99 #if QT_VERSION < 0x040702
100 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
101 case ScreenOrientationLockPortrait:
102 attribute = static_cast<Qt::WidgetAttribute>(128);
104 case ScreenOrientationLockLandscape:
105 attribute = static_cast<Qt::WidgetAttribute>(129);
108 case ScreenOrientationAuto:
109 attribute = static_cast<Qt::WidgetAttribute>(130);
111 #else // QT_VERSION < 0x040702
112 case ScreenOrientationLockPortrait:
113 attribute = Qt::WA_LockPortraitOrientation;
115 case ScreenOrientationLockLandscape:
116 attribute = Qt::WA_LockLandscapeOrientation;
119 case ScreenOrientationAuto:
120 attribute = Qt::WA_AutoOrientation;
122 #endif // QT_VERSION < 0x040702
124 setAttribute(attribute, true);
127 void MainWindow::showExpanded()
129 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
131 #elif defined(Q_WS_MAEMO_5)
137 wordinput->setFocus();
140 void MainWindow::initverbiste()
144 FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
145 if (lang != FrenchVerbDictionary::FRENCH)
147 // TODO: If lang code is not supported?
150 /* Create verb dictionary, accept non-accent input */
151 freVerbDic = new FrenchVerbDictionary(true);
154 void MainWindow::switchLang()
156 FrenchVerbDictionary::Language curlang = freVerbDic->getLanguage();
157 FrenchVerbDictionary::Language targetlang = filItalian->isChecked() ? FrenchVerbDictionary::ITALIAN
158 : FrenchVerbDictionary::FRENCH;
159 if (curlang == targetlang) {
163 std::string conjFN, verbsFN;
164 FrenchVerbDictionary::getXMLFilenames(conjFN, verbsFN, targetlang);
166 freVerbDic = new FrenchVerbDictionary(conjFN, verbsFN, true, targetlang);
169 void MainWindow::startLookup()
171 QString input = wordinput->text().trimmed();
172 if (input.isEmpty()) {
176 btnLookup->setText(tr("Please wait..."));
177 btnLookup->setEnabled(false);
179 /* Pending the lookup job to the next event loop (redraw the button right now) */
180 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
182 /* Get input word to look up */
183 const std::string word = input.toLower().toUtf8().constData();
186 * For each possible deconjugation, take the infinitive form and
187 * obtain its complete conjugation.
189 std::vector<InflectionDesc> infles;
190 bool includePronouns = btnPron->isChecked();
191 bool isItalian = filItalian->isChecked(); // TODO: Will get this value from external
193 freVerbDic->deconjugate(word, infles);
195 resultPages->setUpdatesEnabled(false);
196 std::string prevUTF8Infinitive, prevTemplateName; /* Remember found word
197 to avoid conjugating again */
199 for (std::vector<InflectionDesc>::const_iterator it = infles.begin();
200 it != infles.end(); it++)
202 const InflectionDesc &d = *it;
204 /* If this infinitive has been conjugated, we skip to the next infinitive */
205 if (d.infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName) {
210 getConjugation(*freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
212 if (conjug.size() == 0 // if no tenses
213 || conjug[0].size() == 0 // if no infinitive tense
214 || conjug[0][0].size() == 0 // if no person in inf. tense
215 || conjug[0][0][0].empty()) // if infinitive string empty
220 std::string utf8Infinitive = conjug[0][0][0];
222 /* Add result to GUI (not show yet) */
223 ResultPage *rsp = addResultPage(utf8Infinitive);
225 /* Get modes and tenses of the verb */
227 for (VVVS::const_iterator t = conjug.begin();
228 t != conjug.end(); t++, i++) {
233 assert(i >= 0 && i < 16);
238 std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian);
239 if (utf8TenseName.empty())
242 QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic);
243 rsp->grid->addLayout(cell, row, col);
246 /* Show the result on GUI */
248 prevUTF8Infinitive = utf8Infinitive;
249 prevTemplateName = d.templateName;
252 /* Enable the button again */
253 btnLookup->setEnabled(true);
254 btnLookup->setText("");
255 resultPages->setUpdatesEnabled(true);
258 ResultPage* MainWindow::addResultPage(const std::string &labelText)
260 ResultPage *rp = new ResultPage();
261 QString label = QString::fromUtf8(labelText.c_str());
262 resultPages->addTab(rp->page, label);
266 void MainWindow::clearResults()
268 while (resultPages->count()) {
269 int lastIndex = resultPages->count() - 1;
270 resultPages->widget(lastIndex)->deleteLater();
271 resultPages->removeTab(lastIndex);
275 void MainWindow::startAgain()
279 wordinput->setFocus();
280 btnLookup->setEnabled(true);
283 QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
284 const std::string &tenseName,
285 const std::string &inputWord,
286 FrenchVerbDictionary *verbDict)
288 /* Mode & Tense name */
289 QLabel *tenseLabel = new QLabel();
290 tenseLabel->setText(QString::fromUtf8(tenseName.c_str()));
291 tenseLabel->setStyleSheet("QLabel {background-color: #44A51C;"
292 "border-top-left-radius: 4px;"
293 "border-top-right-radius: 4px;"
294 "padding-left: 10px; padding-right: 10px}");
297 QVBoxLayout *vbox = new QVBoxLayout();
298 vbox->addWidget(tenseLabel);
299 QVector<QString> persons = qgetConjugates(*verbDict, tenseIterator,inputWord,
300 "<font color='#D20020'>", "</font>");
301 for (int i = 0; i < persons.size(); ++i) {
302 QLabel *lb = new QLabel(persons.at(i));
304 vbox->addWidget(lb, 1);
309 /**** For ResultPage class ****/
310 ResultPage::ResultPage()
311 : page(new QScrollArea),
312 grid(new QGridLayout)
316 void ResultPage::packContent()
318 QWidget *immediate = new QWidget();
319 immediate->setLayout(grid);
320 page->setWidget(immediate);