diff options
author | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-12-07 21:57:21 -0500 |
---|---|---|
committer | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-12-07 21:57:21 -0500 |
commit | 940a2361da8f51ab2547f1b7bfd42dc1c8645642 (patch) | |
tree | 5916cfed50ae675ae10275c1134fc522ee59bae3 /src/mainwindow.cpp | |
parent | caa765bff49d54217b75aaf0e7acf4e5392a11e4 (diff) |
added a GUI
Diffstat (limited to 'src/mainwindow.cpp')
-rw-r--r-- | src/mainwindow.cpp | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp new file mode 100644 index 0000000..e4c9675 --- /dev/null +++ b/src/mainwindow.cpp @@ -0,0 +1,147 @@ +#include "mainwindow.h" +#include "settings.h" + +#include <QHBoxLayout> +#include <QVBoxLayout> +#include <QFileDialog> +#include <QSettings> +#include <QLabel> +#include <QGroupBox> +#include <iostream> + +void MainWindow::initialize() { + // create RayTracer + rayTracer = new RayTracer(this); + imageLabel = new QLabel(this); + // aspectRatioWidget = new AspectRatioWidget(this); + // aspectRatioWidget->setAspectWidget(imageLabel, 3.f/4.f); + QHBoxLayout *hLayout = new QHBoxLayout; // horizontal alignment + QVBoxLayout *vLayout = new QVBoxLayout(); // vertical alignment + vLayout->setAlignment(Qt::AlignTop); + hLayout->addLayout(vLayout); + hLayout->addWidget(imageLabel, 1); + this->setLayout(hLayout); + + QFont font; + font.setPointSize(12); + font.setBold(true); + QLabel *w_label = new QLabel(); // Width label + w_label->setText("W value:"); + + // Create file uploader for scene file + uploadFile = new QPushButton(); + uploadFile->setText(QStringLiteral("Upload Scene File")); + + saveImage = new QPushButton(); + saveImage->setText(QStringLiteral("Save image")); + + QGroupBox *wLayout = new QGroupBox(); // horizonal w slider alignment + QHBoxLayout *lw = new QHBoxLayout(); + + wSlider = new QSlider(Qt::Orientation::Horizontal); // W value slider + wSlider->setTickInterval(1); + wSlider->setMinimum(1); + wSlider->setMaximum(100); + wSlider->setValue(1); + + wBox = new QDoubleSpinBox(); + wBox->setMinimum(0.01f); + wBox->setMaximum(1.f); + wBox->setSingleStep(0.01f); + wBox->setValue(0.01f); + + lw->addWidget(wSlider); + lw->addWidget(wBox); + wLayout->setLayout(lw); + + vLayout->addWidget(uploadFile); + vLayout->addWidget(saveImage); + vLayout->addWidget(w_label); + vLayout->addWidget(wLayout); + + connectUIElements(); + + onValChangeWBox(0.01f); +} + +void MainWindow::finish() { +// realtime->finish(); +// delete(realtime); +} + +void MainWindow::connectUIElements() { + connectUploadFile(); + connectSaveImage(); + connectW(); +} + +void MainWindow::connectUploadFile() { + connect(uploadFile, &QPushButton::clicked, this, &MainWindow::onUploadFile); +} + +void MainWindow::connectSaveImage() { + connect(saveImage, &QPushButton::clicked, this, &MainWindow::onSaveImage); +} + +void MainWindow::connectW() { + connect(wSlider, &QSlider::valueChanged, this, &MainWindow::onValChangeWSlider); + connect(wBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), + this, &MainWindow::onValChangeWBox); +} + +void MainWindow::onUploadFile() { + // Get abs path of scene file + QString configFilePath = QFileDialog::getOpenFileName(this, tr("Upload File"), + QDir::currentPath() + .append(QDir::separator()) + .append("scenefiles") + .append(QDir::separator()) + .append("lights-camera") + .append(QDir::separator()) + .append("required"), tr("Scene Files (*.json)")); + if (configFilePath.isNull()) { + std::cout << "Failed to load null scenefile." << std::endl; + return; + } + + settings.sceneFilePath = configFilePath.toStdString(); + + std::cout << "Loaded scenefile: \"" << configFilePath.toStdString() << "\"." << std::endl; + + rayTracer->sceneChanged(imageLabel); +} + +void MainWindow::onSaveImage() { + if (settings.sceneFilePath.empty()) { + std::cout << "No scene file loaded." << std::endl; + return; + } + std::string sceneName = settings.sceneFilePath.substr(0, settings.sceneFilePath.find_last_of(".")); + sceneName = sceneName.substr(sceneName.find_last_of("/")+1); + QString filePath = QFileDialog::getSaveFileName(this, tr("Save Image"), + QDir::currentPath() + .append(QDir::separator()) + .append("student_outputs") + .append(QDir::separator()) + .append("lights-camera") + .append(QDir::separator()) + .append("required") + .append(QDir::separator()) + .append(sceneName), tr("Image Files (*.png)")); + std::cout << "Saving image to: \"" << filePath.toStdString() << "\"." << std::endl; +// realtime->saveViewportImage(filePath.toStdString()); +} + +void MainWindow::onValChangeWSlider(int newValue) { + //wSlider->setValue(newValue); + wBox->setValue(newValue/100.f); + settings.w = wBox->value(); + rayTracer->settingsChanged(imageLabel); +} + +void MainWindow::onValChangeWBox(double newValue) { + wSlider->setValue(int(newValue*100.f)); + //wBox->setValue(newValue); + settings.w = wBox->value(); + rayTracer->settingsChanged(imageLabel); +} |