DYTSrouce/src/ui/Panel/ImagePanel.cpp

207 lines
4.8 KiB
C++
Raw Normal View History

2025-11-02 08:36:07 +00:00
#include "ui/Panel/ImagePanel.h"
#include "ui/DockWidget.h"
#include "ui/DockTitleBar.h"
#include "common/SpdLogger.h"
#include <QHBoxLayout>
#include <QFileInfo>
#include <QMessageBox>
2025-11-03 09:03:08 +00:00
#include "workspace/WorkSpaceManager.h"
#include "workspace/WorkSpace.h"
2025-11-02 08:36:07 +00:00
ImagePanel::ImagePanel(int index, const QString& filePath, QWidget* parent)
2025-11-03 09:03:08 +00:00
: DataPanel(index, FileEntryType::Image, filePath, parent)
2025-11-02 08:36:07 +00:00
{
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()
{
2025-11-02 09:51:46 +00:00
QGridLayout* pMainLyt = new QGridLayout(this);
2025-11-03 09:03:08 +00:00
pMainLyt->setContentsMargins(0, 0, 0, 0);
2025-11-02 09:51:46 +00:00
setLayout(pMainLyt);
2025-11-03 09:03:08 +00:00
setMinimumHeight(100);
2025-11-02 08:36:07 +00:00
}
QString ImagePanel::GetTypeDisplayName() const
{
return "Image";
}
void ImagePanel::OnDataPanelUpdated(FileEntryImage* fileEntry)
{
QString strName = fileEntry->GetName();
updateTitle(strName);
2025-11-03 09:03:08 +00:00
FileEntryImage::ChartProperties propChart = fileEntry->GetChartProperties();
QString strFile = fileEntry->GetPath() + "/" + fileEntry->GetFileName();
FileEntryImage::ImageProperties listCurve = fileEntry->GetImageProperties();
updateParseFile(strFile, propChart.timeParam, listCurve);
2025-11-02 08:36:07 +00:00
}
void ImagePanel::OnTimeChanged(double time)
{
2025-11-03 09:03:08 +00:00
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));
}
}
}
2025-11-02 08:36:07 +00:00
}
void ImagePanel::updateTitle(const QString & title)
{
if (nullptr != dockWidget_)
{
dockWidget_->setWindowTitle(title);
}
2025-11-03 09:03:08 +00:00
}
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();
2025-11-02 08:36:07 +00:00
}