diff --git a/CMakeLists.txt b/CMakeLists.txt index 79d71cd..bb3b6e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,9 @@ set(PROJECT_SOURCES mainwindow.cpp mainwindow.h mainwindow.ui + global.h + readthread.cpp + readthread.h ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) @@ -40,7 +43,7 @@ else() else() add_executable(ReadLog ${PROJECT_SOURCES} - qcustomplot.cpp qcustomplot.h + ) endif() endif() diff --git a/ReadLog.pro b/ReadLog.pro index e936a67..48f3445 100644 --- a/ReadLog.pro +++ b/ReadLog.pro @@ -10,17 +10,23 @@ CONFIG += c++11 SOURCES += \ main.cpp \ - mainwindow.cpp - # qcustomplot.cpp + mainwindow.cpp \ + qcustomplot.cpp \ + readthread.cpp HEADERS += \ - mainwindow.h - # qcustomplot.h + global.h \ + mainwindow.h \ + qcustomplot.h \ + readthread.h FORMS += \ mainwindow.ui +RC_ICONS = logo.ico + # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target + diff --git a/global.h b/global.h new file mode 100644 index 0000000..447de00 --- /dev/null +++ b/global.h @@ -0,0 +1,30 @@ +#ifndef READLOG_GLOBAL_H +#define READLOG_GLOBAL_H +#include +#include +#include +#include +#include + +extern QVector> g_param[4]; //存储4条基带的数据 +extern QMap namelist; //存储数据名以及数据是否是数字 +extern QFile file; //打开文件 +extern QCompleter *completer; + + + + + + + + + + + + + + + + + +#endif //READLOG_GLOBAL_H diff --git a/logo.ico b/logo.ico new file mode 100644 index 0000000..68176a2 Binary files /dev/null and b/logo.ico differ diff --git a/main.cpp b/main.cpp index 9c658e2..eb698b2 100644 --- a/main.cpp +++ b/main.cpp @@ -1,12 +1,37 @@ #include "mainwindow.h" #include +#include #include +#include "global.h" -int main(int argc, char *argv[]) -{ + +//初始化数据 +QVector> g_param[4]; +QMap namelist; +QFile file; +QCompleter *completer; + +int main(int argc, char *argv[]) { + //设置logo + QApplication::setWindowIcon(QIcon("logo.ico")); + //放大窗口 +// qputenv("QT_SCALE_FACTOR", "2.0"); + //设置使用UTF8字符集,正确显示中文 + QTextCodec *codec = QTextCodec::codecForName("UTF-8"); + QTextCodec::setCodecForLocale(codec); + + //运行程序 QApplication a(argc, argv); - MainWindow w; - w.show(); +/* 获取分辨率 + QDesktopWidget *desktopWidget = QApplication::desktop(); + //获取可用桌面大小 + QRect deskRect = desktopWidget->availableGeometry(); + qDebug() << deskRect.width() << "x" << deskRect.height(); + //获取设备屏幕大小 + QRect screenRect = desktopWidget->screenGeometry(); + qDebug() << screenRect.width() << "x" << screenRect.height();*/ + MainWindow w; + w.show(); return a.exec(); } diff --git a/mainwindow.cpp b/mainwindow.cpp index 7cf4cbc..6be9fc1 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,22 +1,99 @@ +#include +#include +#include +#include +#include #include "mainwindow.h" - #include "ui_mainwindow.h" -#include -#include -#include -#include MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent) - , ui(new Ui::MainWindow) -{ + : QMainWindow(parent), ui(new Ui::MainWindow), readThread(new ReadThread) { ui->setupUi(this); + // 创建版本号标签并添加到状态栏的右端 + QLabel *m_versionLabel = new QLabel(tr("v1.2.0"), this); + ui->statusbar->addPermanentWidget(m_versionLabel); + //隐藏进度条 + ui->progressBar->close(); + + /*信号和槽*/ + //readThread + connect(readThread, &ReadThread::clearCombo, this, &MainWindow::clearCombo); + connect(readThread, &ReadThread::showStatusbar, this, &MainWindow::showStatusbar); + connect(readThread, &ReadThread::setProgressBar, this, &MainWindow::setProgressBar); + connect(readThread, &ReadThread::setBeginTime, this, &MainWindow::setBeginTime); + connect(readThread, &ReadThread::setEndTime, this, &MainWindow::setEndTime); + connect(readThread, &ReadThread::addCombo, this, &MainWindow::addCombo); + connect(readThread, &ReadThread::closeProg, this, &MainWindow::closeProg); } -MainWindow::~MainWindow() -{ +MainWindow::~MainWindow() { delete ui; } +void MainWindow::on_openFileBtn_clicked() { + //选择文件,并获取文件名 + QString fileName = QFileDialog::getOpenFileName(); + //打开文件 + file.setFileName(fileName); + //打开失败 + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::warning(this, "错误", "文件打开失败"); + ui->statusbar->clearMessage(); + clearCombo(); + return; + } + //获取文件大小 + qint64 fileSize = file.size(); + //设置进度条 + ui->progressBar->setMaximum((int) fileSize); + ui->progressBar->setValue(0); + ui->progressBar->show(); + //执行读取 + readThread->start(); +} + + +void MainWindow::on_timeCustomBtn_clicked() { + ui->progressBar->close(); +} + + +void MainWindow::on_dataCustomBtn_clicked() { + +} + +void MainWindow::clearCombo() { + ui->comboBox->clear(); +} + +void MainWindow::showStatusbar(const QString &str) { + ui->statusbar->showMessage(str); +} + +void MainWindow::setProgressBar(int bytesRead) { + ui->progressBar->setValue(bytesRead); +} + +void MainWindow::setBeginTime(const QDateTime &beginTime) { + ui->beginTimeEdit->setDateTime(beginTime); +} + +void MainWindow::setEndTime(const QDateTime &endTime) { + ui->endTimeEdit->setDateTime(endTime); +} + +void MainWindow::addCombo(const QStringList &names) { + //设置下拉框数据 + ui->comboBox->addItems(names); + //根据输入匹配下拉框 + completer = new QCompleter(names); + completer->setFilterMode(Qt::MatchContains); + ui->comboBox->setCompleter(completer); +} + +void MainWindow::closeProg() { + ui->progressBar->close(); +} + diff --git a/mainwindow.h b/mainwindow.h index 429ed6d..7a9e615 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -3,22 +3,59 @@ #include +#include "global.h" +#include "readthread.h" + QT_BEGIN_NAMESPACE namespace Ui { -class MainWindow; + class MainWindow; } QT_END_NAMESPACE -class MainWindow : public QMainWindow -{ - Q_OBJECT +class MainWindow : public QMainWindow { +Q_OBJECT public: - MainWindow(QWidget *parent = nullptr); - ~MainWindow(); + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow() override; + + +private slots: + + /*打开文件*/ + void on_openFileBtn_clicked(); + + /*绘制时间图*/ + void on_timeCustomBtn_clicked(); + + /*绘制数据变化图*/ + void on_dataCustomBtn_clicked(); + + /*清空下拉框*/ + void clearCombo(); + + /*设置底部左侧状态栏文字*/ + void showStatusbar(const QString &str); + + /*设置进度条进度*/ + void setProgressBar(int bytesRead); + + /*设置开始时间*/ + void setBeginTime(const QDateTime &beginTime); + + /*设置结束时间*/ + void setEndTime(const QDateTime &endTime); + + /*设置下拉框*/ + void addCombo(const QStringList &names); + + /*关闭进度条*/ + void closeProg(); private: Ui::MainWindow *ui; + ReadThread *readThread; }; + #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index d055eb2..cfc8eb1 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -2,6 +2,12 @@ MainWindow + + Qt::NonModal + + + true + 0 @@ -10,13 +16,16 @@ 700 + + false + - MainWindow + 日志读取 - + 宋体 @@ -28,7 +37,7 @@ - + 0 @@ -50,16 +59,22 @@ - + + + + 0 + 0 + + 0 - 40 + 45 - + 0 @@ -69,7 +84,7 @@ 16777215 - 16777215 + 25 @@ -87,7 +102,7 @@ - + 宋体 @@ -99,7 +114,7 @@ - + 0 @@ -121,13 +136,19 @@ - + 0 0 + + + 0 + 70 + + 宋体 @@ -141,39 +162,39 @@ Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - - 基带 1 - - - - - - - 基带 2 - - - - + 基带 3 + + + + 基带 2 + + + - + 基带 4 + + + + 基带 1 + + + - + Qt::Vertical @@ -186,7 +207,7 @@ - + Qt::Vertical @@ -224,7 +245,7 @@ - + 0 @@ -270,7 +291,7 @@ - + 0 @@ -295,7 +316,7 @@ - + 0 @@ -320,7 +341,7 @@ - + Qt::Vertical diff --git a/readthread.cpp b/readthread.cpp new file mode 100644 index 0000000..ced7faf --- /dev/null +++ b/readthread.cpp @@ -0,0 +1,130 @@ +#include +#include "readthread.h" + +ReadThread::ReadThread(QObject *parent) + : QThread{parent} { + /*正则表达式*/ + //时间 + dateTimeRegex.setPattern("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3})"); + //基带 + jdRegex.setPattern(R"(\[.*(\d)\])"); + //匹配数字和可选的正负号 + pattern.setPattern("^[+-]?([1-9][0-9]*|0)[.]?[0-9]*$"); + //科学计数法 + kexue.setPattern("^[0-9].[0-9]e[+-][0-9][0-9]"); +} + +bool ReadThread::isNumeric(QString data) { + bool flag = false; + patternMatch = pattern.match(data); + if (patternMatch.hasMatch()) { + flag = true; + } + kexueMatch = kexue.match(data); + if (kexueMatch.hasMatch()) { + flag = true; + } + return flag; +} + +void ReadThread::run() { + //初始化数据 + int bytesRead = 0; //进度条数值 + for (auto &i: g_param) { + i.clear(); + } + QMap temData; + bool isBeginTime = true; + //清除下拉框数据 + emit clearCombo(); + if (!namelist.empty()) { + namelist.clear(); + completer->deleteLater(); + } + + //设置状态栏 + emit showStatusbar("正在打开文件"); + + //读取文件 + QTextStream in(&file); + while (!in.atEnd()) { + //读取一行 + QString line = in.readLine(); + + //更新进度条 + bytesRead += line.size(); + emit setProgressBar(bytesRead * 2); + + /* 匹配数据 */ + //匹配日期时间 + QMap temMap; + int JD; + QString temtime; + QString temjd; + matchDateTime = dateTimeRegex.match(line); + if (matchDateTime.hasMatch()) { + temtime = matchDateTime.captured(1); + temMap["时间"] = temtime; + } else { + continue; + } + //匹配基带 + matchLogLevel = jdRegex.match(line); + if (matchLogLevel.hasMatch()) { + temjd = matchLogLevel.captured(1); + temMap["基带"] = temjd; + JD = temjd.toInt() - 1; + } else { + continue; + } + + //设置开始时间 + if (isBeginTime) { + emit setBeginTime(QDateTime::fromString(temtime, "yyyy-MM-dd hh:mm:ss:zzz")); + isBeginTime = false; + } + temData.clear(); + temData["时间"] = temtime; + //将左右拆分 + QStringList dataParts = line.split(']'); + //判断右侧是否有数据 + if (dataParts[1].isEmpty()) { + continue; + } + //将右侧数据拆分 + QStringList data = dataParts[1].split(','); + for (int i = 0; i < data.size(); i++) { + //将每一条数据拆为数据名和值 + QStringList tem = data[i].split('='); + if (tem.size() < 2) { + continue; + } + if (!namelist.contains(tem[0])) { + namelist[tem[0]] = isNumeric(tem[1]); + if (tem[0].contains("版本号")) { + namelist[tem[0]] = false; + } + } + temData[tem[0]] = tem[1]; + } + //记录数据 + g_param[JD].append(temData); + + } + + //设置结束时间 + emit setEndTime(QDateTime::fromString(temData["时间"], "yyyy-MM-dd hh:mm:ss:zzz")); + + //设置状态栏 + emit showStatusbar("打开成功:" + file.fileName()); + + //添加下拉框数据 + emit addCombo(namelist.keys()); + + //关闭 + emit closeProg(); + file.close(); + temData.clear(); + quit(); +} + diff --git a/readthread.h b/readthread.h new file mode 100644 index 0000000..b33fdf1 --- /dev/null +++ b/readthread.h @@ -0,0 +1,49 @@ +#ifndef READTHREAD_H +#define READTHREAD_H + +#include +#include +#include +#include +#include "global.h" + + +class ReadThread : public QThread { +Q_OBJECT +public: + explicit ReadThread(QObject *parent = nullptr); + + bool isNumeric(QString data); + +signals: + + void clearCombo(); + + void showStatusbar(QString str); + + void setProgressBar(int bytesRead); + + void setBeginTime(QDateTime beginTime); + + void setEndTime(QDateTime beginTime); + + void addCombo(QStringList names); + + void closeProg(); + +protected: + void run() Q_DECL_OVERRIDE; + +private: + QRegularExpression dateTimeRegex; + QRegularExpressionMatch matchDateTime; + QRegularExpression jdRegex; + QRegularExpressionMatch matchLogLevel; + QRegularExpression pattern; + QRegularExpressionMatch patternMatch; + QRegularExpression kexue; + QRegularExpressionMatch kexueMatch; + +}; + +#endif // READTHREAD_H