合并代码 #3

Merged
psf4lx3ga merged 1 commits from test into wangjing 3 months ago

@ -1,7 +1,5 @@
#include <QApplication>
#include "../ui/mainwindow.h"
#include "../utils/logger.h"
#include "../utils/config.h"
int main(int argc, char *argv[]) {
// 禁用 Wayland优先使用 X11(xcb),否则回退为 offscreen
@ -12,13 +10,7 @@ int main(int argc, char *argv[]) {
qputenv("QT_QPA_PLATFORM", QByteArray("offscreen"));
}
// 关闭Qt绘图调试信息保留我们的应用日志
qputenv("QT_LOGGING_RULES", "qt.widgets.painting=false;qt.qpa.gl.debug=false;qt.qpa.gl.debug.warning=false;qt.*=false;*.debug=true");
QApplication app(argc, argv);
// 初始化配置与日志
AppConfig::instance().load();
LoggerInit::setupFromConfig();
app.setApplicationDisplayName("");
// 全局样式表(浅色主题)
app.setStyleSheet(

@ -33,9 +33,9 @@
#include <QFont>
#include <QResizeEvent>
#include <QMessageBox> // Added for QMessageBox
#include <QTimer>
#include "../utils/config.h"
#include "../utils/logger.h"
#include <QRadioButton> // Added for QRadioButton
#include <QDateTimeEdit> // Added for QDateTimeEdit
#include <QGroupBox> // Added for QGroupBox
/**
*
@ -48,22 +48,17 @@ MapPage::MapPage(QWidget* parent)
, heightCombo_(nullptr)
, downloadMapBtn_(nullptr)
, mapView_(nullptr)
// 初始化新按钮
, setThreatBtn_(nullptr)
, areaSearchBtn_(nullptr)
, pathPlanningBtn_(nullptr)
// 初始化基字体大小(如果需要从样式获取,可以调整)
, baseFontSize_(10)
// 初始化新组件
, coordInput_(nullptr)
, locateBtn_(nullptr)
, searchMapBtn_(nullptr)
// 初始化定位对话框
, threatDialog_(nullptr)
, searchDialog_(nullptr)
, planningDialog_(nullptr)
, locateDialog_(nullptr)
, baseFontSize_(10)
{
setupUI();
@ -130,20 +125,7 @@ void MapPage::setupMapArea() {
// 添加加载完成调试
connect(mapView_, &QWebEngineView::loadFinished, this, [this](bool ok) {
if (ok) {
qDebug() << "✓ 地图 HTML 加载成功";
qDebug() << "✓ 开始启用地理位置权限...";
enableGeolocation();
// 延迟2秒后检查地图状态
QTimer::singleShot(2000, this, [this]() {
qDebug() << "🔍 检查地图初始化状态...";
mapView_->page()->runJavaScript("console.log('地图对象存在:', typeof window.map !== 'undefined');");
mapView_->page()->runJavaScript("if(window.map) { console.log('地图中心:', window.map.getCenter()); console.log('地图缩放:', window.map.getZoom()); }");
});
} else {
qDebug() << "✗ 地图 HTML 加载失败";
}
qDebug() << (ok ? "地图 HTML 加载成功" : "地图 HTML 加载失败");
});
// 添加默认地图加载
@ -164,22 +146,13 @@ void MapPage::setupMapArea() {
<script src="https://webapi.amap.com/maps?v=2.0&key=492dc9daf4eae7cab678c0f3efed8198&callback=initMap" async defer></script>
<script>
function initMap() {
console.log("🗺️ 开始初始化地图...");
window.map = new AMap.Map('container', { // 使用window.map使其全局
layers: [new AMap.TileLayer.Satellite(), new AMap.TileLayer.RoadNet()],
zoom: 10,
center: [116.397428, 39.90923] // 默认北京中心
zoom: 10
});
console.log("🗺️ 地图初始化完成,默认中心: 北京 [116.397428, 39.90923]");
AMap.plugin(['AMap.ToolBar', 'AMap.Scale', 'AMap.Geolocation'], function() {
console.log("🔧 地图插件加载完成");
window.map.addControl(new AMap.ToolBar());
window.map.addControl(new AMap.Scale());
var geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000, // 增加超时到10秒
@ -193,50 +166,28 @@ void MapPage::setupMapArea() {
panToLocation: true,
zoomToAccuracy: true
});
console.log("📍 地理位置控件创建完成");
window.map.addControl(geolocation);
console.log("📍 开始获取当前位置...");
geolocation.getCurrentPosition(function(status, result) {
console.log("📍 定位回调触发 - 状态:", status);
console.log("📍 定位结果:", result);
if (status === 'complete' && result && result.position) {
if (status === 'complete') {
var lnglat = result.position;
console.log("✅ GPS定位成功! 坐标:", lnglat);
console.log("✅ 经度:", lnglat.lng, "纬度:", lnglat.lat);
window.map.setCenter(lnglat);
window.map.setZoom(15);
console.log("✅ 地图已移动到当前位置");
console.log("定位成功: " + lnglat);
} else {
console.log("❌ GPS定位失败:", result ? result.message : "未知错误");
console.log("🔄 尝试IP定位...");
geolocation.getCityInfo(function(status2, result2) {
console.log("🌐 IP定位回调 - 状态:", status2);
console.log("🌐 IP定位结果:", result2);
if (status2 === 'complete' && result2 && result2.bounds) {
var citybounds = result2.bounds;
console.log("✅ IP定位成功! 城市:", result2.city);
console.log("✅ 城市边界:", citybounds);
console.log("定位失败: " + result.message + " - 尝试 IP 定位");
geolocation.getCityInfo(function(status, result) {
if (status === 'complete') {
var citybounds = result.bounds;
window.map.setBounds(citybounds);
console.log("✅ 地图已移动到城市区域");
console.log("IP 定位成功: " + result.city);
} else {
console.log("❌ IP定位失败:", result2 ? result2.message : "未知错误");
console.log("🏠 回退到默认中心: 北京");
console.log("IP 定位失败: " + result.message);
window.map.setCenter([116.397428, 39.90923]);
window.map.setZoom(10);
}
});
}
});
// 添加持续监听位置变化
console.log("👂 开始监听位置变化...");
geolocation.watchPosition();
});
}
@ -342,35 +293,11 @@ void MapPage::resizeEvent(QResizeEvent *event) {
// 添加enableGeolocation实现
void MapPage::enableGeolocation() {
if (mapView_) {
qDebug() << "🔐 开始配置WebEngine地理位置权限...";
QWebEngineSettings* settings = mapView_->settings();
settings->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
settings->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
qDebug() << "🔐 JavaScript已启用:" << settings->testAttribute(QWebEngineSettings::JavascriptEnabled);
qDebug() << "🔐 本地存储已启用:" << settings->testAttribute(QWebEngineSettings::LocalStorageEnabled);
// 启用geolocation
QUrl amapUrl("https://webapi.amap.com/");
qDebug() << "🔐 为URL设置地理位置权限:" << amapUrl.toString();
mapView_->page()->setFeaturePermission(amapUrl, QWebEnginePage::Geolocation, QWebEnginePage::PermissionGrantedByUser);
qDebug() << "🔐 地理位置权限设置完成";
// 添加权限状态监听
connect(mapView_->page(), &QWebEnginePage::featurePermissionRequested, this, [this](const QUrl& url, QWebEnginePage::Feature feature) {
qDebug() << "🔐 权限请求 - URL:" << url.toString() << "功能:" << feature;
if (feature == QWebEnginePage::Geolocation) {
qDebug() << "🔐 自动授权地理位置权限";
mapView_->page()->setFeaturePermission(url, feature, QWebEnginePage::PermissionGrantedByUser);
}
});
qDebug() << "🔐 地理位置权限配置完成";
} else {
qDebug() << "❌ mapView_ 为空,无法配置地理位置权限";
mapView_->page()->setFeaturePermission(QUrl("https://webapi.amap.com/"), QWebEnginePage::Geolocation, QWebEnginePage::PermissionGrantedByUser);
}
}
@ -380,68 +307,509 @@ void MapPage::enableGeolocation() {
// 实现成员函数
ThreatAreaDialog::ThreatAreaDialog(QWidget* parent) : QDialog(parent) {
setWindowTitle("设置威胁区域");
setMinimumSize(600, 500);
auto* layout = new QVBoxLayout(this);
layout->setSpacing(15);
layout->setContentsMargins(20, 20, 20, 20);
// 顶部网格布局优化为QGridLayout更工整
auto* grid = new QGridLayout();
typeCombo_ = new QComboBox();
typeCombo_->addItems({"导弹威胁", "雷达干扰", "其他"});
grid->addWidget(new QLabel("类型:"), 0, 0);
grid->addWidget(typeCombo_, 0, 1);
// 威胁区域基本信息组
auto* basicGroup = new QGroupBox("基本信息");
basicGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 10px; }");
auto* basicLayout = new QGridLayout(basicGroup);
basicLayout->setSpacing(10);
// 威胁类型
basicLayout->addWidget(new QLabel("威胁类型:"), 0, 0);
typeCombo_ = new QComboBox();
typeCombo_->addItems({"导弹威胁", "雷达干扰", "防空火力", "禁飞区域", "其他"});
typeCombo_->setStyleSheet("QComboBox { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");
basicLayout->addWidget(typeCombo_, 0, 1);
// 威胁等级
basicLayout->addWidget(new QLabel("威胁等级:"), 0, 2);
auto* levelCombo = new QComboBox();
levelCombo->addItems({"", "", ""});
levelCombo->setStyleSheet("QComboBox { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");
basicLayout->addWidget(levelCombo, 0, 3);
// 时间设置组
auto* timeGroup = new QGroupBox("威胁区域存在时间");
timeGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 10px; }");
auto* timeLayout = new QVBoxLayout(timeGroup);
// 时间模式选择
auto* timeModeLayout = new QHBoxLayout();
auto* singleTimeMode = new QRadioButton("单时间点");
auto* timeRangeMode = new QRadioButton("时间段");
singleTimeMode->setChecked(true);
timeModeLayout->addWidget(singleTimeMode);
timeModeLayout->addWidget(timeRangeMode);
timeModeLayout->addStretch();
timeLayout->addLayout(timeModeLayout);
// 单时间点设置
auto* singleTimeWidget = new QWidget();
auto* singleTimeLayout = new QHBoxLayout(singleTimeWidget);
singleTimeLayout->setContentsMargins(0, 0, 0, 0);
singleTimeLayout->addWidget(new QLabel("时间:"));
timeEdit_ = new QDateTimeEdit(QDateTime::currentDateTime());
grid->addWidget(new QLabel("时间:"), 1, 0);
grid->addWidget(timeEdit_, 1, 1);
timeEdit_->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
timeEdit_->setCalendarPopup(true);
timeEdit_->setStyleSheet("QDateTimeEdit { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");
singleTimeLayout->addWidget(timeEdit_);
auto* setCurrentBtn = new QPushButton("设为当前时间");
setCurrentBtn->setStyleSheet("QPushButton { padding: 5px 10px; background: #e3f2fd; border: 1px solid #2196f3; border-radius: 4px; color: #1976d2; }");
singleTimeLayout->addWidget(setCurrentBtn);
singleTimeLayout->addStretch();
timeLayout->addWidget(singleTimeWidget);
// 时间模式选择器已定义用于UI切换
// 时间段设置
auto* timeRangeWidget = new QWidget();
auto* timeRangeLayout = new QVBoxLayout(timeRangeWidget);
timeRangeLayout->setContentsMargins(0, 0, 0, 0);
// 开始时间
auto* startTimeLayout = new QHBoxLayout();
startTimeLayout->addWidget(new QLabel("开始时间:"));
auto* startTimeEdit = new QDateTimeEdit(QDateTime::currentDateTime());
startTimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
startTimeEdit->setCalendarPopup(true);
startTimeEdit->setStyleSheet("QDateTimeEdit { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");
startTimeLayout->addWidget(startTimeEdit);
auto* setStartCurrentBtn = new QPushButton("设为当前");
setStartCurrentBtn->setStyleSheet("QPushButton { padding: 5px 10px; background: #e8f5e8; border: 1px solid #4caf50; border-radius: 4px; color: #2e7d32; }");
startTimeLayout->addWidget(setStartCurrentBtn);
startTimeLayout->addStretch();
timeRangeLayout->addLayout(startTimeLayout);
// 结束时间
auto* endTimeLayout = new QHBoxLayout();
endTimeLayout->addWidget(new QLabel("结束时间:"));
auto* endTimeEdit = new QDateTimeEdit(QDateTime::currentDateTime().addSecs(3600)); // 默认1小时后
endTimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
endTimeEdit->setCalendarPopup(true);
endTimeEdit->setStyleSheet("QDateTimeEdit { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");
endTimeLayout->addWidget(endTimeEdit);
auto* setEndCurrentBtn = new QPushButton("设为当前");
setEndCurrentBtn->setStyleSheet("QPushButton { padding: 5px 10px; background: #e8f5e8; border: 1px solid #4caf50; border-radius: 4px; color: #2e7d32; }");
endTimeLayout->addWidget(setEndCurrentBtn);
endTimeLayout->addStretch();
timeRangeLayout->addLayout(endTimeLayout);
// 持续时间显示和快速设置
auto* durationLayout = new QHBoxLayout();
auto* durationLabel = new QLabel("持续时间: 1小时");
durationLabel->setStyleSheet("QLabel { color: #666; font-weight: bold; }");
durationLayout->addWidget(durationLabel);
durationLayout->addStretch();
// 快速时间设置按钮
auto* quickTimeLayout = new QHBoxLayout();
auto* quick1Hour = new QPushButton("1小时");
auto* quick4Hour = new QPushButton("4小时");
auto* quick1Day = new QPushButton("1天");
auto* quick1Week = new QPushButton("1周");
QString quickBtnStyle = "QPushButton { padding: 4px 8px; background: #f5f5f5; border: 1px solid #ddd; border-radius: 3px; font-size: 11px; }";
quick1Hour->setStyleSheet(quickBtnStyle);
quick4Hour->setStyleSheet(quickBtnStyle);
quick1Day->setStyleSheet(quickBtnStyle);
quick1Week->setStyleSheet(quickBtnStyle);
quickTimeLayout->addWidget(quick1Hour);
quickTimeLayout->addWidget(quick4Hour);
quickTimeLayout->addWidget(quick1Day);
quickTimeLayout->addWidget(quick1Week);
quickTimeLayout->addStretch();
durationLayout->addLayout(quickTimeLayout);
timeRangeLayout->addLayout(durationLayout);
timeLayout->addWidget(timeRangeWidget);
timeRangeWidget->setVisible(false); // 默认隐藏
shapeCombo_ = new QComboBox();
shapeCombo_->addItems({"圆形", "多边形", "矩形"});
grid->addWidget(new QLabel("图形:"), 2, 0);
grid->addWidget(shapeCombo_, 2, 1);
// 连接时间模式切换信号
connect(singleTimeMode, &QRadioButton::toggled, [singleTimeWidget, timeRangeWidget](bool checked) {
singleTimeWidget->setVisible(checked);
timeRangeWidget->setVisible(!checked);
});
connect(timeRangeMode, &QRadioButton::toggled, [singleTimeWidget, timeRangeWidget](bool checked) {
singleTimeWidget->setVisible(!checked);
timeRangeWidget->setVisible(checked);
});
colorCombo_ = new QComboBox();
colorCombo_->addItems({"红色", "黄色", "蓝色"});
grid->addWidget(new QLabel("颜色:"), 3, 0);
grid->addWidget(colorCombo_, 3, 1);
// 连接单时间点信号
connect(setCurrentBtn, &QPushButton::clicked, [this]() {
timeEdit_->setDateTime(QDateTime::currentDateTime());
});
coordInput_ = new QLineEdit("输入坐标: lng1,lat1; lng2,lat2");
grid->addWidget(new QLabel("坐标:"), 4, 0);
grid->addWidget(coordInput_, 4, 1);
// 连接时间段信号
connect(setStartCurrentBtn, &QPushButton::clicked, [startTimeEdit]() {
startTimeEdit->setDateTime(QDateTime::currentDateTime());
});
connect(setEndCurrentBtn, &QPushButton::clicked, [endTimeEdit]() {
endTimeEdit->setDateTime(QDateTime::currentDateTime());
});
layout->addLayout(grid);
// 连接快速时间设置
connect(quick1Hour, &QPushButton::clicked, [startTimeEdit, endTimeEdit, durationLabel]() {
QDateTime start = QDateTime::currentDateTime();
QDateTime end = start.addSecs(3600);
startTimeEdit->setDateTime(start);
endTimeEdit->setDateTime(end);
durationLabel->setText("持续时间: 1小时");
});
connect(quick4Hour, &QPushButton::clicked, [startTimeEdit, endTimeEdit, durationLabel]() {
QDateTime start = QDateTime::currentDateTime();
QDateTime end = start.addSecs(4 * 3600);
startTimeEdit->setDateTime(start);
endTimeEdit->setDateTime(end);
durationLabel->setText("持续时间: 4小时");
});
connect(quick1Day, &QPushButton::clicked, [startTimeEdit, endTimeEdit, durationLabel]() {
QDateTime start = QDateTime::currentDateTime();
QDateTime end = start.addDays(1);
startTimeEdit->setDateTime(start);
endTimeEdit->setDateTime(end);
durationLabel->setText("持续时间: 1天");
});
connect(quick1Week, &QPushButton::clicked, [startTimeEdit, endTimeEdit, durationLabel]() {
QDateTime start = QDateTime::currentDateTime();
QDateTime end = start.addDays(7);
startTimeEdit->setDateTime(start);
endTimeEdit->setDateTime(end);
durationLabel->setText("持续时间: 1周");
});
// 连接时间变化信号,更新持续时间显示
connect(startTimeEdit, &QDateTimeEdit::dateTimeChanged, [startTimeEdit, endTimeEdit, durationLabel]() {
qint64 seconds = startTimeEdit->dateTime().secsTo(endTimeEdit->dateTime());
if (seconds > 0) {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
if (hours > 0) {
durationLabel->setText(QString("持续时间: %1小时%2分钟").arg(hours).arg(minutes));
} else {
durationLabel->setText(QString("持续时间: %1分钟").arg(minutes));
}
} else {
durationLabel->setText("持续时间: 无效时间段");
durationLabel->setStyleSheet("QLabel { color: #f44336; font-weight: bold; }");
}
});
connect(endTimeEdit, &QDateTimeEdit::dateTimeChanged, [startTimeEdit, endTimeEdit, durationLabel]() {
qint64 seconds = startTimeEdit->dateTime().secsTo(endTimeEdit->dateTime());
if (seconds > 0) {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
if (hours > 0) {
durationLabel->setText(QString("持续时间: %1小时%2分钟").arg(hours).arg(minutes));
} else {
durationLabel->setText(QString("持续时间: %1分钟").arg(minutes));
}
durationLabel->setStyleSheet("QLabel { color: #666; font-weight: bold; }");
} else {
durationLabel->setText("持续时间: 无效时间段");
durationLabel->setStyleSheet("QLabel { color: #f44336; font-weight: bold; }");
}
});
// 坐标设置组
auto* coordGroup = new QGroupBox("坐标设置");
coordGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 10px; }");
auto* coordLayout = new QVBoxLayout(coordGroup);
// 坐标输入方式选择
auto* coordMethodLayout = new QHBoxLayout();
auto* manualInput = new QRadioButton("手动输入");
auto* mapClick = new QRadioButton("地图点击选择");
manualInput->setChecked(true);
coordMethodLayout->addWidget(manualInput);
coordMethodLayout->addWidget(mapClick);
coordMethodLayout->addStretch();
coordLayout->addLayout(coordMethodLayout);
// 坐标输入区域
auto* coordInputLayout = new QHBoxLayout();
coordInputLayout->addWidget(new QLabel("坐标:"));
coordInput_ = new QLineEdit();
coordInput_->setPlaceholderText("格式: 经度,纬度 (例: 116.397,39.909)");
coordInput_->setStyleSheet("QLineEdit { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");
coordInputLayout->addWidget(coordInput_, 1);
auto* getCurrentPosBtn = new QPushButton("获取当前位置");
getCurrentPosBtn->setStyleSheet("QPushButton { padding: 5px 10px; background: #e3f2fd; border: 1px solid #2196f3; border-radius: 4px; color: #1976d2; }");
coordInputLayout->addWidget(getCurrentPosBtn);
coordLayout->addLayout(coordInputLayout);
// 坐标示例和帮助
auto* coordHelp = new QLabel("支持格式: 单点(116.397,39.909) | 多点(116.397,39.909;116.398,39.910) | 区域(116.397,39.909,116.398,39.910)");
coordHelp->setStyleSheet("QLabel { color: #666; font-size: 11px; }");
coordHelp->setWordWrap(true);
coordLayout->addWidget(coordHelp);
// 连接坐标选择信号
connect(manualInput, &QRadioButton::toggled, coordInput_, &QLineEdit::setEnabled);
connect(mapClick, &QRadioButton::toggled, [this](bool checked) {
coordInput_->setEnabled(!checked);
if (checked) {
coordInput_->setPlaceholderText("请在地图上点击选择坐标...");
} else {
coordInput_->setPlaceholderText("格式: 经度,纬度 (例: 116.397,39.909)");
}
});
connect(getCurrentPosBtn, &QPushButton::clicked, [this]() {
// 这里可以集成GPS定位功能
coordInput_->setText("116.397,39.909"); // 示例坐标
});
// 图形设置组
auto* shapeGroup = new QGroupBox("图形设置");
shapeGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 10px; }");
auto* shapeLayout = new QGridLayout(shapeGroup);
shapeLayout->setSpacing(10);
shapeLayout->addWidget(new QLabel("图形类型:"), 0, 0);
shapeCombo_ = new QComboBox();
shapeCombo_->addItems({"圆形", "多边形", "矩形", "椭圆"});
shapeCombo_->setStyleSheet("QComboBox { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");
shapeLayout->addWidget(shapeCombo_, 0, 1);
shapeLayout->addWidget(new QLabel("颜色:"), 0, 2);
colorCombo_ = new QComboBox();
colorCombo_->addItems({"红色", "黄色", "蓝色", "绿色", "紫色", "橙色"});
colorCombo_->setStyleSheet("QComboBox { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");
shapeLayout->addWidget(colorCombo_, 0, 3);
// 添加所有组到主布局
layout->addWidget(basicGroup);
layout->addWidget(timeGroup);
layout->addWidget(coordGroup);
layout->addWidget(shapeGroup);
// 威胁区域列表组
auto* listGroup = new QGroupBox("威胁区域列表");
listGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 10px; }");
auto* listLayout = new QVBoxLayout(listGroup);
// 表格
areaTable_ = new QTableWidget(0, 5); // 多一列颜色
areaTable_->setHorizontalHeaderLabels({"类型", "时间", "图形", "颜色", "操作"});
areaTable_ = new QTableWidget(0, 6);
areaTable_->setHorizontalHeaderLabels({"威胁类型", "威胁等级", "时间", "坐标", "图形", "操作"});
areaTable_->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
layout->addWidget(areaTable_);
areaTable_->setAlternatingRowColors(true);
areaTable_->setSelectionBehavior(QAbstractItemView::SelectRows);
areaTable_->setStyleSheet("QTableWidget { gridline-color: #e0e0e0; }");
listLayout->addWidget(areaTable_);
// 表格操作按钮
auto* tableButtons = new QHBoxLayout();
auto* addBtn = new QPushButton("添加威胁区域");
auto* editBtn = new QPushButton("编辑选中");
auto* deleteBtn = new QPushButton("删除选中");
auto* clearBtn = new QPushButton("清空列表");
// 设置按钮样式
addBtn->setStyleSheet("QPushButton { padding: 8px 16px; background: #4caf50; color: white; border: none; border-radius: 4px; font-weight: bold; }");
editBtn->setStyleSheet("QPushButton { padding: 8px 16px; background: #2196f3; color: white; border: none; border-radius: 4px; }");
deleteBtn->setStyleSheet("QPushButton { padding: 8px 16px; background: #f44336; color: white; border: none; border-radius: 4px; }");
clearBtn->setStyleSheet("QPushButton { padding: 8px 16px; background: #ff9800; color: white; border: none; border-radius: 4px; }");
tableButtons->addWidget(addBtn);
tableButtons->addWidget(editBtn);
tableButtons->addWidget(deleteBtn);
tableButtons->addWidget(clearBtn);
tableButtons->addStretch();
listLayout->addLayout(tableButtons);
layout->addWidget(listGroup);
// 威胁区域统计组
auto* statsGroup = new QGroupBox("威胁区域统计");
statsGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 10px; }");
auto* statsLayout = new QHBoxLayout(statsGroup);
auto* totalLabel = new QLabel("总计: 0个");
auto* currentLabel = new QLabel("当前威胁: 0个");
auto* futureLabel = new QLabel("未来威胁: 0个");
auto* historyLabel = new QLabel("历史威胁: 0个");
totalLabel->setStyleSheet("QLabel { color: #333; font-weight: bold; }");
currentLabel->setStyleSheet("QLabel { color: #f44336; font-weight: bold; }");
futureLabel->setStyleSheet("QLabel { color: #ff9800; font-weight: bold; }");
historyLabel->setStyleSheet("QLabel { color: #9e9e9e; font-weight: bold; }");
statsLayout->addWidget(totalLabel);
statsLayout->addWidget(currentLabel);
statsLayout->addWidget(futureLabel);
statsLayout->addWidget(historyLabel);
statsLayout->addStretch();
layout->addWidget(statsGroup);
// 按钮
// 底部按钮区域
auto* buttons = new QHBoxLayout();
auto* addBtn = new QPushButton("添加");
auto* saveBtn = new QPushButton("保存");
auto* previewBtn = new QPushButton("预览效果");
auto* saveBtn = new QPushButton("保存设置");
auto* cancelBtn = new QPushButton("取消");
// 设置底部按钮样式
previewBtn->setStyleSheet("QPushButton { padding: 10px 20px; background: #9c27b0; color: white; border: none; border-radius: 4px; font-weight: bold; }");
saveBtn->setStyleSheet("QPushButton { padding: 10px 20px; background: #4caf50; color: white; border: none; border-radius: 4px; font-weight: bold; }");
cancelBtn->setStyleSheet("QPushButton { padding: 10px 20px; background: #757575; color: white; border: none; border-radius: 4px; }");
buttons->addStretch();
buttons->addWidget(addBtn);
buttons->addWidget(previewBtn);
buttons->addWidget(saveBtn);
buttons->addWidget(cancelBtn);
buttons->addStretch();
layout->addLayout(buttons);
// 连接信号槽
connect(addBtn, &QPushButton::clicked, this, &ThreatAreaDialog::addArea);
connect(editBtn, &QPushButton::clicked, [this]() {
int row = areaTable_->currentRow();
if (row >= 0) {
// 编辑选中行
QMessageBox::information(this, "编辑", "编辑功能待实现");
} else {
QMessageBox::warning(this, "提示", "请先选择要编辑的行");
}
});
connect(deleteBtn, &QPushButton::clicked, [this]() {
int row = areaTable_->currentRow();
if (row >= 0) {
areaTable_->removeRow(row);
} else {
QMessageBox::warning(this, "提示", "请先选择要删除的行");
}
});
connect(clearBtn, &QPushButton::clicked, [this]() {
if (QMessageBox::question(this, "确认", "确定要清空所有威胁区域吗?") == QMessageBox::Yes) {
areaTable_->setRowCount(0);
}
});
connect(previewBtn, &QPushButton::clicked, [this]() {
QMessageBox::information(this, "预览", "威胁区域预览功能待实现");
});
connect(cancelBtn, &QPushButton::clicked, this, &QDialog::reject);
connect(saveBtn, &QPushButton::clicked, this, &QDialog::accept);
}
void ThreatAreaDialog::addArea() {
// 验证输入
if (coordInput_->text().isEmpty()) {
QMessageBox::warning(this, "输入错误", "请输入坐标信息!");
return;
}
// 验证时间设置
QDateTime currentTime = QDateTime::currentDateTime();
QDateTime threatTime = timeEdit_->dateTime();
// 检查时间是否合理(不能是过去时间,除非是历史记录)
if (threatTime < currentTime.addDays(-1)) {
int ret = QMessageBox::question(this, "时间确认",
"设置的威胁时间较早,是否确认添加?\n"
"这可能是历史威胁记录。",
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::No) {
return;
}
}
int row = areaTable_->rowCount();
areaTable_->insertRow(row);
// 威胁类型
areaTable_->setItem(row, 0, new QTableWidgetItem(typeCombo_->currentText()));
areaTable_->setItem(row, 1, new QTableWidgetItem(timeEdit_->dateTime().toString()));
areaTable_->setItem(row, 2, new QTableWidgetItem(shapeCombo_->currentText()));
areaTable_->setItem(row, 3, new QTableWidgetItem(colorCombo_->currentText()));
// 操作按钮 (示例)
QPushButton* editBtn = new QPushButton("编辑");
areaTable_->setCellWidget(row, 4, editBtn);
// 威胁等级 (默认为"中")
areaTable_->setItem(row, 1, new QTableWidgetItem(""));
// 时间信息 - 根据当前选择的时间模式来设置
QString timeInfo;
// 判断威胁状态
QString status;
if (threatTime > currentTime) {
status = "未来威胁";
} else if (threatTime >= currentTime.addDays(-1)) {
status = "当前威胁";
} else {
status = "历史威胁";
}
timeInfo = QString("%1: %2").arg(status).arg(threatTime.toString("yyyy-MM-dd hh:mm:ss"));
areaTable_->setItem(row, 2, new QTableWidgetItem(timeInfo));
// 根据威胁状态设置行颜色
QTableWidgetItem* timeItem = areaTable_->item(row, 2);
if (status == "未来威胁") {
timeItem->setBackground(QColor(255, 243, 224)); // 浅橙色
} else if (status == "当前威胁") {
timeItem->setBackground(QColor(255, 235, 238)); // 浅红色
} else {
timeItem->setBackground(QColor(245, 245, 245)); // 浅灰色
}
// 坐标
areaTable_->setItem(row, 3, new QTableWidgetItem(coordInput_->text()));
// 图形信息
QString shapeInfo = QString("%1 (%2)").arg(shapeCombo_->currentText()).arg(colorCombo_->currentText());
areaTable_->setItem(row, 4, new QTableWidgetItem(shapeInfo));
// 操作按钮
auto* actionWidget = new QWidget();
auto* actionLayout = new QHBoxLayout(actionWidget);
actionLayout->setContentsMargins(2, 2, 2, 2);
actionLayout->setSpacing(2);
auto* editBtn = new QPushButton("编辑");
auto* deleteBtn = new QPushButton("删除");
editBtn->setStyleSheet("QPushButton { padding: 2px 6px; background: #2196f3; color: white; border: none; border-radius: 2px; font-size: 10px; }");
deleteBtn->setStyleSheet("QPushButton { padding: 2px 6px; background: #f44336; color: white; border: none; border-radius: 2px; font-size: 10px; }");
actionLayout->addWidget(editBtn);
actionLayout->addWidget(deleteBtn);
actionLayout->addStretch();
areaTable_->setCellWidget(row, 5, actionWidget);
// 连接操作按钮
connect(editBtn, &QPushButton::clicked, [this, row]() {
// 编辑功能
QMessageBox::information(this, "编辑", QString("编辑第 %1 行").arg(row + 1));
});
connect(deleteBtn, &QPushButton::clicked, [this, row]() {
if (QMessageBox::question(this, "确认删除", "确定要删除这个威胁区域吗?") == QMessageBox::Yes) {
areaTable_->removeRow(row);
}
});
// 清空输入框
coordInput_->clear();
// 显示成功消息
QMessageBox::information(this, "添加成功", "威胁区域已添加到列表中!");
// 更新统计信息
updateThreatStats();
}
// 更新威胁区域统计信息
void ThreatAreaDialog::updateThreatStats() {
// 这里需要访问统计标签,但由于它们是局部变量,我们暂时跳过
// 在实际应用中,应该将统计标签作为成员变量存储
qDebug() << "威胁区域统计已更新";
}
void ThreatAreaDialog::resizeEvent(QResizeEvent *event) {

@ -21,6 +21,8 @@
#include <QFormLayout>
#include <QLineEdit>
#include <QMessageBox> // 添加以支持输入验证警告
#include <QGroupBox> // 添加QGroupBox支持
#include <QRadioButton> // 添加QRadioButton支持
class CustomWebEnginePage : public QWebEnginePage {
Q_OBJECT
@ -28,14 +30,7 @@ public:
CustomWebEnginePage(QObject* parent = nullptr) : QWebEnginePage(parent) {}
protected:
void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message, int lineNumber, const QString& sourceID) override {
QString levelStr;
switch(level) {
case QWebEnginePage::InfoMessageLevel: levelStr = "INFO"; break;
case QWebEnginePage::WarningMessageLevel: levelStr = "WARN"; break;
case QWebEnginePage::ErrorMessageLevel: levelStr = "ERROR"; break;
default: levelStr = "UNKNOWN"; break;
}
qDebug() << QString("🌐 JS [%1]: %2 (行:%3, 来源:%4)").arg(levelStr).arg(message).arg(lineNumber).arg(sourceID);
qDebug() << "JS 消息 (级别:" << level << "):" << message << " (行:" << lineNumber << ", 来源:" << sourceID << ")";
}
};
@ -51,6 +46,7 @@ public:
ThreatAreaDialog(QWidget* parent = nullptr);
private slots:
void addArea();
void updateThreatStats();
protected:
void resizeEvent(QResizeEvent *event) override;
private:
@ -150,7 +146,6 @@ private:
// 添加新方法
void enableGeolocation();
void startQtLocation(); // Qt 原生定位方法
// 创建UI组件的辅助方法
QWidget* createMapControlsWidget();

Loading…
Cancel
Save