compiler errors for Qt6 fixed

This commit is contained in:
Uwe Rathmann 2022-03-23 14:43:05 +01:00
parent 1cc0dff8d5
commit 9be4562d8f
2 changed files with 103 additions and 26 deletions

View File

@ -1,15 +1,91 @@
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskHunspellTextPredictor.h" #include "QskHunspellTextPredictor.h"
#include <QDebug> #include <qlocale.h>
#include <QDir> #include <qtimer.h>
#include <QFileInfo> #include <qfile.h>
#include <QLocale> #include <qdir.h>
#include <QTextCodec> #include <qdebug.h>
#include <QTimer>
#include <QVector>
#include <hunspell/hunspell.h> #include <hunspell/hunspell.h>
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
#include <qtextcodec.h>
namespace
{
class StringConverter
{
public:
StringConverter( const QByteArray& encoding )
: m_codec( QTextCodec::codecForName( encoding ) )
{
}
inline QString fromHunspell( const char* text ) const
{
if ( m_codec )
return m_codec->toUnicode( text );
return QString::fromUtf8( text );
}
inline QByteArray toHunspell( const QString& text ) const
{
if ( m_codec )
return m_codec->fromUnicode( text );
return text.toUtf8();
}
private:
QTextCodec* m_codec;
};
}
#else
#include <qstringconverter.h>
namespace
{
class StringConverter
{
public:
StringConverter( const QByteArray& encoding )
: m_decoder( encoding )
, m_encoder( encoding )
{
}
inline QString fromHunspell( const char* text ) const
{
if ( m_decoder.isValid() )
return m_decoder.decode( text );
return QString::fromUtf8( text );
}
inline QByteArray toHunspell( const QString& text ) const
{
if ( m_encoder.isValid() )
return m_encoder.encode( text );
return text.toUtf8();
}
private:
mutable QStringDecoder m_decoder;
mutable QStringEncoder m_encoder;
};
}
#endif
class QskHunspellTextPredictor::PrivateData class QskHunspellTextPredictor::PrivateData
{ {
public: public:
@ -63,19 +139,21 @@ QPair< QString, QString > QskHunspellTextPredictor::affAndDicFile(
void QskHunspellTextPredictor::loadDictionaries() void QskHunspellTextPredictor::loadDictionaries()
{ {
QString userPaths = QString::fromUtf8( qgetenv( "QSK_HUNSPELL_PATH" ) ); const auto splitBehavior =
#if QT_VERSION >= QT_VERSION_CHECK( 5, 14, 0 )
#if defined( Q_OS_WIN32 ) Qt::SkipEmptyParts;
QChar separator( ';' );
QStringList defaultPaths;
#else #else
QChar separator( ':' ); QString::SkipEmptyParts;
QStringList defaultPaths = { QStringLiteral( "/usr/share/hunspell" ),
QStringLiteral( "/usr/share/myspell/dicts" ) };
#endif #endif
QStringList paths = userPaths.split( separator, QString::SkipEmptyParts ); const auto userPaths = QString::fromUtf8( qgetenv( "QSK_HUNSPELL_PATH" ) );
paths.append( defaultPaths );
auto paths = userPaths.split( QDir::listSeparator(), splitBehavior );
#if !defined( Q_OS_WIN32 )
paths += QStringLiteral( "/usr/share/hunspell" );
paths += QStringLiteral( "/usr/share/myspell/dicts" );
#endif
for( const auto& path : paths ) for( const auto& path : paths )
{ {
@ -106,21 +184,19 @@ void QskHunspellTextPredictor::request( const QString& text )
return; return;
} }
StringConverter converter( m_data->hunspellEncoding );
char** suggestions; char** suggestions;
const auto codec = QTextCodec::codecForName( m_data->hunspellEncoding ); const int count = Hunspell_suggest( m_data->hunspellHandle,
const QByteArray word = codec ? codec->fromUnicode( text ) : text.toUtf8(); &suggestions, converter.toHunspell( text ).constData() );
const int count = Hunspell_suggest(
m_data->hunspellHandle, &suggestions, word.constData() );
QStringList candidates; QStringList candidates;
candidates.reserve( count ); candidates.reserve( count );
for ( int i = 0; i < count; i++ ) for ( int i = 0; i < count; i++ )
{ {
const QString suggestion = codec ? codec->toUnicode( suggestions[ i ] ) const auto suggestion = converter.fromHunspell( suggestions[ i ] );
: QString::fromUtf8( suggestions [ i ] );
if ( suggestion.startsWith( text ) ) if ( suggestion.startsWith( text ) )
candidates.prepend( suggestion ); candidates.prepend( suggestion );
@ -133,3 +209,5 @@ void QskHunspellTextPredictor::request( const QString& text )
m_data->candidates = candidates; m_data->candidates = candidates;
Q_EMIT predictionChanged( text, m_data->candidates ); Q_EMIT predictionChanged( text, m_data->candidates );
} }
#include "moc_QskHunspellTextPredictor.cpp"

View File

@ -8,8 +8,7 @@
#include "QskTextPredictor.h" #include "QskTextPredictor.h"
#include <QPair> #include <qpair.h>
#include <memory> #include <memory>
class QSK_EXPORT QskHunspellTextPredictor : public QskTextPredictor class QSK_EXPORT QskHunspellTextPredictor : public QskTextPredictor