1
#include "mainwindow.h"
3
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
4
#include <QGuiApplication>
8
#include <maliit/namespace.h>
9
#include <maliit/inputmethod.h>
13
#include <QVBoxLayout>
19
const char * const TextPrompt = "Double-click this text area to invoke virtual keyboard ...";
20
const QString serverName("maliit-server"); //TODO: when maliit and example application is split out, look up in .pc file
22
bool enableFullScreenMode()
24
return (qApp->arguments().contains("-fullscreen"));
37
: QTextEdit(TextPrompt)
42
void focusInEvent(QFocusEvent *e)
45
// On first text edit, clear pre-set TextPrompt:
46
if (not was_focused && toPlainText() == QString(TextPrompt)) {
51
QTextEdit::focusInEvent(e);
55
MainWindow::MainWindow()
57
, m_server_process(new QProcess(this))
58
, m_orientation_index(0)
60
, m_grid(new QGridLayout)
61
, m_start_server(new QPushButton)
62
, m_rotate_keyboard(new QPushButton("Rotate input method"))
64
m_server_process->setProcessChannelMode(QProcess::ForwardedChannels);
66
connect(m_server_process, SIGNAL(stateChanged(QProcess::ProcessState)),
67
this, SLOT(onServerStateChanged()));
69
connect(m_start_server, SIGNAL(clicked()),
70
this, SLOT(onStartServerClicked()));
71
connect(m_rotate_keyboard, SIGNAL(clicked()),
72
this, SLOT(onRotateKeyboardClicked()));
75
onServerStateChanged();
77
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
78
// Work around a bug in maliit input method support where primary orientation is always portrait
79
windowHandle()->reportContentOrientationChange(windowHandle()->screen()->primaryOrientation());
83
void MainWindow::initUI()
85
setWindowTitle("Maliit test application");
88
QHBoxLayout *buttons = new QHBoxLayout;
89
buttons->addWidget(m_start_server);
90
buttons->addWidget(m_rotate_keyboard);
91
// Clicking the button will steal focus from the text edit, thus hiding
92
// the virtual keyboard:
93
buttons->addWidget(new QPushButton("Dismiss input method"));
95
m_grid->addLayout(buttons, m_grid_row, 1);
100
QTextEdit *entry = 0;
102
m_grid->addWidget(label = new QLabel("multi line"), m_grid_row, 0);
103
m_grid->addWidget(entry = new QTextEdit, m_grid_row, 1);
106
label->setToolTip("Qt::ImhNone");
107
entry->setInputMethodHints(Qt::ImhNone);
108
entry->installEventFilter(this);
110
// single line, emulating content types via Qt::InputMethodHints:
111
insertIntoGrid("single line", Qt::ImhNone,
113
insertIntoGrid("password", Qt::ImhHiddenText|Qt::ImhNoPredictiveText,
114
"Qt::ImhHiddenText|Qt::ImhNoPredictiveText");
115
insertIntoGrid("numbers only", Qt::ImhFormattedNumbersOnly,
116
"Qt::ImhFormattedNumbersOnly");
117
insertIntoGrid("dialer input", Qt::ImhDialableCharactersOnly,
118
"Qt::ImhDialableCharactersOnly");
119
insertIntoGrid("symbol view", Qt::ImhPreferNumbers,
120
"Qt::ImhPreferNumbers");
121
insertIntoGrid("e-mail", Qt::ImhEmailCharactersOnly,
122
"Qt::ImhEmailCharactersOnly");
123
insertIntoGrid("website", Qt::ImhUrlCharactersOnly,
124
"Qt::ImhUrlCharactersOnly");
126
// Don't want other buttons to steal focus:
127
m_start_server->setFocusPolicy(Qt::NoFocus);
128
m_rotate_keyboard->setFocusPolicy(Qt::NoFocus);
130
QPushButton *close_app = new QPushButton("Close application");
131
m_grid->addWidget(close_app, m_grid_row, 1);
134
connect(close_app, SIGNAL(clicked()),
135
this, SLOT(close()));
137
setCentralWidget(new QWidget);
138
centralWidget()->setLayout(m_grid);
140
if (enableFullScreenMode()) {
147
void MainWindow::insertIntoGrid(const QString &description,
148
const Qt::InputMethodHints &hints,
149
const QString &tooltip)
152
QLineEdit *entry = 0;
154
m_grid->addWidget(label = new QLabel(description), m_grid_row, 0);
155
m_grid->addWidget(entry = new QLineEdit, m_grid_row, 1);
158
label->setToolTip(tooltip);
159
entry->setInputMethodHints(hints);
160
entry->installEventFilter(this);
163
MainWindow::~MainWindow()
165
m_server_process->terminate();
168
bool MainWindow::eventFilter(QObject *watched,
173
// Let the input method show up on focus-in, not on second click:
174
if (event->type() == QFocusEvent::FocusIn) {
175
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
176
qApp->inputMethod()->show();
178
if (QInputContext *ic = qApp->inputContext()) {
179
QEvent im_request(QEvent::RequestSoftwareInputPanel);
180
ic->filterEvent(&im_request);
188
void MainWindow::onStartServerClicked()
191
QStringList arguments;
192
arguments << "-bypass-wm-hint";
194
// Self-compositing is currently only supported in fullscreen mode:
195
if (enableFullScreenMode()) {
196
arguments << "-use-self-composition";
199
if (m_server_process->state() != QProcess::NotRunning) {
200
m_server_process->terminate();
202
m_server_process->start(serverName, arguments);
206
void MainWindow::onServerStateChanged()
208
switch (m_server_process->state()) {
209
case QProcess::Running:
210
m_start_server->setText("(running) Stop input method server");
213
case QProcess::Starting:
214
m_start_server->setText("(starting) Stop input method server");
217
case QProcess::NotRunning:
218
m_start_server->setText("(stopped) Start input method server");
226
void MainWindow::onRotateKeyboardClicked()
228
++m_orientation_index;
230
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
231
static const Qt::ScreenOrientation orientations[] = {
232
Qt::LandscapeOrientation,
233
Qt::PortraitOrientation,
234
Qt::InvertedLandscapeOrientation,
235
Qt::InvertedPortraitOrientation
238
windowHandle()->reportContentOrientationChange(orientations[m_orientation_index % 4]);
240
static const Maliit::OrientationAngle orientations[] = {
247
Maliit::InputMethod::instance()->setOrientationAngle(orientations[m_orientation_index % 4]);