2018-04-06 17:30:24 +02:00
|
|
|
|
/******************************************************************************
|
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
|
|
|
|
* This file may be used under the terms of the QSkinny License, Version 1.0
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "QskInputPanel.h"
|
2018-04-10 16:51:35 +02:00
|
|
|
|
#include "QskVirtualKeyboard.h"
|
|
|
|
|
#include "QskInputSuggestionBar.h"
|
2018-04-18 19:41:46 +02:00
|
|
|
|
#include "QskTextInput.h"
|
|
|
|
|
#include "QskTextLabel.h"
|
2018-04-10 16:51:35 +02:00
|
|
|
|
#include "QskLinearBox.h"
|
2018-04-06 17:30:24 +02:00
|
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QLocale>
|
2018-04-11 08:58:14 +02:00
|
|
|
|
#include <QGuiApplication>
|
2018-04-10 16:51:35 +02:00
|
|
|
|
|
2018-04-06 17:30:24 +02:00
|
|
|
|
QString qskNativeLocaleString( const QLocale& locale )
|
|
|
|
|
{
|
|
|
|
|
switch( locale.language() )
|
|
|
|
|
{
|
|
|
|
|
case QLocale::Bulgarian:
|
|
|
|
|
return QStringLiteral( "български език" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Czech:
|
|
|
|
|
return QStringLiteral( "Čeština" );
|
|
|
|
|
|
|
|
|
|
case QLocale::German:
|
|
|
|
|
return QStringLiteral( "Deutsch" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Danish:
|
|
|
|
|
return QStringLiteral( "Dansk" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Greek:
|
|
|
|
|
return QStringLiteral( "Eλληνικά" );
|
|
|
|
|
|
|
|
|
|
case QLocale::English:
|
|
|
|
|
{
|
|
|
|
|
switch( locale.country() )
|
|
|
|
|
{
|
|
|
|
|
case QLocale::Canada:
|
|
|
|
|
case QLocale::UnitedStates:
|
|
|
|
|
case QLocale::UnitedStatesMinorOutlyingIslands:
|
|
|
|
|
case QLocale::UnitedStatesVirginIslands:
|
|
|
|
|
return QStringLiteral( "English (US)" );
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return QStringLiteral( "English (UK)" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case QLocale::Spanish:
|
|
|
|
|
return QStringLiteral( "Español" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Finnish:
|
|
|
|
|
return QStringLiteral( "Suomi" );
|
|
|
|
|
|
|
|
|
|
case QLocale::French:
|
|
|
|
|
return QStringLiteral( "Français" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Hungarian:
|
|
|
|
|
return QStringLiteral( "Magyar" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Italian:
|
|
|
|
|
return QStringLiteral( "Italiano" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Japanese:
|
|
|
|
|
return QStringLiteral( "日本語" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Latvian:
|
|
|
|
|
return QStringLiteral( "Latviešu" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Lithuanian:
|
|
|
|
|
return QStringLiteral( "Lietuvių" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Dutch:
|
|
|
|
|
return QStringLiteral( "Nederlands" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Portuguese:
|
|
|
|
|
return QStringLiteral( "Português" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Romanian:
|
|
|
|
|
return QStringLiteral( "Română" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Russia:
|
|
|
|
|
return QStringLiteral( "Русский" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Slovenian:
|
|
|
|
|
return QStringLiteral( "Slovenščina" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Slovak:
|
|
|
|
|
return QStringLiteral( "Slovenčina" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Turkish:
|
|
|
|
|
return QStringLiteral( "Türkçe" );
|
|
|
|
|
|
|
|
|
|
case QLocale::Chinese:
|
|
|
|
|
return QStringLiteral( "中文" );
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return QLocale::languageToString( locale.language() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
class TextInput : public QskTextInput
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TextInput( QQuickItem* parentItem = nullptr ):
|
|
|
|
|
QskTextInput( parentItem )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSK_SUBCONTROL( QskInputPanel, Panel )
|
|
|
|
|
|
2018-04-10 16:51:35 +02:00
|
|
|
|
class QskInputPanel::PrivateData
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-04-18 19:41:46 +02:00
|
|
|
|
PrivateData():
|
|
|
|
|
hasInputProxy( true )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-12 12:03:51 +02:00
|
|
|
|
QskLinearBox* layout;
|
2018-04-18 19:41:46 +02:00
|
|
|
|
QskTextLabel* prompt;
|
2018-04-23 14:06:40 +02:00
|
|
|
|
TextInput* inputProxy;
|
2018-04-10 16:51:35 +02:00
|
|
|
|
QskInputSuggestionBar* suggestionBar;
|
|
|
|
|
QskVirtualKeyboard* keyboard;
|
2018-04-18 19:41:46 +02:00
|
|
|
|
|
|
|
|
|
bool hasInputProxy : 1;
|
2018-04-10 16:51:35 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QskInputPanel::QskInputPanel( QQuickItem* parent ):
|
2018-04-18 19:41:46 +02:00
|
|
|
|
Inherited( parent ),
|
2018-04-10 16:51:35 +02:00
|
|
|
|
m_data( new PrivateData() )
|
|
|
|
|
{
|
|
|
|
|
setAutoLayoutChildren( true );
|
2018-04-12 12:03:51 +02:00
|
|
|
|
initSizePolicy( QskSizePolicy::Expanding, QskSizePolicy::Constrained );
|
2018-04-10 16:51:35 +02:00
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
m_data->prompt = new QskTextLabel();
|
|
|
|
|
m_data->prompt->setVisible( false );
|
|
|
|
|
|
2018-04-23 14:06:40 +02:00
|
|
|
|
m_data->inputProxy = new TextInput();
|
|
|
|
|
m_data->inputProxy->setVisible( m_data->hasInputProxy );
|
2018-04-10 16:51:35 +02:00
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
m_data->suggestionBar = new QskInputSuggestionBar();
|
2018-04-10 16:51:35 +02:00
|
|
|
|
m_data->suggestionBar->setVisible( false );
|
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
m_data->keyboard = new QskVirtualKeyboard();
|
|
|
|
|
|
|
|
|
|
auto layout = new QskLinearBox( Qt::Vertical, this );
|
|
|
|
|
|
|
|
|
|
layout->addItem( m_data->prompt, Qt::AlignLeft | Qt::AlignHCenter );
|
2018-04-23 14:06:40 +02:00
|
|
|
|
layout->addItem( m_data->inputProxy, Qt::AlignLeft | Qt::AlignHCenter );
|
2018-04-18 19:41:46 +02:00
|
|
|
|
layout->addStretch( 10 );
|
|
|
|
|
layout->addItem( m_data->suggestionBar );
|
|
|
|
|
layout->addItem( m_data->keyboard );
|
|
|
|
|
|
|
|
|
|
m_data->layout = layout;
|
|
|
|
|
|
2018-04-10 16:51:35 +02:00
|
|
|
|
connect( m_data->suggestionBar, &QskInputSuggestionBar::suggested,
|
|
|
|
|
this, &QskInputPanel::commitCandidate );
|
|
|
|
|
|
|
|
|
|
connect( m_data->keyboard, &QskVirtualKeyboard::keySelected,
|
|
|
|
|
this, &QskInputPanel::commitKey );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QskInputPanel::~QskInputPanel()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
QskAspect::Subcontrol QskInputPanel::effectiveSubcontrol(
|
|
|
|
|
QskAspect::Subcontrol subControl ) const
|
|
|
|
|
{
|
|
|
|
|
if( subControl == QskBox::Panel )
|
|
|
|
|
return QskInputPanel::Panel;
|
|
|
|
|
|
|
|
|
|
return subControl;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-12 12:03:51 +02:00
|
|
|
|
qreal QskInputPanel::heightForWidth( qreal width ) const
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
This code looks like as it could be generalized
|
|
|
|
|
and moved to QskLinearBox. TODO ...
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const auto margins = this->margins();
|
|
|
|
|
|
|
|
|
|
width -= margins.left() + margins.right();
|
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
const auto padding = innerPadding(
|
|
|
|
|
Panel, QSizeF( width, width ) );
|
|
|
|
|
|
|
|
|
|
width -= padding.left() + padding.right();
|
|
|
|
|
|
2018-04-12 12:03:51 +02:00
|
|
|
|
qreal height = m_data->keyboard->heightForWidth( width );
|
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
const QskControl* controls[] =
|
2018-04-23 14:06:40 +02:00
|
|
|
|
{ m_data->prompt, m_data->inputProxy, m_data->suggestionBar };
|
2018-04-18 19:41:46 +02:00
|
|
|
|
|
|
|
|
|
for ( auto control : controls )
|
2018-04-12 12:03:51 +02:00
|
|
|
|
{
|
2018-04-18 19:41:46 +02:00
|
|
|
|
if ( control->isVisible() )
|
|
|
|
|
{
|
|
|
|
|
height += m_data->layout->spacing();
|
|
|
|
|
height += control->sizeHint().height();
|
|
|
|
|
}
|
2018-04-12 12:03:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
height += padding.top() + padding.bottom();
|
2018-04-12 12:03:51 +02:00
|
|
|
|
height += margins.top() + margins.bottom();
|
|
|
|
|
|
|
|
|
|
return height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qreal QskInputPanel::widthForHeight( qreal height ) const
|
|
|
|
|
{
|
|
|
|
|
const auto margins = this->margins();
|
|
|
|
|
|
|
|
|
|
height -= margins.top() + margins.bottom();
|
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
const auto padding = innerPadding(
|
|
|
|
|
Panel, QSizeF( height, height ) );
|
|
|
|
|
|
|
|
|
|
height -= padding.top() + padding.bottom();
|
|
|
|
|
|
|
|
|
|
const QskControl* controls[] =
|
2018-04-23 14:06:40 +02:00
|
|
|
|
{ m_data->prompt, m_data->inputProxy, m_data->suggestionBar };
|
2018-04-18 19:41:46 +02:00
|
|
|
|
|
|
|
|
|
for ( auto control : controls )
|
2018-04-12 12:03:51 +02:00
|
|
|
|
{
|
2018-04-18 19:41:46 +02:00
|
|
|
|
if ( control->isVisible() )
|
|
|
|
|
{
|
|
|
|
|
height -= m_data->layout->spacing();
|
|
|
|
|
height -= control->sizeHint().height();
|
|
|
|
|
}
|
2018-04-12 12:03:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qreal width = m_data->keyboard->widthForHeight( height );
|
2018-04-18 19:41:46 +02:00
|
|
|
|
|
|
|
|
|
width += padding.left() + padding.right();
|
2018-04-12 12:03:51 +02:00
|
|
|
|
width += margins.left() + margins.right();
|
|
|
|
|
|
|
|
|
|
return width;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 19:41:46 +02:00
|
|
|
|
QString QskInputPanel::inputPrompt() const
|
|
|
|
|
{
|
|
|
|
|
return m_data->prompt->text();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QskInputPanel::setInputPrompt( const QString& text )
|
|
|
|
|
{
|
|
|
|
|
auto prompt = m_data->prompt;
|
|
|
|
|
|
|
|
|
|
if ( text != prompt->text() )
|
|
|
|
|
{
|
|
|
|
|
prompt->setText( text );
|
|
|
|
|
|
|
|
|
|
if ( m_data->hasInputProxy )
|
|
|
|
|
prompt->setVisible( !text.isEmpty() );
|
|
|
|
|
|
|
|
|
|
Q_EMIT inputPromptChanged( text );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QskInputPanel::hasInputProxy() const
|
|
|
|
|
{
|
|
|
|
|
return m_data->hasInputProxy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QskInputPanel::setInputProxy( bool on )
|
|
|
|
|
{
|
|
|
|
|
if ( m_data->hasInputProxy == on )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_data->hasInputProxy = on;
|
2018-04-23 14:06:40 +02:00
|
|
|
|
m_data->inputProxy->setVisible( on );
|
2018-04-18 19:41:46 +02:00
|
|
|
|
|
|
|
|
|
auto prompt = m_data->prompt;
|
|
|
|
|
|
|
|
|
|
if ( on )
|
|
|
|
|
prompt->setVisible( !prompt->text().isEmpty() );
|
|
|
|
|
else
|
|
|
|
|
prompt->setVisible( false );
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 14:06:40 +02:00
|
|
|
|
void QskInputPanel::updateInputProxy( const QQuickItem* inputItem )
|
|
|
|
|
{
|
|
|
|
|
if ( inputItem == nullptr )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QInputMethodQueryEvent event( Qt::ImQueryAll );
|
|
|
|
|
QCoreApplication::sendEvent( const_cast< QQuickItem* >( inputItem ), &event );
|
|
|
|
|
|
|
|
|
|
const auto proxy = m_data->inputProxy;
|
|
|
|
|
|
|
|
|
|
if ( event.queries() & Qt::ImHints )
|
|
|
|
|
{
|
|
|
|
|
const auto hints = static_cast< Qt::InputMethodHints >(
|
|
|
|
|
event.value( Qt::ImHints ).toInt() );
|
|
|
|
|
|
|
|
|
|
const auto echoMode = ( hints & Qt::ImhHiddenText )
|
|
|
|
|
? QskTextInput::PasswordEchoOnEdit : QskTextInput::Normal;
|
|
|
|
|
|
|
|
|
|
proxy->setEchoMode( echoMode );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( event.queries() & Qt::ImSurroundingText )
|
|
|
|
|
{
|
|
|
|
|
const auto text = event.value( Qt::ImSurroundingText ).toString();
|
|
|
|
|
proxy->setText( text );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( event.queries() & Qt::ImCursorPosition )
|
|
|
|
|
{
|
|
|
|
|
const auto pos = event.value( Qt::ImCursorPosition ).toInt();
|
|
|
|
|
proxy->setCursorPosition( pos );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
if ( event.queries() & Qt::ImCurrentSelection )
|
|
|
|
|
{
|
|
|
|
|
const auto text = event.value( Qt::ImCursorPosition ).toString();
|
|
|
|
|
if ( !text.isEmpty() )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if ( event.queries() & Qt::ImMaximumTextLength )
|
|
|
|
|
{
|
|
|
|
|
const auto length = event.value( Qt::ImMaximumTextLength ).toInt();
|
|
|
|
|
proxy->setMaxLength( length );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-10 16:51:35 +02:00
|
|
|
|
bool QskInputPanel::isCandidatesEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return m_data->suggestionBar->isVisible();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector< QString > QskInputPanel::candidates() const
|
|
|
|
|
{
|
|
|
|
|
return m_data->suggestionBar->candidates();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QskInputPanel::setCandidatesEnabled( bool on )
|
|
|
|
|
{
|
|
|
|
|
m_data->suggestionBar->setVisible( on );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QskInputPanel::setCandidates( const QVector< QString >& candidates )
|
|
|
|
|
{
|
|
|
|
|
m_data->suggestionBar->setCandidates( candidates );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QskInputPanel::commitCandidate( int index )
|
|
|
|
|
{
|
|
|
|
|
m_data->suggestionBar->setCandidates( QVector< QString >() );
|
|
|
|
|
|
|
|
|
|
QGuiApplication::inputMethod()->invokeAction(
|
|
|
|
|
static_cast< QInputMethod::Action >( SelectCandidate ), index );
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 16:32:48 +02:00
|
|
|
|
void QskInputPanel::commitKey( int key )
|
2018-04-10 16:51:35 +02:00
|
|
|
|
{
|
|
|
|
|
QGuiApplication::inputMethod()->invokeAction(
|
|
|
|
|
static_cast< QInputMethod::Action >( Compose ), key );
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 16:32:48 +02:00
|
|
|
|
void QskInputPanel::keyPressEvent( QKeyEvent* event )
|
|
|
|
|
{
|
|
|
|
|
// animate the corresponding key button TODO
|
|
|
|
|
|
|
|
|
|
switch( event->key() )
|
|
|
|
|
{
|
|
|
|
|
case Qt::Key_Return:
|
|
|
|
|
case Qt::Key_Escape:
|
|
|
|
|
{
|
|
|
|
|
commitKey( event->key() );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
const auto text = event->text();
|
|
|
|
|
if ( !text.isEmpty() )
|
|
|
|
|
commitKey( text[0].unicode() );
|
|
|
|
|
else
|
|
|
|
|
commitKey( event->key() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QskInputPanel::keyReleaseEvent( QKeyEvent* event )
|
|
|
|
|
{
|
|
|
|
|
return Inherited::keyReleaseEvent( event );
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-10 16:51:35 +02:00
|
|
|
|
#include "moc_QskInputPanel.cpp"
|