From 787cfcfc16112b562b25b6eee91d2b0064678880 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 11 Feb 2023 16:14:15 +0200 Subject: [PATCH] Use QStringConverter to replace QTextCodec. --- .../dialogs/dialogmeasurementscsvcolumns.h | 11 +- src/app/tape/tmainwindow.cpp | 13 +- src/app/valentina/core/vcmdexport.cpp | 7 +- src/app/valentina/mainwindow.cpp | 14 +- src/app/valentina/mainwindowsnogui.cpp | 9 +- src/libs/ifc/xml/vdomdocument.cpp | 2 + src/libs/qmuparser/qmudef.cpp | 41 +++++ src/libs/vdxf/libdxfrw/drw_base.h | 1 - .../vdxf/libdxfrw/intern/drw_textcodec.cpp | 69 ++----- src/libs/vdxf/libdxfrw/intern/drw_textcodec.h | 12 +- src/libs/vdxf/vdxfengine.cpp | 9 +- src/libs/vdxf/vdxfengine.h | 8 +- src/libs/vmisc/defglobal.h | 5 + src/libs/vmisc/dialogs/dialogexporttocsv.cpp | 17 +- src/libs/vmisc/qxtcsvmodel.cpp | 32 ++-- src/libs/vmisc/qxtcsvmodel.h | 17 +- src/libs/vmisc/vabstractapplication.cpp | 43 ++++- src/libs/vmisc/vabstractapplication.h | 15 +- src/libs/vmisc/vcommonsettings.cpp | 10 +- src/libs/vmisc/vmisc.qbs | 86 +++++---- src/libs/vmisc/vtextcodec.cpp | 174 ++++++++++++++++++ src/libs/vmisc/vtextcodec.h | 78 ++++++++ src/libs/vpropertyexplorer/vpropertydef.cpp | 40 ++++ src/test/ValentinaTest/tst_dxf.cpp | 13 +- 24 files changed, 578 insertions(+), 148 deletions(-) create mode 100644 src/libs/vmisc/vtextcodec.cpp create mode 100644 src/libs/vmisc/vtextcodec.h diff --git a/src/app/tape/dialogs/dialogmeasurementscsvcolumns.h b/src/app/tape/dialogs/dialogmeasurementscsvcolumns.h index 7470e14b9..b4c2dd311 100644 --- a/src/app/tape/dialogs/dialogmeasurementscsvcolumns.h +++ b/src/app/tape/dialogs/dialogmeasurementscsvcolumns.h @@ -33,7 +33,10 @@ #include "../vformat/vdimensions.h" class QxtCsvModel; -class QTextCodec; + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +class VTextCodec; +#endif enum class IndividualMeasurementsColumns: qint8 { @@ -75,7 +78,7 @@ public: void SetWithHeader(bool withHeader); void SetSeparator(const QChar &separator); - void SetCodec(QTextCodec *codec); + void SetCodec(VTextCodec *codec); protected: void changeEvent(QEvent* event) override; @@ -92,7 +95,7 @@ private: QString m_fileName; bool m_withHeader{false}; QChar m_separator{','}; - QTextCodec *m_codec{nullptr}; + VTextCodec *m_codec{nullptr}; QVector m_columnsMap{}; MeasurementsType m_type; QList m_dimensions{}; @@ -150,7 +153,7 @@ inline void DialogMeasurementsCSVColumns::SetSeparator(const QChar &separator) } //--------------------------------------------------------------------------------------------------------------------- -inline void DialogMeasurementsCSVColumns::SetCodec(QTextCodec *codec) +inline void DialogMeasurementsCSVColumns::SetCodec(VTextCodec *codec) { m_codec = codec; } diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index 65b28c105..aea617454 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -59,6 +59,12 @@ #include "../vmisc/dialogs/dialogselectlanguage.h" #include "mapplication.h" // Should be last because of definning qApp +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include +#endif + #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0) #include "../vmisc/backport/qscopeguard.h" #else @@ -71,7 +77,6 @@ #include #include #include -#include #include #include @@ -849,7 +854,7 @@ void TMainWindow::ExportToCSVData(const QString &fileName, bool withHeader, int } QString error; - csv.toCSV(fileName, error, withHeader, separator, QTextCodec::codecForMib(mib)); + csv.toCSV(fileName, error, withHeader, separator, VTextCodec::codecForMib(mib)); } //--------------------------------------------------------------------------------------------------------------------- @@ -1102,12 +1107,12 @@ void TMainWindow::ImportDataFromCSV() } columns->SetWithHeader(dialog.IsWithHeader()); columns->SetSeparator(dialog.GetSeparator()); - columns->SetCodec(QTextCodec::codecForMib(dialog.GetSelectedMib())); + columns->SetCodec(VTextCodec::codecForMib(dialog.GetSelectedMib())); if (columns->exec() == QDialog::Accepted) { QxtCsvModel csv(fileName, nullptr, dialog.IsWithHeader(), dialog.GetSeparator(), - QTextCodec::codecForMib(dialog.GetSelectedMib())); + VTextCodec::codecForMib(dialog.GetSelectedMib())); const QVector map = columns->ColumnsMap(); if (m_m->Type() == MeasurementsType::Individual) diff --git a/src/app/valentina/core/vcmdexport.cpp b/src/app/valentina/core/vcmdexport.cpp index 00285e150..26044e89c 100644 --- a/src/app/valentina/core/vcmdexport.cpp +++ b/src/app/valentina/core/vcmdexport.cpp @@ -36,7 +36,12 @@ #include "../vmisc/dialogs/dialogexporttocsv.h" #include "../vlayout/vlayoutgenerator.h" #include + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else #include +#endif VCommandLinePtr VCommandLine::instance = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) @@ -736,7 +741,7 @@ void VCommandLine::InitCommandLineOptions() "Qt. Default value depend from system. On Windows, the codec will be based on a system locale. On Unix " "systems, the codec will might fall back to using the iconv library if no builtin codec for the locale can be " "found. Valid values for this installation:") + DialogExportToCSV::MakeHelpCodecsList(), - translate("VCommandLine", "Codec name"), QString(QTextCodec::codecForLocale()->name())}, + translate("VCommandLine", "Codec name"), QString(VTextCodec::codecForLocale()->name())}, {LONG_OPTION_CSVSEPARATOR, translate("VCommandLine", "Specify csv separator character. Default value is '%1'. Valid characters:") .arg(VCommonSettings::GetDefCSVSeparator()) + DialogExportToCSV::MakeHelpSeparatorList(), diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index b3f56c3a0..f4782da13 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -49,11 +49,16 @@ #include "../ifc/xml/vvitconverter.h" #include "../vwidgets/vwidgetpopup.h" #include "../vwidgets/vmaingraphicsscene.h" - #include "../vtools/undocommands/undogroup.h" #include "../vformat/vpatternrecipe.h" #include "../vlayout/dialogs/watermarkwindow.h" +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include +#endif + #if QT_VERSION < QT_VERSION_CHECK(5, 7, 0) #include "../vmisc/backport/qoverload.h" #endif // QT_VERSION < QT_VERSION_CHECK(5, 7, 0) @@ -201,7 +206,6 @@ #include #include #include -#include #include #include #include @@ -1924,7 +1928,7 @@ void MainWindow::ExportToCSVData(const QString &fileName, bool withHeader, int m SavePreviewCalculation(true); QString error; - csv.toCSV(fileName, error, withHeader, separator, QTextCodec::codecForMib(mib)); + csv.toCSV(fileName, error, withHeader, separator, VTextCodec::codecForMib(mib)); } //--------------------------------------------------------------------------------------------------------------------- @@ -6735,10 +6739,10 @@ auto MainWindow::DoFMExport(const VCommandLinePtr &expParams) -> bool } const QString codecName = expParams->OptCSVCodecName(); - int mib = QTextCodec::codecForLocale()->mibEnum(); + int mib = VTextCodec::codecForLocale()->mibEnum(); if (not codecName.isEmpty()) { - if (QTextCodec *codec = QTextCodec::codecForName(codecName.toLatin1())) + if (VTextCodec *codec = VTextCodec::codecForName(codecName.toLatin1())) { mib = codec->mibEnum(); } diff --git a/src/app/valentina/mainwindowsnogui.cpp b/src/app/valentina/mainwindowsnogui.cpp index e610454af..64096015f 100644 --- a/src/app/valentina/mainwindowsnogui.cpp +++ b/src/app/valentina/mainwindowsnogui.cpp @@ -48,6 +48,12 @@ #include "../vmisc/vvalentinasettings.h" #include "../vdxf/libdxfrw/drw_base.h" +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include +#endif + #include #include #include @@ -62,7 +68,6 @@ #include #include #include -#include #if defined(Q_OS_WIN32) && QT_VERSION >= QT_VERSION_CHECK(5, 7, 0) #include @@ -1308,7 +1313,7 @@ bool MainWindowsNoGUI::ExportFMeasurementsToCSVData(const QString &fileName, boo } QString error; - const bool success = csv.toCSV(fileName, error, withHeader, separator, QTextCodec::codecForMib(mib)); + const bool success = csv.toCSV(fileName, error, withHeader, separator, VTextCodec::codecForMib(mib)); if (not success) { diff --git a/src/libs/ifc/xml/vdomdocument.cpp b/src/libs/ifc/xml/vdomdocument.cpp index 9a63c474f..9e676c40b 100644 --- a/src/libs/ifc/xml/vdomdocument.cpp +++ b/src/libs/ifc/xml/vdomdocument.cpp @@ -791,7 +791,9 @@ bool VDomDocument::SaveDocument(const QString &fileName, QString &error) } // Left these strings in case we will need them for testing purposes // QTextStream out(&file); +//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // out.setCodec("UTF-8"); +//#endif // save(out, indent); success = file.commit(); diff --git a/src/libs/qmuparser/qmudef.cpp b/src/libs/qmuparser/qmudef.cpp index db2247969..62f0dc1bf 100644 --- a/src/libs/qmuparser/qmudef.cpp +++ b/src/libs/qmuparser/qmudef.cpp @@ -372,6 +372,7 @@ bool SupportedLocale(const QLocale &locale) locale.decimalPoint().size() == 1 && locale.groupSeparator().size() == 1; #else + Q_UNUSED(locale) return true; #endif } @@ -417,7 +418,11 @@ QChar LocaleSign0(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'0'}; +#else + return QChar('0'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -429,7 +434,11 @@ QChar LocaleSign1(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'1'}; +#else + return QChar('1'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -441,7 +450,11 @@ QChar LocaleSign2(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'2'}; +#else + return QChar('2'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -453,7 +466,11 @@ QChar LocaleSign3(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'3'}; +#else + return QChar('3'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -465,7 +482,11 @@ QChar LocaleSign4(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'4'}; +#else + return QChar('4'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -477,7 +498,11 @@ QChar LocaleSign5(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'5'}; +#else + return QChar('5'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -489,7 +514,11 @@ QChar LocaleSign6(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'6'}; +#else + return QChar('6'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -501,7 +530,11 @@ QChar LocaleSign7(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'7'}; +#else + return QChar('7'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -513,7 +546,11 @@ QChar LocaleSign8(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'8'}; +#else + return QChar('8'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -525,7 +562,11 @@ QChar LocaleSign9(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'9'}; +#else + return QChar('9'); +#endif } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vdxf/libdxfrw/drw_base.h b/src/libs/vdxf/libdxfrw/drw_base.h index 2c84853de..32638004c 100644 --- a/src/libs/vdxf/libdxfrw/drw_base.h +++ b/src/libs/vdxf/libdxfrw/drw_base.h @@ -110,7 +110,6 @@ typedef float dfloat32; /* 32 bit floating point */ typedef double ddouble64; /* 64 bit floating point */ typedef long double ddouble80; /* 80 bit floating point */ - namespace DRW { //! Version numbers for the DXF Format. diff --git a/src/libs/vdxf/libdxfrw/intern/drw_textcodec.cpp b/src/libs/vdxf/libdxfrw/intern/drw_textcodec.cpp index a9fcc67c7..856515a9b 100644 --- a/src/libs/vdxf/libdxfrw/intern/drw_textcodec.cpp +++ b/src/libs/vdxf/libdxfrw/intern/drw_textcodec.cpp @@ -4,46 +4,16 @@ #include #include #include -#include #include #include "../drw_base.h" #include "../vmisc/vabstractvalapplication.h" #include "../ifc/exception/vexception.h" -namespace -{ -QMap QtCodecs() -{ - return QMap - { - {"ANSI_874", {"ANSI_874", "CP874", "windows-874", "MS874", "x-windows-874", "TIS-620", "IBM1162", - "x-IBM874"}}, // Latin/Thai - {"ANSI_932", {"ANSI_932", "CP932", "SHIFT-JIS", "SHIFT_JIS", "CSSHIFTJIS", "CSWINDOWS31J", "MS_KANJI", - "X-MS-CP932", "X-SJIS", "EUCJP", "EUC-JP", "CSEUCPKDFMTJAPANESE", "X-EUC", "X-EUC-JP", "IBM-943", - "JIS7"}}, // Japanese - {"ANSI_936", {"ANSI_936", "GBK", "CP936", "MS936", "Windows-936", "GB2312", - "CHINESE"}}, // Chinese PRC GBK (XGB) simplified - {"ANSI_949", {"ANSI_949", "Windows-949", "MS949", "CP949"}}, // Korean - {"ANSI_950", {"ANSI_950", "BIG5", "windows-950-2000", "csBig5", "windows-950", "x-windows-950", "x-big5", - "ms950"}}, // Chinese Big5 (Taiwan, Hong Kong SAR) - {"ANSI_1250", {"ANSI_1250", "CP1250", "windows-1250", "ibm-1250_P100-1995", - "ibm-1250"}}, //Central Europe and Eastern Europe - {"ANSI_1251", {"ANSI_1251", "CP1251", "windows-1251", "ANSI1251", "ibm-5347_P100-1998", - "ibm-5347"}}, // Cyrillic script - {"ANSI_1252", {"ANSI_1252", "CP1252", "windows-1252", "LATIN1", "ISO-8859-1", "CP819", "CSISO", "IBM819", - "ISO_8859-1", "APPLE ROMAN", "ISO8859-1", "ISO8859-15", "ISO-IR-100", "L1", - "IBM 850", "850"}}, // Western Europe - {"ANSI_1253", {"ANSI_1253", "CP1253", "windows-1253"}}, // Greek - {"ANSI_1254", {"ANSI_1254", "CP1254", "windows-1254"}}, // Turkish - {"ANSI_1255", {"ANSI_1255", "CP1255", "windows-1255"}}, // Hebrew - {"ANSI_1256", {"ANSI_1256", "CP1256", "windows-1256", "x-windows-1256S"}}, // Arabic - {"ANSI_1257", {"ANSI_1257", "CP1257", "windows-1257"}}, // Baltic - {"ANSI_1258", {"ANSI_1258", "CP1258", "windows-1258"}}, // Vietnamese - {"UTF-8", {"UTF-8", "UTF8", "UTF8-BIT"}}, - {"UTF-16", {"UTF-16", "UTF16", "UTF16-BIT"}}, - }; -} -} // namespace +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include +#endif DRW_TextCodec::DRW_TextCodec() : version(DRW::AC1021) @@ -123,29 +93,22 @@ void DRW_TextCodec::setCodePage(const std::string &c, bool dxfFormat){ if (version < DRW::AC1021) { if (cp == "UTF-8") - { //DXF older than 2007 are write in win codepages + { // DXF older than 2007 are write in win codepages cp = "ANSI_1252"; } conv = DRW_TextCodec::CodecForName(QString::fromStdString(cp)); } else { - if (dxfFormat) - { - conv = DRW_TextCodec::CodecForName(QString::fromStdString("UTF-8")); - } - else - { - conv = DRW_TextCodec::CodecForName(QString::fromStdString("UTF-16")); - } + conv = DRW_TextCodec::CodecForName(dxfFormat ? QStringLiteral("UTF-8") : QStringLiteral("UTF-16")); } if (conv == nullptr) { const QString errorMsg = QCoreApplication::translate("DRW_TextCodec", "No available codec for code page '%1'.") .arg(cp.c_str()); - VAbstractApplication::VApp()->IsPedantic() ? throw VException(errorMsg) : - qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg; + VAbstractApplication::VApp()->IsPedantic() + ? throw VException(errorMsg) : qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg; if (version < DRW::AC1021 && cp == "UTF-8") { @@ -157,7 +120,7 @@ void DRW_TextCodec::setCodePage(const std::string &c, bool dxfFormat){ QMap DRW_TextCodec::DXFCodePageMap() { - return QMap + static auto map = QMap { {"ANSI_874", {"ANSI_874", "CP874", "ISO8859-11", "TIS-620"}}, // Latin/Thai {"ANSI_932", {"ANSI_932", "SHIFT-JIS", "SHIFT_JIS", "CSSHIFTJIS", "CSWINDOWS31J", "MS_KANJI", "X-MS-CP932", @@ -170,8 +133,8 @@ QMap DRW_TextCodec::DXFCodePageMap() "BIG5-HKSCS"}}, // Chinese Big5 (Taiwan, Hong Kong SAR) {"ANSI_1250", {"ANSI_1250", "CP1250", "ISO8859-2"}}, //Central Europe and Eastern Europe {"ANSI_1251", {"ANSI_1251", "CP1251", "ISO8859-5", "KOI8-R", "KOI8-U", "IBM 866"}}, // Cyrillic script - {"ANSI_1252", {"ANSI_1252", "CP1252", "LATIN1", "ISO-8859-1", "CP819", "CSISO", "IBM819", "ISO_8859-1", - "APPLE ROMAN", "ISO8859-1", "ISO8859-15", "ISO-IR-100", "L1", "IBM 850"}}, // Western Europe + {"ANSI_1252", {"ANSI_1252", "CP1252", "LATIN1", "ISO-8859-1", "CP819", "CSISO", "IBM819", "L1", + "ISO_8859-1", "APPLE ROMAN", "ISO8859-1", "ISO8859-15", "ISO-IR-100", "IBM 850"}}, // Western Europe {"ANSI_1253", {"ANSI_1253", "CP1253", "ISO8859-7"}}, // Greek {"ANSI_1254", {"ANSI_1254", "CP1254", "ISO8859-9", "iso8859-3"}}, // Turkish {"ANSI_1255", {"ANSI_1255", "CP1255", "ISO8859-8"}}, // Hebrew @@ -181,17 +144,19 @@ QMap DRW_TextCodec::DXFCodePageMap() {"UTF-8", {"UTF-8", "UTF8", "UTF8-BIT"}}, {"UTF-16", {"UTF-16", "UTF16", "UTF16-BIT"}}, }; + + return map; } -QTextCodec *DRW_TextCodec::CodecForName(const QString &name) +VTextCodec *DRW_TextCodec::CodecForName(const QString &name) { - QMap knownCodecs = QtCodecs(); + QMap knownCodecs = DXFCodePageMap(); if (knownCodecs.contains(name)) { QStringList aliases = knownCodecs.value(name); for (auto &alias : aliases) { - if (QTextCodec *codec = QTextCodec::codecForName(alias.toLatin1())) + if (VTextCodec *codec = VTextCodec::codecForName(alias.toLatin1())) { return codec; } diff --git a/src/libs/vdxf/libdxfrw/intern/drw_textcodec.h b/src/libs/vdxf/libdxfrw/intern/drw_textcodec.h index f303d27fe..af38adf22 100644 --- a/src/libs/vdxf/libdxfrw/intern/drw_textcodec.h +++ b/src/libs/vdxf/libdxfrw/intern/drw_textcodec.h @@ -4,9 +4,13 @@ #include #include #include "../drw_base.h" -#include "../vmisc/defglobal.h" -class QTextCodec; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include "../vmisc/defglobal.h" +#include +#endif #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) class QStringList; @@ -27,7 +31,7 @@ public: std::string getCodePage() const {return cp;} static QMap DXFCodePageMap(); - static QTextCodec* CodecForName(const QString &name); + static VTextCodec* CodecForName(const QString &name); private: static auto correctCodePage(const std::string& s) -> std::string; @@ -37,7 +41,7 @@ private: Q_DISABLE_COPY_MOVE(DRW_TextCodec) // NOLINT DRW::Version version{DRW::UNKNOWNV}; std::string cp{}; - QTextCodec *conv{nullptr}; + VTextCodec *conv{nullptr}; }; #endif // DRW_TEXTCODEC_H diff --git a/src/libs/vdxf/vdxfengine.cpp b/src/libs/vdxf/vdxfengine.cpp index da9e04d00..8f4430905 100644 --- a/src/libs/vdxf/vdxfengine.cpp +++ b/src/libs/vdxf/vdxfengine.cpp @@ -41,12 +41,17 @@ #include #include #include -#include #include #include #include #include +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include +#endif + #include "../vmisc/def.h" #if QT_VERSION < QT_VERSION_CHECK(5, 5, 0) #include "../vmisc/diagnostic.h" @@ -1251,7 +1256,7 @@ auto VDxfEngine::AAMAPoint(const QPointF &pos, const UTF8STRING &layer) const -> } //--------------------------------------------------------------------------------------------------------------------- -auto VDxfEngine::FromUnicodeToCodec(const QString &str, QTextCodec *codec) -> std::string +auto VDxfEngine::FromUnicodeToCodec(const QString &str, VTextCodec *codec) -> std::string { return codec->fromUnicode(str).toStdString(); } diff --git a/src/libs/vdxf/vdxfengine.h b/src/libs/vdxf/vdxfengine.h index 82ed4f86f..525350f63 100644 --- a/src/libs/vdxf/vdxfengine.h +++ b/src/libs/vdxf/vdxfengine.h @@ -39,6 +39,7 @@ #include #include "../vmisc/def.h" +#include "../vmisc/defglobal.h" #include "dxfdef.h" #include "libdxfrw/drw_base.h" @@ -50,7 +51,10 @@ class DRW_Entity; class dx_ifaceBlock; class VLayoutPoint; class DRW_Point; -class QTextCodec; + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +class VTextCodec; +#endif class VDxfEngine final : public QPaintEngine { @@ -158,7 +162,7 @@ private: Q_REQUIRED_RESULT auto CreateAAMAPolygon(const QVector &polygon, const UTF8STRING &layer, bool forceClosed) -> P *; - static auto FromUnicodeToCodec(const QString &str, QTextCodec *codec) -> std::string; + static auto FromUnicodeToCodec(const QString &str, VTextCodec *codec) -> std::string; auto GetFileNameForLocale() const -> std::string; }; diff --git a/src/libs/vmisc/defglobal.h b/src/libs/vmisc/defglobal.h index 44b7e5939..569c617a6 100644 --- a/src/libs/vmisc/defglobal.h +++ b/src/libs/vmisc/defglobal.h @@ -125,4 +125,9 @@ using vsizetype = qsizetype; using vsizetype = int; #endif +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +class QTextCodec; +using VTextCodec = QTextCodec; +#endif + #endif // DEFGLOBAL_H diff --git a/src/libs/vmisc/dialogs/dialogexporttocsv.cpp b/src/libs/vmisc/dialogs/dialogexporttocsv.cpp index 381a05883..a6323af34 100644 --- a/src/libs/vmisc/dialogs/dialogexporttocsv.cpp +++ b/src/libs/vmisc/dialogs/dialogexporttocsv.cpp @@ -35,9 +35,14 @@ #include #include -#include #include +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vtextcodec.h" +#else +#include +#endif + #if QT_VERSION < QT_VERSION_CHECK(5, 7, 0) #include "../vmisc/backport/qoverload.h" #endif // QT_VERSION < QT_VERSION_CHECK(5, 7, 0) @@ -51,10 +56,10 @@ DialogExportToCSV::DialogExportToCSV(QWidget *parent) { ui->setupUi(this); - const QList mibs = QTextCodec::availableMibs(); + const QList mibs = VTextCodec::availableMibs(); for (auto mib : mibs) { - if (QTextCodec *codec = QTextCodec::codecForMib(mib)) + if (VTextCodec *codec = VTextCodec::codecForMib(mib)) { ui->comboBoxCodec->addItem(codec->name(), mib); } @@ -214,7 +219,7 @@ void DialogExportToCSV::ShowPreview() ui->groupBoxPreview->setVisible(true); - QxtCsvModel csv(m_fileName, nullptr, IsWithHeader(), GetSeparator(), QTextCodec::codecForMib(GetSelectedMib())); + QxtCsvModel csv(m_fileName, nullptr, IsWithHeader(), GetSeparator(), VTextCodec::codecForMib(GetSelectedMib())); const int columns = csv.columnCount(); const int rows = csv.rowCount(); @@ -285,10 +290,10 @@ void DialogExportToCSV::ShowFilePreview(const QString &fileName) QString DialogExportToCSV::MakeHelpCodecsList() { QString out = QStringLiteral("\n"); - const QList list = QTextCodec::availableMibs(); + const QList list = VTextCodec::availableMibs(); for (int i = 0; i < list.size(); ++i) { - if (QTextCodec *codec = QTextCodec::codecForMib(list.at(i))) + if (VTextCodec *codec = VTextCodec::codecForMib(list.at(i))) { out += QStringLiteral("\t* ") + codec->name(); out += i < list.size()-1 ? QLatin1String(",\n") : QLatin1String(".\n"); diff --git a/src/libs/vmisc/qxtcsvmodel.cpp b/src/libs/vmisc/qxtcsvmodel.cpp index 931b7a2dd..336294cb2 100644 --- a/src/libs/vmisc/qxtcsvmodel.cpp +++ b/src/libs/vmisc/qxtcsvmodel.cpp @@ -41,6 +41,12 @@ #include #include +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include +#endif + #if QT_VERSION < QT_VERSION_CHECK(5, 5, 0) #include "../vmisc/diagnostic.h" #endif // QT_VERSION < QT_VERSION_CHECK(5, 5, 0) @@ -85,7 +91,7 @@ QxtCsvModel::QxtCsvModel(QObject *parent) : QAbstractTableModel(parent) \sa setSource */ -QxtCsvModel::QxtCsvModel(QIODevice *file, QObject *parent, bool withHeader, QChar separator, QTextCodec* codec) +QxtCsvModel::QxtCsvModel(QIODevice *file, QObject *parent, bool withHeader, QChar separator, VTextCodec* codec) : QAbstractTableModel(parent) { QXT_INIT_PRIVATE(QxtCsvModel) @@ -102,7 +108,7 @@ QxtCsvModel::QxtCsvModel(QIODevice *file, QObject *parent, bool withHeader, QCha \sa setSource */ -QxtCsvModel::QxtCsvModel(const QString &filename, QObject *parent, bool withHeader, QChar separator, QTextCodec* codec) +QxtCsvModel::QxtCsvModel(const QString &filename, QObject *parent, bool withHeader, QChar separator, VTextCodec* codec) : QAbstractTableModel(parent) { QXT_INIT_PRIVATE(QxtCsvModel) @@ -183,7 +189,7 @@ QVariant QxtCsvModel::headerData(int section, Qt::Orientation orientation, int r Reads in a CSV file from the provided \a file using \a codec. */ -void QxtCsvModel::setSource(const QString &filename, bool withHeader, QChar separator, QTextCodec* codec) +void QxtCsvModel::setSource(const QString &filename, bool withHeader, QChar separator, VTextCodec* codec) { QFile src(filename); setSource(&src, withHeader, separator, codec); @@ -198,7 +204,7 @@ void QxtCsvModel::setSource(const QString &filename, bool withHeader, QChar sepa \sa quoteMode */ -void QxtCsvModel::setSource(QIODevice *file, bool withHeader, QChar separator, QTextCodec* codec) +void QxtCsvModel::setSource(QIODevice *file, bool withHeader, QChar separator, VTextCodec* codec) { QxtCsvModelPrivate* d_ptr = &qxt_d(); bool headerSet = !withHeader; @@ -221,10 +227,10 @@ void QxtCsvModel::setSource(QIODevice *file, bool withHeader, QChar separator, Q QChar ch, buffer(0); bool readCR = false; QTextStream stream(file); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - codec ? stream.setCodec(codec) : stream.setAutoDetectUnicode(true); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + codec ? stream.setEncoding(codec->Encoding()) : stream.setAutoDetectUnicode(true); #else - stream.setAutoDetectUnicode(true); + codec ? stream.setCodec(codec) : stream.setAutoDetectUnicode(true); #endif while (not stream.atEnd()) { @@ -594,7 +600,7 @@ static QString qxt_addCsvQuotes(QxtCsvModel::QuoteMode mode, QString field) Fields in the output file will be separated by \a separator. Set \a withHeader to true to output a row of headers at the top of the file. */ -bool QxtCsvModel::toCSV(QIODevice* dest, QString &error, bool withHeader, QChar separator, QTextCodec* codec) const +bool QxtCsvModel::toCSV(QIODevice* dest, QString &error, bool withHeader, QChar separator, VTextCodec* codec) const { const QxtCsvModelPrivate& d_ptr = qxt_d(); int row, col, rows, cols; @@ -610,12 +616,16 @@ bool QxtCsvModel::toCSV(QIODevice* dest, QString &error, bool withHeader, QChar } } QTextStream stream(dest); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + if (codec) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + stream.setEncoding(codec->Encoding()); +#else stream.setCodec(codec); - } #endif + } + if (withHeader) { data = QString(); @@ -664,7 +674,7 @@ bool QxtCsvModel::toCSV(QIODevice* dest, QString &error, bool withHeader, QChar to output a row of headers at the top of the file. */ bool QxtCsvModel::toCSV(const QString &filename, QString &error, bool withHeader, QChar separator, - QTextCodec* codec) const + VTextCodec* codec) const { QFile dest(filename); return toCSV(&dest, error, withHeader, separator, codec); diff --git a/src/libs/vmisc/qxtcsvmodel.h b/src/libs/vmisc/qxtcsvmodel.h index 73b746721..fad20f1f5 100644 --- a/src/libs/vmisc/qxtcsvmodel.h +++ b/src/libs/vmisc/qxtcsvmodel.h @@ -49,7 +49,10 @@ #include "def.h" class QxtCsvModelPrivate; -class QTextCodec; + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +class VTextCodec; +#endif class QxtCsvModel final : public QAbstractTableModel { @@ -57,9 +60,9 @@ class QxtCsvModel final : public QAbstractTableModel public: explicit QxtCsvModel(QObject *parent = nullptr); explicit QxtCsvModel(QIODevice *file, QObject *parent = nullptr, bool withHeader = false, QChar separator = ',', - QTextCodec *codec = nullptr); + VTextCodec *codec = nullptr); explicit QxtCsvModel(const QString &filename, QObject *parent = nullptr, bool withHeader = false, - QChar separator = ',', QTextCodec *codec = nullptr); + QChar separator = ',', VTextCodec *codec = nullptr); virtual ~QxtCsvModel() = default; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override; @@ -91,14 +94,14 @@ public: bool removeColumn(int col, const QModelIndex& parent = QModelIndex()); virtual bool removeColumns(int col, int count, const QModelIndex& parent = QModelIndex()) override; - void setSource(QIODevice *file, bool withHeader = false, QChar separator = ',', QTextCodec* codec = nullptr); + void setSource(QIODevice *file, bool withHeader = false, QChar separator = ',', VTextCodec* codec = nullptr); void setSource(const QString &filename, bool withHeader = false, QChar separator = ',', - QTextCodec* codec = nullptr); + VTextCodec* codec = nullptr); bool toCSV(QIODevice *file, QString &error, bool withHeader = false, QChar separator = ',', - QTextCodec* codec = nullptr) const; + VTextCodec* codec = nullptr) const; bool toCSV(const QString &filename, QString &error, bool withHeader = false, QChar separator = ',', - QTextCodec* codec = nullptr) const; + VTextCodec* codec = nullptr) const; enum QuoteOption { NoQuotes = 0, SingleQuote = 1, diff --git a/src/libs/vmisc/vabstractapplication.cpp b/src/libs/vmisc/vabstractapplication.cpp index c46ca905d..e60731ce0 100644 --- a/src/libs/vmisc/vabstractapplication.cpp +++ b/src/libs/vmisc/vabstractapplication.cpp @@ -39,7 +39,13 @@ #include #include -#include "../vmisc/compatibility.h" +#include "compatibility.h" + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include +#endif #ifdef Q_OS_UNIX # include @@ -145,6 +151,19 @@ VAbstractApplication::VAbstractApplication(int &argc, char **argv) connect(this, &QApplication::aboutToQuit, this, &VAbstractApplication::AboutToQuit); } +//--------------------------------------------------------------------------------------------------------------------- +VAbstractApplication::~VAbstractApplication() +{ +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QHashIterator i(m_codecs); + while (i.hasNext()) + { + i.next(); + delete i.value(); + } +#endif +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief translationsPath return path to the root directory that contain QM files. @@ -360,6 +379,28 @@ QFileDialog::Options VAbstractApplication::NativeFileDialog(QFileDialog::Options return options; } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +//--------------------------------------------------------------------------------------------------------------------- +VTextCodec *VAbstractApplication::TextCodecCache(QStringConverter::Encoding encoding) const +{ + if (m_codecs.contains(encoding)) + { + return m_codecs.value(encoding); + } + + return nullptr; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractApplication::CacheTextCodec(QStringConverter::Encoding encoding, VTextCodec *codec) +{ + if (not m_codecs.contains(encoding)) + { + m_codecs.insert(encoding, codec); + } +} +#endif + //--------------------------------------------------------------------------------------------------------------------- void VAbstractApplication::CheckSystemLocale() { diff --git a/src/libs/vmisc/vabstractapplication.h b/src/libs/vmisc/vabstractapplication.h index f42a278c8..ef7989d49 100644 --- a/src/libs/vmisc/vabstractapplication.h +++ b/src/libs/vmisc/vabstractapplication.h @@ -49,6 +49,10 @@ class QUndoStack; class VAbstractApplication;// use in define class VCommonSettings; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +class VTextCodec; +#endif + QT_WARNING_PUSH QT_WARNING_DISABLE_GCC("-Wsuggest-final-types") QT_WARNING_DISABLE_GCC("-Wsuggest-final-methods") @@ -58,7 +62,7 @@ class VAbstractApplication : public QApplication Q_OBJECT // NOLINT public: VAbstractApplication(int &argc, char ** argv); - virtual ~VAbstractApplication() =default; + virtual ~VAbstractApplication(); virtual const VTranslateVars *TrVars()=0; @@ -84,6 +88,11 @@ public: QFileDialog::Options NativeFileDialog(QFileDialog::Options options = QFileDialog::Options()) const; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + VTextCodec * TextCodecCache(QStringConverter::Encoding encoding) const; + void CacheTextCodec(QStringConverter::Encoding encoding, VTextCodec *codec); +#endif + #if defined(Q_OS_WIN) static void WinAttachConsole(); #endif @@ -116,6 +125,10 @@ protected slots: private: Q_DISABLE_COPY_MOVE(VAbstractApplication) // NOLINT +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QHash m_codecs{}; +#endif + void ClearTranslation(); }; diff --git a/src/libs/vmisc/vcommonsettings.cpp b/src/libs/vmisc/vcommonsettings.cpp index 0fbf71c1d..2315b647d 100644 --- a/src/libs/vmisc/vcommonsettings.cpp +++ b/src/libs/vmisc/vcommonsettings.cpp @@ -35,13 +35,19 @@ #include #include #include -#include #include #include #include #include +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "vtextcodec.h" +#else +#include +#endif + #include "../vmisc/def.h" +#include "../vmisc/defglobal.h" #include "../vmisc/compatibility.h" #include "../vmisc/literals.h" @@ -1009,7 +1015,7 @@ auto VCommonSettings::GetCSVCodec() const -> int //--------------------------------------------------------------------------------------------------------------------- auto VCommonSettings::GetDefCSVCodec() -> int { - return QTextCodec::codecForLocale()->mibEnum(); + return VTextCodec::codecForLocale()->mibEnum(); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vmisc/vmisc.qbs b/src/libs/vmisc/vmisc.qbs index 867b89e35..9ae86708d 100644 --- a/src/libs/vmisc/vmisc.qbs +++ b/src/libs/vmisc/vmisc.qbs @@ -9,45 +9,53 @@ VLib { } name: "VMiscLib" - files: [ - "def.cpp", - "testpath.cpp", - "vabstractvalapplication.cpp", - "vabstractapplication.cpp", - "projectversion.cpp", - "vcommonsettings.cpp", - "vvalentinasettings.cpp", - "commandoptions.cpp", - "qxtcsvmodel.cpp", - "vtablesearch.cpp", - "literals.cpp", - "vmodifierkey.cpp", - "compatibility.h", - "lambdaconstants.h", - "def.h", - "testpath.h", - "vabstractvalapplication.h", - "vmath.h", - "vabstractapplication.h", - "projectversion.h", - "vcommonsettings.h", - "vvalentinasettings.h", - "debugbreak.h", - "vlockguard.h", - "vsysexits.h", - "commandoptions.h", - "qxtcsvmodel.h", - "vtablesearch.h", - "diagnostic.h", - "customevents.h", - "defglobal.h", - "testvapplication.h", - "literals.h", - "qt_dispatch/qt_dispatch.h", - "vdatastreamenum.h", - "vmodifierkey.h", - "typedef.h", - ] + files: { + var files = [ + "def.cpp", + "testpath.cpp", + "vabstractvalapplication.cpp", + "vabstractapplication.cpp", + "projectversion.cpp", + "vcommonsettings.cpp", + "vvalentinasettings.cpp", + "commandoptions.cpp", + "qxtcsvmodel.cpp", + "vtablesearch.cpp", + "literals.cpp", + "vmodifierkey.cpp", + "compatibility.h", + "lambdaconstants.h", + "def.h", + "testpath.h", + "vabstractvalapplication.h", + "vmath.h", + "vabstractapplication.h", + "projectversion.h", + "vcommonsettings.h", + "vvalentinasettings.h", + "debugbreak.h", + "vlockguard.h", + "vsysexits.h", + "commandoptions.h", + "qxtcsvmodel.h", + "vtablesearch.h", + "diagnostic.h", + "customevents.h", + "defglobal.h", + "testvapplication.h", + "literals.h", + "qt_dispatch/qt_dispatch.h", + "vdatastreamenum.h", + "vmodifierkey.h", + "typedef.h", + ] + + if (Utilities.versionCompare(Qt.core.version, "6") >= 0) { + files.push("vtextcodec.cpp", "vtextcodec.h"); + } + + return files; + } Group { name: "AppImage" diff --git a/src/libs/vmisc/vtextcodec.cpp b/src/libs/vmisc/vtextcodec.cpp new file mode 100644 index 000000000..05278f3d2 --- /dev/null +++ b/src/libs/vmisc/vtextcodec.cpp @@ -0,0 +1,174 @@ +/************************************************************************ + ** + ** @file vtextcodec.cpp + ** @author Roman Telezhynskyi + ** @date 11 2, 2023 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2023 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ +#include "vtextcodec.h" + +#include +#include + +#include "vabstractapplication.h" +#include "compatibility.h" + +namespace +{ +//--------------------------------------------------------------------------------------------------------------------- +auto QtCodecs() -> QMap +{ + static auto codecs = QMap + { + {QStringConverter::Latin1, {"ISO-8859-1", "ANSI_1252", "CP1252", "windows-1252", "LATIN1", "CP819", "CSISO", + "IBM819", "ISO_8859-1", "APPLE ROMAN", "ISO8859-1", "ISO8859-15", "ISO-IR-100", "L1", "IBM 850", + "850"}}, // Western Europe + {QStringConverter::Utf8, {"UTF-8", "UTF8", "UTF8-BIT"}}, + {QStringConverter::Utf16, {"UTF-16", "UTF16", "UTF16-BIT"}}, + {QStringConverter::Utf16BE, {"UTF-16BE"}}, + {QStringConverter::Utf16LE, {"UTF-16LE"}}, + {QStringConverter::Utf32, {"UTF-32"}}, + {QStringConverter::Utf32BE, {"UTF-32BE"}}, + {QStringConverter::Utf32LE, {"UTF-32LE"}}, + {QStringConverter::System, {"System"}}, + }; + + return codecs; +} + +//--------------------------------------------------------------------------------------------------------------------- +auto CodecMibs() -> QMap +{ + static auto mibs = QMap + { + {QStringConverter::Utf8, 106}, + {QStringConverter::Utf16, 1015}, + {QStringConverter::Utf16BE, 1013}, + {QStringConverter::Utf16LE, 1014}, + {QStringConverter::Utf32, 1017}, + {QStringConverter::Utf32BE, 1018}, + {QStringConverter::Utf32LE, 1019}, + {QStringConverter::Latin1, 4}, + {QStringConverter::System, 0}, + }; + + return mibs; +} +} // namespace + +//--------------------------------------------------------------------------------------------------------------------- +VTextCodec::VTextCodec(QStringConverter::Encoding encoding) + : m_encoding(encoding) +{} + +//--------------------------------------------------------------------------------------------------------------------- +auto VTextCodec::codecForName(const QString &name) -> VTextCodec * +{ + QMap codecs = QtCodecs(); + + auto i = codecs.constBegin(); + while (i != codecs.constEnd()) + { + QStringList aliases = i.value(); + if (aliases.contains(name)) + { + return MakeCodec(i.key()); + } + ++i; + } + + return nullptr; +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VTextCodec::codecForLocale() -> VTextCodec * +{ + return MakeCodec(QStringConverter::System); +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VTextCodec::codecForMib(int mib) -> VTextCodec * +{ + QMap mibs = CodecMibs(); + + auto i = mibs.constBegin(); + while (i != mibs.constEnd()) + { + if (mib == i.value()) + { + return MakeCodec(i.key()); + } + ++i; + } + + return nullptr; +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VTextCodec::availableMibs() -> QList +{ + return CodecMibs().values(); +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VTextCodec::availableCodecs() -> QList +{ + QMap codecs = QtCodecs(); + + QList names; + names.reserve(codecs.size()); + + auto i = codecs.constBegin(); + while (i != codecs.constEnd()) + { + names.append(ConstFirst(i.value()).toLatin1()); + } + + return names; +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VTextCodec::name() const -> QString +{ + return ConstFirst(QtCodecs().value(m_encoding)); +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VTextCodec::mibEnum() const -> int +{ + return CodecMibs().value(m_encoding, 0); +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VTextCodec::MakeCodec(QStringConverter::Encoding encoding) -> VTextCodec * +{ + VTextCodec *codec = VAbstractApplication::VApp()->TextCodecCache(encoding); + if (codec != nullptr) + { + return codec; + } + + codec = new VTextCodec(encoding); + VAbstractApplication::VApp()->CacheTextCodec(encoding, codec); + return codec; +} diff --git a/src/libs/vmisc/vtextcodec.h b/src/libs/vmisc/vtextcodec.h new file mode 100644 index 000000000..b2ec897ed --- /dev/null +++ b/src/libs/vmisc/vtextcodec.h @@ -0,0 +1,78 @@ +/************************************************************************ + ** + ** @file vtextcodec.h + ** @author Roman Telezhynskyi + ** @date 11 2, 2023 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2023 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ +#ifndef VTEXTCODEC_H +#define VTEXTCODEC_H + +class VTextCodec +{ +public: + virtual ~VTextCodec() = default; + + static auto codecForName(const QString &name) -> VTextCodec *; + static auto codecForLocale() -> VTextCodec *; + static auto codecForMib(int mib) -> VTextCodec *; + static auto availableMibs() -> QList; + static auto availableCodecs() -> QList; + + auto fromUnicode(const QString &str) const -> QByteArray; + auto toUnicode(const char *chars) const -> QString; + + auto name() const -> QString; + auto mibEnum() const -> int; + + auto Encoding() const -> QStringConverter::Encoding; + +protected: + explicit VTextCodec(QStringConverter::Encoding encoding); +private: + Q_DISABLE_COPY_MOVE(VTextCodec) // NOLINT + + QStringConverter::Encoding m_encoding{QStringConverter::Utf8}; + + static auto MakeCodec(QStringConverter::Encoding encoding) -> VTextCodec *; +}; + +//--------------------------------------------------------------------------------------------------------------------- +inline auto VTextCodec::Encoding() const -> QStringConverter::Encoding +{ + return m_encoding; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline auto VTextCodec::fromUnicode(const QString &str) const -> QByteArray +{ + return QStringEncoder(m_encoding)(str); +} + +//--------------------------------------------------------------------------------------------------------------------- +inline auto VTextCodec::toUnicode(const char *chars) const -> QString +{ + return QStringDecoder(m_encoding)(chars); +} + +#endif // VTEXTCODEC_H diff --git a/src/libs/vpropertyexplorer/vpropertydef.cpp b/src/libs/vpropertyexplorer/vpropertydef.cpp index c69db2ab2..a29c0be6c 100644 --- a/src/libs/vpropertyexplorer/vpropertydef.cpp +++ b/src/libs/vpropertyexplorer/vpropertydef.cpp @@ -69,7 +69,11 @@ QChar VPELocaleSign0(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'0'}; +#else + return QChar('0'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -81,7 +85,11 @@ QChar VPELocaleSign1(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'1'}; +#else + return QChar('1'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -93,7 +101,11 @@ QChar VPELocaleSign2(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'2'}; +#else + return QChar('2'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -105,7 +117,11 @@ QChar VPELocaleSign3(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'3'}; +#else + return QChar('3'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -117,7 +133,11 @@ QChar VPELocaleSign4(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'4'}; +#else + return QChar('4'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -129,7 +149,11 @@ QChar VPELocaleSign5(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'5'}; +#else + return QChar('5'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -141,7 +165,11 @@ QChar VPELocaleSign6(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'6'}; +#else + return QChar('6'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -153,7 +181,11 @@ QChar VPELocaleSign7(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'7'}; +#else + return QChar('7'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -165,7 +197,11 @@ QChar VPELocaleSign8(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'8'}; +#else + return QChar('8'); +#endif } //--------------------------------------------------------------------------------------------------------------------- @@ -177,7 +213,11 @@ QChar VPELocaleSign9(const QLocale &locale) return sign.front(); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return {'9'}; +#else + return QChar('9'); +#endif } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/test/ValentinaTest/tst_dxf.cpp b/src/test/ValentinaTest/tst_dxf.cpp index 743a5ad68..14be7d5cb 100644 --- a/src/test/ValentinaTest/tst_dxf.cpp +++ b/src/test/ValentinaTest/tst_dxf.cpp @@ -27,7 +27,6 @@ *************************************************************************/ #include "tst_dxf.h" -#include #include #include "../vmisc/def.h" @@ -35,6 +34,12 @@ #include "../vdxf/dxfdef.h" #include "../vdxf/libdxfrw/intern/drw_textcodec.h" +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include "../vmisc/vtextcodec.h" +#else +#include +#endif + #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) #include "../vmisc/backport/text.h" #endif @@ -44,7 +49,7 @@ namespace //--------------------------------------------------------------------------------------------------------------------- QStringList AvailableCodecs() { - QList codecs = QTextCodec::availableCodecs(); + QList codecs = VTextCodec::availableCodecs(); QSet uniqueNames; for(auto &codec: codecs) { @@ -53,7 +58,7 @@ QStringList AvailableCodecs() return ConvertToList(uniqueNames); } -} +} // namespace //--------------------------------------------------------------------------------------------------------------------- TST_DXF::TST_DXF(QObject *parent) @@ -113,7 +118,7 @@ void TST_DXF::TestCodecPage() QVERIFY (not dxfCodePage.isEmpty()); - QTextCodec *codec = DRW_TextCodec::CodecForName(dxfCodePage); + VTextCodec *codec = DRW_TextCodec::CodecForName(dxfCodePage); QVERIFY2(codec != nullptr, qUtf8Printable(QStringLiteral("No codec for dxf codepage %1 found.") .arg(dxfCodePage)));