8786183a3a6e247769635995cac1ea06c9f180c1
[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     labVerb = new QLabel();
36     labVerb->setMinimumWidth(250);
37     btlayout->addWidget(btnClear);
38     btlayout->addWidget(labVerb);
39     btlayout->addWidget(wordinput);
40     btnLookup = new QPushButton;  // Lookup button
41     btnLookup->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_search.png"));
42     btlayout->addWidget(btnLookup);
43
44     mlayout->addLayout(btlayout);
45     cent->setLayout(mlayout);
46
47     // Clear the word input when Clear button is tapped
48     connect(btnClear, SIGNAL(clicked()), this, SLOT(clearResults()));
49
50     connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
51     connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
52 }
53
54 MainWindow::~MainWindow()
55 {
56     delete ui;
57     delete freVerbDic;
58 }
59
60 void MainWindow::setOrientation(ScreenOrientation orientation)
61 {
62 #if defined(Q_OS_SYMBIAN)
63     // If the version of Qt on the device is < 4.7.2, that attribute won't work
64     if (orientation != ScreenOrientationAuto) {
65         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
66         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
67             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
68             return;
69         }
70     }
71 #endif // Q_OS_SYMBIAN
72
73     Qt::WidgetAttribute attribute;
74     switch (orientation) {
75 #if QT_VERSION < 0x040702
76     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
77     case ScreenOrientationLockPortrait:
78         attribute = static_cast<Qt::WidgetAttribute>(128);
79         break;
80     case ScreenOrientationLockLandscape:
81         attribute = static_cast<Qt::WidgetAttribute>(129);
82         break;
83     default:
84     case ScreenOrientationAuto:
85         attribute = static_cast<Qt::WidgetAttribute>(130);
86         break;
87 #else // QT_VERSION < 0x040702
88     case ScreenOrientationLockPortrait:
89         attribute = Qt::WA_LockPortraitOrientation;
90         break;
91     case ScreenOrientationLockLandscape:
92         attribute = Qt::WA_LockLandscapeOrientation;
93         break;
94     default:
95     case ScreenOrientationAuto:
96         attribute = Qt::WA_AutoOrientation;
97         break;
98 #endif // QT_VERSION < 0x040702
99     };
100     setAttribute(attribute, true);
101 }
102
103 void MainWindow::showExpanded()
104 {
105 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
106     showFullScreen();
107 #elif defined(Q_WS_MAEMO_5)
108     showMaximized();
109 #else
110     show();
111 #endif
112     wordinput->setFocus();
113 }
114
115 void  MainWindow::initverbiste()
116 {
117     langCode = "fr";
118 }
119
120 void MainWindow::startLookup()
121 {
122     QString input = wordinput->text();
123
124     FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
125     if (lang != FrenchVerbDictionary::FRENCH)
126     {
127         // TODO: If lang code is not supported?
128     }
129
130     /* Create verb dictionary, accept non-accent input */
131     freVerbDic = new FrenchVerbDictionary(true);
132
133     /* Get input word to look up */
134     const std::string word = input.toLower().toUtf8().constData();
135
136     /*
137      *  For each possible deconjugation, take the infinitive form and
138      *  obtain its complete conjugation.
139      */
140     std::vector<InflectionDesc> v;
141     bool includePronouns = FALSE;    // TODO: Will get this value from external
142     bool isItalian = FALSE;          // TODO: Will get this value from external
143
144     freVerbDic->deconjugate(word, v);
145
146     std::string prevUTF8Infinitive, prevTemplateName;
147     for (std::vector<InflectionDesc>::const_iterator it = v.begin();
148          it != v.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         //QString infVerb = QString::fromUtf8(utf8Infinitive.c_str());
170         //labVerb->setText(infVerb);
171
172         /* Get modes and tenses of the verb */
173         int i = 0;
174         for (VVVS::const_iterator t = conjug.begin();
175              t != conjug.end(); t++, i++)
176         {
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 #ifdef DEBUG
191             qDebug() << utf8TenseName.c_str();
192 #endif
193             QVBoxLayout *cell = makeResultCell(*t, utf8TenseName, word, freVerbDic);
194             rsp->grid->addLayout(cell, row, col);
195 #ifdef DEBUG
196             qDebug() << "Add cell to " << row << col;
197 #endif
198         }
199         rsp->packContent();
200         prevUTF8Infinitive = utf8Infinitive;
201         prevTemplateName = d.templateName;
202     }
203 }
204
205 ResultPage* MainWindow::addResultPage(const std::string &labelText)
206 {
207     ResultPage *rp = new ResultPage();
208     QString label = QString::fromUtf8(labelText.c_str());
209     resultPages->addTab(rp->page, label);
210     return rp;
211 }
212
213 void MainWindow::clearResults()
214 {
215     wordinput->clear();
216     labVerb->clear();
217
218     while (resultPages->count()) {
219         int lastIndex = resultPages->count() - 1;
220         resultPages->widget(lastIndex)->deleteLater();
221         resultPages->removeTab(lastIndex);
222     }
223     wordinput->setFocus();
224 }
225
226 QVBoxLayout* MainWindow::makeResultCell(const VVS &tenseIterator,
227                                         const std::string &tenseName,
228                                         const std::string &inputWord,
229                                         FrenchVerbDictionary *verbDict)
230 {
231     QLabel *tenseLabel = new QLabel();
232     tenseLabel->setText(QString::fromUtf8(tenseName.c_str()));
233     QVBoxLayout *vbox = new QVBoxLayout();
234     vbox->addWidget(tenseLabel);
235     std::string conjugated = createTableCellText(
236                                     *verbDict,
237                                     tenseIterator,
238                                     inputWord,
239                                     "",
240                                     "");
241     QLabel *conjResult = new QLabel();
242     conjResult->setText(QString::fromUtf8(conjugated.c_str()));
243     vbox->addWidget(conjResult);
244     return vbox;
245 }
246
247 /**** For ResultPage class ****/
248 ResultPage::ResultPage()
249     : page(new QScrollArea),
250       grid(new QGridLayout)
251 {
252 }
253
254 void ResultPage::packContent()
255 {
256     QWidget *immediate = new QWidget();
257     immediate->setLayout(grid);
258     page->setWidget(immediate);
259 }
260