DYTSrouce/src/ui/WorkSpace/AddLightFileDlg.cpp

347 lines
12 KiB
C++
Raw Normal View History

2025-10-22 17:40:44 +00:00
#include "AddLightFileDlg.h"
2025-10-26 08:44:23 +00:00
#include <QFileDialog>
2025-10-22 17:40:44 +00:00
#include <QFileInfo>
2025-10-26 08:44:23 +00:00
#include <QMessageBox>
#include <QColorDialog>
#include <QListWidget>
#include "workspace/WorkSpace.h"
#include "workspace/WorkSpaceManager.h"
2025-10-22 17:40:44 +00:00
#include "ui_AddLightFileDlg.h"
AddLightFileDlg::AddLightFileDlg(QWidget* parent)
: BaseAddFileDlg(FileEntryType::Light, parent)
2025-10-26 08:44:23 +00:00
, ui(new Ui::AddLightFileDlg)
, currentLightIndex_(-1)
, openColor_(0, 255, 0) // Default to green color
, closeColor_(255, 0, 0) { // Default to red color
2025-10-22 17:40:44 +00:00
2025-10-26 08:44:23 +00:00
SetupUI(ui);
2025-10-22 17:40:44 +00:00
SetTitle(getDialogTitle());
2025-10-26 08:44:23 +00:00
setupConnections();
// Initialize color properties
colorProperties_.openColor = openColor_;
colorProperties_.closeColor = closeColor_;
// Update color previews
updateOpenColorPreview(openColor_);
updateCloseColorPreview(closeColor_);
2025-10-22 17:40:44 +00:00
}
AddLightFileDlg::~AddLightFileDlg() {
delete ui;
}
2025-10-26 08:44:23 +00:00
void AddLightFileDlg::setupConnections() {
// File selection connections
connect(ui->selectFileBtn, &QToolButton::clicked, this, &AddLightFileDlg::OnSelectFile);
// Color selection connections
connect(ui->openColorButton, &QPushButton::clicked, this, &AddLightFileDlg::onOpenColorButtonClicked);
connect(ui->closeColorButton, &QPushButton::clicked, this, &AddLightFileDlg::onCloseColorButtonClicked);
// Light management connections
connect(ui->addLightBtn, &QPushButton::clicked, this, &AddLightFileDlg::onAddLightClicked);
connect(ui->removeLightBtn, &QPushButton::clicked, this, &AddLightFileDlg::onRemoveLightClicked);
connect(ui->lightListWidget, &QListWidget::currentRowChanged, this, &AddLightFileDlg::onLightSelectionChanged);
connect(ui->lightListWidget, &QListWidget::itemClicked, this, &AddLightFileDlg::onLightListWidgetItemClicked);
// Light properties connections
connect(ui->lightNameEdit, &QLineEdit::textChanged, this, &AddLightFileDlg::onLightNameChanged);
connect(ui->lightIndexSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &AddLightFileDlg::onLightIndexChanged);
// Dialog buttons
connect(ui->addFileBtn, &QPushButton::clicked, this, &AddLightFileDlg::onSure);
connect(ui->cancelBtn, &QPushButton::clicked, this, &QDialog::reject);
}
void AddLightFileDlg::updateFileInfo(const QString& filePath) {
QFileInfo fileInfo(filePath);
if (fileInfo.exists()) {
ui->fileNameValue->setText(fileInfo.fileName());
qint64 size = fileInfo.size();
QString sizeText;
if (size < 1024) {
sizeText = QString("%1 B").arg(size);
}
else if (size < 1024 * 1024) {
sizeText = QString("%1 KB").arg(size / 1024.0, 0, 'f', 1);
}
else {
sizeText = QString("%1 MB").arg(size / (1024.0 * 1024.0), 0, 'f', 1);
}
ui->fileSizeValue->setText(sizeText);
} else {
ui->fileNameValue->setText("-");
ui->fileSizeValue->setText("-");
}
ui->filePathEdit->setText(filePath);
}
void AddLightFileDlg::onOpenColorButtonClicked() {
QColor color = QColorDialog::getColor(openColor_, this, "Select Open Color");
if (color.isValid()) {
openColor_ = color;
colorProperties_.openColor = color;
updateOpenColorPreview(color);
}
}
void AddLightFileDlg::onCloseColorButtonClicked() {
QColor color = QColorDialog::getColor(closeColor_, this, "Select Close Color");
if (color.isValid()) {
closeColor_ = color;
colorProperties_.closeColor = color;
updateCloseColorPreview(color);
}
}
void AddLightFileDlg::onAddLightClicked() {
// Save current light properties if any light is selected
if (currentLightIndex_ >= 0) {
saveLightProperties();
}
// Create new light with default properties
FileEntryLight::LightProperty newLight;
newLight.name = generateLightName();
newLight.index = lights_.size();
// Add to lights list
lights_.append(newLight);
// Add to UI list widget
QListWidgetItem* item = new QListWidgetItem(QString("%1 [Index: %2]")
.arg(newLight.name)
.arg(newLight.index));
ui->lightListWidget->addItem(item);
// Select the new item
ui->lightListWidget->setCurrentRow(lights_.size() - 1);
currentLightIndex_ = lights_.size() - 1;
2025-10-22 17:40:44 +00:00
2025-10-26 08:44:23 +00:00
// Enable properties editing
enableLightProperties(true);
updateLightProperties();
}
void AddLightFileDlg::onRemoveLightClicked() {
int currentRow = ui->lightListWidget->currentRow();
if (currentRow >= 0 && currentRow < lights_.size()) {
// Remove from data
lights_.removeAt(currentRow);
// Remove from UI
delete ui->lightListWidget->takeItem(currentRow);
// Update current index
if (lights_.isEmpty()) {
currentLightIndex_ = -1;
enableLightProperties(false);
clearLightProperties();
} else {
// Select next item or previous if at end
int newIndex = qMin(currentRow, lights_.size() - 1);
ui->lightListWidget->setCurrentRow(newIndex);
currentLightIndex_ = newIndex;
updateLightProperties();
}
// Update list display with new indices
for (int i = 0; i < lights_.size(); ++i) {
lights_[i].index = i;
QListWidgetItem* item = ui->lightListWidget->item(i);
if (item) {
item->setText(QString("%1 [Index: %2]")
.arg(lights_[i].name)
.arg(lights_[i].index));
}
}
}
}
void AddLightFileDlg::onLightListWidgetItemClicked(QListWidgetItem* item) {
Q_UNUSED(item)
onLightSelectionChanged();
}
void AddLightFileDlg::onLightSelectionChanged() {
int currentRow = ui->lightListWidget->currentRow();
if (currentRow >= 0 && currentRow < lights_.size()) {
// Save previous light properties
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
saveLightProperties();
}
currentLightIndex_ = currentRow;
enableLightProperties(true);
updateLightProperties();
} else {
currentLightIndex_ = -1;
enableLightProperties(false);
clearLightProperties();
}
}
void AddLightFileDlg::onLightNameChanged() {
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
QString newName = ui->lightNameEdit->text();
lights_[currentLightIndex_].name = newName;
// Update list widget item text
QListWidgetItem* item = ui->lightListWidget->item(currentLightIndex_);
if (item) {
item->setText(QString("%1 [Index: %2]")
.arg(newName)
.arg(lights_[currentLightIndex_].index));
}
}
}
void AddLightFileDlg::onLightIndexChanged() {
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
int newIndex = ui->lightIndexSpinBox->value();
lights_[currentLightIndex_].index = newIndex;
// Update list widget item text
QListWidgetItem* item = ui->lightListWidget->item(currentLightIndex_);
if (item) {
item->setText(QString("%1 [Index: %2]")
.arg(lights_[currentLightIndex_].name)
.arg(newIndex));
}
}
}
void AddLightFileDlg::updateOpenColorPreview(const QColor& color) {
QString styleSheet = QString("background-color: rgb(%1, %2, %3); border: 1px solid black;")
.arg(color.red())
.arg(color.green())
.arg(color.blue());
ui->openColorPreview->setStyleSheet(styleSheet);
}
void AddLightFileDlg::updateCloseColorPreview(const QColor& color) {
QString styleSheet = QString("background-color: rgb(%1, %2, %3); border: 1px solid black;")
.arg(color.red())
.arg(color.green())
.arg(color.blue());
ui->closeColorPreview->setStyleSheet(styleSheet);
}
void AddLightFileDlg::addLightToList(const FileEntryLight::LightProperty& light) {
QListWidgetItem* item = new QListWidgetItem(QString("%1 [Index: %2]")
.arg(light.name)
.arg(light.index));
ui->lightListWidget->addItem(item);
}
void AddLightFileDlg::updateLightProperties() {
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
const FileEntryLight::LightProperty& light = lights_[currentLightIndex_];
ui->lightNameEdit->setText(light.name);
ui->lightIndexSpinBox->setValue(light.index);
}
}
void AddLightFileDlg::saveLightProperties() {
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
lights_[currentLightIndex_].name = ui->lightNameEdit->text();
lights_[currentLightIndex_].index = ui->lightIndexSpinBox->value();
}
}
void AddLightFileDlg::clearLightProperties() {
ui->lightNameEdit->clear();
ui->lightIndexSpinBox->setValue(0);
}
void AddLightFileDlg::enableLightProperties(bool enabled) {
ui->lightPropertiesGroupBox->setEnabled(enabled);
}
QString AddLightFileDlg::generateLightName() {
return QString("Light %1").arg(lights_.size() + 1);
}
bool AddLightFileDlg::validateSpecificParams() {
// Check if at least one light is defined
if (lights_.isEmpty()) {
QMessageBox::warning(this, "Validation Error", "At least one light must be defined.");
2025-10-22 17:40:44 +00:00
return false;
}
return true;
}
QString AddLightFileDlg::getFileFilter() const {
2025-10-26 08:44:23 +00:00
return "Light Data Files (*.txt *.csv *.dat);;All Files (*.*)";
2025-10-22 17:40:44 +00:00
}
QString AddLightFileDlg::getDialogTitle() const {
2025-10-26 08:44:23 +00:00
return "Add Light Data File";
2025-10-22 17:40:44 +00:00
}
2025-10-26 08:44:23 +00:00
void AddLightFileDlg::onSure() {
// Save current light properties if any light is selected
if (currentLightIndex_ >= 0) {
saveLightProperties();
}
if (!validateSpecificParams()) {
return;
}
2025-10-22 17:40:44 +00:00
2025-10-26 08:44:23 +00:00
// Get current workspace
WorkSpace* workspace = WorkSpaceManager::Get().GetCurrent();
if (!workspace) {
QMessageBox::warning(this, tr("Error"), tr("Unable to get current workspace"));
return;
}
2025-10-22 17:40:44 +00:00
2025-10-26 08:44:23 +00:00
// Create FileEntryLight and set properties
auto lightEntry = CreateFileEntryLight(getSelectedFilePath());
if (lightEntry) {
// Set color properties
lightEntry->SetColorProperties(colorProperties_);
// Add all light properties
for (const auto& light : lights_) {
lightEntry->AddLightProperty(light);
}
// Add to workspace
auto result = workspace->SetFileEntry(lightEntry);
if (result != WorkSpace::FileEntryResult::Ok) {
QString errorMsg;
switch (result) {
case WorkSpace::FileEntryResult::LimitExceeded:
errorMsg = tr("Curve file count has reached the limit (9 files)");
break;
case WorkSpace::FileEntryResult::Duplicate:
errorMsg = tr("File already exists");
break;
case WorkSpace::FileEntryResult::CopyFailed:
errorMsg = tr("File copy failed");
break;
case WorkSpace::FileEntryResult::InvalidFile:
errorMsg = tr("Invalid file");
break;
default:
errorMsg = tr("Failed to add file");
break;
}
QMessageBox::warning(this, tr("Error"), errorMsg);
return;
}
accept();
} else {
QMessageBox::critical(this, "Error", "Failed to create light file entry.");
2025-10-22 17:40:44 +00:00
}
}