1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
/******************************************************************************
*
* Copyright (C) 1997-2022 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
*/
#ifndef ADAPTER_H
#define ADAPTER_H
#include <memory>
#include <QtGlobal>
#include <QString>
#include <QTextStream>
#include <QPointF>
#include <QMouseEvent>
/** @file
* @brief compatibility adapters for Qt5/Qt6 support.
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QTextCodec>
class TextCodecAdapter
{
public:
TextCodecAdapter(const QByteArray &name)
{
m_codec = QTextCodec::codecForName(name);
if (m_codec==0) // fallback: use UTF-8
{
m_codec = QTextCodec::codecForName("UTF-8");
}
}
QByteArray encode(const QString &input) { return m_codec ? m_codec->fromUnicode(input) : input.toLatin1(); }
QString decode(const QByteArray &input) { return m_codec ? m_codec->toUnicode(input) : QString::fromLatin1(input); }
void applyToStream(QTextStream &t) { t.setCodec(m_codec); }
bool isValid() const { return m_codec!=0; }
private:
QTextCodec *m_codec = 0; // object is owned by Qt
};
#else // Qt6+
#include <QStringEncoder>
#include <QStringDecoder>
#include <QStringConverter>
class TextCodecAdapter
{
public:
TextCodecAdapter(const QByteArray &name)
{
auto encodingOpt = QStringConverter::encodingForName(name);
if (encodingOpt)
{
m_encoding = *encodingOpt;
}
m_encoder = std::make_unique<QStringEncoder>(m_encoding);
m_decoder = std::make_unique<QStringDecoder>(m_encoding);
}
QByteArray encode(const QString &input) { return m_encoder ? m_encoder->encode(input) : input.toLatin1(); }
QString decode(const QByteArray &input) { return m_decoder ? m_decoder->decode(input) : QString::fromLatin1(input); }
void applyToStream(QTextStream &t) { t.setEncoding(m_encoding); }
bool isValid() const { return m_decoder!=0; }
private:
std::unique_ptr<QStringEncoder> m_encoder;
std::unique_ptr<QStringDecoder> m_decoder;
QStringConverter::Encoding m_encoding = QStringConverter::Utf8;
};
#endif
inline qreal getMouseYPositionFromEvent(QMouseEvent *m)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return m->y();
#else
return m->position().y();
#endif
}
#endif
|