From 09cb96f97b4ca0cc525b0a6fffaec395c608abcd Mon Sep 17 00:00:00 2001 From: luocai <168062547@qq.com> Date: Fri, 24 May 2024 10:23:05 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E7=89=B9=E5=BE=81=E5=80=BC?= =?UTF-8?q?model=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Analyser/CMakeLists.txt | 1 + Analyser/ModuleCommunication.h | 2 +- Analyser/PalmFeatureTableModel.cpp | 17 +++++++++++++++++ Analyser/PalmFeatureTableModel.h | 19 +++++++++++++++++++ Analyser/Widget.cpp | 19 +++++++++++++------ Analyser/Widget.h | 8 +++++++- Database/CMakeLists.txt | 1 + Database/DataStructure.cpp | 5 +++++ Database/DataStructure.h | 23 +++++++++++++++++++++++ Database/Database.cpp | 4 ++-- Database/Database.h | 11 ++--------- 11 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 Analyser/PalmFeatureTableModel.cpp create mode 100644 Analyser/PalmFeatureTableModel.h create mode 100644 Database/DataStructure.cpp create mode 100644 Database/DataStructure.h diff --git a/Analyser/CMakeLists.txt b/Analyser/CMakeLists.txt index ddc6d93..c555c56 100644 --- a/Analyser/CMakeLists.txt +++ b/Analyser/CMakeLists.txt @@ -9,6 +9,7 @@ add_executable(Analyser Analyser.rc CategoryLogSinkBackend.h CategoryLogSinkBackend.cpp Widget.h Widget.cpp ModuleCommunication.h ModuleCommunication.cpp + PalmFeatureTableModel.h PalmFeatureTableModel.cpp ) target_link_libraries(Analyser diff --git a/Analyser/ModuleCommunication.h b/Analyser/ModuleCommunication.h index 1a154f2..7cb5059 100644 --- a/Analyser/ModuleCommunication.h +++ b/Analyser/ModuleCommunication.h @@ -1,7 +1,7 @@ #ifndef MODULECOMMUNICATION_H #define MODULECOMMUNICATION_H -#include "Database.h" +#include "DataStructure.h" #include #include diff --git a/Analyser/PalmFeatureTableModel.cpp b/Analyser/PalmFeatureTableModel.cpp new file mode 100644 index 0000000..808ecf6 --- /dev/null +++ b/Analyser/PalmFeatureTableModel.cpp @@ -0,0 +1,17 @@ +#include "PalmFeatureTableModel.h" + +PalmFeatureTableModel::PalmFeatureTableModel(QObject *parent) { +} + +int PalmFeatureTableModel::rowCount(const QModelIndex &parent) const { + return static_cast(m_features.size()); +} + +int PalmFeatureTableModel::columnCount(const QModelIndex &parent) const { + return 2; +} + +QVariant PalmFeatureTableModel::data(const QModelIndex &index, int role) const { + QVariant ret; + return ret; +} diff --git a/Analyser/PalmFeatureTableModel.h b/Analyser/PalmFeatureTableModel.h new file mode 100644 index 0000000..29c4c87 --- /dev/null +++ b/Analyser/PalmFeatureTableModel.h @@ -0,0 +1,19 @@ +#ifndef __PALMFEATURETABLEMODEL_H__ +#define __PALMFEATURETABLEMODEL_H__ + +#include "DataStructure.h" +#include + +class PalmFeatureTableModel : public QAbstractTableModel { + Q_OBJECT +public: + PalmFeatureTableModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const final; + int columnCount(const QModelIndex &parent = QModelIndex()) const final; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const final; + +private: + PalmFeatures m_features; +}; + +#endif // __PALMFEATURETABLEMODEL_H__ diff --git a/Analyser/Widget.cpp b/Analyser/Widget.cpp index 9136d16..1748775 100644 --- a/Analyser/Widget.cpp +++ b/Analyser/Widget.cpp @@ -1,7 +1,9 @@ #include "Widget.h" #include "BoostLog.h" #include "CategoryLogSinkBackend.h" +#include "Database.h" #include "ModuleCommunication.h" +#include "PalmFeatureTableModel.h" #include #include #include @@ -11,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -51,10 +54,12 @@ Widget::Widget(QWidget *parent) : QWidget{parent} { connect(btn, &QPushButton::clicked, this, &Widget::onClearLogButtonClicked); logLayout->addWidget(btn, 0, Qt::AlignBottom | Qt::AlignRight); + m_featureTableView = new QTableView(); + auto tabWidget = new QTabWidget(); tabWidget->addTab(m_logBrowser, "日志"); tabWidget->addTab(new QWidget(), "视频流"); - tabWidget->addTab(new QWidget(), "已注册列表"); + tabWidget->addTab(m_featureTableView, "本地特征值列表"); m_commandGroupBox = initializeCommandGroupBox(); @@ -68,6 +73,8 @@ Widget::Widget(QWidget *parent) : QWidget{parent} { layout->addWidget(tabWidget, 3); m_database = std::make_shared(); + m_featureModel = new PalmFeatureTableModel(this); + m_featureTableView->setModel(m_featureModel); QTimer::singleShot(0, this, [this]() { onSerialRefreshButtonClicked(); @@ -194,14 +201,14 @@ QGroupBox *Widget::initializeUvcGroupBox() { } void Widget::onNewPalmFeature(const PalmFeature &feature) { + auto palms = m_database->palmFeatures(); + if (std::find(palms.cbegin(), palms.cend(), feature) != palms.cend()) { + LOG(warning) << "本地数据库已有相同特征数据。"; + return; + } if (!m_database->addPalmFeature(feature)) { LOG(error) << "add palm feature failed."; } - - auto palms = m_database->palmFeatures(); - for (auto &p : palms) { - LOG(info) << p.id << " " << p.username << " " << p.feature.size(); - } } void Widget::onSerialConnectButtonClicked() { diff --git a/Analyser/Widget.h b/Analyser/Widget.h index ca403c0..f56ce83 100644 --- a/Analyser/Widget.h +++ b/Analyser/Widget.h @@ -1,7 +1,7 @@ #ifndef WIDGET_H #define WIDGET_H -#include "Database.h" +#include "DataStructure.h" #include class QPushButton; @@ -9,7 +9,10 @@ class QTextBrowser; class QComboBox; class QLineEdit; class QGroupBox; +class QTableView; class ModuleCommunication; +class PalmFeatureTableModel; +class Database; class Widget : public QWidget { Q_OBJECT @@ -63,6 +66,9 @@ private: std::shared_ptr m_communication; std::shared_ptr m_database; + + PalmFeatureTableModel *m_featureModel = nullptr; + QTableView *m_featureTableView = nullptr; }; #endif // WIDGET_H diff --git a/Database/CMakeLists.txt b/Database/CMakeLists.txt index b110c51..9becdbd 100644 --- a/Database/CMakeLists.txt +++ b/Database/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(Database Database.h Database.cpp + DataStructure.h DataStructure.cpp # shell.c sqlite3.h sqlite3.c sqlite3ext.h diff --git a/Database/DataStructure.cpp b/Database/DataStructure.cpp new file mode 100644 index 0000000..2507445 --- /dev/null +++ b/Database/DataStructure.cpp @@ -0,0 +1,5 @@ +#include "DataStructure.h" + +bool operator==(const PalmFeature &lhs, const PalmFeature &rhs) { + return lhs.feature == rhs.feature; +} diff --git a/Database/DataStructure.h b/Database/DataStructure.h new file mode 100644 index 0000000..35277f7 --- /dev/null +++ b/Database/DataStructure.h @@ -0,0 +1,23 @@ +#ifndef __DATASTRUCTURE_H__ +#define __DATASTRUCTURE_H__ + +#include +#include + +class PalmFeature { +public: + int64_t id; // 对应本地数据库的id + std::string username; + std::vector feature; +}; + +using PalmFeatures = std::vector; + +bool operator==(const PalmFeature &lhs, const PalmFeature &rhs); + +namespace std { +template <> +struct hash {}; +} // namespace std + +#endif // __DATASTRUCTURE_H__ diff --git a/Database/Database.cpp b/Database/Database.cpp index 1344613..a51544e 100644 --- a/Database/Database.cpp +++ b/Database/Database.cpp @@ -23,8 +23,8 @@ bool Database::addPalmFeature(const PalmFeature &palm) { return true; } -std::vector Database::palmFeatures() const { - std::vector ret; +PalmFeatures Database::palmFeatures() const { + PalmFeatures ret; sqlite3_stmt *statement = nullptr; constexpr const char *sql = "SELECT * FROM palm_feature"; if (sqlite3_prepare_v2(m_sqlite, sql, -1, &statement, NULL) != SQLITE_OK) { diff --git a/Database/Database.h b/Database/Database.h index 7a1fe96..ddbbb55 100644 --- a/Database/Database.h +++ b/Database/Database.h @@ -1,23 +1,16 @@ #ifndef __DATABASE_H__ #define __DATABASE_H__ -#include -#include +#include "DataStructure.h" struct sqlite3; -class PalmFeature { -public: - int64_t id; // 对应本地数据库的id - std::string username; - std::vector feature; -}; class Database{ public: bool open(const std::string &path); bool addPalmFeature(const PalmFeature &palm); - std::vector palmFeatures() const; + PalmFeatures palmFeatures() const; private: void initializeTables();