aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtualkeyboard/declarativeshifthandler.cpp
blob: fbb43b3251ef3b9a4a7b3c849069ad6dbe3a730d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Quick Enterprise Controls add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "declarativeshifthandler.h"
#include "declarativeinputcontext.h"

class DeclarativeShiftHandlerPrivate
{
public:
    DeclarativeInputContext *inputContext;
    QString sentenceEndingCharacters;
};

/*!
    \qmltype ShiftHandler
    \inqmlmodule QtQuick.Enterprise.VirtualKeyboard
    \ingroup qtvirtualkeyboard-qml
    \instantiates DeclarativeShiftHandler
    \brief Manages the shift state.
*/

/*!
    \class DeclarativeShiftHandler
    \inmodule InputFramework
    \brief Manages the shift state.
*/

DeclarativeShiftHandler::DeclarativeShiftHandler(DeclarativeInputContext *parent) :
    QObject(parent),
    d_ptr(new DeclarativeShiftHandlerPrivate())
{
    Q_D(DeclarativeShiftHandler);
    d->inputContext = parent;
    if (d->inputContext) {
        connect(d->inputContext, SIGNAL(inputMethodHintsChanged()), SLOT(restart()));
        connect(d->inputContext, SIGNAL(focusEditorChanged()), SLOT(restart()));
        connect(d->inputContext, SIGNAL(preeditTextChanged()), SLOT(autoCapitalize()));
        connect(d->inputContext, SIGNAL(cursorPositionChanged()), SLOT(autoCapitalize()));
    }
    d->sentenceEndingCharacters = ".!?";
}

/*!
    \internal
*/
DeclarativeShiftHandler::~DeclarativeShiftHandler()
{

}

QString DeclarativeShiftHandler::sentenceEndingCharacters() const
{
    Q_D(const DeclarativeShiftHandler);
    return d->sentenceEndingCharacters;
}

void DeclarativeShiftHandler::setSentenceEndingCharacters(const QString &value)
{
    Q_D(DeclarativeShiftHandler);
    if (d->sentenceEndingCharacters != value) {
        d->sentenceEndingCharacters = value;
        autoCapitalize();
        emit sentenceEndingCharactersChanged();
    }
}

void DeclarativeShiftHandler::reset()
{
    Q_D(DeclarativeShiftHandler);
    if (d->inputContext->inputItem()) {
        bool preferUpperCase = (d->inputContext->inputMethodHints() & Qt::ImhPreferUppercase);
        d->inputContext->setShift(preferUpperCase);
        d->inputContext->setCapsLock(preferUpperCase);
    }
}

void DeclarativeShiftHandler::autoCapitalize()
{
    Q_D(DeclarativeShiftHandler);
    if (!isEnabled() || d->inputContext->capsLock())
        return;
    if (!d->inputContext->preeditText().isEmpty()) {
        if (isEnabled() && !d->inputContext->capsLock()) {
            d->inputContext->setShift(false);
        }
    } else {
        int cursorPosition = d->inputContext->cursorPosition();
        bool preferLowerCase = d->inputContext->inputMethodHints() & Qt::ImhPreferLowercase;
        if (cursorPosition == 0) {
            d->inputContext->setShift(!preferLowerCase);
        } else {
            QString text = d->inputContext->surroundingText();
            text.truncate(cursorPosition);
            text = text.trimmed();
            if (text.length() == 0)
                d->inputContext->setShift(!preferLowerCase);
            else if (text.length() > 0 && d->sentenceEndingCharacters.indexOf(text[text.length() - 1]) >= 0)
                d->inputContext->setShift(!preferLowerCase);
            else
                d->inputContext->setShift(false);
        }
    }
}

void DeclarativeShiftHandler::restart()
{
    reset();
    autoCapitalize();
}

bool DeclarativeShiftHandler::isEnabled() const
{
    static const Qt::InputMethodHints disallowFlags = (Qt::ImhNoAutoUppercase | Qt::ImhUppercaseOnly| Qt::ImhLowercaseOnly |
        Qt::ImhEmailCharactersOnly | Qt::ImhUrlCharactersOnly | Qt::ImhDialableCharactersOnly |
        Qt::ImhFormattedNumbersOnly | Qt::ImhDigitsOnly);
    Q_D(const DeclarativeShiftHandler);
    return !(d->inputContext->inputMethodHints() & disallowFlags);
}

/*!
    \property DeclarativeShiftHandler::sentenceEndingCharacters

    This property specifies the sentence ending characters which
    will cause shift state change.

    By default, the property is initialized to sentence
    ending characters found in the ASCII range (i.e. ".!?").
*/

/*!
    \qmlproperty string ShiftHandler::sentenceEndingCharacters

    This property specifies the sentence ending characters which
    will cause shift state change.

    By default, the property is initialized to sentence
    ending characters found in the ASCII range (i.e. ".!?").
*/