Tools: Add UTF-8, GB18030 auto convert.
This commit is contained in:
parent
4992e61376
commit
b16e902dbe
|
@ -27,7 +27,7 @@ set(PROJECT_BUNDLE_NAME ${PROJECT_NAME})
|
|||
set(version_str "${CMAKE_PROJECT_VERSION_MAJOR},${CMAKE_PROJECT_VERSION_MINOR},${CMAKE_PROJECT_VERSION_PATCH}")
|
||||
add_definitions(-DPROTOCOLPARSER_VERSION=${version_str})
|
||||
|
||||
find_package(Qt6 COMPONENTS Quick SerialPort LinguistTools WebView REQUIRED)
|
||||
find_package(Qt6 COMPONENTS Quick SerialPort LinguistTools WebView Core5Compat REQUIRED)
|
||||
|
||||
set(
|
||||
sources_files source/main.cpp include/serialportmanager.h source/serialportmanager.cpp include/eventsbus.h source/eventsbus.cpp
|
||||
|
@ -152,6 +152,7 @@ if(RIBBONUI_BUILD_STATIC_LIB)
|
|||
Qt::Quick
|
||||
Qt::SerialPort
|
||||
Qt::WebView
|
||||
Qt::Core5Compat
|
||||
RibbonUIplugin
|
||||
sm_crypto
|
||||
FramelessHelper::Core
|
||||
|
@ -163,6 +164,7 @@ else()
|
|||
Qt::Quick
|
||||
Qt::SerialPort
|
||||
Qt::WebView
|
||||
Qt::Core5Compat
|
||||
RibbonUI
|
||||
sm_crypto
|
||||
FramelessHelper::Core
|
||||
|
|
|
@ -22,7 +22,9 @@ public:
|
|||
Q_INVOKABLE QString fileSuffix(const QString &filePath);
|
||||
Q_INVOKABLE QString fileDir(const QString &filePath);
|
||||
Q_INVOKABLE void writeDirtoTempDir(QString path);
|
||||
Q_INVOKABLE void writeFiletoDir(QString content, QString path, QString name);
|
||||
Q_INVOKABLE void writeFiletoDir(QString content, QString path, QString name, QByteArray tcodec = "UTF-8");
|
||||
Q_INVOKABLE QString GetCorrectUnicode(const QByteArray &ba);
|
||||
Q_INVOKABLE QString GetCorrectUnicode(const QString &ba);
|
||||
|
||||
private:
|
||||
explicit Tools(QObject *parent = nullptr);
|
||||
|
|
|
@ -111,7 +111,7 @@ Item {
|
|||
let base_url = '${base_url}';
|
||||
var defaults = {
|
||||
html: true,
|
||||
xhtmlOut: false,
|
||||
xhtmlOut: true,
|
||||
breaks: false,
|
||||
linkify: true,
|
||||
typographer: true,
|
||||
|
@ -185,8 +185,8 @@ Item {
|
|||
let file_name = full_file_name.split('.')
|
||||
let html_name = `${file_name[0]}.html`
|
||||
let prefix = `file:${Qt.platform.os === 'windows' ? '///' : '//'}` + Tools.baseDir
|
||||
let html = `<!DOCTYPE html><html data-theme=${RibbonTheme.dark_mode ? 'dark' : 'light'}>` +
|
||||
'<head><meta charset="UTF-8", name="viewport" content="width=device-width, initial-scale=1">' +
|
||||
let html = `<!DOCTYPE html><html data-theme=${RibbonTheme.dark_mode ? 'dark' : 'light'} xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">` +
|
||||
`<head><meta charset="UTF-8", name="viewport" content="width=device-width, initial-scale=1">` +
|
||||
`<title>${full_file_name}</title>` +
|
||||
`<link rel="stylesheet" href=${prefix}resources/theme.css></style>` +
|
||||
`<link rel="stylesheet" href=${prefix}resources/github-${RibbonTheme.dark_mode ? 'dark' : 'light'}.css></style>` +
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <QFileInfo>
|
||||
#include <QStandardPaths>
|
||||
#include <QDirIterator>
|
||||
#include <QTextCodec>
|
||||
|
||||
Tools::Tools(QObject *parent)
|
||||
: QObject{parent}
|
||||
|
@ -93,13 +94,36 @@ void Tools::writeDirtoTempDir(QString path)
|
|||
}
|
||||
}
|
||||
|
||||
void Tools::writeFiletoDir(QString content, QString path, QString name)
|
||||
void Tools::writeFiletoDir(QString content, QString path, QString name, QByteArray tcodec)
|
||||
{
|
||||
QFile file(path+'/'+name);
|
||||
if (!file.open(QIODevice::WriteOnly)) {
|
||||
qDebug() << "File open fail: " + file.fileName();
|
||||
return;
|
||||
}
|
||||
file.write(content.toLocal8Bit());
|
||||
QTextCodec *codec = QTextCodec::codecForName(tcodec);
|
||||
file.write(codec->fromUnicode(GetCorrectUnicode(content)));
|
||||
file.close();
|
||||
}
|
||||
|
||||
QString Tools::GetCorrectUnicode(const QByteArray &ba)
|
||||
{
|
||||
QTextCodec::ConverterState state;
|
||||
QTextCodec *codec = QTextCodec::codecForName("GB18030");
|
||||
QString text = codec->toUnicode( ba.constData(), ba.size(), &state);
|
||||
if (state.invalidChars > 0)
|
||||
{
|
||||
text = QTextCodec::codecForName( "UTF-8" )->toUnicode(ba);
|
||||
return text;
|
||||
}
|
||||
else
|
||||
{
|
||||
text = ba;
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
QString Tools::GetCorrectUnicode(const QString &ba)
|
||||
{
|
||||
return GetCorrectUnicode(QByteArray(ba.toStdString().data()));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue