DYTSrouce/src/ui/Panel/LightPanel.cpp
2025-11-02 11:36:10 +08:00

225 lines
5.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ui/Panel/LightPanel.h"
#include "ui/DockWidget.h"
#include "ui/DockTitleBar.h"
#include "common/SpdLogger.h"
#include <QHBoxLayout>
#include <QFileInfo>
#include <QMessageBox>
#include <QVariant>
LightPanel::LightPanel(int index, const QString& filePath, QWidget* parent)
: DataPanel(index, FileEntryType::Light, filePath, parent)
{
LOG_INFO("Created LightPanel {} for file: {}", index, filePath.toStdString());
}
LightPanel::LightPanel(int index, std::shared_ptr<FileEntryLight> fileEntry, QWidget* parent)
: DataPanel(index, fileEntry, parent)
{
if (fileEntry) {
LOG_INFO("Created LightPanel {} for chart: {}", index, fileEntry->GetName().toStdString());
// Override the title with chart name
title_ = QString("Light Panel %1 - %2").arg(index).arg(fileEntry->GetName());
}
else {
LOG_WARN("Created LightPanel {} with null chart data", index);
}
}
LightPanel::~LightPanel()
{
LOG_INFO("Destroyed LightPanel {}", GetIndex());
}
void LightPanel::RefreshPanel()
{
// Implement curve-specific refresh logic here
DataPanel::RefreshPanel();
if (auto fileEntry = fileEntry_->AsLight()) {
OnDataPanelUpdated(fileEntry);
}
LOG_INFO("Refreshed TablePanel {}", GetIndex());
}
void LightPanel::InitUI()
{
QGridLayout* pMainLyt = new QGridLayout(this);
pMainLyt->setContentsMargins(5, 0, 5, 0);
setLayout(pMainLyt);
}
QString LightPanel::GetTypeDisplayName() const
{
return "Light";
}
void LightPanel::OnDataPanelUpdated(FileEntryLight* fileEntry)
{
QString strName = fileEntry->GetName();
updateTitle(strName);
FileEntryLight::ColorProperties propChart = fileEntry->GetColorProperties();
QString openColor = QColorToString(propChart.openColor);
QString closeColor = QColorToString(propChart.closeColor);
updateLampColor(openColor, closeColor);
QString strFile = fileEntry->GetPath() + "/" + fileEntry->GetFileName();
FileEntryLight::LightRowProperties listCurve = fileEntry->GetLightProperties();
updateParseFile(strFile, propChart.timeParam, listCurve);
}
void LightPanel::OnTimeChanged(double time)
{
if (m_dataLamp.size() > 0)
{
QMap< double, QVariantMap >::const_iterator ite = m_dataLamp.lowerBound(time);
if (ite == m_dataLamp.end())
{
ite--;
}
QVariantMap mapData = ite.value();
for (QVariantMap::Iterator it = mapData.begin(); it != mapData.end(); it++)
{
QString strKey = it.key();
int nState = it.value().toFloat();
SignalLabel* pLampLab = m_mapLamp.value(strKey);
if (pLampLab)
{
QString strStyle = m_lampColor.value(nState);
pLampLab->setStyleSheet(strStyle);
}
}
}
}
void LightPanel::updateTitle(const QString & title)
{
if (nullptr != dockWidget_)
{
dockWidget_->setWindowTitle(title);
}
}
void LightPanel::updateParseFile(const QString & strFile, int nT, FileEntryLight::LightRowProperties listCurve)
{
if (strFile.isEmpty())
{
QMessageBox::information(nullptr, QString::fromLocal8Bit("<EFBFBD><EFBFBD>ʾ"), QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
return;
}
m_dataLamp.clear();
clearLightPanel();
QGridLayout* layout = qobject_cast<QGridLayout*>(this->layout());
if (!layout)
{
return;
}
QFile file(strFile);
if (file.open(QIODevice::ReadOnly))
{
for (int nI = 0; nI < listCurve.size(); nI++)
{
FileEntryLight::LightRowProperty prop = listCurve.at(nI);
for (auto i = 0; i < prop.name.size(); ++i)
{
SignalLabel* pLampLab = new SignalLabel;
pLampLab->setFixedSize(24, 24);
QString strStyle = m_lampColor.value(0);
pLampLab->setStyleSheet(strStyle);
QLabel* pTextLab = new QLabel;
pTextLab->setText(prop.name.at(i));
QHBoxLayout* pLyt = new QHBoxLayout;
pLyt->addWidget(pLampLab);
pLyt->addWidget(pTextLab);
layout->addLayout(pLyt, nI, i);
QString strKey = QString::number(nI) + "-" + QString::number(i);
m_mapLamp.insert(strKey, pLampLab);
}
}
while (!file.atEnd())
{
QString strLine = file.readLine().simplified();
if (!strLine.isEmpty())
{
QStringList listLine = strLine.split(" ");
double t = listLine.at(nT).toDouble();
QVariantMap mapData;
for (int nI = 0; nI < listCurve.size(); nI++)
{
FileEntryLight::LightRowProperty prop = listCurve.at(nI);
for (int nJ = 0; nJ < prop.data.size(); ++nJ)
{
int nIndex = prop.data.at(nJ);
QString nState = listLine.at(nIndex);
QString strKey = QString::number(nI) + "-" + QString::number(nJ);
mapData.insert(strKey, nState);
}
}
m_dataLamp.insert(t, mapData);
}
}
file.close();
}
}
void LightPanel::updateLampColor(const QString & strOpenColor, const QString & strCloseColor)
{
m_lampColor.clear();
{
QString strStyle = "QLabel{background-color: rgb(" + strCloseColor + ");border-radius: 10px;}; ";
m_lampColor.insert(0, strStyle);
}
{
QString strStyle = "QLabel{background-color: rgb(" + strOpenColor + ");border-radius: 10px;}; ";
m_lampColor.insert(1, strStyle);
}
}
void LightPanel::clearLightPanel()
{
if (auto* layout = qobject_cast<QGridLayout*>(this->layout()))
{
while (layout->count() > 0)
{
QLayoutItem* item = layout->takeAt(0);
if (item)
{
auto* childLayout = item->layout();
while (childLayout->count() > 0)
{
QLayoutItem* itemChild = childLayout->takeAt(0);
if (itemChild)
{
if (auto* w = itemChild->widget())
{
w->deleteLater();
}
delete itemChild;
}
}
delete item;
}
}
}
m_mapLamp.clear();
}