Can get infinitive verb now.
[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 = qobject_cast<QVBoxLayout *>(cent->layout());
23     mlayout = new QVBoxLayout;
24     btlayout = new QHBoxLayout;
25
26     QScrollArea *scrollArea = new QScrollArea;
27     scrollArea->setBackgroundRole(QPalette::Dark);
28     mlayout->addWidget(scrollArea);
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     QObject::connect(btnClear, SIGNAL(clicked()), wordinput, SLOT(clear()));
47     QObject::connect(btnClear, SIGNAL(clicked()), labVerb, SLOT(clear()));
48
49     QObject::connect(wordinput, SIGNAL(returnPressed()), this, SLOT(startLookup()));
50     QObject::connect(btnLookup, SIGNAL(clicked()), this, SLOT(startLookup()));
51 }
52
53 MainWindow::~MainWindow()
54 {
55     delete ui;
56 }
57
58 void MainWindow::setOrientation(ScreenOrientation orientation)
59 {
60 #if defined(Q_OS_SYMBIAN)
61     // If the version of Qt on the device is < 4.7.2, that attribute won't work
62     if (orientation != ScreenOrientationAuto) {
63         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
64         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
65             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
66             return;
67         }
68     }
69 #endif // Q_OS_SYMBIAN
70
71     Qt::WidgetAttribute attribute;
72     switch (orientation) {
73 #if QT_VERSION < 0x040702
74     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
75     case ScreenOrientationLockPortrait:
76         attribute = static_cast<Qt::WidgetAttribute>(128);
77         break;
78     case ScreenOrientationLockLandscape:
79         attribute = static_cast<Qt::WidgetAttribute>(129);
80         break;
81     default:
82     case ScreenOrientationAuto:
83         attribute = static_cast<Qt::WidgetAttribute>(130);
84         break;
85 #else // QT_VERSION < 0x040702
86     case ScreenOrientationLockPortrait:
87         attribute = Qt::WA_LockPortraitOrientation;
88         break;
89     case ScreenOrientationLockLandscape:
90         attribute = Qt::WA_LockLandscapeOrientation;
91         break;
92     default:
93     case ScreenOrientationAuto:
94         attribute = Qt::WA_AutoOrientation;
95         break;
96 #endif // QT_VERSION < 0x040702
97     };
98     setAttribute(attribute, true);
99 }
100
101 void MainWindow::showExpanded()
102 {
103 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
104     showFullScreen();
105 #elif defined(Q_WS_MAEMO_5)
106     showMaximized();
107 #else
108     show();
109 #endif
110 }
111
112 void MainWindow::startLookup()
113 {
114     QString input = wordinput->text();
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     /* Get input word to look up */
126     const std::string word = input.toLower().toUtf8().constData();
127
128     /*
129      *  For each possible deconjugation, take the infinitive form and
130      *  obtain its complete conjugation.
131      */
132     std::vector<InflectionDesc> v;
133     bool includePronouns = FALSE;    // TODO: Will get this value from external
134
135     freVerbDic->deconjugate(word, v);
136
137     std::string prevUTF8Infinitive, prevTemplateName;
138     for (std::vector<InflectionDesc>::const_iterator it = v.begin();
139          it != v.end(); it++)
140     {
141         const InflectionDesc &d = *it;
142         VVVS conjug;
143         getConjugation(freVerbDic, d.infinitive, d.templateName, conjug, includePronouns);
144
145         if (conjug.size() == 0           // if no tenses
146             || conjug[0].size() == 0     // if no infinitive tense
147             || conjug[0][0].size() == 0  // if no person in inf. tense
148             || conjug[0][0][0].empty())  // if infinitive string empty
149         {
150             continue;
151         }
152
153         std::string utf8Infinitive = conjug[0][0][0];
154         if (utf8Infinitive == prevUTF8Infinitive && d.templateName == prevTemplateName)
155             // This result is duplicated
156             continue;
157
158         /* Show on GUI */
159         labVerb->setText(QString::fromUtf8(utf8Infinitive.c_str()));
160     }
161 }
162
163 void  MainWindow::initverbiste()
164 {
165     langCode = "fr";
166 }