diff options
author | Chris Xiong <chirs241097@gmail.com> | 2023-11-26 21:29:16 -0500 |
---|---|---|
committer | Chris Xiong <chirs241097@gmail.com> | 2023-11-26 21:29:16 -0500 |
commit | f7def0d883fa5cab62ef61b40b9f857245be8e6c (patch) | |
tree | d29322dd4be6e98c0740ae6df4dc70eb41614a76 /qmidiplayer-desktop/qmpmainwindow.cpp | |
parent | 60989e52b3f3bc0a95d3e61bd8e59fa4d9b7ab83 (diff) | |
download | QMidiPlayer-f7def0d883fa5cab62ef61b40b9f857245be8e6c.tar.xz |
Initial Qt 6 port. Many bug fixes.
Diffstat (limited to 'qmidiplayer-desktop/qmpmainwindow.cpp')
-rw-r--r-- | qmidiplayer-desktop/qmpmainwindow.cpp | 65 |
1 files changed, 52 insertions, 13 deletions
diff --git a/qmidiplayer-desktop/qmpmainwindow.cpp b/qmidiplayer-desktop/qmpmainwindow.cpp index c02a206..9b18e0d 100644 --- a/qmidiplayer-desktop/qmpmainwindow.cpp +++ b/qmidiplayer-desktop/qmpmainwindow.cpp @@ -1,11 +1,12 @@ #include <cstdio> #include <cmath> -#include <functional> +#include <unicode/ustring.h> +#include <unicode/unistr.h> +#include <unicode/ucnv.h> #include <QUrl> #include <QFileInfo> #include <QMimeData> #include <QFont> -#include <QTextCodec> #include <QDirIterator> #include <QMessageBox> #include <QCheckBox> @@ -406,19 +407,11 @@ void qmpMainWindow::switchTrack(QString s, bool interrupt) } std::string qmpMainWindow::getTitle() { - if (settings->getOptionEnumIntOptName("Midi/TextEncoding") == "Unicode") - return std::string(player->getTitle()); - return QTextCodec::codecForName( - settings->getOptionEnumIntOptName("Midi/TextEncoding").c_str())-> - toUnicode(player->getTitle()).toStdString(); + return decodeStdString(player->getTitle()); } std::wstring qmpMainWindow::getWTitle() { - if (settings->getOptionEnumIntOptName("Midi/TextEncoding") == "Unicode") - return QString(player->getTitle()).toStdWString(); - return QTextCodec::codecForName( - settings->getOptionEnumIntOptName("Midi/TextEncoding").c_str())-> - toUnicode(player->getTitle()).toStdWString(); + return decodeStdWString(player->getTitle()); } void qmpMainWindow::playerSetup(IFluidSettings *fs) @@ -505,7 +498,16 @@ void qmpMainWindow::registerMidiOptions() settings->registerOptionBool("MIDI", "Send system exclusive messages", "Midi/SendSysEx", true); settings->registerOptionBool("MIDI", "Wait for remaining voice before stopping", "Midi/WaitVoice", true); settings->registerOptionInt("MIDI", "Fluidsynth Device ID", "FluidSynth/DeviceID", 0x00, 0x7E, 0x10); - settings->registerOptionEnumInt("MIDI", "Text encoding", "Midi/TextEncoding", {"Unicode", "Big5", "Big5-HKSCS", "CP949", "EUC-JP", "EUC-KR", "GB18030", "KOI8-R", "KOI8-U", "Macintosh", "Shift-JIS"}, 0); + UErrorCode e = U_ZERO_ERROR; + auto ucnvc = ucnv_countAvailable(); + std::vector<std::string> converters; + for (auto i = 0; i < ucnvc; ++i) + { + auto n = ucnv_getAvailableName(i); + auto sn = ucnv_getStandardName(n, "MIME", &e); + if (sn) converters.push_back(std::string(sn)); + } + settings->registerOptionEnumInt("MIDI", "Text encoding", "Midi/TextEncoding", converters, 0); } void qmpMainWindow::registerBehaviorOptions() @@ -517,6 +519,43 @@ void qmpMainWindow::registerBehaviorOptions() settings->registerOptionEnumInt("Behavior", "Icon Theme", "Behavior/IconTheme", {"Auto", "Dark", "Light"}, 0); } +QString qmpMainWindow::decodeString(const char *str) +{ + std::string enc("utf-8"); + if (ref && ref->settings) + enc = ref->settings->getOptionEnumIntOptName("Midi/TextEncoding"); + icu::UnicodeString us(str, enc.c_str()); + std::string r; + return QString::fromUtf8(us.toUTF8String(r).c_str()); +} + +std::string qmpMainWindow::decodeStdString(const char *str) +{ + std::string enc("utf-8"); + if (ref && ref->settings) + enc = ref->settings->getOptionEnumIntOptName("Midi/TextEncoding"); + icu::UnicodeString us(str, enc.c_str()); + std::string r; + us.toUTF8String(r); + return r; +} + +std::wstring qmpMainWindow::decodeStdWString(const char *str) +{ + std::string enc("utf-8"); + if (ref && ref->settings) + enc = ref->settings->getOptionEnumIntOptName("Midi/TextEncoding"); + icu::UnicodeString us(str, enc.c_str()); + std::wstring r; + int32_t sz = 0; + UErrorCode e = U_ZERO_ERROR; + u_strToWCS(nullptr, 0, &sz, us.getBuffer(), us.length(), &e); + r.resize(sz + 1); + e = U_ZERO_ERROR; + u_strToWCS(r.data(), r.size(), nullptr, us.getBuffer(), us.length(), &e); + return r; +} + void qmpMainWindow::on_pbPlayPause_clicked() { if (stopped) |