fix:解决中文乱码BUG。

This commit is contained in:
taynpg 2024-05-24 09:18:06 +08:00
parent 019740f550
commit 3d1fcc5dc8
6 changed files with 90 additions and 80 deletions

View File

@ -1,7 +1,7 @@
{ {
"files.autoSave": "onFocusChange", "files.autoSave": "onFocusChange",
"editor.fontSize": 16, "editor.fontSize": 16,
"editor.fontFamily": "'Mononoki Nerd Font Mono', 'Mononoki Nerd Font Mono', 'Mononoki Nerd Font Mono'", "editor.fontFamily": "'IBM Plex Mono', 'IBM Plex Mono', 'MIBM Plex Mono'",
"cmake.configureOnOpen": true, "cmake.configureOnOpen": true,
"cmake.debugConfig": { "cmake.debugConfig": {
"console": "integratedTerminal", "console": "integratedTerminal",

View File

@ -16,7 +16,7 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
{ {
ui->setupUi(this); ui->setupUi(this);
setWindowTitle(u8"OneLevelXmlOpr v1.2.13"); setWindowTitle(u8"OneLevelXmlOpr v1.2.14");
setWindowIcon(QIcon("://resource/xml.ico")); setWindowIcon(QIcon("://resource/xml.ico"));
QScreen* primaryScreen = QGuiApplication::primaryScreen(); QScreen* primaryScreen = QGuiApplication::primaryScreen();
@ -185,7 +185,7 @@ void MainWidget::generate_table_widget()
[&](QTableWidgetItem* item) { item_changed_handle(item); }); [&](QTableWidgetItem* item) { item_changed_handle(item); });
connect(tab_widget_, &QTableWidget::customContextMenuRequested, this, &MainWidget::show_custom_menu); connect(tab_widget_, &QTableWidget::customContextMenuRequested, this, &MainWidget::show_custom_menu);
auto config = ini_.get_config(); auto config = ini_.get_config();
auto keys = splitString(config.purpose, ","); auto keys = CUtil::splitString(config.purpose, ",");
keys_.clear(); keys_.clear();
QStringList list; QStringList list;
for (const auto& item : keys) { for (const auto& item : keys) {
@ -240,6 +240,7 @@ void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles, st
tab_widget_->clearContents(); tab_widget_->clearContents();
tab_widget_->setRowCount(0); tab_widget_->setRowCount(0);
auto_add_ = true;
for (auto p = (page - 1) * g_OnePage; p < all_size && p < max_show; ++p) { for (auto p = (page - 1) * g_OnePage; p < all_size && p < max_show; ++p) {
int row_cnt = tab_widget_->rowCount(); int row_cnt = tab_widget_->rowCount();
tab_widget_->insertRow(row_cnt); tab_widget_->insertRow(row_cnt);
@ -249,6 +250,7 @@ void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles, st
cur_page_ = page; cur_page_ = page;
ui->edAllPage->setText(QString::number(all_page_)); ui->edAllPage->setText(QString::number(all_page_));
judge_btn_page(); judge_btn_page();
auto_add_ = false;
} }
void MainWidget::judge_btn_page() void MainWidget::judge_btn_page()
@ -285,11 +287,9 @@ void MainWidget::read(const QString& file_path)
return; return;
} }
auto_add_ = true;
generate_table_widget(); generate_table_widget();
push_content(vec_); push_content(vec_);
current_ = vec_; current_ = vec_;
auto_add_ = false;
ui->edStatus->setText(file_path); ui->edStatus->setText(file_path);
ui->btnRead->setEnabled(false); ui->btnRead->setEnabled(false);
@ -350,7 +350,7 @@ void MainWidget::item_changed_handle(QTableWidgetItem* item)
if (result == nullptr) { if (result == nullptr) {
return; return;
} }
result->SetAttribute(keys_[col].c_str(), item->text().toLocal8Bit().constData()); result->SetAttribute(keys_[col].c_str(), item->text().toStdString().c_str());
} }
void MainWidget::save() void MainWidget::save()
@ -495,8 +495,8 @@ void MainWidget::insert_one_line(Element_t* ele, int row)
// wgItem->setFlags(wgItem->flags() | Qt::ItemIsUserCheckable); // wgItem->setFlags(wgItem->flags() | Qt::ItemIsUserCheckable);
// wgItem->setCheckState(Qt::Checked); // wgItem->setCheckState(Qt::Checked);
} }
QString sda(data);
wgItem->setText(QString(data)); wgItem->setText(sda);
tab_widget_->setItem(row, i, wgItem); tab_widget_->setItem(row, i, wgItem);
} }
} }

View File

@ -2,23 +2,9 @@
#include <QMessageBox> #include <QMessageBox>
#include <QFileDialog> #include <QFileDialog>
std::vector<std::string> splitString(const std::string& input, const std::string& delimiter) #ifdef _WIN32
{ #include <windows.h>
std::vector<std::string> tokens; #endif
size_t pos = 0;
std::string backup = input;
std::string token;
while ((pos = backup.find(delimiter)) != std::string::npos) {
token = backup.substr(0, pos);
tokens.push_back(token);
backup.erase(0, pos + delimiter.length());
}
// Push the remaining part after the last delimiter
tokens.push_back(backup);
return tokens;
}
void CUtil::msg(QWidget* parent, const QString& content) void CUtil::msg(QWidget* parent, const QString& content)
{ {
@ -42,29 +28,75 @@ bool CUtil::affirm(QWidget* parent, const QString& titile, const QString& conten
QString CUtil::select_file(QWidget* parent, const QString& info, const QString& filter) QString CUtil::select_file(QWidget* parent, const QString& info, const QString& filter)
{ {
QString filePath = QString filePath = QFileDialog::getOpenFileName(parent, info, QDir::homePath(), filter);
QFileDialog::getOpenFileName(parent, info, QDir::homePath(), filter);
return filePath; return filePath;
} }
void CUtil::sort_by_repeat(std::vector<std::string>& vec) void CUtil::sort_by_repeat(std::vector<std::string>& vec)
{ {
std::size_t len1 = 0; std::size_t len1 = 0;
std::size_t len2 = 0; std::size_t len2 = 0;
std::size_t cur = 0; std::size_t cur = 0;
auto compare = [&](const std::string& str1, const std::string& str2) { auto compare = [&](const std::string& str1, const std::string& str2) {
len1 = str1.size(); len1 = str1.size();
len2 = str2.size(); len2 = str2.size();
if (cur >= len1 || cur >= len2) { if (cur >= len1 || cur >= len2) {
return len1 > len2; return len1 > len2;
} }
return str1[cur] > str2[cur]; return str1[cur] > str2[cur];
}; };
std::size_t max_len = 0; std::size_t max_len = 0;
for (const auto& data : vec) { for (const auto& data : vec) {
max_len = data.size() > max_len ? data.size() : max_len; max_len = data.size() > max_len ? data.size() : max_len;
} }
for (cur = 0; cur < max_len; ++cur) { for (cur = 0; cur < max_len; ++cur) {
std::sort(vec.begin(), vec.end(), compare); std::sort(vec.begin(), vec.end(), compare);
} }
} }
#ifdef _WIN32
std::string CUtil::utf8_to_gbk(const std::string& utf8_str)
{
// UTF-8 to Wide Char (UTF-16)
int wide_char_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
if (wide_char_len == 0) {
return "";
}
std::wstring wide_str(wide_char_len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, &wide_str[0], wide_char_len);
// Wide Char (UTF-16) to GBK
int gbk_len = WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (gbk_len == 0) {
return "";
}
std::string gbk_str(gbk_len, 0);
WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, &gbk_str[0], gbk_len, nullptr, nullptr);
return gbk_str;
}
std::vector<std::string> CUtil::splitString(const std::string& input, const std::string& delimiter)
{
std::vector<std::string> tokens;
size_t pos = 0;
std::string backup = input;
std::string token;
while ((pos = backup.find(delimiter)) != std::string::npos) {
token = backup.substr(0, pos);
tokens.push_back(token);
backup.erase(0, pos + delimiter.length());
}
// Push the remaining part after the last delimiter
tokens.push_back(backup);
return tokens;
}
#else
std::string CUtil::utf8_to_gbk(const std::string& utf8_str)
{
return utf8_str;
}
#endif

View File

@ -12,18 +12,21 @@ struct OprBase {
std::string xml_path{}; std::string xml_path{};
}; };
std::vector<std::string> splitString(const std::string& input, const std::string& delimiter);
class CUtil class CUtil
{ {
public: public:
CUtil() = default; CUtil() = default;
~CUtil() = default; ~CUtil() = default;
public: public:
static void msg(QWidget* parent, const QString& content); static void msg(QWidget* parent, const QString& content);
static bool affirm(QWidget* parent, const QString& titile, const QString& content); static bool affirm(QWidget* parent, const QString& titile, const QString& content);
static QString select_file(QWidget* parent, const QString& info, const QString& filter); static QString select_file(QWidget* parent, const QString& info, const QString& filter);
static void sort_by_repeat(std::vector<std::string>& vec); static void sort_by_repeat(std::vector<std::string>& vec);
public:
static std::string utf8_to_gbk(const std::string& utf8_str);
static std::vector<std::string> splitString(const std::string& input, const std::string& delimiter);
}; };
#endif #endif

View File

@ -24,6 +24,7 @@ bool ConfigIni::set_xml_path(const std::string& path)
if (ini_.IsEmpty()) { if (ini_.IsEmpty()) {
return false; return false;
} }
ini_.SetValue("Basic", "xml_path", path.c_str()); ini_.SetValue("Basic", "xml_path", path.c_str());
if (ini_.SaveFile(ini_path_.c_str()) != SI_OK) { if (ini_.SaveFile(ini_path_.c_str()) != SI_OK) {
return false; return false;

View File

@ -1,32 +1,6 @@
#include "xml_opr.h" #include "xml_opr.h"
#include <filesystem> #include <filesystem>
#ifdef _WIN32
#include <windows.h>
std::string utf8_to_gbk(const std::string& utf8_str)
{
// UTF-8 to Wide Char (UTF-16)
int wide_char_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
if (wide_char_len == 0) {
throw std::runtime_error("Failed to convert UTF-8 to wide char");
}
std::wstring wide_str(wide_char_len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, &wide_str[0], wide_char_len);
// Wide Char (UTF-16) to GBK
int gbk_len = WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (gbk_len == 0) {
throw std::runtime_error("Failed to convert wide char to GBK");
}
std::string gbk_str(gbk_len, 0);
WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, &gbk_str[0], gbk_len, nullptr, nullptr);
return gbk_str;
}
#endif
namespace fs = std::filesystem; namespace fs = std::filesystem;
CXmlOpr::CXmlOpr() = default; CXmlOpr::CXmlOpr() = default;
@ -35,7 +9,7 @@ CXmlOpr::~CXmlOpr() = default;
bool CXmlOpr::open(const std::string& xml_path) bool CXmlOpr::open(const std::string& xml_path)
{ {
#ifdef _WIN32 #ifdef _WIN32
if (doc_.LoadFile(utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) { if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
return false; return false;
} }
#else #else
@ -81,7 +55,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
std::string next_node{}; std::string next_node{};
std::string node_path = opr_base_.node_path; std::string node_path = opr_base_.node_path;
keys_.clear(); keys_.clear();
auto keys = splitString(opr_base_.purpose, ","); auto keys = CUtil::splitString(opr_base_.purpose, ",");
for (const auto& item : keys) { for (const auto& item : keys) {
if (item.empty()) { if (item.empty()) {
continue; continue;
@ -89,7 +63,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
keys_.push_back(item); keys_.push_back(item);
} }
auto nodes = splitString(opr_base_.node_path, "/"); auto nodes = CUtil::splitString(opr_base_.node_path, "/");
for (const auto& item : nodes) { for (const auto& item : nodes) {
if (item.empty()) { if (item.empty()) {