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