This commit is contained in:
taynpg 2025-03-03 22:56:33 +08:00
parent 7a523d9137
commit 9209337d1b
12 changed files with 385 additions and 70 deletions

View File

@ -163,7 +163,8 @@
"qfiledialog": "cpp",
"qvboxlayout": "cpp",
"qapplication": "cpp",
"qmessagebox": "cpp"
"qmessagebox": "cpp",
"qmainwindow": "cpp"
},
"makefile.configureOnOpen": false,
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",

View File

@ -1,8 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(std_reg LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
project(std_reg VERSION 1.0 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
@ -18,11 +16,59 @@ endif()
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
add_executable(std_reg
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
set(PROJECT_SOURCES
main.cpp
mainwidget.cpp
mainwidget.h resource/ico.rc
mainwidget.ui std_reg.qrc
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(std_reg
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET std_reg APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(std_reg SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(std_reg
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(std_reg PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.std_reg)
endif()
set_target_properties(std_reg PROPERTIES
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
include(GNUInstallDirs)
install(TARGETS std_reg
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(std_reg)
endif()

View File

@ -1,11 +1,18 @@
#include "mainwindow.h"
#include "mainwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QFont font = a.font();
font.setHintingPreference(QFont::PreferNoHinting);
font.setPointSize(13);
a.setFont(font);
CTryReg w;
a.setStyle("windows");
w.show();
return a.exec();
}

162
mainwidget.cpp Normal file
View File

@ -0,0 +1,162 @@
#include "mainwidget.h"
#include "./ui_mainwidget.h"
#include <QDir>
#include <QMessageBox>
#include <regex>
CTryReg::CTryReg(QWidget* parent) : QWidget(parent), ui(new Ui::CTryReg)
{
ui->setupUi(this);
setWindowTitle("STD_REG 1.0");
ui->edRet->setReadOnly(true);
connect(ui->btnSearch, &QPushButton::clicked, this, &CTryReg::reg_search);
connect(ui->btnMatch, &QPushButton::clicked, this, &CTryReg::reg_match);
connect(ui->btnIterator, &QPushButton::clicked, this, &CTryReg::reg_iterator);
connect(ui->btnExit, &QPushButton::clicked, this, &CTryReg::close);
connect(ui->btnOpenHis, &QPushButton::clicked, this, &CTryReg::open_his);
QString home = QDir::homePath();
QDir dir(home);
his_path_ = dir.absoluteFilePath(".config/std_reg");
QDir path(his_path_);
if (!path.exists()) {
path.mkpath(his_path_);
}
his_path_ = his_path_ + "/history.txt";
}
CTryReg::~CTryReg()
{
delete ui;
}
void CTryReg::reg_search()
{
ui->listWidget->clear();
ui->edRet->clear();
std::string pattern = ui->edReg->text().toStdString();
std::string str = ui->plainTextEdit->toPlainText().toStdString();
if (pattern.empty() || str.empty()) {
return;
}
try {
std::smatch match;
std::regex rg(pattern);
if (std::regex_search(str, match, rg)) {
ui->edRet->setText("Success.");
for (const auto& m : match) {
ui->listWidget->addItem(QString::fromStdString(m.str()));
}
} else {
ui->edRet->setText("Failed.");
}
} catch (const std::exception& e) {
ui->edRet->setText(QString::fromStdString(e.what()));
}
append_his(QString::fromStdString(str), QString::fromStdString(pattern));
}
void CTryReg::reg_match()
{
ui->listWidget->clear();
ui->edRet->clear();
std::string pattern = ui->edReg->text().toStdString();
std::string str = ui->plainTextEdit->toPlainText().toStdString();
if (pattern.empty() || str.empty()) {
return;
}
try {
std::regex rg(pattern);
if (std::regex_match(str, rg)) {
ui->edRet->setText("Success.");
} else {
ui->edRet->setText("Failed.");
}
} catch (const std::exception& e) {
ui->edRet->setText(QString::fromStdString(e.what()));
}
append_his(QString::fromStdString(str), QString::fromStdString(pattern));
}
void CTryReg::reg_iterator()
{
ui->listWidget->clear();
ui->edRet->clear();
std::string pattern = ui->edReg->text().toStdString();
std::string str = ui->plainTextEdit->toPlainText().toStdString();
if (pattern.empty() || str.empty()) {
return;
}
try {
std::regex rg(pattern);
auto begin = std::sregex_iterator(str.begin(), str.end(), rg);
auto end = std::sregex_iterator();
if (begin != end) {
ui->edRet->setText("Success.");
while (begin != end) {
for (const auto& item : *begin) {
ui->listWidget->addItem(QString::fromStdString(item.str()));
}
++begin;
}
} else {
ui->edRet->setText("Failed.");
}
} catch (const std::exception& e) {
ui->edRet->setText(QString::fromStdString(e.what()));
}
append_his(QString::fromStdString(str), QString::fromStdString(pattern));
}
bool CTryReg::append_his(const QString& da, const QString& db)
{
QFile file(his_path_);
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
return false;
}
QTextStream in(&file);
QString content = in.readAll();
QString search_pattern = "【字符串】\n" + da + "\n【表达式】\n" + db + "\n";
if (content.contains(search_pattern)) {
file.close();
return true;
}
QTextStream out(&file);
out.setDevice(&file); // 重新设置输出流到文件末尾
QString currentDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
out << currentDateTime << "\n";
out << "【字符串】\n" << da << "\n";
out << "【表达式】\n" << db << "\n\n";
file.close();
return true;
}
void CTryReg::open_his()
{
if (!QFileInfo::exists(his_path_)) {
QMessageBox::warning(nullptr, "错误", "文件不存在:" + his_path_);
return;
}
#ifdef Q_OS_WIN
// Windows 系统使用 "notepad" 或者直接调用文件
process.start("notepad", QStringList() << his_path_);
#elif defined(Q_OS_MAC)
// macOS 系统使用 "open" 命令
process.start("open", QStringList() << his_path_);
#else
// Linux 或其他类 Unix 系统使用 "xdg-open"
process.start("xdg-open", QStringList() << his_path_);
#endif
// 检查是否启动成功
if (!process.waitForStarted()) {
QMessageBox::warning(nullptr, "错误", "无法启动系统编辑器!");
}
}

35
mainwidget.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include <QProcess>
QT_BEGIN_NAMESPACE
namespace Ui {
class CTryReg;
}
QT_END_NAMESPACE
class CTryReg : public QWidget
{
Q_OBJECT
public:
CTryReg(QWidget *parent = nullptr);
~CTryReg();
private:
void reg_search();
void reg_match();
void reg_iterator();
bool append_his(const QString& da, const QString& db);
void open_his();
private:
QString his_path_{};
QProcess process;
private:
Ui::CTryReg *ui;
};
#endif // MAINWIDGET_H

116
mainwidget.ui Normal file
View File

@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CTryReg</class>
<widget class="QWidget" name="CTryReg">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1162</width>
<height>426</height>
</rect>
</property>
<property name="windowTitle">
<string>CTryReg</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>字符串</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit"/>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>结果</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="listWidget"/>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>表达式:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="edReg"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>成功否:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="edRet"/>
</item>
<item>
<widget class="QPushButton" name="btnExit">
<property name="text">
<string>退出</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnOpenHis">
<property name="text">
<string>打开历史</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnSearch">
<property name="text">
<string>search</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMatch">
<property name="text">
<string>match</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnIterator">
<property name="text">
<string>iterator</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -1,15 +0,0 @@
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}

View File

@ -1,21 +0,0 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

BIN
resource/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

1
resource/ico.rc Normal file
View File

@ -0,0 +1 @@
IDI_ICON1 ICON DISCARDABLE "favicon.ico"

5
std_reg.qrc Normal file
View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>resource/favicon.ico</file>
</qresource>
</RCC>