simplifying QskInputCompositionModel
This commit is contained in:
parent
67dee082ec
commit
07d5d933c1
@ -40,17 +40,12 @@ class QskInputCompositionModel::PrivateData
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QString preedit;
|
QString preedit;
|
||||||
QTextCharFormat preeditFormat;
|
|
||||||
QList< QInputMethodEvent::Attribute > preeditAttributes;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QskInputCompositionModel::QskInputCompositionModel( QskInputContext* context ):
|
QskInputCompositionModel::QskInputCompositionModel( QskInputContext* context ):
|
||||||
QObject( context ),
|
QObject( context ),
|
||||||
m_data( new PrivateData )
|
m_data( new PrivateData )
|
||||||
{
|
{
|
||||||
m_data->preeditFormat.setFontUnderline( true );
|
|
||||||
m_data->preeditAttributes.append( QInputMethodEvent::Attribute(
|
|
||||||
QInputMethodEvent::TextFormat, 0, 0, m_data->preeditFormat ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QskInputCompositionModel::~QskInputCompositionModel()
|
QskInputCompositionModel::~QskInputCompositionModel()
|
||||||
@ -96,12 +91,7 @@ void QskInputCompositionModel::composeKey( Qt::Key key )
|
|||||||
if ( !m_data->preedit.isEmpty() )
|
if ( !m_data->preedit.isEmpty() )
|
||||||
{
|
{
|
||||||
m_data->preedit.chop( 1 );
|
m_data->preedit.chop( 1 );
|
||||||
|
sendPreeditTextEvent( polishPreedit( m_data->preedit ) );
|
||||||
const QString displayText = polishPreedit( m_data->preedit );
|
|
||||||
m_data->preeditAttributes.first().length = displayText.length();
|
|
||||||
|
|
||||||
QInputMethodEvent event( displayText, m_data->preeditAttributes );
|
|
||||||
sendCompositionEvent( &event );
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -171,6 +161,7 @@ void QskInputCompositionModel::composeKey( Qt::Key key )
|
|||||||
|
|
||||||
const auto firstCandidate = candidateCount() > 0 ? candidate( 0 ) : QString();
|
const auto firstCandidate = candidateCount() > 0 ? candidate( 0 ) : QString();
|
||||||
const auto oldPreedit = m_data->preedit;
|
const auto oldPreedit = m_data->preedit;
|
||||||
|
|
||||||
m_data->preedit += qskKeyString( key );
|
m_data->preedit += qskKeyString( key );
|
||||||
auto displayPreedit = polishPreedit( m_data->preedit );
|
auto displayPreedit = polishPreedit( m_data->preedit );
|
||||||
|
|
||||||
@ -202,10 +193,7 @@ void QskInputCompositionModel::composeKey( Qt::Key key )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_data->preeditAttributes.first().length = displayPreedit.length();
|
sendPreeditTextEvent( displayPreedit );
|
||||||
|
|
||||||
QInputMethodEvent event( displayPreedit, m_data->preeditAttributes );
|
|
||||||
sendCompositionEvent( &event );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QskInputCompositionModel::clearPreedit()
|
void QskInputCompositionModel::clearPreedit()
|
||||||
@ -224,11 +212,6 @@ QString QskInputCompositionModel::candidate( int ) const
|
|||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method is called before displaying a new preedit string. It can be used
|
|
||||||
// to return a modified preedit string which is not stored as the underlying
|
|
||||||
// preedit text. This allows for marking up the preedit text without changing the
|
|
||||||
// data model. If the actual text needs to be modified, it is safe to call
|
|
||||||
// setPreeditText() here.
|
|
||||||
QString QskInputCompositionModel::polishPreedit( const QString& preedit )
|
QString QskInputCompositionModel::polishPreedit( const QString& preedit )
|
||||||
{
|
{
|
||||||
return preedit;
|
return preedit;
|
||||||
@ -236,12 +219,11 @@ QString QskInputCompositionModel::polishPreedit( const QString& preedit )
|
|||||||
|
|
||||||
void QskInputCompositionModel::commit( const QString& text )
|
void QskInputCompositionModel::commit( const QString& text )
|
||||||
{
|
{
|
||||||
QInputMethodEvent event( QString(), { } );
|
QInputMethodEvent event;
|
||||||
event.setCommitString( text );
|
event.setCommitString( text );
|
||||||
sendCompositionEvent( &event );
|
context()->sendEventToInputItem( &event );
|
||||||
|
|
||||||
m_data->preedit.clear();
|
clearPreedit();
|
||||||
polishPreedit( m_data->preedit );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QskInputCompositionModel::commitCandidate( int index )
|
void QskInputCompositionModel::commitCandidate( int index )
|
||||||
@ -249,9 +231,16 @@ void QskInputCompositionModel::commitCandidate( int index )
|
|||||||
commit( candidate( index ) );
|
commit( candidate( index ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
void QskInputCompositionModel::sendCompositionEvent( QInputMethodEvent* event )
|
void QskInputCompositionModel::sendPreeditTextEvent( const QString& text )
|
||||||
{
|
{
|
||||||
context()->sendEventToInputItem( event );
|
QTextCharFormat format;
|
||||||
|
format.setFontUnderline( true );
|
||||||
|
|
||||||
|
const QInputMethodEvent::Attribute attribute(
|
||||||
|
QInputMethodEvent::TextFormat, 0, text.length(), format );
|
||||||
|
|
||||||
|
QInputMethodEvent event( text, { attribute } );
|
||||||
|
context()->sendEventToInputItem( &event );
|
||||||
}
|
}
|
||||||
|
|
||||||
void QskInputCompositionModel::sendKeyEvents( int key )
|
void QskInputCompositionModel::sendKeyEvents( int key )
|
||||||
|
@ -7,10 +7,8 @@
|
|||||||
#define QSK_INPUT_COMPOSITION_MODEL_H
|
#define QSK_INPUT_COMPOSITION_MODEL_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
class QInputMethodEvent;
|
|
||||||
class QskInputContext;
|
class QskInputContext;
|
||||||
|
|
||||||
class QskInputCompositionModel : public QObject
|
class QskInputCompositionModel : public QObject
|
||||||
@ -26,25 +24,24 @@ public:
|
|||||||
|
|
||||||
void commit( const QString& );
|
void commit( const QString& );
|
||||||
virtual void commitCandidate( int );
|
virtual void commitCandidate( int );
|
||||||
void composeKey( Qt::Key );
|
|
||||||
|
|
||||||
void clearPreedit();
|
void composeKey( Qt::Key );
|
||||||
|
|
||||||
virtual int candidateCount() const;
|
virtual int candidateCount() const;
|
||||||
virtual QString candidate( int ) const;
|
virtual QString candidate( int ) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Used for text composition
|
|
||||||
virtual bool hasIntermediate() const;
|
virtual bool hasIntermediate() const;
|
||||||
virtual QString polishPreedit( const QString& preedit );
|
virtual QString polishPreedit( const QString& preedit );
|
||||||
|
|
||||||
QskInputContext* context() const;
|
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void candidatesChanged();
|
void candidatesChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void sendCompositionEvent( QInputMethodEvent* e );
|
void clearPreedit();
|
||||||
|
QskInputContext* context() const;
|
||||||
|
|
||||||
|
void sendPreeditTextEvent( const QString& );
|
||||||
void sendKeyEvents( int key );
|
void sendKeyEvents( int key );
|
||||||
|
|
||||||
class PrivateData;
|
class PrivateData;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user