change:格式化xml单元设定一个窗口,可以选择性格式化哪些具体内容。

This commit is contained in:
taynpg 2024-09-15 15:08:32 +08:00
parent 0f65ef220a
commit 693a371e0d
8 changed files with 376 additions and 17 deletions

View File

@ -31,6 +31,7 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
include_directories(3rd)
include_directories(src)
include_directories(form)
set(PROJECT_SOURCES
main.cpp
MainWidget.cpp
@ -42,6 +43,7 @@ set(PROJECT_SOURCES
src/attribute_edit.cpp flatgray.qrc
src/data_edit.h src/data_edit.cpp src/data_edit.ui
src/QCustomQLineEdit.h src/QCustomQLineEdit.cpp
form/qformatset.h form/qformatset.cpp form/qformatset.ui
)
if (MSVC)

View File

@ -11,6 +11,7 @@
#include "./ui_MainWidget.h"
#include "public_def.h"
#include "qformatset.h"
#include "src/data_edit.h"
constexpr std::size_t g_OnePage = 100;
@ -20,7 +21,7 @@ MainWidget::MainWidget(QWidget* parent)
{
ui->setupUi(this);
setWindowTitle(u8"OneLevelXmlOpr v1.3.6");
setWindowTitle(u8"OneLevelXmlOpr v1.3.7");
setWindowIcon(QIcon("://resource/xml.ico"));
QScreen* primaryScreen = QGuiApplication::primaryScreen();
@ -966,16 +967,25 @@ void MainWidget::unit_change()
bool MainWidget::format_xml()
{
std::string xml_path = ui->edStatus->text().toStdString();
QFormatSet set;
set.exec();
if (!set.isok_) {
return false;
}
std::string xml_path = set.xml_path_;
if (xml_path.empty()) {
return false;
}
if (!CUtil::affirm(this, u8"确认", u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) {
if (!CUtil::affirm(
this, u8"确认",
u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) {
return false;
}
if (!xml_.handle_save(xml_path)) {
if (!xml_.handle_save(xml_path, set.values_)) {
CUtil::msg(this, u8"重排版内容失败");
return false;
}

87
form/qformatset.cpp Normal file
View File

@ -0,0 +1,87 @@
#include "qformatset.h"
#include "../public_def.h"
#include "ui_qformatset.h"
QFormatSet::QFormatSet(QWidget* parent)
: QDialog(parent), ui(new Ui::QFormatSet)
{
ui->setupUi(this);
connect(ui->btnOk, &QPushButton::clicked, this, [&]() { handle(); });
connect(ui->btnCancel, &QPushButton::clicked, this, [&]() {
isok_ = false;
close();
});
connect(ui->cbAll, &QCheckBox::toggled, this, [&]() { check_select(); });
connect(ui->btnSelect, &QPushButton::clicked, this, [&]() {
QString file = CUtil::select_file(this, u8"请选择xml文件",
u8"XML(*.xml);;所有文件 (*)");
if (file.isEmpty()) {
return;
}
ui->edXmlPath->setText(file);
});
}
QFormatSet::~QFormatSet()
{
delete ui;
}
void QFormatSet::handle()
{
values_.clear();
if (ui->cbAdd->isChecked()) {
values_.push_back("+");
}
if (ui->cbAnd->isChecked()) {
values_.push_back("&&");
}
if (ui->cbOr->isChecked()) {
values_.push_back("||");
}
if (ui->cbMul->isChecked()) {
values_.push_back("*");
}
if (ui->cbSub->isChecked()) {
values_.push_back("-");
}
if (ui->cbDiv->isChecked()) {
values_.push_back("/");
}
if (ui->cbLess->isChecked()) {
values_.push_back("<");
}
if (ui->cbMore->isChecked()) {
values_.push_back(">");
}
if (ui->cbEq->isChecked()) {
values_.push_back("==");
}
if (ui->cbNotEq->isChecked()) {
values_.push_back("!=");
}
xml_path_ = ui->edXmlPath->text().toStdString();
isok_ = true;
close();
}
void QFormatSet::check_select()
{
auto set_check = [&](QCheckBox* ed, bool check) {
if (ed) {
ed->setChecked(check);
}
};
bool check = ui->cbAll->isChecked();
set_check(ui->cbAdd, check);
set_check(ui->cbSub, check);
set_check(ui->cbMul, check);
set_check(ui->cbDiv, check);
set_check(ui->cbLess, check);
set_check(ui->cbMore, check);
set_check(ui->cbOr, check);
set_check(ui->cbAnd, check);
set_check(ui->cbEq, check);
set_check(ui->cbNotEq, check);
}

32
form/qformatset.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef QFORMATSET_H
#define QFORMATSET_H
#include <QDialog>
#include <string>
namespace Ui {
class QFormatSet;
}
class QFormatSet : public QDialog
{
Q_OBJECT
public:
explicit QFormatSet(QWidget *parent = nullptr);
~QFormatSet();
private:
void handle();
void check_select();
private:
Ui::QFormatSet *ui;
public:
std::vector<std::string> values_{};
std::string xml_path_{};
bool isok_{false};
};
#endif // QFORMATSET_H

202
form/qformatset.ui Normal file
View File

@ -0,0 +1,202 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QFormatSet</class>
<widget class="QDialog" name="QFormatSet">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>529</width>
<height>200</height>
</rect>
</property>
<property name="windowTitle">
<string>重排版</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>操作</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCustomQLineEdit" name="edXmlPath"/>
</item>
<item>
<widget class="QPushButton" name="btnSelect">
<property name="text">
<string>选择</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>设置值内容指定字符前后空格</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QCheckBox" name="cbAnd">
<property name="text">
<string>与(双&amp;&amp;)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbOr">
<property name="text">
<string>或(||)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbLess">
<property name="text">
<string>小于(&lt;)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbMore">
<property name="text">
<string>大于(&gt;)</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QCheckBox" name="cbAdd">
<property name="text">
<string>加(+)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbSub">
<property name="text">
<string>减(-)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbMul">
<property name="text">
<string>乘(*)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbDiv">
<property name="text">
<string>除(/)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbEq">
<property name="text">
<string>等(==)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbNotEq">
<property name="text">
<string>不等(!=)</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QCheckBox" name="cbAll">
<property name="text">
<string>全选</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnOk">
<property name="text">
<string>确认</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnCancel">
<property name="text">
<string>取消</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QCustomQLineEdit</class>
<extends>QLineEdit</extends>
<header>QCustomQLineEdit.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -10,7 +10,7 @@ void QCustomQLineEdit::dragEnterEvent(QDragEnterEvent* event)
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
QLineEdit::dragEnterEvent(event);
//QLineEdit::dragEnterEvent(event);
}
void QCustomQLineEdit::dragMoveEvent(QDragMoveEvent* event)
@ -28,7 +28,7 @@ void QCustomQLineEdit::dropEvent(QDropEvent* event)
event->accept();
}
}
QLineEdit::dropEvent(event);
//QLineEdit::dropEvent(event);
}
QCustomQLineEdit::~QCustomQLineEdit()

View File

@ -262,14 +262,33 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec,
}
return true;
}
std::string CXmlOpr::handle_space(const std::string& content)
std::string CXmlOpr::handle_space(const std::string& content,
const std::vector<std::string>& keychars)
{
std::string result;
size_t pos = 0;
size_t len = content.length();
std::ostringstream oss;
for (auto i = size_t(0); i < keychars.size(); ++i) {
if (i > 0) {
oss << "|";
}
std::string op(keychars[i]);
for (const char& c : op) {
if (c == '|' || c == '\\' || c == '(' || c == ')' || c == '[' ||
c == ']' || c == '{' || c == '}' || c == '^' || c == '$' ||
c == '.' || c == '*' || c == '+' || c == '?' || c == '|' ||
c == '\\') {
oss << '\\'; // 添加转义字符
}
oss << c;
}
}
// Define a regular expression for the operators
std::regex operators_regex(R"(&&|!=|==|<|>|\|\||\+|-|\*|/)");
// std::regex operators_regex(R"(&&|!=|==|<|>|\|\||\+|-|\*|/)");
std::regex operators_regex(oss.str());
while (pos < len) {
size_t start_quote = content.find('"', pos);
@ -316,7 +335,8 @@ std::string CXmlOpr::handle_space(const std::string& content)
return result;
}
bool CXmlOpr::handle_transfer(const std::string& path)
bool CXmlOpr::handle_transfer(const std::string& path,
const std::vector<std::string>& keychars)
{
std::ifstream file(path);
if (!file.is_open()) {
@ -361,13 +381,18 @@ bool CXmlOpr::handle_transfer(const std::string& path)
pos += 1; // Move past the replaced character
}
// 处理空格格式化
std::string sec_handle = handle_space(result);
std::ofstream ofile(path);
if (!ofile.is_open()) {
return false;
}
if (keychars.size() < 1) {
ofile << result;
} else {
// 处理空格格式化
std::string sec_handle = handle_space(result, keychars);
ofile << sec_handle;
}
return true;
}
@ -410,7 +435,8 @@ bool CXmlOpr::save()
return true;
}
bool CXmlOpr::handle_save(const std::string& path)
bool CXmlOpr::handle_save(const std::string& path,
const std::vector<std::string>& keychars)
{
if (!open(path)) {
return false;
@ -419,7 +445,7 @@ bool CXmlOpr::handle_save(const std::string& path)
if (ret != tinyxml2::XML_SUCCESS) {
return false;
}
if (!handle_transfer(xml_path_)) {
if (!handle_transfer(xml_path_, keychars)) {
return false;
}
return true;

View File

@ -37,8 +37,8 @@ public:
bool check_key_exists(const Property_t& property);
bool check_key_exists(const std::string& key);
bool save();
bool handle_save(const std::string& path);
std::string handle_space(const std::string& content);
bool handle_save(const std::string& path, const std::vector<std::string>& keychars);
std::string handle_space(const std::string& content, const std::vector<std::string>& keychars);
public:
Element_t* copy_element(Element_t* ele);
@ -50,7 +50,7 @@ public:
bool import_newer_data(const std::vector<std::string>& vec,
std::size_t& success_count);
// 处理转义
bool handle_transfer(const std::string& path);
bool handle_transfer(const std::string& path, const std::vector<std::string>& keychars);
public:
void get_attributes(Element_t* ele, Property_t& vec);