From 0c5dc0ce37fafdb7b81c4509bbe7ad962a5bfe98 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Wed, 9 May 2018 17:01:30 +0200 Subject: [PATCH] registration of text predictions removed from QskInputContext --- inputcontext/QskInputContextPlugin.cpp | 41 +++++++--- src/inputpanel/QskInputContext.cpp | 107 +++++++------------------ src/inputpanel/QskInputContext.h | 8 +- 3 files changed, 62 insertions(+), 94 deletions(-) diff --git a/inputcontext/QskInputContextPlugin.cpp b/inputcontext/QskInputContextPlugin.cpp index c7936327..4bbe5f78 100644 --- a/inputcontext/QskInputContextPlugin.cpp +++ b/inputcontext/QskInputContextPlugin.cpp @@ -7,19 +7,46 @@ #include #include "QskInputContext.h" -#include "QskPinyinTextPredictor.h" + +#define HUNSPELL 0 + +#if HUNSPELL #include "QskHunspellTextPredictor.h" +#endif #include #include #include +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 } } diff --git a/src/inputpanel/QskInputContext.cpp b/src/inputpanel/QskInputContext.cpp index 2ae8bd92..9c935e72 100644 --- a/src/inputpanel/QskInputContext.cpp +++ b/src/inputpanel/QskInputContext.cpp @@ -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 ) diff --git a/src/inputpanel/QskInputContext.h b/src/inputpanel/QskInputContext.h index ce73c770..431d1e33 100644 --- a/src/inputpanel/QskInputContext.h +++ b/src/inputpanel/QskInputContext.h @@ -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; };