ReportGeneration/DataVisualization/ChartLogGeneration/settingsdialog.cpp

155 lines
5.1 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 "settingsdialog.h"
SettingsDialog::SettingsDialog(int curveCount, QWidget *parent) :
QDialog(parent),
// curveColors{Qt::blue, Qt::green},
backgroundColor(Qt::white)
{
initializeCurveColors(100, 123);
// 创建UI元素
QLabel *xMinLabel = new QLabel("X Axis Min:", this);
xMinLineEdit = new QLineEdit(this);
xMinLineEdit->setText("5");
QLabel *xMaxLabel = new QLabel("X Axis Max:", this);
xMaxLineEdit = new QLineEdit(this);
xMaxLineEdit->setText("10000");
QLabel *yMinLabel = new QLabel("Y Axis Min:", this);
yMinLineEdit = new QLineEdit(this);
yMinLineEdit->setText("0.01");
QLabel *yMaxLabel = new QLabel("Y Axis Max:", this);
yMaxLineEdit = new QLineEdit(this);
yMaxLineEdit->setText("1000");
QLabel *backgroundColorLabel = new QLabel("Background Color:", this);
backgroundColorButton = new QPushButton(this);
updateColorButton(backgroundColorButton, backgroundColor);
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);
layout->addWidget(backgroundColorLabel, 4 + curveCount, 0);
layout->addWidget(backgroundColorButton, 4 + curveCount, 1);
layout->addWidget(gridCheckBox, 5 + curveCount, 0, 1, 2);
// 确认和取消按钮
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);
// 设置对话框的主要布局
setLayout(layout);
// 连接按钮信号到槽
connect(backgroundColorButton, &QPushButton::clicked, this, [=](){
QColor color = QColorDialog::getColor(backgroundColor, this);
if (color.isValid()) {
backgroundColor = color;
updateColorButton(backgroundColorButton, backgroundColor);
}
});
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);
}
});
}
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];
}
return QColor(Qt::black);
}
QColor SettingsDialog::getBackgroundColor() const {
return backgroundColor;
}
QVector<QColor> SettingsDialog::getCurveColors() const {
return curveColors;
}
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()));
}