registration of text predictions removed from QskInputContext
This commit is contained in:
parent
e5d6fe0dc3
commit
0c5dc0ce37
@ -7,19 +7,46 @@
|
||||
#include <qpa/qplatforminputcontext.h>
|
||||
|
||||
#include "QskInputContext.h"
|
||||
#include "QskPinyinTextPredictor.h"
|
||||
|
||||
#define HUNSPELL 0
|
||||
|
||||
#if HUNSPELL
|
||||
#include "QskHunspellTextPredictor.h"
|
||||
#endif
|
||||
|
||||
#include <QLocale>
|
||||
#include <QRectF>
|
||||
#include <QEvent>
|
||||
|
||||
namespace
|
||||
{
|
||||
class InputContext : public QskInputContext
|
||||
{
|
||||
public:
|
||||
virtual QskTextPredictor* textPredictor( const QLocale& locale ) const
|
||||
{
|
||||
#if HUNSPELL
|
||||
/*
|
||||
For the moment we manage the text prediction in the context
|
||||
plugin - but of course it has to be moved somewhere else
|
||||
*/
|
||||
if ( locale.language() == QLocale::English )
|
||||
return new QskHunspellTextPredictor();
|
||||
#endif
|
||||
|
||||
return QskInputContext::textPredictor( locale );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
QPlatformInputContext is no stable public API.
|
||||
So we forward everything to QskInputContext
|
||||
*/
|
||||
class QskPlatformInputContext final : public QPlatformInputContext
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
using Inherited = QPlatformInputContext;
|
||||
|
||||
public:
|
||||
@ -67,7 +94,7 @@ QskPlatformInputContext::QskPlatformInputContext()
|
||||
auto context = QskInputContext::instance();
|
||||
if ( context == nullptr )
|
||||
{
|
||||
context = new QskInputContext();
|
||||
context = new InputContext();
|
||||
QskInputContext::setInstance( context );
|
||||
}
|
||||
|
||||
@ -89,16 +116,6 @@ void QskPlatformInputContext::updateContext()
|
||||
|
||||
connect( m_context, &QskInputContext::panelRectChanged,
|
||||
this, &QPlatformInputContext::emitKeyboardRectChanged );
|
||||
|
||||
#if 1
|
||||
m_context->registerPredictor( QLocale(),
|
||||
new QskHunspellTextPredictor() );
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
m_context->registerPredictor(
|
||||
QLocale::Chinese, new QskPinyinTextPredictor() );
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,6 @@
|
||||
|
||||
#include "QskInputContext.h"
|
||||
#include "QskInputPanel.h"
|
||||
#include "QskTextPredictor.h"
|
||||
#include "QskInputPanel.h"
|
||||
#include "QskInputEngine.h"
|
||||
|
||||
@ -66,58 +65,6 @@ QskInputContext* QskInputContext::instance()
|
||||
return qskInputContext;
|
||||
}
|
||||
|
||||
static inline uint qskHashLocale( const QLocale& locale )
|
||||
{
|
||||
return uint( locale.language() + ( uint( locale.country() ) << 16 ) );
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
class PredictorTable
|
||||
{
|
||||
public:
|
||||
void replace( const QLocale& locale, QskTextPredictor* predictor )
|
||||
{
|
||||
const auto key = qskHashLocale( locale );
|
||||
|
||||
if ( predictor )
|
||||
{
|
||||
const auto it = hashTab.find( key );
|
||||
if ( it != hashTab.end() )
|
||||
{
|
||||
if ( it.value() == predictor )
|
||||
return;
|
||||
|
||||
delete it.value();
|
||||
*it = predictor;
|
||||
}
|
||||
else
|
||||
{
|
||||
hashTab.insert( key, predictor );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto it = hashTab.find( key );
|
||||
if ( it != hashTab.end() )
|
||||
{
|
||||
delete it.value();
|
||||
hashTab.erase( it );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QskTextPredictor* find( const QLocale& locale )
|
||||
{
|
||||
const auto key = qskHashLocale( locale );
|
||||
return hashTab.value( key, nullptr );
|
||||
}
|
||||
|
||||
private:
|
||||
QHash< uint, QskTextPredictor* > hashTab;
|
||||
};
|
||||
}
|
||||
|
||||
class QskInputContext::PrivateData
|
||||
{
|
||||
public:
|
||||
@ -129,9 +76,9 @@ public:
|
||||
QskPopup* inputPopup = nullptr;
|
||||
QskWindow* inputWindow = nullptr;
|
||||
|
||||
PredictorTable predictorTable;
|
||||
|
||||
QskInputEngine* engine = nullptr;
|
||||
|
||||
bool isPredictorDirty = true;
|
||||
};
|
||||
|
||||
QskInputContext::QskInputContext():
|
||||
@ -163,7 +110,7 @@ QskInputPanel* QskInputContext::inputPanel() const
|
||||
this, &QskInputContext::activeChanged );
|
||||
|
||||
connect( panel, &QskControl::localeChanged,
|
||||
this, []{ qskSendToPlatformContext( QEvent::LocaleChange ); } );
|
||||
this, &QskInputContext::updateLocale );
|
||||
|
||||
m_data->inputPanel = panel;
|
||||
}
|
||||
@ -313,8 +260,7 @@ void QskInputContext::showPanel()
|
||||
}
|
||||
}
|
||||
|
||||
m_data->engine->setPredictor(
|
||||
m_data->predictorTable.find( locale() ) );
|
||||
updatePredictor();
|
||||
|
||||
panel->setLocale( locale() );
|
||||
panel->attachInputItem( m_data->inputItem );
|
||||
@ -396,6 +342,28 @@ QLocale QskInputContext::locale() const
|
||||
return QLocale();
|
||||
}
|
||||
|
||||
void QskInputContext::updateLocale()
|
||||
{
|
||||
m_data->isPredictorDirty = true;
|
||||
|
||||
if ( isActive() )
|
||||
updatePredictor();
|
||||
|
||||
qskSendToPlatformContext( QEvent::LocaleChange );
|
||||
}
|
||||
|
||||
void QskInputContext::updatePredictor()
|
||||
{
|
||||
if ( m_data->isPredictorDirty )
|
||||
{
|
||||
if ( m_data->engine )
|
||||
{
|
||||
m_data->engine->setPredictor( textPredictor( locale() ) );
|
||||
m_data->isPredictorDirty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QskInputContext::setFocusObject( QObject* focusObject )
|
||||
{
|
||||
if ( m_data->inputItem == nullptr || m_data->inputItem == focusObject )
|
||||
@ -446,28 +414,9 @@ void QskInputContext::setFocusObject( QObject* focusObject )
|
||||
m_data->inputItem = nullptr;
|
||||
}
|
||||
|
||||
void QskInputContext::registerPredictor(
|
||||
const QLocale& locale, QskTextPredictor* predictor )
|
||||
QskTextPredictor* QskInputContext::textPredictor( const QLocale& ) const
|
||||
{
|
||||
auto oldPredictor = m_data->predictorTable.find( locale );
|
||||
if ( predictor == oldPredictor )
|
||||
return;
|
||||
|
||||
if ( predictor )
|
||||
predictor->setParent( this );
|
||||
|
||||
m_data->predictorTable.replace( locale, predictor );
|
||||
|
||||
if ( oldPredictor )
|
||||
delete oldPredictor;
|
||||
|
||||
if ( qskHashLocale( locale ) == qskHashLocale( this->locale() ) )
|
||||
m_data->engine->setPredictor( predictor );
|
||||
}
|
||||
|
||||
QskTextPredictor* QskInputContext::registeredPredictor( const QLocale& locale )
|
||||
{
|
||||
return m_data->predictorTable.find( locale );
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void QskInputContext::processClickAt( int cursorPosition )
|
||||
|
@ -34,9 +34,6 @@ public:
|
||||
|
||||
virtual QLocale locale() const;
|
||||
|
||||
void registerPredictor( const QLocale&, QskTextPredictor* );
|
||||
QskTextPredictor* registeredPredictor( const QLocale& );
|
||||
|
||||
virtual QQuickItem* inputItem() const;
|
||||
virtual QskInputPanel* inputPanel() const;
|
||||
|
||||
@ -56,6 +53,8 @@ protected:
|
||||
virtual void showPanel();
|
||||
virtual void hidePanel();
|
||||
|
||||
virtual QskTextPredictor* textPredictor( const QLocale& ) const;
|
||||
|
||||
private:
|
||||
friend class QskPlatformInputContext;
|
||||
|
||||
@ -65,6 +64,9 @@ private:
|
||||
virtual void processClickAt( int cursorPosition );
|
||||
virtual void commitPrediction( bool );
|
||||
|
||||
void updateLocale();
|
||||
void updatePredictor();
|
||||
|
||||
class PrivateData;
|
||||
std::unique_ptr< PrivateData > m_data;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user