Delete some debug symbols
[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 }
17
18 void MainWindow::setupcodedUI()
19 {
20     cent = centralWidget();
21     mlayout = new QVBoxLayout;
22     btlayout = new QHBoxLayout;
23
24     resultPages = new QTabWidget;
25     resultPages->setStyleSheet("QTabBar::tab { height: 50px }");
26     mlayout->addWidget(resultPages);
27
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);
39
40     mlayout->addLayout(btlayout);
41     cent->setLayout(mlayout);
42
43     // Clear the word input when Clear button is tapped
44     connect(btnClear, SIGNAL(clicked()), this, SLOT(startAgain()));
45
46     connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
47     connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
48     connect(btnPron, SIGNAL(clicked()), this, SLOT(startLookup()));
49
50     /* Icon */
51     QIcon *icon = new QIcon();
52     icon->addFile(ICONFILE);
53     setWindowIcon(*icon);
54
55     /* About Dialog */
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>"));
59
60     /* Menu */
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()));
64     /* Menu filters */
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()));
75 }
76
77 MainWindow::~MainWindow()
78 {
79     delete ui;
80     delete freVerbDic;
81     delete aboutDialog;
82 }
83
84 void MainWindow::setOrientation(ScreenOrientation orientation)
85 {
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");
92             return;
93         }
94     }
95 #endif // Q_OS_SYMBIAN
96
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);
103         break;
104     case ScreenOrientationLockLandscape:
105         attribute = static_cast<Qt::WidgetAttribute>(129);
106         break;
107     default:
108     case ScreenOrientationAuto:
109         attribute = static_cast<Qt::WidgetAttribute>(130);
110         break;
111 #else // QT_VERSION < 0x040702
112     case ScreenOrientationLockPortrait:
113         attribute = Qt::WA_LockPortraitOrientation;
114         break;
115     case ScreenOrientationLockLandscape:
116         attribute = Qt::WA_LockLandscapeOrientation;
117         break;
118     default:
119     case ScreenOrientationAuto:
120         attribute = Qt::WA_AutoOrientation;
121         break;
122 #endif // QT_VERSION < 0x040702
123     };
124     setAttribute(attribute, true);
125 }
126
127 void MainWindow::showExpanded()
128 {
129 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
130     showFullScreen();
131 #elif defined(Q_WS_MAEMO_5)
132     showMaximized();
133 #else
134     show();
135 #endif
136     initverbiste();
137     wordinput->setFocus();
138 }
139
140 void  MainWindow::initverbiste()
141 {
142     langCode = "fr";
143
144     FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
145     if (lang != FrenchVerbDictionary::FRENCH)
146     {
147         // TODO: If lang code is not supported?
148     }
149
150     /* Create verb dictionary, accept non-accent input */
151     freVerbDic = new FrenchVerbDictionary(true);
152 }
153
154 void MainWindow::switchLang()
155 {
156     FrenchVerbDictionary::Language curlang = freVerbDic->getLanguage();
157     FrenchVerbDictionary::Language targetlang = filItalian->isChecked() ? FrenchVerbDictionary::ITALIAN
158                                                                         : FrenchVerbDictionary::FRENCH;
159     if (curlang == targetlang) {
160         return;
161     }
162     /* If lang change */
163     std::string conjFN, verbsFN;
164     FrenchVerbDictionary::getXMLFilenames(conjFN, verbsFN, targetlang);
165     delete freVerbDic;
166     freVerbDic = new FrenchVerbDictionary(conjFN, verbsFN, true, targetlang);
167 }
168
169 void MainWindow::startLookup()
170 {
171     QString input = wordinput->text().trimmed();
172     if (input.isEmpty()) {
173         return;
174     }
175
176     btnLookup->setText(tr("Please wait..."));
177     btnLookup->setEnabled(false);
178     clearResults();
179     /* Pending the lookup job to the next event loop (redraw the button right now) */
180     QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
181
182     /* Get input word to look up */
183     const std::string word = input.toLower().toUtf8().constData();
184
185     /*
186      *  For each possible deconjugation, take the infinitive form and
187      *  obtain its complete conjugation.
188      */
189     std::vector<InflectionDesc> infles;
190     bool includePronouns = btnPron->isChecked();
191     bool isItalian = filItalian->isChecked();          // TODO: Will get this value from external
192
193     freVerbDic->deconjugate(word, infles);
194
195     resultPages->setUpdatesEnabled(false);
196     std::string prevUTF8Infinitive, prevTemplateName; /* Remember found word
197     to avoid conjugating again */
198
199     for (std::vector<InflectionDesc>::const_iterator it = infles.begin();
200          it != infles.end(); it++)
201     {
202         const InflectionDesc &d = *it;
203
204         /* If this infinitive has been conjugated, we skip to the next infinitive */
205         if (d.infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName) {
206             continue;
207         }
208
209         VVVS conjug;
210         getConjugation(*freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
211
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
216         {
217             continue;
218         }
219
220         std::string utf8Infinitive = conjug[0][0][0];
221
222         /* Add result to GUI (not show yet) */
223         ResultPage *rsp = addResultPage(utf8Infinitive);
224
225         /* Get modes and tenses of the verb */
226         int i = 0;
227         for (VVVS::const_iterator t = conjug.begin();
228              t != conjug.end(); t++, i++) {
229             if (i == 1)
230                 i = 4;
231             else if (i == 11)
232                 i = 12;
233             assert(i >= 0 && i < 16);
234
235             int row = i / 4;
236             int col = i % 4;
237
238             std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian);
239             if (utf8TenseName.empty())
240                 continue;
241
242             QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic);
243             rsp->grid->addLayout(cell, row, col);
244         }
245
246         /* Show the result on GUI */
247         rsp->packContent();
248         prevUTF8Infinitive = utf8Infinitive;
249         prevTemplateName = d.templateName;
250     }
251
252     /* Enable the button again */
253     btnLookup->setEnabled(true);
254     btnLookup->setText("");
255     resultPages->setUpdatesEnabled(true);
256 }
257
258 ResultPage* MainWindow::addResultPage(const std::string &labelText)
259 {
260     ResultPage *rp = new ResultPage();
261     QString label = QString::fromUtf8(labelText.c_str());
262     resultPages->addTab(rp->page, label);
263     return rp;
264 }
265
266 void MainWindow::clearResults()
267 {
268     while (resultPages->count()) {
269         int lastIndex = resultPages->count() - 1;
270         resultPages->widget(lastIndex)->deleteLater();
271         resultPages->removeTab(lastIndex);
272     }
273 }
274
275 void MainWindow::startAgain()
276 {
277     wordinput->clear();
278     clearResults();
279     wordinput->setFocus();
280     btnLookup->setEnabled(true);
281 }
282
283 QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
284                                         const std::string &tenseName,
285                                         const std::string &inputWord,
286                                         FrenchVerbDictionary *verbDict)
287 {
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                               "padding-left: 10px; padding-right: 10px}");
293
294     /* Conjugaison */
295     QVBoxLayout *vbox = new QVBoxLayout();
296     vbox->addWidget(tenseLabel);
297     QVector<QString> persons = qgetConjugates(*verbDict, tenseIterator,inputWord,
298                                               "<font color='#D20020'>", "</font>");
299     for (int i = 0; i < persons.size(); ++i) {
300         QLabel *lb = new QLabel(persons.at(i));
301         lb->setMargin(4);
302         vbox->addWidget(lb, 1);
303     }
304     return vbox;
305 }
306
307 /**** For ResultPage class ****/
308 ResultPage::ResultPage()
309     : page(new QScrollArea),
310       grid(new QGridLayout)
311 {
312 }
313
314 void ResultPage::packContent()
315 {
316     QWidget *immediate = new QWidget();
317     immediate->setLayout(grid);
318     page->setWidget(immediate);
319 }
320