qskinny/inputcontext/QskHunspellCompositionModel.cpp

76 lines
1.8 KiB
C++
Raw Normal View History

#include "QskHunspellCompositionModel.h"
#include <QVector>
#include "hunspell.h"
class QskHunspellCompositionModel::PrivateData
{
public:
Hunhandle* hunspellHandle;
QVector< QString > candidates;
};
2018-04-04 15:19:51 +02:00
QskHunspellCompositionModel::QskHunspellCompositionModel( QskInputContext* context ):
2018-04-20 08:52:26 +02:00
Inherited( Words, context ),
m_data( new PrivateData() )
{
#if 1
2018-04-04 15:19:51 +02:00
// TODO: loading the language specific one depending on the locale
m_data->hunspellHandle = Hunspell_create(
"/usr/share/hunspell/en_US.aff",
"/usr/share/hunspell/en_US.dic" );
#endif
}
QskHunspellCompositionModel::~QskHunspellCompositionModel()
{
Hunspell_destroy( m_data->hunspellHandle );
}
int QskHunspellCompositionModel::candidateCount() const
{
return m_data->candidates.count();
}
QString QskHunspellCompositionModel::candidate( int pos ) const
{
return m_data->candidates[ pos ];
}
2018-04-20 08:52:26 +02:00
void QskHunspellCompositionModel::resetCandidates()
{
2018-04-20 08:52:26 +02:00
if ( !m_data->candidates.isEmpty() )
{
m_data->candidates.clear();
2018-04-20 08:52:26 +02:00
Q_EMIT candidatesChanged();
}
2018-04-20 08:52:26 +02:00
}
2018-04-20 08:52:26 +02:00
void QskHunspellCompositionModel::requestCandidates( const QString& text )
{
char** suggestions;
const QByteArray word = text.toUtf8(); // ### do we need to check the encoding
2018-04-20 08:52:26 +02:00
const int count = Hunspell_suggest(
m_data->hunspellHandle, &suggestions, word.constData() );
2018-04-20 08:52:26 +02:00
QVector< QString > candidates;
candidates.reserve( count );
2018-04-20 08:52:26 +02:00
for( int i = 0; i < count; i++ )
{
const QString suggestion = QString::fromUtf8( suggestions[i] ); // ### encoding?
2018-04-20 08:52:26 +02:00
if( suggestion.startsWith( text ) )
candidates.prepend( suggestion );
else
candidates.append( suggestion );
}
2018-04-20 08:52:26 +02:00
Hunspell_free_list( m_data->hunspellHandle, &suggestions, count );
2018-04-20 08:52:26 +02:00
m_data->candidates = candidates;
Q_EMIT candidatesChanged();
}