DYTSrouce/src/ui/Panel/ImagePanel.cpp
2025-11-03 17:03:08 +08:00

207 lines
4.8 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/ImagePanel.h"
#include "ui/DockWidget.h"
#include "ui/DockTitleBar.h"
#include "common/SpdLogger.h"
#include <QHBoxLayout>
#include <QFileInfo>
#include <QMessageBox>
#include "workspace/WorkSpaceManager.h"
#include "workspace/WorkSpace.h"
ImagePanel::ImagePanel(int index, const QString& filePath, QWidget* parent)
: DataPanel(index, FileEntryType::Image, filePath, parent)
{
LOG_INFO("Created ImagePanel {} for file: {}", index, filePath.toStdString());
}
ImagePanel::ImagePanel(int index, std::shared_ptr<FileEntryImage> fileEntry, QWidget* parent)
: DataPanel(index, fileEntry, parent)
{
if (fileEntry) {
LOG_INFO("Created ImagePanel {} for chart: {}", index, fileEntry->GetName().toStdString());
// Override the title with chart name
title_ = QString("Image Panel %1 - %2").arg(index).arg(fileEntry->GetName());
}
else {
LOG_WARN("Created ImagePanel {} with null chart data", index);
}
}
ImagePanel::~ImagePanel()
{
LOG_INFO("Destroyed ImagePanel {}", GetIndex());
}
void ImagePanel::RefreshPanel()
{
// Implement curve-specific refresh logic here
DataPanel::RefreshPanel();
if (auto fileEntry = fileEntry_->AsImage()) {
OnDataPanelUpdated(fileEntry);
}
LOG_INFO("Refreshed ImagePanel {}", GetIndex());
}
void ImagePanel::InitUI()
{
QGridLayout* pMainLyt = new QGridLayout(this);
pMainLyt->setContentsMargins(0, 0, 0, 0);
setLayout(pMainLyt);
setMinimumHeight(100);
}
QString ImagePanel::GetTypeDisplayName() const
{
return "Image";
}
void ImagePanel::OnDataPanelUpdated(FileEntryImage* fileEntry)
{
QString strName = fileEntry->GetName();
updateTitle(strName);
FileEntryImage::ChartProperties propChart = fileEntry->GetChartProperties();
QString strFile = fileEntry->GetPath() + "/" + fileEntry->GetFileName();
FileEntryImage::ImageProperties listCurve = fileEntry->GetImageProperties();
updateParseFile(strFile, propChart.timeParam, listCurve);
}
void ImagePanel::OnTimeChanged(double time)
{
if (m_dataImage.size() > 0)
{
QMap< double, QVariantMap >::const_iterator ite = m_dataImage.lowerBound(time);
if (ite == m_dataImage.end())
{
ite--;
}
QVariantMap mapData = ite.value();
for (QVariantMap::Iterator it = mapData.begin(); it != mapData.end(); it++)
{
QString strKey = it.key();
QString sData = it.value().toString();
QLabel* pImgLabel = m_mapImage.value(strKey);
if (pImgLabel)
{
QImage px;
px.load(sData);
pImgLabel->setPixmap(QPixmap::fromImage(px));
}
}
}
}
void ImagePanel::updateTitle(const QString & title)
{
if (nullptr != dockWidget_)
{
dockWidget_->setWindowTitle(title);
}
}
void ImagePanel::updateParseFile(const QString & strFile, int nT, FileEntryImage::ImageProperties 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_dataImage.clear();
clearImagePanel();
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++)
{
FileEntryImage::ImageProperty prop = listCurve.at(nI);
for (auto i = 0; i < prop.names.size(); ++i)
{
QLabel* pImgLabel = new QLabel;
pImgLabel->setStyleSheet(QString("background-color: rgb(190, 190, 190, 255);"));
pImgLabel->setScaledContents(true);
layout->addWidget(pImgLabel, nI, i);
QString strKey = QString::number(nI) + "-" + QString::number(i);
m_mapImage.insert(strKey, pImgLabel);
}
}
QString strDir = WorkSpaceManager::Get().GetCurrent()->GetDir();
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++)
{
FileEntryImage::ImageProperty prop = listCurve.at(nI);
for (int nJ = 0; nJ < prop.datas.size(); ++nJ)
{
int nIndex = prop.datas.at(nJ);
QString sImage = listLine.at(nIndex);
if (prop.path.isEmpty())
{
sImage = strDir + "/" + prop.names.at(nI) + "/" + sImage + "." + prop.suffix;
}
else
{
sImage = prop.path + "/" + prop.names.at(nI) + "/" + sImage + "." + prop.suffix;
}
QString strKey = QString::number(nI) + "-" + QString::number(nJ);
mapData.insert(strKey, sImage);
}
}
m_dataImage.insert(t, mapData);
}
}
file.close();
}
}
void ImagePanel::clearImagePanel()
{
if (auto* layout = qobject_cast<QGridLayout*>(this->layout()))
{
while (layout->count() > 0)
{
QLayoutItem* item = layout->takeAt(0);
if (item)
{
if (auto* w = item->widget())
{
w->deleteLater();
}
delete item;
}
}
}
m_mapImage.clear();
}