2018-02-06 14:55:35 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
|
|
|
* This file may be used under the terms of the QSkinny License, Version 1.0
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
#include "QskInputContext.h"
|
|
|
|
|
|
|
|
#include "QskInputCompositionModel.h"
|
|
|
|
#include "QskPinyinCompositionModel.h"
|
|
|
|
|
2018-03-28 09:01:25 -07:00
|
|
|
#include <QskVirtualKeyboard.h>
|
2017-07-21 18:21:34 +02:00
|
|
|
#include <QskDialog.h>
|
|
|
|
#include <QskWindow.h>
|
|
|
|
#include <QskSetup.h>
|
|
|
|
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QQmlContext>
|
|
|
|
#include <QQmlEngine>
|
|
|
|
#include <QQuickItem>
|
|
|
|
#include <QQuickWindow>
|
|
|
|
#include <QRectF>
|
|
|
|
|
|
|
|
QskInputContext::QskInputContext():
|
2018-03-14 18:00:55 +01:00
|
|
|
Inherited(),
|
|
|
|
m_inputCompositionModel( new QskInputCompositionModel )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
|
|
|
connect( qskSetup, &QskSetup::inputPanelChanged,
|
|
|
|
this, &QskInputContext::setInputPanel );
|
|
|
|
setInputPanel( qskSetup->inputPanel() );
|
2018-03-14 17:30:39 +01:00
|
|
|
|
2018-03-14 18:00:55 +01:00
|
|
|
// We could connect candidatesChanged() here, but we don't emit
|
|
|
|
// the signal in the normal composition model anyhow
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QskInputContext::~QskInputContext()
|
|
|
|
{
|
|
|
|
if ( m_inputPanel )
|
|
|
|
delete m_inputPanel;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QskInputContext::isValid() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskInputContext::update( Qt::InputMethodQueries queries )
|
|
|
|
{
|
2018-03-14 17:30:39 +01:00
|
|
|
if ( !m_inputItem )
|
2017-07-21 18:21:34 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
QInputMethodQueryEvent queryEvent( queries );
|
2018-03-14 17:30:39 +01:00
|
|
|
if ( !QCoreApplication::sendEvent( m_inputItem, &queryEvent ) )
|
2017-07-21 18:21:34 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Qt::ImCursorRectangle
|
|
|
|
// Qt::ImFont
|
|
|
|
// Qt::ImCursorPosition
|
|
|
|
// Qt::ImSurroundingText // important for chinese input
|
|
|
|
// Qt::ImCurrentSelection // important for prediction
|
|
|
|
// Qt::ImMaximumTextLength // should be monitored
|
|
|
|
// Qt::ImAnchorPosition
|
|
|
|
|
|
|
|
if ( queries & Qt::ImHints )
|
|
|
|
{
|
|
|
|
const Qt::InputMethodHints hints =
|
|
|
|
static_cast< Qt::InputMethodHints >( queryEvent.value( Qt::ImHints ).toInt() );
|
|
|
|
Q_UNUSED(hints);
|
|
|
|
//ImhHiddenText = 0x1, // might need to disable certain checks
|
|
|
|
//ImhSensitiveData = 0x2, // shouldn't change anything
|
|
|
|
//ImhNoAutoUppercase = 0x4, // if we support auto uppercase, disable it
|
|
|
|
//ImhPreferNumbers = 0x8, // default to number keyboard
|
|
|
|
//ImhPreferUppercase = 0x10, // start with shift on
|
|
|
|
//ImhPreferLowercase = 0x20, // start with shift off
|
|
|
|
//ImhNoPredictiveText = 0x40, // ignored for now
|
|
|
|
|
|
|
|
//ImhDate = 0x80, // ignored for now (no date keyboard)
|
|
|
|
//ImhTime = 0x100, // ignored for know (no time keyboard)
|
|
|
|
|
|
|
|
//ImhPreferLatin = 0x200, // can be used to launch chinese kb in english mode
|
|
|
|
|
|
|
|
//ImhMultiLine = 0x400, // not useful?
|
|
|
|
|
|
|
|
//ImhDigitsOnly // default to number keyboard, disable other keys
|
|
|
|
//ImhFormattedNumbersOnly // hard to say
|
|
|
|
//ImhUppercaseOnly // caps-lock, disable shift
|
|
|
|
//ImhLowercaseOnly // disable shift
|
|
|
|
//ImhDialableCharactersOnly // dial pad (calculator?)
|
|
|
|
//ImhEmailCharactersOnly // disable certain symbols (email-only kb?)
|
|
|
|
//ImhUrlCharactersOnly // disable certain symbols (url-only kb?)
|
|
|
|
//ImhLatinOnly // disable chinese input
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( queries & Qt::ImPreferredLanguage )
|
|
|
|
{
|
|
|
|
const auto locale = queryEvent.value( Qt::ImPreferredLanguage ).toLocale();
|
|
|
|
if ( m_inputPanel )
|
|
|
|
m_inputPanel->setLocale( locale );
|
|
|
|
|
|
|
|
auto modelChanged = false;
|
|
|
|
const bool currentModelIsPinyin =
|
|
|
|
dynamic_cast< QskPinyinCompositionModel* >( m_inputCompositionModel.get() );
|
|
|
|
const bool localeIsChinese = locale.language() == QLocale::Chinese;
|
|
|
|
if ( localeIsChinese && !currentModelIsPinyin )
|
|
|
|
{
|
|
|
|
if ( m_inputPanel )
|
|
|
|
m_inputPanel->disconnect( m_inputCompositionModel.get() );
|
|
|
|
|
|
|
|
m_inputCompositionModel.reset( new QskPinyinCompositionModel );
|
|
|
|
modelChanged = true;
|
|
|
|
}
|
|
|
|
else if ( !m_inputCompositionModel || ( !localeIsChinese && currentModelIsPinyin ) ) // Install default
|
|
|
|
{
|
|
|
|
if ( m_inputPanel )
|
|
|
|
m_inputPanel->disconnect( m_inputCompositionModel.get() );
|
|
|
|
|
|
|
|
m_inputCompositionModel.reset( new QskInputCompositionModel );
|
|
|
|
modelChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( modelChanged && m_inputPanel )
|
|
|
|
{
|
|
|
|
QObject::connect(
|
|
|
|
m_inputCompositionModel.get(), &QskInputCompositionModel::groupsChanged,
|
2018-03-28 09:01:25 -07:00
|
|
|
m_inputPanel.data(), &QskVirtualKeyboard::setPreeditGroups );
|
2017-07-21 18:21:34 +02:00
|
|
|
QObject::connect(
|
|
|
|
m_inputCompositionModel.get(), &QskInputCompositionModel::candidatesChanged,
|
|
|
|
this, &QskInputContext::handleCandidatesChanged );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Qt::ImAbsolutePosition
|
|
|
|
// Qt::ImTextBeforeCursor // important for chinese
|
|
|
|
// Qt::ImTextAfterCursor // important for chinese
|
|
|
|
// Qt::ImPlatformData // hard to say...
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF QskInputContext::keyboardRect() const
|
|
|
|
{
|
|
|
|
if ( m_inputPanel
|
|
|
|
&& QskDialog::instance()->policy() != QskDialog::TopLevelWindow )
|
|
|
|
{
|
2018-03-28 10:05:20 -07:00
|
|
|
return m_inputPanel->geometry();
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Inherited::keyboardRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QskInputContext::isAnimating() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskInputContext::showInputPanel()
|
|
|
|
{
|
|
|
|
if ( !m_inputPanel )
|
|
|
|
{
|
2018-03-28 09:01:25 -07:00
|
|
|
setInputPanel( new QskVirtualKeyboard );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
if ( QskDialog::instance()->policy() == QskDialog::TopLevelWindow )
|
|
|
|
{
|
|
|
|
auto window = new QskWindow;
|
|
|
|
window->setFlags( Qt::Tool | Qt::WindowDoesNotAcceptFocus );
|
|
|
|
window->resize( 800, 240 ); // ### what size?
|
|
|
|
m_inputPanel->setParentItem( window->contentItem() );
|
|
|
|
connect( window, &QskWindow::visibleChanged,
|
|
|
|
this, &QskInputContext::emitInputPanelVisibleChanged );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto window = qobject_cast< QQuickWindow* >( QGuiApplication::focusWindow() );
|
|
|
|
if ( window )
|
|
|
|
{
|
|
|
|
m_inputPanel->setParentItem( window->contentItem() );
|
|
|
|
m_inputPanel->setSize( window->size() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto window = m_inputPanel->window();
|
|
|
|
if ( window && window != QGuiApplication::focusWindow() )
|
|
|
|
window->show();
|
|
|
|
else
|
|
|
|
m_inputPanel->setVisible( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskInputContext::hideInputPanel()
|
|
|
|
{
|
|
|
|
if ( !m_inputPanel )
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto window = m_inputPanel->window();
|
|
|
|
if ( window && window != QGuiApplication::focusWindow() )
|
|
|
|
window->hide();
|
|
|
|
else
|
|
|
|
m_inputPanel->setVisible( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QskInputContext::isInputPanelVisible() const
|
|
|
|
{
|
|
|
|
return m_inputPanel && m_inputPanel->isVisible()
|
|
|
|
&& m_inputPanel->window() && m_inputPanel->window()->isVisible();
|
|
|
|
}
|
|
|
|
|
|
|
|
QLocale QskInputContext::locale() const
|
|
|
|
{
|
|
|
|
return m_inputPanel ? m_inputPanel->locale() : QLocale();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskInputContext::setFocusObject( QObject* focusObject )
|
|
|
|
{
|
|
|
|
m_focusObject = focusObject;
|
|
|
|
if ( !m_focusObject )
|
2018-03-14 17:30:39 +01:00
|
|
|
{
|
|
|
|
m_inputItem = nullptr;
|
|
|
|
m_inputCompositionModel->setInputItem( nullptr );
|
2017-07-21 18:21:34 +02:00
|
|
|
return;
|
2018-03-14 17:30:39 +01:00
|
|
|
}
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2018-03-14 17:30:39 +01:00
|
|
|
bool inputItemChanged = false;
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2018-03-14 17:30:39 +01:00
|
|
|
auto focusQuickItem = qobject_cast< QQuickItem* >( focusObject );
|
|
|
|
if( focusQuickItem )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2018-03-14 17:30:39 +01:00
|
|
|
// Do not change the input item when panel buttons get the focus:
|
|
|
|
if( qskNearestFocusScope( focusQuickItem ) != m_inputPanel )
|
|
|
|
{
|
|
|
|
m_inputItem = focusQuickItem;
|
|
|
|
m_inputCompositionModel->setInputItem( m_inputItem ); // ### use a signal/slot connection
|
|
|
|
inputItemChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( inputItemChanged )
|
|
|
|
{
|
|
|
|
QInputMethodQueryEvent queryEvent( Qt::ImEnabled );
|
|
|
|
if ( !QCoreApplication::sendEvent( m_inputItem, &queryEvent ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( !queryEvent.value( Qt::ImEnabled ).toBool() )
|
|
|
|
{
|
|
|
|
hideInputPanel();
|
|
|
|
return;
|
|
|
|
}
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update( Qt::InputMethodQuery( Qt::ImQueryAll & ~Qt::ImEnabled ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskInputContext::invokeAction( QInputMethod::Action action, int cursorPosition )
|
|
|
|
{
|
|
|
|
Q_UNUSED(cursorPosition);
|
|
|
|
|
|
|
|
if ( !m_inputPanel )
|
|
|
|
return;
|
|
|
|
|
2018-03-28 09:01:25 -07:00
|
|
|
switch ( static_cast< QskVirtualKeyboard::Action >( action ) )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2018-03-28 09:01:25 -07:00
|
|
|
case QskVirtualKeyboard::Compose:
|
2017-07-21 18:21:34 +02:00
|
|
|
m_inputCompositionModel->composeKey( static_cast< Qt::Key >( cursorPosition ) );
|
|
|
|
break;
|
2018-03-28 09:01:25 -07:00
|
|
|
case QskVirtualKeyboard::SelectGroup:
|
2017-07-21 18:21:34 +02:00
|
|
|
m_inputCompositionModel->setGroupIndex( cursorPosition );
|
|
|
|
break;
|
2018-03-28 09:01:25 -07:00
|
|
|
case QskVirtualKeyboard::SelectCandidate:
|
2017-07-21 18:21:34 +02:00
|
|
|
m_inputCompositionModel->commitCandidate( cursorPosition );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskInputContext::emitAnimatingChanged()
|
|
|
|
{
|
|
|
|
QPlatformInputContext::emitAnimatingChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskInputContext::handleCandidatesChanged()
|
|
|
|
{
|
|
|
|
QVector< Qt::Key > candidates( m_inputCompositionModel->candidateCount() );
|
|
|
|
for ( int i = 0; i < candidates.length(); ++i )
|
|
|
|
{
|
|
|
|
candidates[i] = static_cast< Qt::Key >( m_inputCompositionModel->candidate( i ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_inputPanel->setPreeditCandidates( candidates );
|
|
|
|
}
|
|
|
|
|
2018-03-28 09:01:25 -07:00
|
|
|
void QskInputContext::setInputPanel( QskVirtualKeyboard* inputPanel )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
|
|
|
if ( m_inputPanel == inputPanel )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( m_inputPanel )
|
|
|
|
{
|
2018-03-28 10:05:20 -07:00
|
|
|
QObject::disconnect( m_inputPanel, &QskVirtualKeyboard::visibleChanged,
|
|
|
|
this, &QPlatformInputContext::emitInputPanelVisibleChanged );
|
|
|
|
QObject::disconnect( m_inputPanel, &QskVirtualKeyboard::keyboardRectChanged,
|
|
|
|
this, &QPlatformInputContext::emitKeyboardRectChanged );
|
|
|
|
QObject::disconnect( m_inputPanel, &QskVirtualKeyboard::localeChanged,
|
|
|
|
this, &QPlatformInputContext::emitLocaleChanged );
|
2017-07-21 18:21:34 +02:00
|
|
|
if ( m_inputCompositionModel )
|
|
|
|
m_inputPanel->disconnect( m_inputCompositionModel.get() );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_inputPanel = inputPanel;
|
|
|
|
if ( !m_inputPanel )
|
|
|
|
return;
|
|
|
|
|
2018-03-28 10:05:20 -07:00
|
|
|
QObject::connect( m_inputPanel, &QskVirtualKeyboard::visibleChanged,
|
|
|
|
this, &QPlatformInputContext::emitInputPanelVisibleChanged );
|
|
|
|
QObject::connect( m_inputPanel, &QskVirtualKeyboard::keyboardRectChanged,
|
|
|
|
this, &QPlatformInputContext::emitKeyboardRectChanged );
|
|
|
|
QObject::connect( m_inputPanel, &QskVirtualKeyboard::localeChanged,
|
|
|
|
this, &QPlatformInputContext::emitLocaleChanged );
|
2017-07-21 18:21:34 +02:00
|
|
|
if ( m_inputCompositionModel )
|
|
|
|
{
|
|
|
|
QObject::connect(
|
|
|
|
m_inputCompositionModel.get(), &QskInputCompositionModel::groupsChanged,
|
2018-03-28 09:01:25 -07:00
|
|
|
m_inputPanel.data(), &QskVirtualKeyboard::setPreeditGroups );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_QskInputContext.cpp"
|