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

« back to all changes in this revision

Viewing changes to examples/plugins/cxx/override/overrideinputmethod.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
/* * This file is part of Maliit framework *
 
2
 *
 
3
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 
4
 * All rights reserved.
 
5
 *
 
6
 * Contact: maliit-discuss@lists.maliit.org
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public
 
10
 * License version 2.1 as published by the Free Software Foundation
 
11
 * and appearing in the file LICENSE.LGPL included in the packaging
 
12
 * of this file.
 
13
 */
 
14
 
 
15
#include "overrideinputmethod.h"
 
16
 
 
17
#include <maliit/plugins/abstractinputmethodhost.h>
 
18
 
 
19
#include <QDebug>
 
20
#include <QApplication>
 
21
#include <QDesktopWidget>
 
22
#include <QKeyEvent>
 
23
 
 
24
namespace {
 
25
    const char * const overrideSubViewId("OverridePluginSubview1");
 
26
    const char * const actionKeyName = "actionKey";
 
27
    const char * const actionKeyLabel = "Enter";
 
28
}
 
29
 
 
30
using Maliit::Plugins::AbstractSurface;
 
31
using Maliit::Plugins::AbstractWidgetSurface;
 
32
 
 
33
OverrideInputMethod::OverrideInputMethod(MAbstractInputMethodHost *host)
 
34
    : MAbstractInputMethod(host)
 
35
    , surfaceFactory(host->surfaceFactory())
 
36
    , surface(qSharedPointerDynamicCast<AbstractWidgetSurface>(surfaceFactory->create(AbstractSurface::PositionCenterBottom | AbstractSurface::TypeWidget)))
 
37
    , mainWidget(new QPushButton(surface->widget()))
 
38
    , showIsInhibited(false)
 
39
    , showRequested(false)
 
40
    , activeActionKeyOverride()
 
41
{
 
42
    // Set up UI
 
43
    mainWidget->setText(actionKeyLabel);
 
44
    connect(mainWidget, SIGNAL(clicked()), this, SLOT(handleButtonClicked()));
 
45
 
 
46
    // Used only for unittest/sanity test
 
47
    inputMethodHost()->sendCommitString("Maliit");
 
48
    inputMethodHost()->sendPreeditString("Mali", QList<Maliit::PreeditTextFormat>(), 0, 6);
 
49
 
 
50
    mainWidget->show();
 
51
}
 
52
 
 
53
OverrideInputMethod::~OverrideInputMethod()
 
54
{}
 
55
 
 
56
// Slot for our action key
 
57
void OverrideInputMethod::handleButtonClicked()
 
58
{
 
59
    const QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
 
60
 
 
61
    inputMethodHost()->sendKeyEvent(event);
 
62
}
 
63
 
 
64
void OverrideInputMethod::show()
 
65
{
 
66
    showRequested = true;
 
67
    if (showIsInhibited) {
 
68
        return;
 
69
    }
 
70
 
 
71
    // Set size of the input method
 
72
    const QSize &screenSize = surfaceFactory->screenSize();
 
73
    surface->setSize(QSize(screenSize.width() - 200, 200));
 
74
    mainWidget->resize(mainWidget->parentWidget()->size());
 
75
 
 
76
    surface->show();
 
77
}
 
78
 
 
79
void OverrideInputMethod::hide()
 
80
{
 
81
    if (!showRequested) {
 
82
        return;
 
83
    }
 
84
    showRequested = false;
 
85
 
 
86
    surface->hide();
 
87
}
 
88
 
 
89
QList<MAbstractInputMethod::MInputMethodSubView>
 
90
OverrideInputMethod::subViews(Maliit::HandlerState state) const
 
91
{
 
92
    QList<MAbstractInputMethod::MInputMethodSubView> subViews;
 
93
 
 
94
    if (state == Maliit::OnScreen) {
 
95
        MAbstractInputMethod::MInputMethodSubView subView1;
 
96
        subView1.subViewId = overrideSubViewId;
 
97
        subView1.subViewTitle = "Override plugin subview 1";
 
98
        subViews.append(subView1);
 
99
    }
 
100
    return subViews;
 
101
}
 
102
 
 
103
QString OverrideInputMethod::activeSubView(Maliit::HandlerState state) const
 
104
{
 
105
    QString subView = (state == Maliit::OnScreen) ? overrideSubViewId : "";
 
106
    return subView;
 
107
}
 
108
 
 
109
void OverrideInputMethod::setState(const QSet<Maliit::HandlerState> &state)
 
110
{
 
111
    if (state.contains(Maliit::OnScreen)) {
 
112
        if (showRequested && !showIsInhibited) {
 
113
            surface->show();
 
114
        }
 
115
    } else {
 
116
        surface->hide();
 
117
    }
 
118
}
 
119
 
 
120
void OverrideInputMethod::handleClientChange()
 
121
{
 
122
    if (showRequested) {
 
123
        hide();
 
124
    }
 
125
}
 
126
 
 
127
void OverrideInputMethod::handleVisualizationPriorityChange(bool inhibitShow)
 
128
{
 
129
    if (showIsInhibited == inhibitShow) {
 
130
        return;
 
131
    }
 
132
    showIsInhibited = inhibitShow;
 
133
 
 
134
    if (showRequested) {
 
135
        if (inhibitShow) {
 
136
            surface->hide();
 
137
        } else {
 
138
            surface->show();
 
139
        }
 
140
    }
 
141
}
 
142
 
 
143
void OverrideInputMethod::handleAppOrientationAboutToChange(int angle)
 
144
{
 
145
    // Rotate your input method UI here
 
146
    Q_UNUSED(angle);
 
147
}
 
148
 
 
149
void OverrideInputMethod::handleAppOrientationChanged(int angle)
 
150
{
 
151
    // Can typically be forwarded to handleAppOrientationAboutToChange
 
152
    // as long as that method will not do anything when newAngle == previousAngle
 
153
    Q_UNUSED(angle);
 
154
}
 
155
 
 
156
void OverrideInputMethod::update()
 
157
{
 
158
    // empty default implementation
 
159
}
 
160
 
 
161
void OverrideInputMethod::reset()
 
162
{
 
163
    // empty default implementation
 
164
}
 
165
 
 
166
void OverrideInputMethod::handleFocusChange(bool focusIn)
 
167
{
 
168
    // empty default implementation
 
169
    Q_UNUSED(focusIn);
 
170
}
 
171
 
 
172
void OverrideInputMethod::switchContext(Maliit::SwitchDirection direction,
 
173
                                        bool enableAnimation)
 
174
{
 
175
    // empty default implementation
 
176
    Q_UNUSED(direction);
 
177
    Q_UNUSED(enableAnimation);
 
178
}
 
179
 
 
180
void OverrideInputMethod::setPreedit(const QString &preeditString,
 
181
                                     int cursorPos)
 
182
{
 
183
    // empty default implementation
 
184
    Q_UNUSED(preeditString);
 
185
    Q_UNUSED(cursorPos);
 
186
}
 
187
 
 
188
void OverrideInputMethod::setActiveSubView(const QString &subViewId,
 
189
                                           Maliit::HandlerState state)
 
190
{
 
191
    // Ignored as input method only support one subview
 
192
    Q_UNUSED(subViewId);
 
193
    Q_UNUSED(state);
 
194
}
 
195
 
 
196
void OverrideInputMethod::setKeyOverrides(const QMap<QString, QSharedPointer<MKeyOverride> > &overrides)
 
197
{
 
198
    if (activeActionKeyOverride) {
 
199
        disconnect(activeActionKeyOverride.data(), SIGNAL(keyAttributesChanged(const QString &, const MKeyOverride::KeyOverrideAttributes)),
 
200
                   this, SLOT(onKeyAttributesChanged(const QString &, const MKeyOverride::KeyOverrideAttributes)));
 
201
        activeActionKeyOverride.clear();
 
202
    }
 
203
 
 
204
    QMap<QString, QSharedPointer<MKeyOverride> >::const_iterator actionKeyOverrideIter(overrides.find(actionKeyName));
 
205
 
 
206
    if (actionKeyOverrideIter != overrides.end()) {
 
207
        QSharedPointer<MKeyOverride> overrideShared = *actionKeyOverrideIter;
 
208
 
 
209
        if (overrideShared) {
 
210
            connect(overrideShared.data(), SIGNAL(keyAttributesChanged(const QString &, const MKeyOverride::KeyOverrideAttributes)),
 
211
                    this, SLOT(onKeyAttributesChanged(const QString &, const MKeyOverride::KeyOverrideAttributes)));
 
212
            activeActionKeyOverride = overrideShared;
 
213
        }
 
214
    }
 
215
 
 
216
    updateActionKey(MKeyOverride::All);
 
217
}
 
218
 
 
219
void OverrideInputMethod::onKeyAttributesChanged(const QString &keyId,
 
220
                                                 const MKeyOverride::KeyOverrideAttributes changedAttributes)
 
221
{
 
222
    if (keyId == actionKeyName) {
 
223
        updateActionKey(changedAttributes);
 
224
    }
 
225
}
 
226
 
 
227
void OverrideInputMethod::updateActionKey (const MKeyOverride::KeyOverrideAttributes changedAttributes)
 
228
{
 
229
    const bool useKeyOverride(activeActionKeyOverride);
 
230
 
 
231
    if (changedAttributes & MKeyOverride::Label) {
 
232
        bool useDefault(false);
 
233
 
 
234
        if (useKeyOverride) {
 
235
            const QString label(activeActionKeyOverride->label());
 
236
 
 
237
            if (label.isEmpty()) {
 
238
                useDefault = true;
 
239
            } else {
 
240
                mainWidget->setText(label);
 
241
            }
 
242
        } else {
 
243
            useDefault = true;
 
244
        }
 
245
 
 
246
        if (useDefault) {
 
247
            mainWidget->setText(actionKeyLabel);
 
248
        }
 
249
    }
 
250
    if (changedAttributes & MKeyOverride::Icon) {
 
251
        // maybe later
 
252
    }
 
253
    if (changedAttributes & MKeyOverride::Highlighted) {
 
254
        // maybe later
 
255
    }
 
256
    if (changedAttributes & MKeyOverride::Enabled) {
 
257
        // maybe later
 
258
    }
 
259
}