Get tense name.
[mverbiste] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "gui/conjugation.h"
4
5 #include <QtCore/QCoreApplication>
6 #include <QDebug>
7
8 MainWindow::MainWindow(QWidget *parent)
9     : QMainWindow(parent), ui(new Ui::MainWindow)
10 {
11 #ifdef Q_WS_MAEMO_5
12     this->setAttribute(Qt::WA_Maemo5StackedWindow);
13     this->setWindowFlags(Qt::Window);
14 #endif
15     ui->setupUi(this);
16     setupcodedUI();
17     initverbiste();
18 }
19
20 void MainWindow::setupcodedUI()
21 {
22     cent = centralWidget();
23     mlayout = new QVBoxLayout;
24     btlayout = new QHBoxLayout;
25
26     resultPages = new QTabWidget;
27     resultPages->setTabPosition(QTabWidget::West);
28     mlayout->addWidget(resultPages);
29
30     btnClear = new QPushButton;
31     btnClear->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_delete.png"));
32     wordinput = new QLineEdit;
33     labVerb = new QLabel();
34     labVerb->setMinimumWidth(250);
35     btlayout->addWidget(btnClear);
36     btlayout->addWidget(labVerb);
37     btlayout->addWidget(wordinput);
38     btnLookup = new QPushButton;  // Lookup button
39     btnLookup->setIcon(QIcon("/usr/share/icons/hicolor/64x64/hildon/general_search.png"));
40     btlayout->addWidget(btnLookup);
41
42     mlayout->addLayout(btlayout);
43     cent->setLayout(mlayout);
44
45     // Clear the word input when Clear button is tapped
46     connect(btnClear, SIGNAL(clicked()), this, SLOT(clearResults()));
47
48     connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
49     connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
50 }
51
52 MainWindow::~MainWindow()
53 {
54     delete ui;
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
117 void MainWindow::startLookup()
118 {
119     QString input = wordinput->text();
120
121     FrenchVerbDictionary::Language lang = FrenchVerbDictionary::parseLanguageCode(langCode);
122     if (lang != FrenchVerbDictionary::FRENCH)
123     {
124         // TODO: If lang code is not supported?
125     }
126
127     /* Create verb dictionary, accept non-accent input */
128     freVerbDic = new FrenchVerbDictionary(true);
129
130     /* Get input word to look up */
131     const std::string word = input.toLower().toUtf8().constData();
132
133     /*
134      *  For each possible deconjugation, take the infinitive form and
135      *  obtain its complete conjugation.
136      */
137     std::vector<InflectionDesc> v;
138     bool includePronouns = FALSE;    // TODO: Will get this value from external
139     bool isItalian = FALSE;          // TODO: Will get this value from external
140
141     freVerbDic->deconjugate(word, v);
142
143     std::string prevUTF8Infinitive, prevTemplateName;
144     for (std::vector<InflectionDesc>::const_iterator it = v.begin();
145          it != v.end(); it++)
146     {
147         const InflectionDesc &d = *it;
148         VVVS conjug;
149         getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
150
151         if (conjug.size() == 0           // if no tenses
152             || conjug[0].size() == 0     // if no infinitive tense
153             || conjug[0][0].size() == 0  // if no person in inf. tense
154             || conjug[0][0][0].empty())  // if infinitive string empty
155         {
156             continue;
157         }
158
159         std::string utf8Infinitive = conjug[0][0][0];
160         if (utf8Infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName)
161             // This result is duplicated
162             continue;
163
164         /* Show on GUI */
165         ResultPage *rsp = addResultPage(utf8Infinitive);
166         //QString infVerb = QString::fromUtf8(utf8Infinitive.c_str());
167         //labVerb->setText(infVerb);
168
169         /* Get modes and tenses of the verb */
170         int i = 0;
171         for (VVVS::const_iterator t = conjug.begin();
172              t != conjug.end(); t++, i++)
173         {
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             qDebug() << row << col;
183
184             std::string utf8TenseName = getTenseNameForTableCell(row, col, isItalian);
185             if (utf8TenseName.empty())
186                 continue;
187
188             qDebug() << utf8TenseName.c_str();
189         }
190
191         prevUTF8Infinitive = utf8Infinitive;
192         prevTemplateName = d.templateName;
193     }
194 }
195
196 ResultPage* MainWindow::addResultPage(const std::string &labelText)
197 {
198     ResultPage *rp = new ResultPage();
199     QString label = QString::fromUtf8(labelText.c_str());
200     resultPages->addTab(rp->page, label);
201     return rp;
202 }
203
204 void MainWindow::clearResults()
205 {
206     wordinput->clear();
207     labVerb->clear();
208
209     while (resultPages->count()) {
210         int lastIndex = resultPages->count() - 1;
211         resultPages->widget(lastIndex)->deleteLater();
212         resultPages->removeTab(lastIndex);
213     }
214     wordinput->setFocus();
215 }
216
217 /**** For ResultPage class ****/
218 ResultPage::ResultPage()
219     : page(new QScrollArea),
220       table(new QTableWidget)
221 {
222 }
223
224