diff --git a/.vscode/settings.json b/.vscode/settings.json index a5cfbda..14c0ece 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,7 @@ { "files.autoSave": "onFocusChange", "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.debugConfig": { "console": "integratedTerminal", diff --git a/MainWidget.cpp b/MainWidget.cpp index c1e29e2..9175383 100644 --- a/MainWidget.cpp +++ b/MainWidget.cpp @@ -16,7 +16,7 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget { ui->setupUi(this); - setWindowTitle(u8"OneLevelXmlOpr v1.2.13"); + setWindowTitle(u8"OneLevelXmlOpr v1.2.14"); setWindowIcon(QIcon("://resource/xml.ico")); QScreen* primaryScreen = QGuiApplication::primaryScreen(); @@ -185,7 +185,7 @@ void MainWidget::generate_table_widget() [&](QTableWidgetItem* item) { item_changed_handle(item); }); connect(tab_widget_, &QTableWidget::customContextMenuRequested, this, &MainWidget::show_custom_menu); auto config = ini_.get_config(); - auto keys = splitString(config.purpose, ","); + auto keys = CUtil::splitString(config.purpose, ","); keys_.clear(); QStringList list; for (const auto& item : keys) { @@ -240,6 +240,7 @@ void MainWidget::push_content(const std::vector& eles, st tab_widget_->clearContents(); tab_widget_->setRowCount(0); + auto_add_ = true; for (auto p = (page - 1) * g_OnePage; p < all_size && p < max_show; ++p) { int row_cnt = tab_widget_->rowCount(); tab_widget_->insertRow(row_cnt); @@ -249,6 +250,7 @@ void MainWidget::push_content(const std::vector& eles, st cur_page_ = page; ui->edAllPage->setText(QString::number(all_page_)); judge_btn_page(); + auto_add_ = false; } void MainWidget::judge_btn_page() @@ -285,11 +287,9 @@ void MainWidget::read(const QString& file_path) return; } - auto_add_ = true; generate_table_widget(); push_content(vec_); current_ = vec_; - auto_add_ = false; ui->edStatus->setText(file_path); ui->btnRead->setEnabled(false); @@ -350,7 +350,7 @@ void MainWidget::item_changed_handle(QTableWidgetItem* item) if (result == nullptr) { 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() @@ -495,8 +495,8 @@ void MainWidget::insert_one_line(Element_t* ele, int row) // wgItem->setFlags(wgItem->flags() | Qt::ItemIsUserCheckable); // wgItem->setCheckState(Qt::Checked); } - - wgItem->setText(QString(data)); + QString sda(data); + wgItem->setText(sda); tab_widget_->setItem(row, i, wgItem); } } diff --git a/public_def.cpp b/public_def.cpp index 3a5ec0a..d84d70d 100644 --- a/public_def.cpp +++ b/public_def.cpp @@ -2,23 +2,9 @@ #include #include -std::vector splitString(const std::string& input, const std::string& delimiter) -{ - std::vector 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; -} +#ifdef _WIN32 +#include +#endif 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 filePath = - QFileDialog::getOpenFileName(parent, info, QDir::homePath(), filter); + QString filePath = QFileDialog::getOpenFileName(parent, info, QDir::homePath(), filter); return filePath; } void CUtil::sort_by_repeat(std::vector& vec) { - std::size_t len1 = 0; - std::size_t len2 = 0; - std::size_t cur = 0; - auto compare = [&](const std::string& str1, const std::string& str2) { - len1 = str1.size(); - len2 = str2.size(); - if (cur >= len1 || cur >= len2) { - return len1 > len2; - } - return str1[cur] > str2[cur]; - }; - std::size_t max_len = 0; - for (const auto& data : vec) { - max_len = data.size() > max_len ? data.size() : max_len; - } - for (cur = 0; cur < max_len; ++cur) { - std::sort(vec.begin(), vec.end(), compare); - } -} \ No newline at end of file + std::size_t len1 = 0; + std::size_t len2 = 0; + std::size_t cur = 0; + auto compare = [&](const std::string& str1, const std::string& str2) { + len1 = str1.size(); + len2 = str2.size(); + if (cur >= len1 || cur >= len2) { + return len1 > len2; + } + return str1[cur] > str2[cur]; + }; + std::size_t max_len = 0; + for (const auto& data : vec) { + max_len = data.size() > max_len ? data.size() : max_len; + } + for (cur = 0; cur < max_len; ++cur) { + 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 CUtil::splitString(const std::string& input, const std::string& delimiter) +{ + std::vector 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 diff --git a/public_def.h b/public_def.h index 8d947fb..eeb7a78 100644 --- a/public_def.h +++ b/public_def.h @@ -12,18 +12,21 @@ struct OprBase { std::string xml_path{}; }; -std::vector splitString(const std::string& input, const std::string& delimiter); - class CUtil { public: CUtil() = default; ~CUtil() = default; + public: - static void msg(QWidget* parent, const QString& content); - static bool affirm(QWidget* parent, const QString& titile, const QString& content); + static void msg(QWidget* parent, 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 void sort_by_repeat(std::vector& vec); + static void sort_by_repeat(std::vector& vec); + +public: + static std::string utf8_to_gbk(const std::string& utf8_str); + static std::vector splitString(const std::string& input, const std::string& delimiter); }; #endif diff --git a/src/config.cpp b/src/config.cpp index 0fe715d..727d7b9 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -24,6 +24,7 @@ bool ConfigIni::set_xml_path(const std::string& path) if (ini_.IsEmpty()) { return false; } + ini_.SetValue("Basic", "xml_path", path.c_str()); if (ini_.SaveFile(ini_path_.c_str()) != SI_OK) { return false; diff --git a/src/xml_opr.cpp b/src/xml_opr.cpp index 13c2220..68e1b6d 100644 --- a/src/xml_opr.cpp +++ b/src/xml_opr.cpp @@ -1,32 +1,6 @@ #include "xml_opr.h" #include -#ifdef _WIN32 -#include -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; CXmlOpr::CXmlOpr() = default; @@ -35,7 +9,7 @@ CXmlOpr::~CXmlOpr() = default; bool CXmlOpr::open(const std::string& xml_path) { #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; } #else @@ -81,7 +55,7 @@ bool CXmlOpr::parse_xml(std::vector& vec) std::string next_node{}; std::string node_path = opr_base_.node_path; keys_.clear(); - auto keys = splitString(opr_base_.purpose, ","); + auto keys = CUtil::splitString(opr_base_.purpose, ","); for (const auto& item : keys) { if (item.empty()) { continue; @@ -89,7 +63,7 @@ bool CXmlOpr::parse_xml(std::vector& vec) keys_.push_back(item); } - auto nodes = splitString(opr_base_.node_path, "/"); + auto nodes = CUtil::splitString(opr_base_.node_path, "/"); for (const auto& item : nodes) { if (item.empty()) {