416 lines
14 KiB
C++
416 lines
14 KiB
C++
#include "AddLightFileDlg.h"
|
|
|
|
#include <QFileDialog>
|
|
#include <QFileInfo>
|
|
#include <QMessageBox>
|
|
#include <QColorDialog>
|
|
#include <QListWidget>
|
|
|
|
#include "workspace/WorkSpace.h"
|
|
#include "workspace/WorkSpaceManager.h"
|
|
|
|
#include "ui_AddLightFileDlg.h"
|
|
|
|
AddLightFileDlg::AddLightFileDlg(QWidget* parent)
|
|
: BaseAddFileDlg(FileEntryType::Light, parent)
|
|
, ui(new Ui::AddLightFileDlg)
|
|
, currentLightIndex_(-1)
|
|
, openColor_(0, 255, 0) // Default to green color
|
|
, closeColor_(255, 0, 0) { // Default to red color
|
|
|
|
SetupUI(ui);
|
|
SetTitle(getDialogTitle());
|
|
setupConnections();
|
|
|
|
// Initialize color properties
|
|
colorProperties_.openColor = openColor_;
|
|
colorProperties_.closeColor = closeColor_;
|
|
|
|
// Update color previews
|
|
updateOpenColorPreview(openColor_);
|
|
updateCloseColorPreview(closeColor_);
|
|
|
|
ui->editDataBtn->setVisible(false);
|
|
}
|
|
|
|
AddLightFileDlg::~AddLightFileDlg() {
|
|
delete ui;
|
|
}
|
|
|
|
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->lightDataEdit, &QLineEdit::textChanged, this, &AddLightFileDlg::onLightDataChanged);
|
|
connect(ui->editDataBtn, &QPushButton::clicked, this, &AddLightFileDlg::onEditDataClicked);
|
|
|
|
// 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::LightRowProperty newLight;
|
|
newLight.name = QStringList() << generateLightName();
|
|
|
|
// Add to lights list
|
|
lights_.append(newLight);
|
|
|
|
// Add to UI list widget
|
|
QListWidgetItem* item = new QListWidgetItem(QString("%1 [Row: %2]")
|
|
.arg(newLight.name.join(", "))
|
|
.arg(lights_.size() - 1));
|
|
ui->lightListWidget->addItem(item);
|
|
|
|
// Select the new item
|
|
ui->lightListWidget->setCurrentRow(lights_.size() - 1);
|
|
currentLightIndex_ = lights_.size() - 1;
|
|
|
|
// 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 row indices
|
|
for (int i = 0; i < lights_.size(); ++i) {
|
|
QListWidgetItem* item = ui->lightListWidget->item(i);
|
|
if (item) {
|
|
item->setText(QString("%1 [Row: %2]")
|
|
.arg(lights_[i].name.join(", "))
|
|
.arg(i));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 namesText = ui->lightNameEdit->text();
|
|
QStringList names = namesText.split(",", Qt::SkipEmptyParts);
|
|
for (int i = 0; i < names.size(); ++i) {
|
|
names[i] = names[i].trimmed();
|
|
}
|
|
lights_[currentLightIndex_].name = names;
|
|
|
|
// Update list widget item text
|
|
QListWidgetItem* item = ui->lightListWidget->item(currentLightIndex_);
|
|
if (item) {
|
|
item->setText(QString("%1 [Row: %2]")
|
|
.arg(names.join(", "))
|
|
.arg(currentLightIndex_));
|
|
}
|
|
}
|
|
}
|
|
|
|
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::LightRowProperty& light) {
|
|
QListWidgetItem* item = new QListWidgetItem(QString("%1 [Row: %2]")
|
|
.arg(light.name.join(", "))
|
|
.arg(lights_.size()));
|
|
ui->lightListWidget->addItem(item);
|
|
}
|
|
|
|
void AddLightFileDlg::updateLightProperties() {
|
|
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
|
|
const FileEntryLight::LightRowProperty& light = lights_[currentLightIndex_];
|
|
ui->lightNameEdit->setText(light.name.join(", "));
|
|
|
|
// Update data edit
|
|
QStringList dataStrings;
|
|
for (int dataValue : light.data) {
|
|
dataStrings.append(QString::number(dataValue));
|
|
}
|
|
ui->lightDataEdit->setText(dataStrings.join(", "));
|
|
|
|
// Update row index display
|
|
ui->rowIndexValue->setText(QString::number(currentLightIndex_));
|
|
|
|
// Update data display
|
|
updateDataDisplay();
|
|
}
|
|
}
|
|
|
|
void AddLightFileDlg::saveLightProperties() {
|
|
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
|
|
// Save names
|
|
QString namesText = ui->lightNameEdit->text();
|
|
QStringList names = namesText.split(",", Qt::SkipEmptyParts);
|
|
for (int i = 0; i < names.size(); ++i) {
|
|
names[i] = names[i].trimmed();
|
|
}
|
|
lights_[currentLightIndex_].name = names;
|
|
|
|
// Save data
|
|
QString dataText = ui->lightDataEdit->text();
|
|
QStringList dataStrings = dataText.split(",", Qt::SkipEmptyParts);
|
|
QList<int> dataValues;
|
|
|
|
for (const QString& str : dataStrings) {
|
|
bool ok;
|
|
int dataValue = str.trimmed().toInt(&ok);
|
|
if (ok) {
|
|
dataValues.append(dataValue);
|
|
}
|
|
}
|
|
lights_[currentLightIndex_].data = dataValues;
|
|
}
|
|
}
|
|
|
|
void AddLightFileDlg::clearLightProperties() {
|
|
ui->lightNameEdit->clear();
|
|
ui->lightDataEdit->clear();
|
|
ui->rowIndexValue->setText("-");
|
|
}
|
|
|
|
void AddLightFileDlg::enableLightProperties(bool enabled) {
|
|
ui->lightPropertiesGroupBox->setEnabled(enabled);
|
|
}
|
|
|
|
QString AddLightFileDlg::generateLightName() {
|
|
return QString("Light_%1").arg(lights_.size() + 1);
|
|
}
|
|
|
|
void AddLightFileDlg::updateDataDisplay() {
|
|
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
|
|
const FileEntryLight::LightRowProperty& light = lights_[currentLightIndex_];
|
|
QStringList dataStrings;
|
|
for (int dataValue : light.data) {
|
|
dataStrings.append(QString::number(dataValue));
|
|
}
|
|
ui->lightDataEdit->setText(dataStrings.join(", "));
|
|
} else {
|
|
ui->lightDataEdit->clear();
|
|
}
|
|
}
|
|
|
|
void AddLightFileDlg::onLightDataChanged() {
|
|
// Parse comma-separated data values and store them
|
|
if (currentLightIndex_ >= 0 && currentLightIndex_ < lights_.size()) {
|
|
QString dataText = ui->lightDataEdit->text();
|
|
QStringList dataStrings = dataText.split(',', Qt::SkipEmptyParts);
|
|
|
|
QList<int> dataValues;
|
|
for (const QString& str : dataStrings) {
|
|
bool ok;
|
|
int dataValue = str.trimmed().toInt(&ok);
|
|
if (ok) {
|
|
dataValues.append(dataValue);
|
|
}
|
|
}
|
|
lights_[currentLightIndex_].data = dataValues;
|
|
}
|
|
}
|
|
|
|
void AddLightFileDlg::onEditDataClicked() {
|
|
// Future implementation: could open a detailed data editor dialog
|
|
// For now, just focus on the data edit field
|
|
ui->lightDataEdit->setFocus();
|
|
}
|
|
|
|
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.");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
QString AddLightFileDlg::getFileFilter() const {
|
|
return "Light Data Files (*.txt *.csv *.dat);;All Files (*.*)";
|
|
}
|
|
|
|
QString AddLightFileDlg::getDialogTitle() const {
|
|
return "Add Light Data File";
|
|
}
|
|
|
|
void AddLightFileDlg::onSure() {
|
|
|
|
QString sName = ui->ChartNameEdit->text();
|
|
// Validate table-specific parameters
|
|
if (sName.isEmpty()) {
|
|
QMessageBox::warning(this, tr("Warning"), tr("Please enter a Chart name."));
|
|
return;
|
|
}
|
|
|
|
// Save current light properties if any light is selected
|
|
if (currentLightIndex_ >= 0) {
|
|
saveLightProperties();
|
|
}
|
|
|
|
if (!validateSpecificParams()) {
|
|
return;
|
|
}
|
|
|
|
// Get current workspace
|
|
WorkSpace* workspace = WorkSpaceManager::Get().GetCurrent();
|
|
if (!workspace) {
|
|
QMessageBox::warning(this, tr("Error"), tr("Unable to get current workspace"));
|
|
return;
|
|
}
|
|
|
|
|
|
// Create FileEntryLight and set properties
|
|
auto lightEntry = CreateFileEntryLight(getSelectedFilePath());
|
|
if (lightEntry) {
|
|
lightEntry->SetName(sName);
|
|
|
|
// 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.");
|
|
}
|
|
} |