DiagramDesigner/diagramCavas/source/monitorDisplaySettingDlg.cpp

460 lines
14 KiB
C++
Raw Permalink Normal View History

2025-12-01 20:29:36 +08:00
#include "monitorDisplaySettingDlg.h"
#include "ui_monitorDisplaySettingDlg.h"
#include "monitorPanel.h"
#include <QColorDialog>
#include <QSvgRenderer>
#include <QPainter>
2025-12-03 18:27:49 +08:00
#include <QMessageBox>
2025-12-01 20:29:36 +08:00
#include "projectModelManager.h"
#include "projectIconSelectionDlg.h"
MonitorDisplaySettingDlg::MonitorDisplaySettingDlg(QWidget *parent)
: QDialog(parent)
, ui(new Ui::monitorDisplaySettingDlg)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());
_parent = dynamic_cast<MonitorPanel*>(parent);
initial();
}
MonitorDisplaySettingDlg::~MonitorDisplaySettingDlg()
{
delete ui;
}
void MonitorDisplaySettingDlg::initial()
{
connect(ui->btn_save,&QPushButton::clicked,this,&MonitorDisplaySettingDlg::onSaveClicked);
connect(ui->btn_cancel,&QPushButton::clicked,this,&MonitorDisplaySettingDlg::onCancelClicked);
2025-12-03 18:27:49 +08:00
connect(ui->cb_type,&QComboBox::currentTextChanged,this,&MonitorDisplaySettingDlg::onDeviceComboBoxChanged);
connect(ui->cb_state,&QComboBox::currentTextChanged,this,&MonitorDisplaySettingDlg::onStateComboBoxChanged);
2025-12-01 20:29:36 +08:00
connect(ui->btn_selectColor, &QPushButton::clicked, this, [this]() {
QColor newColor = QColorDialog::getColor(_curColor, this, "选择颜色");
if (newColor.isValid()) { // 用户点击了OK
_curColor = newColor;
// 更新按钮显示
QPixmap pixmap(16, 16);
pixmap.fill(newColor);
ui->btn_selectColor->setIcon(QIcon(pixmap));
ui->btn_selectColor->setText(newColor.name());
// 触发预览更新
2025-12-03 18:27:49 +08:00
if(_curType.isValid() && _curState.isValid()){
2025-12-01 20:29:36 +08:00
_tempSetting[_curType][_curState].sColor = _curColor.name();
ui->content->setColors(_curColor);
}
}
});
connect(ui->checkBox_custom, &QCheckBox::toggled, this,&MonitorDisplaySettingDlg::onCheckboxToggled);
connect(ui->btn_selectIcon, &QPushButton::clicked, this,&MonitorDisplaySettingDlg::onIconSelectClicked);
}
void MonitorDisplaySettingDlg::showDlg()
{
show();
_tempSetting = _parent->getModelController()->getMonitorDisplaySetting();
2025-12-03 18:27:49 +08:00
ui->cb_state->blockSignals(true);
ui->cb_type->blockSignals(true);
2025-12-01 20:29:36 +08:00
ui->cb_type->clear();
ui->cb_state->clear();
2025-12-03 18:27:49 +08:00
ui->cb_state->blockSignals(false);
ui->cb_type->blockSignals(false);
2025-12-01 20:29:36 +08:00
for(auto iter = _tempSetting.begin();iter != _tempSetting.end();++iter){
ui->cb_type->addItem(iter.key().sName,iter.key().sTag);
}
2025-12-03 18:27:49 +08:00
// 2. 如果有设备,选择第一个设备
// if (ui->cb_type->count() > 0) {
// QString firstDevice = ui->cb_type->itemText(0);
// onDeviceComboBoxChanged(firstDevice);
// }
2025-12-01 20:29:36 +08:00
}
void MonitorDisplaySettingDlg::onSaveClicked()
{
2025-12-03 18:27:49 +08:00
if (validateCurrentDeviceState()) {
if (!saveCurrentSettingsWithIcon()) {
QMessageBox::warning(this, "Warning", "Failed to save current settings");
}
}
2025-12-01 20:29:36 +08:00
_parent->getModelController()->getMonitorDisplaySetting() = _tempSetting;
2025-12-03 18:27:49 +08:00
_parent->getModelController()->updateMonitorDisplay();
2025-12-01 20:29:36 +08:00
hide();
}
void MonitorDisplaySettingDlg::onCancelClicked()
{
hide();
}
void MonitorDisplaySettingDlg::onCheckboxToggled(bool val)
{
if(val){
ui->btn_selectColor->setEnabled(true);
ui->btn_selectIcon->setEnabled(true);
ui->sb_width->setEnabled(true);
ui->sb_height->setEnabled(true);
ui->cb_effect->setEnabled(true);
}
else{
ui->btn_selectColor->setEnabled(false);
ui->btn_selectIcon->setEnabled(false);
ui->sb_width->setEnabled(false);
ui->sb_height->setEnabled(false);
ui->cb_effect->setEnabled(false);
}
}
void MonitorDisplaySettingDlg::onIconSelectClicked()
{
2025-12-03 18:27:49 +08:00
// if(!_curMeta.isEmpty() && !_curModel.isEmpty()){
// auto mapAllSvg = ProjectModelManager::instance().getData()[_curMeta][_curModel].modelSetting.mapSvg;
// ProjectIconSelectionDlg dialog(mapAllSvg, this);
// if (dialog.exec() == QDialog::Accepted) {
// QByteArray selectedSVG = dialog.selectedSVG();
// if (!selectedSVG.isEmpty()) {
// QSvgRenderer renderer(selectedSVG);
// QPixmap pixmap(32, 32);
// pixmap.fill(Qt::transparent);
// QPainter painter(&pixmap);
// renderer.render(&painter);
// ui->btn_selectIcon->setIcon(QIcon(pixmap));
// if (validateSvgData(selectedSVG)) {
// updateIconDisplay(selectedSVG);
// if(_curType.isValid() && _curState.isValid()){
// _tempSetting[_curType][_curState].bytPicture = selectedSVG;
// }
// }
// }
// }
// }
if(_curMeta.isEmpty() || _curModel.isEmpty()){
return;
}
auto mapAllSvg = ProjectModelManager::instance().getData()[_curMeta][_curModel].modelSetting.mapSvg;
ProjectIconSelectionDlg dialog(mapAllSvg, this);
if (dialog.exec() == QDialog::Accepted) {
QByteArray selectedSVG = dialog.selectedSVG();
if (!selectedSVG.isEmpty()) {
QSvgRenderer renderer(selectedSVG);
QPixmap pixmap(32, 32);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
renderer.render(&painter);
ui->btn_selectIcon->setIcon(QIcon(pixmap));
if (validateSvgData(selectedSVG)) {
updateIconDisplay(selectedSVG);
// 立即更新当前设备状态的图标数据
if(_curType.isValid() && _curState.isValid()){
2025-12-01 20:29:36 +08:00
_tempSetting[_curType][_curState].bytPicture = selectedSVG;
}
}
}
}
}
2025-12-03 18:27:49 +08:00
void MonitorDisplaySettingDlg::onDeviceComboBoxChanged(const QString& str)
2025-12-01 20:29:36 +08:00
{
2025-12-03 18:27:49 +08:00
if (str.isEmpty()) {
return;
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
// 1. 保存当前设备的非图标数据
if (_curType.isValid() && _curState.isValid()) {
saveCurrentSettingsWithIcon();
clearIconDisplay();
}
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
// 2. 构建新设备
monitorItemTypeStruct newType;
newType.sTag = ui->cb_type->currentData().toString();
newType.sName = str;
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
if (!newType.isValid()) {
return;
}
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
// 3. 检查新设备是否存在
if (!_tempSetting.contains(newType)) {
_curType = newType;
_curState = monitorItemStateStruct();
2025-12-01 20:29:36 +08:00
clearStateDisplay();
return;
}
2025-12-03 18:27:49 +08:00
// 4. 清空状态下拉框
ui->cb_state->blockSignals(true);
ui->cb_state->clear();
ui->cb_state->blockSignals(false);
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
// 5. 填充新设备的状态
auto& stateMap = _tempSetting[newType];
for (auto iter = stateMap.begin(); iter != stateMap.end(); ++iter) {
ui->cb_state->addItem(iter.key().sState, static_cast<int>(iter.key().eState));
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
// 6. 更新当前设备
_curType = newType;
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
// 7. 加载第一个状态
if (ui->cb_state->count() > 0) {
loadFirstStateSafely();
} else {
_curState = monitorItemStateStruct();
clearStateDisplay();
2025-12-01 20:29:36 +08:00
}
}
2025-12-03 18:27:49 +08:00
void MonitorDisplaySettingDlg::onStateComboBoxChanged(const QString& str)
2025-12-01 20:29:36 +08:00
{
/*if(_tempSetting.contains(_curType)){
monitorItemStateStruct keyState;
keyState.sState = str;
keyState.eState = monitorItemState(ui->cb_state->currentData().toInt());
if(_tempSetting[_curType].contains(keyState)){
saveSetting(_curType,_curState);
auto info = _tempSetting[_curType][keyState];
ui->checkBox_custom->setChecked(info.bEnable);
if(_curModel != info.sModel){
_curModel = info.sModel;
}
_curState = keyState;
loadSetting(_curType,_curState);
}
}*/
2025-12-03 18:27:49 +08:00
if (str.isEmpty() || str == _curState.sState) {
2025-12-01 20:29:36 +08:00
return;
}
2025-12-03 18:27:49 +08:00
// 1. 保存当前状态的完整数据(包括图标)
if (_curType.isValid() && _curState.isValid()) {
saveCurrentSettingsWithIcon(); // 保存完整数据
}
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
// 2. 获取新状态
2025-12-01 20:29:36 +08:00
QVariant stateData = ui->cb_state->currentData();
if (!stateData.isValid()) {
return;
}
2025-12-03 18:27:49 +08:00
monitorItemStateStruct newState;
newState.sState = str;
newState.eState = static_cast<monitorItemState>(stateData.toInt());
// 3. 更新当前状态
_curState = newState;
// 4. 加载新状态设置
loadSetting(_curType, _curState);
}
void MonitorDisplaySettingDlg::loadSetting(monitorItemTypeStruct type, monitorItemStateStruct state)
{
if (!type.isValid() || !state.isValid()) {
2025-12-01 20:29:36 +08:00
return;
}
2025-12-03 18:27:49 +08:00
if (!_tempSetting.contains(type) || !_tempSetting[type].contains(state)) {
2025-12-01 20:29:36 +08:00
return;
}
2025-12-03 18:27:49 +08:00
auto& setting = _tempSetting[type][state];
// 修复正确设置_curMeta和_curModel
_curMeta = setting.sMeta;
_curModel = setting.sModel;
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
// 更新界面控件
ui->checkBox_custom->setChecked(setting.bEnable);
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
// 设置控件可用性
bool enabled = setting.bEnable;
ui->btn_selectColor->setEnabled(enabled);
ui->btn_selectIcon->setEnabled(enabled);
ui->sb_width->setEnabled(enabled);
ui->sb_height->setEnabled(enabled);
ui->cb_effect->setEnabled(enabled);
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
// 更新颜色
_curColor = QColor(setting.sColor);
if (!_curColor.isValid()) {
_curColor = Qt::black;
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
QPixmap colorPixmap(16, 16);
colorPixmap.fill(_curColor);
ui->btn_selectColor->setIcon(QIcon(colorPixmap));
ui->btn_selectColor->setText(_curColor.name());
// 更新图标
if (!setting.bytPicture.isEmpty()) {
QSvgRenderer renderer(setting.bytPicture);
if (renderer.isValid()) {
QPixmap iconPixmap(32, 32);
iconPixmap.fill(Qt::transparent);
QPainter painter(&iconPixmap);
renderer.render(&painter);
ui->btn_selectIcon->setIcon(QIcon(iconPixmap));
ui->content->setSvgFile(setting.bytPicture);
}
} else {
ui->btn_selectIcon->setIcon(QIcon());
ui->content->clearSvg();
}
// 更新尺寸
ui->sb_width->setValue(setting.nWidth);
ui->sb_height->setValue(setting.nHeight);
// 更新内容显示
ui->content->setCurType(type.sTag);
ui->content->setColors(_curColor);
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
bool MonitorDisplaySettingDlg::saveCurrentSettingsWithIcon()
2025-12-01 20:29:36 +08:00
{
2025-12-03 18:27:49 +08:00
if (!_curType.isValid() || !_curState.isValid()) {
return false;
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
if (!_tempSetting.contains(_curType) || !_tempSetting[_curType].contains(_curState)) {
return false;
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
auto& setting = _tempSetting[_curType][_curState];
int oldSvgSize = setting.bytPicture.size();
// 保存所有数据,包括图标
setting.bEnable = ui->checkBox_custom->isChecked();
setting.sColor = _curColor.name(QColor::HexArgb);
// 图标数据:使用当前显示的图标
QByteArray currentSvg = ui->content->getCurSvg();
if (!currentSvg.isEmpty() && validateSvgData(currentSvg)) {
setting.bytPicture = currentSvg;
}
// 如果图标数据无效,保持原数据不变
setting.nWidth = ui->sb_width->value();
setting.nHeight = ui->sb_height->value();
// 修复_curMeta和_curModel
if (!_curMeta.isEmpty()) {
setting.sMeta = _curMeta;
}
if (!_curModel.isEmpty()) {
setting.sModel = _curModel;
}
return true;
}
void MonitorDisplaySettingDlg::clearStateDisplay()
{
ui->checkBox_custom->setChecked(false);
ui->btn_selectColor->setEnabled(false);
ui->btn_selectIcon->setEnabled(false);
ui->sb_width->setEnabled(false);
ui->sb_height->setEnabled(false);
ui->cb_effect->setEnabled(false);
// 清空图标显示
ui->btn_selectIcon->setIcon(QIcon());
ui->content->clearSvg();
// 重置颜色显示
_curColor = Qt::black;
2025-12-01 20:29:36 +08:00
QPixmap pixmap(16, 16);
pixmap.fill(_curColor);
ui->btn_selectColor->setIcon(QIcon(pixmap));
ui->btn_selectColor->setText(_curColor.name());
2025-12-03 18:27:49 +08:00
// 清空当前状态
_curState = monitorItemStateStruct();
_curModel.clear();
_curMeta.clear();
}
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
void MonitorDisplaySettingDlg::updateIconDisplay(const QByteArray& svgData)
{
if (svgData.isEmpty()) {
clearIconDisplay();
return;
}
2025-12-01 20:29:36 +08:00
2025-12-03 18:27:49 +08:00
if (!validateSvgData(svgData)) {
clearIconDisplay();
return;
}
try {
// 更新按钮图标
QSvgRenderer renderer(svgData);
QPixmap iconPixmap(32, 32);
iconPixmap.fill(Qt::transparent);
QPainter painter(&iconPixmap);
renderer.render(&painter);
ui->btn_selectIcon->setIcon(QIcon(iconPixmap));
// 更新内容显示
ui->content->setSvgFile(svgData);
ui->content->setColors(_curColor);
} catch (...) {
clearIconDisplay();
}
}
void MonitorDisplaySettingDlg::clearIconDisplay()
{
ui->btn_selectIcon->setIcon(QIcon());
ui->content->clearSvg();
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
bool MonitorDisplaySettingDlg::validateSvgData(const QByteArray& svgData) const
2025-12-01 20:29:36 +08:00
{
2025-12-03 18:27:49 +08:00
if (svgData.isEmpty()) {
return false;
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
QSvgRenderer renderer(svgData);
return renderer.isValid();
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
void MonitorDisplaySettingDlg::loadFirstStateSafely()
2025-12-01 20:29:36 +08:00
{
2025-12-03 18:27:49 +08:00
QString firstStateName = ui->cb_state->itemText(0);
QVariant firstStateData = ui->cb_state->itemData(0);
monitorItemStateStruct firstState;
firstState.sState = firstStateName;
firstState.eState = static_cast<monitorItemState>(firstStateData.toInt());
_curState = firstState;
ui->cb_state->setCurrentIndex(0);
loadSetting(_curType, _curState);
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00
bool MonitorDisplaySettingDlg::validateCurrentDeviceState() const
2025-12-01 20:29:36 +08:00
{
2025-12-03 18:27:49 +08:00
return _curType.isValid() && _curState.isValid() &&
_tempSetting.contains(_curType) &&
_tempSetting[_curType].contains(_curState);
2025-12-01 20:29:36 +08:00
}
2025-12-03 18:27:49 +08:00