aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow.cpp
blob: e4c96758e80a01b0c0e4911da560fdb87e468e7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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);
}