460 lines
14 KiB
C++
460 lines
14 KiB
C++
#include "monitorDisplaySettingDlg.h"
|
||
#include "ui_monitorDisplaySettingDlg.h"
|
||
#include "monitorPanel.h"
|
||
#include <QColorDialog>
|
||
#include <QSvgRenderer>
|
||
#include <QPainter>
|
||
#include <QMessageBox>
|
||
#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);
|
||
|
||
connect(ui->cb_type,&QComboBox::currentTextChanged,this,&MonitorDisplaySettingDlg::onDeviceComboBoxChanged);
|
||
connect(ui->cb_state,&QComboBox::currentTextChanged,this,&MonitorDisplaySettingDlg::onStateComboBoxChanged);
|
||
|
||
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());
|
||
// 触发预览更新
|
||
|
||
if(_curType.isValid() && _curState.isValid()){
|
||
_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();
|
||
ui->cb_state->blockSignals(true);
|
||
ui->cb_type->blockSignals(true);
|
||
ui->cb_type->clear();
|
||
ui->cb_state->clear();
|
||
ui->cb_state->blockSignals(false);
|
||
ui->cb_type->blockSignals(false);
|
||
|
||
for(auto iter = _tempSetting.begin();iter != _tempSetting.end();++iter){
|
||
ui->cb_type->addItem(iter.key().sName,iter.key().sTag);
|
||
}
|
||
|
||
// 2. 如果有设备,选择第一个设备
|
||
// if (ui->cb_type->count() > 0) {
|
||
// QString firstDevice = ui->cb_type->itemText(0);
|
||
// onDeviceComboBoxChanged(firstDevice);
|
||
// }
|
||
}
|
||
|
||
void MonitorDisplaySettingDlg::onSaveClicked()
|
||
{
|
||
if (validateCurrentDeviceState()) {
|
||
if (!saveCurrentSettingsWithIcon()) {
|
||
QMessageBox::warning(this, "Warning", "Failed to save current settings");
|
||
}
|
||
}
|
||
_parent->getModelController()->getMonitorDisplaySetting() = _tempSetting;
|
||
_parent->getModelController()->updateMonitorDisplay();
|
||
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()
|
||
{
|
||
// 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()){
|
||
_tempSetting[_curType][_curState].bytPicture = selectedSVG;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void MonitorDisplaySettingDlg::onDeviceComboBoxChanged(const QString& str)
|
||
{
|
||
if (str.isEmpty()) {
|
||
return;
|
||
}
|
||
|
||
// 1. 保存当前设备的非图标数据
|
||
if (_curType.isValid() && _curState.isValid()) {
|
||
saveCurrentSettingsWithIcon();
|
||
clearIconDisplay();
|
||
}
|
||
|
||
// 2. 构建新设备
|
||
monitorItemTypeStruct newType;
|
||
newType.sTag = ui->cb_type->currentData().toString();
|
||
newType.sName = str;
|
||
|
||
if (!newType.isValid()) {
|
||
return;
|
||
}
|
||
|
||
// 3. 检查新设备是否存在
|
||
if (!_tempSetting.contains(newType)) {
|
||
_curType = newType;
|
||
_curState = monitorItemStateStruct();
|
||
clearStateDisplay();
|
||
return;
|
||
}
|
||
|
||
// 4. 清空状态下拉框
|
||
ui->cb_state->blockSignals(true);
|
||
ui->cb_state->clear();
|
||
ui->cb_state->blockSignals(false);
|
||
|
||
// 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));
|
||
}
|
||
|
||
// 6. 更新当前设备
|
||
_curType = newType;
|
||
|
||
// 7. 加载第一个状态
|
||
if (ui->cb_state->count() > 0) {
|
||
loadFirstStateSafely();
|
||
} else {
|
||
_curState = monitorItemStateStruct();
|
||
clearStateDisplay();
|
||
}
|
||
}
|
||
|
||
void MonitorDisplaySettingDlg::onStateComboBoxChanged(const QString& str)
|
||
{
|
||
/*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);
|
||
}
|
||
}*/
|
||
|
||
if (str.isEmpty() || str == _curState.sState) {
|
||
return;
|
||
}
|
||
|
||
// 1. 保存当前状态的完整数据(包括图标)
|
||
if (_curType.isValid() && _curState.isValid()) {
|
||
saveCurrentSettingsWithIcon(); // 保存完整数据
|
||
}
|
||
|
||
// 2. 获取新状态
|
||
QVariant stateData = ui->cb_state->currentData();
|
||
if (!stateData.isValid()) {
|
||
return;
|
||
}
|
||
|
||
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()) {
|
||
return;
|
||
}
|
||
|
||
if (!_tempSetting.contains(type) || !_tempSetting[type].contains(state)) {
|
||
return;
|
||
}
|
||
|
||
auto& setting = _tempSetting[type][state];
|
||
|
||
// 修复:正确设置_curMeta和_curModel
|
||
_curMeta = setting.sMeta;
|
||
_curModel = setting.sModel;
|
||
|
||
// 更新界面控件
|
||
ui->checkBox_custom->setChecked(setting.bEnable);
|
||
|
||
// 设置控件可用性
|
||
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);
|
||
|
||
// 更新颜色
|
||
_curColor = QColor(setting.sColor);
|
||
if (!_curColor.isValid()) {
|
||
_curColor = Qt::black;
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
|
||
bool MonitorDisplaySettingDlg::saveCurrentSettingsWithIcon()
|
||
{
|
||
if (!_curType.isValid() || !_curState.isValid()) {
|
||
return false;
|
||
}
|
||
|
||
if (!_tempSetting.contains(_curType) || !_tempSetting[_curType].contains(_curState)) {
|
||
return false;
|
||
}
|
||
|
||
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;
|
||
QPixmap pixmap(16, 16);
|
||
pixmap.fill(_curColor);
|
||
ui->btn_selectColor->setIcon(QIcon(pixmap));
|
||
ui->btn_selectColor->setText(_curColor.name());
|
||
|
||
// 清空当前状态
|
||
_curState = monitorItemStateStruct();
|
||
_curModel.clear();
|
||
_curMeta.clear();
|
||
}
|
||
|
||
void MonitorDisplaySettingDlg::updateIconDisplay(const QByteArray& svgData)
|
||
{
|
||
if (svgData.isEmpty()) {
|
||
clearIconDisplay();
|
||
return;
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
bool MonitorDisplaySettingDlg::validateSvgData(const QByteArray& svgData) const
|
||
{
|
||
if (svgData.isEmpty()) {
|
||
return false;
|
||
}
|
||
|
||
QSvgRenderer renderer(svgData);
|
||
return renderer.isValid();
|
||
}
|
||
|
||
void MonitorDisplaySettingDlg::loadFirstStateSafely()
|
||
{
|
||
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);
|
||
}
|
||
|
||
bool MonitorDisplaySettingDlg::validateCurrentDeviceState() const
|
||
{
|
||
return _curType.isValid() && _curState.isValid() &&
|
||
_tempSetting.contains(_curType) &&
|
||
_tempSetting[_curType].contains(_curState);
|
||
}
|
||
|