parent
76cd4c9aa6
commit
73c25cb34b
Binary file not shown.
@ -0,0 +1,203 @@
|
||||
/**
|
||||
* @file EnemyStatsDialog.h
|
||||
* @brief 敌情统计对话框定义
|
||||
* @author Qt UI Optimizer
|
||||
* @date 2024-07-08
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
#ifndef ENEMYSTATS_DIALOG_H
|
||||
#define ENEMYSTATS_DIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QTableWidget>
|
||||
#include <QTableWidgetItem>
|
||||
#include <QHeaderView>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QGroupBox>
|
||||
#include <QTimer>
|
||||
#include <QDateTime>
|
||||
#include <QMessageBox>
|
||||
#include <QFileDialog>
|
||||
#include <QTextStream>
|
||||
#include <QApplication>
|
||||
#include <QDesktopWidget>
|
||||
|
||||
/**
|
||||
* @struct EnemyInfo
|
||||
* @brief 敌情信息结构体
|
||||
*/
|
||||
struct EnemyInfo {
|
||||
QString id; ///< 敌人ID/编号
|
||||
double longitude; ///< 经度坐标
|
||||
double latitude; ///< 纬度坐标
|
||||
QString threatLevel; ///< 威胁等级
|
||||
QDateTime discoveryTime; ///< 发现时间
|
||||
QString enemyType; ///< 敌人类型
|
||||
QString status; ///< 状态
|
||||
};
|
||||
|
||||
/**
|
||||
* @class EnemyStatsDialog
|
||||
* @brief 敌情统计对话框
|
||||
*
|
||||
* 提供敌情数据的表格显示、统计分析和数据导出功能
|
||||
*/
|
||||
class EnemyStatsDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param parent 父组件指针
|
||||
*/
|
||||
explicit EnemyStatsDialog(QWidget *parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~EnemyStatsDialog();
|
||||
|
||||
/**
|
||||
* @brief 添加敌情信息
|
||||
* @param enemy 敌情信息结构体
|
||||
*/
|
||||
void addEnemyInfo(const EnemyInfo &enemy);
|
||||
|
||||
/**
|
||||
* @brief 更新敌情信息
|
||||
* @param id 敌人ID
|
||||
* @param enemy 更新后的敌情信息
|
||||
*/
|
||||
void updateEnemyInfo(const QString &id, const EnemyInfo &enemy);
|
||||
|
||||
/**
|
||||
* @brief 删除敌情信息
|
||||
* @param id 敌人ID
|
||||
*/
|
||||
void removeEnemyInfo(const QString &id);
|
||||
|
||||
/**
|
||||
* @brief 清空所有敌情信息
|
||||
*/
|
||||
void clearAllEnemies();
|
||||
|
||||
/**
|
||||
* @brief 获取敌情总数
|
||||
* @return 敌情总数
|
||||
*/
|
||||
int getEnemyCount() const;
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief 敌情数据更新信号
|
||||
* @param totalCount 敌情总数
|
||||
* @param highThreatCount 高威胁敌情数量
|
||||
*/
|
||||
void enemyDataUpdated(int totalCount, int highThreatCount);
|
||||
|
||||
private slots:
|
||||
/**
|
||||
* @brief 刷新数据槽函数
|
||||
*/
|
||||
void onRefreshData();
|
||||
|
||||
/**
|
||||
* @brief 导出数据槽函数
|
||||
*/
|
||||
void onExportData();
|
||||
|
||||
/**
|
||||
* @brief 表格行选择变化槽函数
|
||||
*/
|
||||
void onTableSelectionChanged();
|
||||
|
||||
/**
|
||||
* @brief 自动刷新定时器槽函数
|
||||
*/
|
||||
void onAutoRefresh();
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief 设置UI界面
|
||||
*/
|
||||
void setupUI();
|
||||
|
||||
/**
|
||||
* @brief 设置表格
|
||||
*/
|
||||
void setupTable();
|
||||
|
||||
/**
|
||||
* @brief 设置统计面板
|
||||
*/
|
||||
void setupStatsPanel();
|
||||
|
||||
/**
|
||||
* @brief 应用样式表
|
||||
*/
|
||||
void applyStyles();
|
||||
|
||||
/**
|
||||
* @brief 连接信号槽
|
||||
*/
|
||||
void connectSignals();
|
||||
|
||||
/**
|
||||
* @brief 更新统计信息
|
||||
*/
|
||||
void updateStatistics();
|
||||
|
||||
/**
|
||||
* @brief 加载测试数据
|
||||
*/
|
||||
void loadTestData();
|
||||
|
||||
/**
|
||||
* @brief 获取威胁等级颜色
|
||||
* @param threatLevel 威胁等级
|
||||
* @return 对应的颜色
|
||||
*/
|
||||
QColor getThreatLevelColor(const QString &threatLevel);
|
||||
|
||||
/**
|
||||
* @brief 格式化坐标显示
|
||||
* @param longitude 经度
|
||||
* @param latitude 纬度
|
||||
* @return 格式化后的坐标字符串
|
||||
*/
|
||||
QString formatCoordinates(double longitude, double latitude);
|
||||
|
||||
// UI组件
|
||||
QVBoxLayout *m_mainLayout; ///< 主布局
|
||||
QHBoxLayout *m_contentLayout; ///< 内容布局
|
||||
|
||||
// 表格组件
|
||||
QGroupBox *m_tableGroup; ///< 表格分组框
|
||||
QTableWidget *m_enemyTable; ///< 敌情表格
|
||||
|
||||
// 统计面板组件
|
||||
QGroupBox *m_statsGroup; ///< 统计分组框
|
||||
QLabel *m_totalCountLabel; ///< 总数标签
|
||||
QLabel *m_highThreatLabel; ///< 高威胁数量标签
|
||||
QLabel *m_mediumThreatLabel; ///< 中威胁数量标签
|
||||
QLabel *m_lowThreatLabel; ///< 低威胁数量标签
|
||||
QLabel *m_lastUpdateLabel; ///< 最后更新时间标签
|
||||
|
||||
// 操作按钮
|
||||
QPushButton *m_refreshBtn; ///< 刷新按钮
|
||||
QPushButton *m_exportBtn; ///< 导出按钮
|
||||
QPushButton *m_closeBtn; ///< 关闭按钮
|
||||
|
||||
// 数据存储
|
||||
QList<EnemyInfo> m_enemyList; ///< 敌情信息列表
|
||||
|
||||
// 定时器
|
||||
QTimer *m_autoRefreshTimer; ///< 自动刷新定时器
|
||||
};
|
||||
|
||||
#endif // ENEMYSTATS_DIALOG_H
|
@ -0,0 +1,430 @@
|
||||
/**
|
||||
* @file EnemyDatabase.cpp
|
||||
* @brief 敌情数据库管理类实现
|
||||
* @author Qt UI Optimizer
|
||||
* @date 2024-07-08
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
#include "core/database/EnemyDatabase.h"
|
||||
#include "core/database/DatabaseHelper.h"
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlError>
|
||||
#include <QUuid>
|
||||
|
||||
EnemyDatabase* EnemyDatabase::m_instance = nullptr;
|
||||
|
||||
EnemyDatabase::EnemyDatabase(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_connectionName("EnemyDatabase_Connection")
|
||||
, m_isInitialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
EnemyDatabase::~EnemyDatabase()
|
||||
{
|
||||
if (QSqlDatabase::contains(m_connectionName)) {
|
||||
QSqlDatabase::removeDatabase(m_connectionName);
|
||||
}
|
||||
}
|
||||
|
||||
EnemyDatabase* EnemyDatabase::getInstance()
|
||||
{
|
||||
if (m_instance == nullptr) {
|
||||
m_instance = new EnemyDatabase();
|
||||
}
|
||||
return m_instance;
|
||||
}
|
||||
|
||||
bool EnemyDatabase::initializeDatabase()
|
||||
{
|
||||
if (m_isInitialized) {
|
||||
return true;
|
||||
}
|
||||
|
||||
QSqlDatabase db = DatabaseHelper::createTempConnection(m_connectionName);
|
||||
if (!db.isOpen()) {
|
||||
emit databaseError("无法连接到数据库");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!createTables()) {
|
||||
emit databaseError("创建敌情数据表失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_isInitialized = true;
|
||||
qDebug() << "敌情数据库初始化成功";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EnemyDatabase::createTables()
|
||||
{
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
QString createTableSQL = R"(
|
||||
CREATE TABLE IF NOT EXISTS enemy_records (
|
||||
id VARCHAR(50) PRIMARY KEY,
|
||||
longitude DOUBLE NOT NULL,
|
||||
latitude DOUBLE NOT NULL,
|
||||
threat_level VARCHAR(20) NOT NULL,
|
||||
discovery_time DATETIME NOT NULL,
|
||||
enemy_type VARCHAR(50),
|
||||
status VARCHAR(20) DEFAULT '活跃',
|
||||
description TEXT,
|
||||
update_time DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
INDEX idx_threat_level (threat_level),
|
||||
INDEX idx_status (status),
|
||||
INDEX idx_discovery_time (discovery_time)
|
||||
)
|
||||
)";
|
||||
|
||||
query.prepare(createTableSQL);
|
||||
return executeQuery(query, "创建敌情记录表");
|
||||
}
|
||||
|
||||
QSqlDatabase EnemyDatabase::getDatabase()
|
||||
{
|
||||
if (QSqlDatabase::contains(m_connectionName)) {
|
||||
return QSqlDatabase::database(m_connectionName);
|
||||
}
|
||||
return DatabaseHelper::createTempConnection(m_connectionName);
|
||||
}
|
||||
|
||||
bool EnemyDatabase::executeQuery(QSqlQuery &query, const QString &operation)
|
||||
{
|
||||
if (!query.exec()) {
|
||||
QString errorMsg = QString("%1失败: %2").arg(operation).arg(query.lastError().text());
|
||||
qDebug() << errorMsg;
|
||||
emit databaseError(errorMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EnemyDatabase::addEnemyRecord(const EnemyRecord &record)
|
||||
{
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
QString insertSQL = R"(
|
||||
INSERT INTO enemy_records
|
||||
(id, longitude, latitude, threat_level, discovery_time, enemy_type, status, description, update_time)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
)";
|
||||
|
||||
query.prepare(insertSQL);
|
||||
query.addBindValue(record.id);
|
||||
query.addBindValue(record.longitude);
|
||||
query.addBindValue(record.latitude);
|
||||
query.addBindValue(record.threatLevel);
|
||||
query.addBindValue(record.discoveryTime);
|
||||
query.addBindValue(record.enemyType);
|
||||
query.addBindValue(record.status);
|
||||
query.addBindValue(record.description);
|
||||
query.addBindValue(QDateTime::currentDateTime());
|
||||
|
||||
bool success = executeQuery(query, "添加敌情记录");
|
||||
if (success) {
|
||||
emit enemyDataUpdated();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool EnemyDatabase::updateEnemyRecord(const EnemyRecord &record)
|
||||
{
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
QString updateSQL = R"(
|
||||
UPDATE enemy_records SET
|
||||
longitude = ?, latitude = ?, threat_level = ?, discovery_time = ?,
|
||||
enemy_type = ?, status = ?, description = ?, update_time = ?
|
||||
WHERE id = ?
|
||||
)";
|
||||
|
||||
query.prepare(updateSQL);
|
||||
query.addBindValue(record.longitude);
|
||||
query.addBindValue(record.latitude);
|
||||
query.addBindValue(record.threatLevel);
|
||||
query.addBindValue(record.discoveryTime);
|
||||
query.addBindValue(record.enemyType);
|
||||
query.addBindValue(record.status);
|
||||
query.addBindValue(record.description);
|
||||
query.addBindValue(QDateTime::currentDateTime());
|
||||
query.addBindValue(record.id);
|
||||
|
||||
bool success = executeQuery(query, "更新敌情记录");
|
||||
if (success) {
|
||||
emit enemyDataUpdated();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool EnemyDatabase::deleteEnemyRecord(const QString &enemyId)
|
||||
{
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("DELETE FROM enemy_records WHERE id = ?");
|
||||
query.addBindValue(enemyId);
|
||||
|
||||
bool success = executeQuery(query, "删除敌情记录");
|
||||
if (success) {
|
||||
emit enemyDataUpdated();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
QList<EnemyRecord> EnemyDatabase::getAllEnemyRecords()
|
||||
{
|
||||
QList<EnemyRecord> records;
|
||||
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("SELECT * FROM enemy_records ORDER BY discovery_time DESC");
|
||||
|
||||
if (executeQuery(query, "获取所有敌情记录")) {
|
||||
while (query.next()) {
|
||||
records.append(queryToRecord(query));
|
||||
}
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
EnemyRecord EnemyDatabase::getEnemyRecord(const QString &enemyId)
|
||||
{
|
||||
EnemyRecord record;
|
||||
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return record;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return record;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("SELECT * FROM enemy_records WHERE id = ?");
|
||||
query.addBindValue(enemyId);
|
||||
|
||||
if (executeQuery(query, "获取敌情记录") && query.next()) {
|
||||
record = queryToRecord(query);
|
||||
}
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
QList<EnemyRecord> EnemyDatabase::getEnemyRecordsByThreatLevel(const QString &threatLevel)
|
||||
{
|
||||
QList<EnemyRecord> records;
|
||||
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("SELECT * FROM enemy_records WHERE threat_level = ? ORDER BY discovery_time DESC");
|
||||
query.addBindValue(threatLevel);
|
||||
|
||||
if (executeQuery(query, "按威胁等级获取敌情记录")) {
|
||||
while (query.next()) {
|
||||
records.append(queryToRecord(query));
|
||||
}
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
QList<EnemyRecord> EnemyDatabase::getEnemyRecordsByStatus(const QString &status)
|
||||
{
|
||||
QList<EnemyRecord> records;
|
||||
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("SELECT * FROM enemy_records WHERE status = ? ORDER BY discovery_time DESC");
|
||||
query.addBindValue(status);
|
||||
|
||||
if (executeQuery(query, "按状态获取敌情记录")) {
|
||||
while (query.next()) {
|
||||
records.append(queryToRecord(query));
|
||||
}
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
QList<EnemyRecord> EnemyDatabase::getEnemyRecordsByTimeRange(const QDateTime &startTime, const QDateTime &endTime)
|
||||
{
|
||||
QList<EnemyRecord> records;
|
||||
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("SELECT * FROM enemy_records WHERE discovery_time BETWEEN ? AND ? ORDER BY discovery_time DESC");
|
||||
query.addBindValue(startTime);
|
||||
query.addBindValue(endTime);
|
||||
|
||||
if (executeQuery(query, "按时间范围获取敌情记录")) {
|
||||
while (query.next()) {
|
||||
records.append(queryToRecord(query));
|
||||
}
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
QMap<QString, int> EnemyDatabase::getEnemyStatistics()
|
||||
{
|
||||
QMap<QString, int> statistics;
|
||||
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return statistics;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return statistics;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("SELECT threat_level, COUNT(*) as count FROM enemy_records GROUP BY threat_level");
|
||||
|
||||
if (executeQuery(query, "获取敌情统计")) {
|
||||
while (query.next()) {
|
||||
QString threatLevel = query.value("threat_level").toString();
|
||||
int count = query.value("count").toInt();
|
||||
statistics[threatLevel] = count;
|
||||
}
|
||||
}
|
||||
|
||||
return statistics;
|
||||
}
|
||||
|
||||
bool EnemyDatabase::clearAllEnemyRecords()
|
||||
{
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("DELETE FROM enemy_records");
|
||||
|
||||
bool success = executeQuery(query, "清空所有敌情记录");
|
||||
if (success) {
|
||||
emit enemyDataUpdated();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool EnemyDatabase::enemyExists(const QString &enemyId)
|
||||
{
|
||||
if (!m_isInitialized && !initializeDatabase()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlDatabase db = getDatabase();
|
||||
if (!db.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
query.prepare("SELECT COUNT(*) FROM enemy_records WHERE id = ?");
|
||||
query.addBindValue(enemyId);
|
||||
|
||||
if (executeQuery(query, "检查敌人ID是否存在") && query.next()) {
|
||||
return query.value(0).toInt() > 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QString EnemyDatabase::generateNewEnemyId()
|
||||
{
|
||||
QString prefix = "ENEMY";
|
||||
int counter = 1;
|
||||
QString newId;
|
||||
|
||||
do {
|
||||
newId = QString("%1%2").arg(prefix).arg(counter, 3, 10, QChar('0'));
|
||||
counter++;
|
||||
} while (enemyExists(newId) && counter < 1000);
|
||||
|
||||
return newId;
|
||||
}
|
||||
|
||||
EnemyRecord EnemyDatabase::queryToRecord(const QSqlQuery &query)
|
||||
{
|
||||
EnemyRecord record;
|
||||
record.id = query.value("id").toString();
|
||||
record.longitude = query.value("longitude").toDouble();
|
||||
record.latitude = query.value("latitude").toDouble();
|
||||
record.threatLevel = query.value("threat_level").toString();
|
||||
record.discoveryTime = query.value("discovery_time").toDateTime();
|
||||
record.enemyType = query.value("enemy_type").toString();
|
||||
record.status = query.value("status").toString();
|
||||
record.description = query.value("description").toString();
|
||||
record.updateTime = query.value("update_time").toDateTime();
|
||||
return record;
|
||||
}
|
Loading…
Reference in new issue