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