ReportGeneration/DataVisualization/ChartLogGeneration/settingsdialog.cpp

155 lines
5.1 KiB
C++
Raw Normal View History

2024-08-30 16:46:00 +08:00
#include "settingsdialog.h"
SettingsDialog::SettingsDialog(int curveCount, QWidget *parent) :
QDialog(parent),
// curveColors{Qt::blue, Qt::green},
backgroundColor(Qt::white)
2024-08-30 16:46:00 +08:00
{
initializeCurveColors(100, 123);
2024-08-30 16:46:00 +08:00
// 创建UI元素
QLabel *xMinLabel = new QLabel("X Axis Min:", this);
xMinLineEdit = new QLineEdit(this);
xMinLineEdit->setText("5");
2024-08-30 16:46:00 +08:00
QLabel *xMaxLabel = new QLabel("X Axis Max:", this);
xMaxLineEdit = new QLineEdit(this);
xMaxLineEdit->setText("10000");
2024-08-30 16:46:00 +08:00
QLabel *yMinLabel = new QLabel("Y Axis Min:", this);
yMinLineEdit = new QLineEdit(this);
yMinLineEdit->setText("0.01");
2024-08-30 16:46:00 +08:00
QLabel *yMaxLabel = new QLabel("Y Axis Max:", this);
yMaxLineEdit = new QLineEdit(this);
yMaxLineEdit->setText("1000");
2024-08-30 16:46:00 +08:00
QLabel *backgroundColorLabel = new QLabel("Background Color:", this);
backgroundColorButton = new QPushButton(this);
updateColorButton(backgroundColorButton, backgroundColor);
2024-08-30 16:46:00 +08:00
gridCheckBox = new QCheckBox("Show Grid", this);
gridCheckBox->setChecked(true);
// 设置布局
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(xMinLabel, 0, 0);
layout->addWidget(xMinLineEdit, 0, 1);
layout->addWidget(xMaxLabel, 1, 0);
layout->addWidget(xMaxLineEdit, 1, 1);
layout->addWidget(yMinLabel, 2, 0);
layout->addWidget(yMinLineEdit, 2, 1);
layout->addWidget(yMaxLabel, 3, 0);
layout->addWidget(yMaxLineEdit, 3, 1);
// 添加曲线颜色按钮
setupCurveColorButtons(curveCount);
2024-08-30 16:46:00 +08:00
layout->addWidget(backgroundColorLabel, 4 + curveCount, 0);
layout->addWidget(backgroundColorButton, 4 + curveCount, 1);
layout->addWidget(gridCheckBox, 5 + curveCount, 0, 1, 2);
2024-08-30 16:46:00 +08:00
// 确认和取消按钮
QPushButton *okButton = new QPushButton("OK", this);
QPushButton *cancelButton = new QPushButton("Cancel", this);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout, 6 + curveCount, 0, 1, 2);
2024-08-30 16:46:00 +08:00
// 设置对话框的主要布局
setLayout(layout);
// 连接按钮信号到槽
connect(backgroundColorButton, &QPushButton::clicked, this, [=](){
QColor color = QColorDialog::getColor(backgroundColor, this);
2024-08-30 16:46:00 +08:00
if (color.isValid()) {
backgroundColor = color;
updateColorButton(backgroundColorButton, backgroundColor);
2024-08-30 16:46:00 +08:00
}
});
for (int i = 0; i < curveColorButtons.size(); ++i) {
connect(curveColorButtons[i], &QPushButton::clicked, this, [=]() {
QColor color = QColorDialog::getColor(curveColors[i], this);
if (color.isValid()) {
curveColors[i] = color;
updateColorButton(curveColorButtons[i], color);
}
});
}
2024-08-30 16:46:00 +08:00
connect(okButton, &QPushButton::clicked, this, &QDialog::accept);
connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
}
SettingsDialog::~SettingsDialog() = default;
double SettingsDialog::getXMin() const { return xMinLineEdit->text().toDouble(); }
double SettingsDialog::getXMax() const { return xMaxLineEdit->text().toDouble(); }
double SettingsDialog::getYMin() const { return yMinLineEdit->text().toDouble(); }
double SettingsDialog::getYMax() const { return yMaxLineEdit->text().toDouble(); }
QColor SettingsDialog::getCurveColor(int index) const {
if (index >= 0 && index < curveColors.size()) {
return curveColors[index];
2024-08-30 16:46:00 +08:00
}
return QColor(Qt::black);
2024-08-30 16:46:00 +08:00
}
QColor SettingsDialog::getBackgroundColor() const {
return backgroundColor;
}
QVector<QColor> SettingsDialog::getCurveColors() const {
return curveColors;
2024-08-30 16:46:00 +08:00
}
bool SettingsDialog::isGridVisible() const {
return gridCheckBox->isChecked();
}
void SettingsDialog::setupCurveColorButtons(int curveCount) {
QStringList labels;
for (int i = 0; i < curveCount; ++i) {
labels << QString("Curve %1 Color:").arg(i+1);
}
QGridLayout *layout = qobject_cast<QGridLayout*>(this->layout());
qDeleteAll(curveColorButtons);
curveColorButtons.clear();
for (int i = 0; i < labels.size(); ++i) {
QLabel *label = new QLabel(labels[i], this);
QPushButton *button = new QPushButton(this);
// 这里假设curveColors有足够的元素如果不确定填充默认颜色
if (i < curveColors.size()) {
updateColorButton(button, curveColors[i]);
} else {
updateColorButton(button, Qt::white);
}
layout->addWidget(label, 4 + i, 0);
layout->addWidget(button, 4 + i, 1);
curveColorButtons.append(button);
}
}
void SettingsDialog::initializeCurveColors(int size, quint32 seed){
curveColors.resize(size);
QRandomGenerator random(seed);
// 生成随机颜色并填充 QVector
for (int i = 0; i < curveColors.size(); ++i) {
int red = random.bounded(256);
int green = random.bounded(256);
int blue = random.bounded(256);
curveColors[i] = QColor(red, green, blue);
}
}
void SettingsDialog::updateColorButton(QPushButton *button, const QColor &color) {
button->setStyleSheet(QString("background-color: %1").arg(color.name()));
}