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