~timo-jyrinki/ubuntu/trusty/maliit-framework/fix_qt52

« back to all changes in this revision

Viewing changes to examples/apps/plainqt/mainwindow.cpp

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-01-31 13:26:48 UTC
  • Revision ID: package-import@ubuntu.com-20130131132648-w1u9d2279tppxcft
Tags: upstream-0.94.1
ImportĀ upstreamĀ versionĀ 0.94.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "mainwindow.h"
 
2
 
 
3
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
 
4
#include <QGuiApplication>
 
5
#include <QScreen>
 
6
#include <QWindow>
 
7
#else
 
8
#include <maliit/namespace.h>
 
9
#include <maliit/inputmethod.h>
 
10
#endif
 
11
 
 
12
#include <QtCore>
 
13
#include <QVBoxLayout>
 
14
 
 
15
#include <cstdlib>
 
16
 
 
17
namespace {
 
18
 
 
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
 
21
 
 
22
bool enableFullScreenMode()
 
23
{
 
24
    return (qApp->arguments().contains("-fullscreen"));
 
25
}
 
26
 
 
27
} // namespace
 
28
 
 
29
class MyTextEdit
 
30
    : public QTextEdit
 
31
{
 
32
private:
 
33
    bool was_focused;
 
34
 
 
35
public:
 
36
    MyTextEdit()
 
37
        : QTextEdit(TextPrompt)
 
38
        , was_focused(false)
 
39
    {}
 
40
 
 
41
protected:
 
42
    void focusInEvent(QFocusEvent *e)
 
43
    {
 
44
        toPlainText();
 
45
        // On first text edit, clear pre-set TextPrompt:
 
46
        if (not was_focused && toPlainText() == QString(TextPrompt)) {
 
47
            was_focused = true;
 
48
            setText("");
 
49
        }
 
50
 
 
51
        QTextEdit::focusInEvent(e);
 
52
    }
 
53
};
 
54
 
 
55
MainWindow::MainWindow()
 
56
    : QMainWindow()
 
57
    , m_server_process(new QProcess(this))
 
58
    , m_orientation_index(0)
 
59
    , m_grid_row(0)
 
60
    , m_grid(new QGridLayout)
 
61
    , m_start_server(new QPushButton)
 
62
    , m_rotate_keyboard(new QPushButton("Rotate input method"))
 
63
{
 
64
    m_server_process->setProcessChannelMode(QProcess::ForwardedChannels);
 
65
 
 
66
    connect(m_server_process, SIGNAL(stateChanged(QProcess::ProcessState)),
 
67
            this, SLOT(onServerStateChanged()));
 
68
 
 
69
    connect(m_start_server, SIGNAL(clicked()),
 
70
            this, SLOT(onStartServerClicked()));
 
71
    connect(m_rotate_keyboard, SIGNAL(clicked()),
 
72
            this, SLOT(onRotateKeyboardClicked()));
 
73
 
 
74
    initUI();
 
75
    onServerStateChanged();
 
76
 
 
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());
 
80
#endif
 
81
}
 
82
 
 
83
void MainWindow::initUI()
 
84
{
 
85
    setWindowTitle("Maliit test application");
 
86
    m_grid_row = 0;
 
87
 
 
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"));
 
94
 
 
95
    m_grid->addLayout(buttons, m_grid_row, 1);
 
96
    ++m_grid_row;
 
97
 
 
98
    // multi line:
 
99
    QLabel *label = 0;
 
100
    QTextEdit *entry = 0;
 
101
 
 
102
    m_grid->addWidget(label = new QLabel("multi line"), m_grid_row, 0);
 
103
    m_grid->addWidget(entry = new QTextEdit, m_grid_row, 1);
 
104
    ++m_grid_row;
 
105
 
 
106
    label->setToolTip("Qt::ImhNone");
 
107
    entry->setInputMethodHints(Qt::ImhNone);
 
108
    entry->installEventFilter(this);
 
109
 
 
110
    // single line, emulating content types via Qt::InputMethodHints:
 
111
    insertIntoGrid("single line", Qt::ImhNone,
 
112
                   "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");
 
125
 
 
126
    // Don't want other buttons to steal focus:
 
127
    m_start_server->setFocusPolicy(Qt::NoFocus);
 
128
    m_rotate_keyboard->setFocusPolicy(Qt::NoFocus);
 
129
 
 
130
    QPushButton *close_app = new QPushButton("Close application");
 
131
    m_grid->addWidget(close_app, m_grid_row, 1);
 
132
    ++m_grid_row;
 
133
 
 
134
    connect(close_app, SIGNAL(clicked()),
 
135
            this,     SLOT(close()));
 
136
 
 
137
    setCentralWidget(new QWidget);
 
138
    centralWidget()->setLayout(m_grid);
 
139
 
 
140
    if (enableFullScreenMode()) {
 
141
        showFullScreen();
 
142
    } else {
 
143
        show();
 
144
    }
 
145
}
 
146
 
 
147
void MainWindow::insertIntoGrid(const QString &description,
 
148
                                const Qt::InputMethodHints &hints,
 
149
                                const QString &tooltip)
 
150
{
 
151
    QLabel *label = 0;
 
152
    QLineEdit *entry = 0;
 
153
 
 
154
    m_grid->addWidget(label = new QLabel(description), m_grid_row, 0);
 
155
    m_grid->addWidget(entry = new QLineEdit, m_grid_row, 1);
 
156
    ++m_grid_row;
 
157
 
 
158
    label->setToolTip(tooltip);
 
159
    entry->setInputMethodHints(hints);
 
160
    entry->installEventFilter(this);
 
161
}
 
162
 
 
163
MainWindow::~MainWindow()
 
164
{
 
165
    m_server_process->terminate();
 
166
}
 
167
 
 
168
bool MainWindow::eventFilter(QObject *watched,
 
169
                             QEvent *event)
 
170
{
 
171
    Q_UNUSED(watched)
 
172
 
 
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();
 
177
#else
 
178
        if (QInputContext *ic = qApp->inputContext()) {
 
179
            QEvent im_request(QEvent::RequestSoftwareInputPanel);
 
180
            ic->filterEvent(&im_request);
 
181
        }
 
182
#endif
 
183
    }
 
184
 
 
185
    return false;
 
186
}
 
187
 
 
188
void MainWindow::onStartServerClicked()
 
189
{
 
190
 
 
191
    QStringList arguments;
 
192
    arguments << "-bypass-wm-hint";
 
193
 
 
194
    // Self-compositing is currently only supported in fullscreen mode:
 
195
    if (enableFullScreenMode()) {
 
196
        arguments << "-use-self-composition";
 
197
    }
 
198
 
 
199
    if (m_server_process->state() != QProcess::NotRunning) {
 
200
        m_server_process->terminate();
 
201
    } else {
 
202
        m_server_process->start(serverName, arguments);
 
203
    }
 
204
}
 
205
 
 
206
void MainWindow::onServerStateChanged()
 
207
{
 
208
    switch (m_server_process->state()) {
 
209
    case QProcess::Running:
 
210
        m_start_server->setText("(running) Stop input method server");
 
211
        break;
 
212
 
 
213
    case QProcess::Starting:
 
214
        m_start_server->setText("(starting) Stop input method server");
 
215
        break;
 
216
 
 
217
    case QProcess::NotRunning:
 
218
        m_start_server->setText("(stopped) Start input method server");
 
219
        break;
 
220
 
 
221
    default:
 
222
        break;
 
223
    }
 
224
}
 
225
 
 
226
void MainWindow::onRotateKeyboardClicked()
 
227
{
 
228
    ++m_orientation_index;
 
229
 
 
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
 
236
    };
 
237
 
 
238
    windowHandle()->reportContentOrientationChange(orientations[m_orientation_index % 4]);
 
239
#else
 
240
    static const Maliit::OrientationAngle orientations[] = {
 
241
        Maliit::Angle0,
 
242
        Maliit::Angle90,
 
243
        Maliit::Angle180,
 
244
        Maliit::Angle270
 
245
    };
 
246
 
 
247
    Maliit::InputMethod::instance()->setOrientationAngle(orientations[m_orientation_index % 4]);
 
248
#endif
 
249
}