Change the debug directive.
[mverbiste] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "gui/conjugation.h"
4
5 #include <QtCore/QCoreApplication>
6
7 MainWindow::MainWindow(QWidget *parent)
8     : QMainWindow(parent), ui(new Ui::MainWindow)
9 {
10 #ifdef Q_WS_MAEMO_5
11     this->setAttribute(Qt::WA_Maemo5StackedWindow);
12     this->setWindowFlags(Qt::Window);
13 #endif
14     ui->setupUi(this);
15     setupcodedUI();
16     initverbiste();
17 }
18
19 void MainWindow::setupcodedUI()
20 {
21     cent = centralWidget();
22     mlayout = new QVBoxLayout;
23     btlayout = new QHBoxLayout;
24
25     resultPages = new QTabWidget;
26     resultPages->setTabPosition(QTabWidget::West);
27     mlayout->addWidget(resultPages);
28
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);
37
38     mlayout->addLayout(btlayout);
39     cent->setLayout(mlayout);
40
41     // Clear the word input when Clear button is tapped
42     connect(btnClear, SIGNAL(clicked()), this, SLOT(startAgain()));
43
44     connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
45     connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
46 }
47
48 MainWindow::~MainWindow()
49 {
50     delete ui;
51     delete freVerbDic;
52 }
53
54 void MainWindow::setOrientation(ScreenOrientation orientation)
55 {
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");
62             return;
63         }
64     }
65 #endif // Q_OS_SYMBIAN
66
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);
73         break;
74     case ScreenOrientationLockLandscape:
75         attribute = static_cast<Qt::WidgetAttribute>(129);
76         break;
77     default:
78     case ScreenOrientationAuto:
79         attribute = static_cast<Qt::WidgetAttribute>(130);
80         break;
81 #else // QT_VERSION < 0x040702
82     case ScreenOrientationLockPortrait:
83         attribute = Qt::WA_LockPortraitOrientation;
84         break;
85     case ScreenOrientationLockLandscape:
86         attribute = Qt::WA_LockLandscapeOrientation;
87         break;
88     default:
89     case ScreenOrientationAuto:
90         attribute = Qt::WA_AutoOrientation;
91         break;
92 #endif // QT_VERSION < 0x040702
93     };
94     setAttribute(attribute, true);
95 }
96
97 void MainWindow::showExpanded()
98 {
99 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
100     showFullScreen();
101 #elif defined(Q_WS_MAEMO_5)
102     showMaximized();
103 #else
104     show();
105 #endif
106     wordinput->setFocus();
107 }
108
109 void  MainWindow::initverbiste()
110 {
111     langCode = "fr";
112
113     FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
114     if (lang != FrenchVerbDictionary::FRENCH)
115     {
116         // TODO: If lang code is not supported?
117     }
118
119     /* Create verb dictionary, accept non-accent input */
120     freVerbDic = new FrenchVerbDictionary(true);
121 }
122
123 void MainWindow::startLookup()
124 {
125     btnLookup->setText(tr("Please wait..."));
126     btnLookup->setEnabled(false);
127     clearResults();
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();
131
132     /* Get input word to look up */
133     const std::string word = input.toLower().toUtf8().constData();
134
135     /*
136      *  For each possible deconjugation, take the infinitive form and
137      *  obtain its complete conjugation.
138      */
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
142
143     freVerbDic->deconjugate(word, infles);
144
145     resultPages->setUpdatesEnabled(false);
146     std::string prevUTF8Infinitive, prevTemplateName;
147     for (std::vector<InflectionDesc>::const_iterator it = infles.begin();
148          it != infles.end(); it++)
149     {
150         const InflectionDesc &d = *it;
151         VVVS conjug;
152         getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
153
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
158         {
159             continue;
160         }
161
162         std::string utf8Infinitive = conjug[0][0][0];
163         if (utf8Infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName)
164             // This result is duplicated
165             continue;
166
167         /* Show on GUI */
168         ResultPage *rsp = addResultPage(utf8Infinitive);
169
170         /* Get modes and tenses of the verb */
171         int i = 0;
172         for (VVVS::const_iterator t = conjug.begin();
173              t != conjug.end(); t++, i++) {
174             if (i == 1)
175                 i = 4;
176             else if (i == 11)
177                 i = 12;
178             assert(i >= 0 && i < 16);
179
180             int row = i / 4;
181             int col = i % 4;
182
183             std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian);
184             if (utf8TenseName.empty())
185                 continue;
186
187             QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic);
188             rsp->grid->addLayout(cell, row, col);
189         }
190         rsp->packContent();
191         prevUTF8Infinitive = utf8Infinitive;
192         prevTemplateName = d.templateName;
193     }
194     /* Enable the button again */
195     btnLookup->setEnabled(true);
196     btnLookup->setText("");
197     resultPages->setUpdatesEnabled(true);
198 }
199
200 ResultPage* MainWindow::addResultPage(const std::string &labelText)
201 {
202     ResultPage *rp = new ResultPage();
203     QString label = QString::fromUtf8(labelText.c_str());
204     resultPages->addTab(rp->page, label);
205     return rp;
206 }
207
208 void MainWindow::clearResults()
209 {
210     while (resultPages->count()) {
211         int lastIndex = resultPages->count() - 1;
212         resultPages->widget(lastIndex)->deleteLater();
213         resultPages->removeTab(lastIndex);
214     }
215 }
216
217 void MainWindow::startAgain()
218 {
219     wordinput->clear();
220     clearResults();
221     wordinput->setFocus();
222     btnLookup->setEnabled(true);
223 }
224
225 QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
226                                         const std::string &tenseName,
227                                         const std::string &inputWord,
228                                         FrenchVerbDictionary *verbDict)
229 {
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}");
235
236     /* Conjugaison */
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));
243         lb->setMargin(4);
244         vbox->addWidget(lb, 1);
245     }
246     return vbox;
247 }
248
249 /**** For ResultPage class ****/
250 ResultPage::ResultPage()
251     : page(new QScrollArea),
252       grid(new QGridLayout)
253 {
254 }
255
256 void ResultPage::packContent()
257 {
258     QWidget *immediate = new QWidget();
259     immediate->setLayout(grid);
260     page->setWidget(immediate);
261 }
262