From 9be4562d8f138f8d7d2bac5dbfcaf3baf37064a8 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Wed, 23 Mar 2022 14:43:05 +0100 Subject: [PATCH] compiler errors for Qt6 fixed --- src/inputpanel/QskHunspellTextPredictor.cpp | 126 ++++++++++++++++---- src/inputpanel/QskHunspellTextPredictor.h | 3 +- 2 files changed, 103 insertions(+), 26 deletions(-) diff --git a/src/inputpanel/QskHunspellTextPredictor.cpp b/src/inputpanel/QskHunspellTextPredictor.cpp index ecea9a9c..77099176 100644 --- a/src/inputpanel/QskHunspellTextPredictor.cpp +++ b/src/inputpanel/QskHunspellTextPredictor.cpp @@ -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 -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include +#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) + +#include + +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 + +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 { public: @@ -63,19 +139,21 @@ QPair< QString, QString > QskHunspellTextPredictor::affAndDicFile( void QskHunspellTextPredictor::loadDictionaries() { - QString userPaths = QString::fromUtf8( qgetenv( "QSK_HUNSPELL_PATH" ) ); - -#if defined( Q_OS_WIN32 ) - QChar separator( ';' ); - QStringList defaultPaths; + const auto splitBehavior = +#if QT_VERSION >= QT_VERSION_CHECK( 5, 14, 0 ) + Qt::SkipEmptyParts; #else - QChar separator( ':' ); - QStringList defaultPaths = { QStringLiteral( "/usr/share/hunspell" ), - QStringLiteral( "/usr/share/myspell/dicts" ) }; + QString::SkipEmptyParts; #endif - QStringList paths = userPaths.split( separator, QString::SkipEmptyParts ); - paths.append( defaultPaths ); + const auto userPaths = QString::fromUtf8( qgetenv( "QSK_HUNSPELL_PATH" ) ); + + 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 ) { @@ -106,21 +184,19 @@ void QskHunspellTextPredictor::request( const QString& text ) return; } + StringConverter converter( m_data->hunspellEncoding ); + char** suggestions; - const auto codec = QTextCodec::codecForName( m_data->hunspellEncoding ); - const QByteArray word = codec ? codec->fromUnicode( text ) : text.toUtf8(); - - const int count = Hunspell_suggest( - m_data->hunspellHandle, &suggestions, word.constData() ); + const int count = Hunspell_suggest( m_data->hunspellHandle, + &suggestions, converter.toHunspell( text ).constData() ); QStringList candidates; candidates.reserve( count ); for ( int i = 0; i < count; i++ ) { - const QString suggestion = codec ? codec->toUnicode( suggestions[ i ] ) - : QString::fromUtf8( suggestions [ i ] ); + const auto suggestion = converter.fromHunspell( suggestions[ i ] ); if ( suggestion.startsWith( text ) ) candidates.prepend( suggestion ); @@ -133,3 +209,5 @@ void QskHunspellTextPredictor::request( const QString& text ) m_data->candidates = candidates; Q_EMIT predictionChanged( text, m_data->candidates ); } + +#include "moc_QskHunspellTextPredictor.cpp" diff --git a/src/inputpanel/QskHunspellTextPredictor.h b/src/inputpanel/QskHunspellTextPredictor.h index 610f83e7..e2dab1d9 100644 --- a/src/inputpanel/QskHunspellTextPredictor.h +++ b/src/inputpanel/QskHunspellTextPredictor.h @@ -8,8 +8,7 @@ #include "QskTextPredictor.h" -#include - +#include #include class QSK_EXPORT QskHunspellTextPredictor : public QskTextPredictor