Skip to content

Commit

Permalink
fix: enhance code readability and documentation (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
JxnLexn authored Jan 23, 2025
2 parents ed999d0 + cdf3b6d commit 0a7d837
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
106 changes: 52 additions & 54 deletions raytracer/src/main/java/rayrangers/raytracer/gui/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import io.qt.core.QStringList;
import io.qt.core.QStringListModel;
import io.qt.core.QUrl;
import io.qt.core.Qt;
import io.qt.core.Qt.AspectRatioMode;
import io.qt.gui.QAction;
import io.qt.gui.QDesktopServices;
import io.qt.gui.QFont;
import io.qt.gui.QImage;
import io.qt.gui.QPixmap;
import io.qt.gui.QResizeEvent;
import io.qt.widgets.QApplication;
import io.qt.widgets.QComboBox;
import io.qt.widgets.QDoubleSpinBox;
Expand Down Expand Up @@ -57,6 +55,7 @@ public class Loader extends QMainWindow {
private QDoubleSpinBox cameraYaw;
private QDoubleSpinBox cameraDistance;
private QSlider cameraFov;
// Default values for the size of the image
private int width = 2000;
private int height = 2000;

Expand All @@ -79,10 +78,12 @@ public Loader() {
ui = loader.load(uiFile, this);
uiFile.close();

// Set the default font for the application
QFont defaultFont = new QFont("Arial", 10);
QApplication.setFont(defaultFont);

setFixedSize(960, 580); // Beispielwerte: Breite 800px, Höhe 600px
// Set the size of the window (fixed)
setFixedSize(960, 580);


// Starting point to load all the different UI elements (main window) -----------------------------------------------------------------------
Expand Down Expand Up @@ -171,9 +172,9 @@ public Loader() {
QComboBox renderingAntiAliasing = centralWidget.findChild(QComboBox.class, "comboBox_anti_aliasing");
QComboBox renderingShading = centralWidget.findChild(QComboBox.class, "comboBox_shading");

// set renderingResWidth to 2000
// set renderingResWidth to configured value
renderingResWidth.setValue(width);
// set renderingResHeight to 2000
// set renderingResHeight to configured value
renderingResHeight.setValue(height);

// Lock renderingResWidth and renderingResHeight
Expand Down Expand Up @@ -246,7 +247,6 @@ public Loader() {
QLineEdit renderTime = centralWidget.findChild(QLineEdit.class, "renderTimeLineEdit_info");
QLineEdit qualtiyInfo = centralWidget.findChild(QLineEdit.class, "qualityLineEdit_info");


QToolButton deleteButton = centralWidget.findChild(QToolButton.class, "deleteButton");
QPushButton exportButtonResult = centralWidget.findChild(QPushButton.class, "exportButton_result");
QPushButton sceneButton = centralWidget.findChild(QPushButton.class, "sceneButton_result");
Expand All @@ -255,7 +255,27 @@ public Loader() {
QGraphicsView resultGraphicsView = centralWidget.findChild(QGraphicsView.class, "graphicsView_5");
QGraphicsScene scene = new QGraphicsScene(resultGraphicsView);



// Add functionality to the UI elements of the light configuration window -------------------------------------------------------------------
// Saves the current configuration and switches to the main window
lightDoneButton.clicked.connect(() -> {
centralWidget.setCurrentIndex(0);
setWindowTitle("Main Window");
});


// Add functionality to the UI elements of the result window --------------------------------------------------------------------------------
// Switches back to the main window
sceneButton.clicked.connect(() -> {
centralWidget.setCurrentIndex(0);
setWindowTitle("Main Window");
});

deleteButton.clicked.connect(() -> {
scene.clear();
resultGraphicsView.setScene(scene);
});

// Add functionality to the UI elements of the main window ----------------------------------------------------------------------------------

// Switches to the object configuration window and opens file picker
Expand Down Expand Up @@ -363,13 +383,6 @@ public Loader() {
progressBar.setFormat("Ready");
}).start();
});


// Add functionality to the UI elements of the object configuration window ------------------------------------------------------------------

objectPosX.valueChanged.connect(() -> {
System.out.println("Test");
});


// Saves the current configuration and switches to the main window
Expand All @@ -378,27 +391,6 @@ public Loader() {
setWindowTitle("Main Window");
});


// Add functionality to the UI elements of the light configuration window -------------------------------------------------------------------
// Saves the current configuration and switches to the main window
lightDoneButton.clicked.connect(() -> {
centralWidget.setCurrentIndex(0);
setWindowTitle("Main Window");
});


// Add functionality to the UI elements of the result window --------------------------------------------------------------------------------
// Switches back to the main window
sceneButton.clicked.connect(() -> {
centralWidget.setCurrentIndex(0);
setWindowTitle("Main Window");
});

deleteButton.clicked.connect(() -> {
scene.clear();
resultGraphicsView.setScene(scene);
});

/**
* Exports the rendered image as a PNG file.
*/
Expand Down Expand Up @@ -437,47 +429,47 @@ public Loader() {


// Add functionality to the UI elements of the menu bar -------------------------------------------------------------------------------------
// "File" -> "New Scene"
// "File" -> "New Scene" button
newScene.triggered.connect(() -> {
System.out.println("New Scene");
});

// "File" -> "Import Scene"
// "File" -> "Import Scene" button
importScene.triggered.connect(() -> {
System.out.println("Import Scene is not yet implemented :(");
});

// "File" -> "Export Scene"
// "File" -> "Export Scene" button
saveScene.triggered.connect(() -> {
System.out.println("Export Scene is not yet implemented :(");
});

// "File" -> "Close App"
// "File" -> "Close App" button
closeApp.triggered.connect(() -> {
// Perform multiple actions
System.out.println("Closing application");
QApplication.quit();
});

// "Edit" -> "Undo"
// "Edit" -> "Undo" button
undo.triggered.connect(() -> {
System.out.println("Undo is not yet implemented :(");
});

// "Edit" -> "Redo"
// "Edit" -> "Redo" button
redo.triggered.connect(() -> {
System.out.println("Redo is not yet implemented :(");
});

// "Help" -> "About"
// "Help" -> "About" button
about.triggered.connect(() -> {
QMessageBox aboutBox = new QMessageBox(this);
aboutBox.setWindowTitle("About");
aboutBox.setText("Version: 1.0\nDeveloped by: Team Raytracers\nContributors: Jan Birkert, Chantal Deusch, Adrian Desiderato, Jan Gaschler, David Geri, Paulina Pyczot, Lisa Korntheuer");
aboutBox.exec();
});

// "Help" -> "Tutorial"
// "Help" -> "Tutorial" button
help.triggered.connect(() -> {
int answer = QMessageBox.question(
this,
Expand All @@ -495,8 +487,10 @@ public Loader() {
// Enable 'startButton'
if (startButton != null) {
startButton.clicked.connect(() -> {
// Start the progress bar and disable the start button
startProgressBar();
startButton.setEnabled(false);
// Create a new thread to start the rendering process
new Thread(() -> {
System.out.println("Starting Prototype main");
Camera camera = getCameraFromGUI();
Expand All @@ -507,35 +501,34 @@ public Loader() {
}
System.out.println("Prototype main finished");

// Set render time
double renderTimeResult = Worker.getRenderTime();
renderTime.setText(String.format("%.2f", renderTimeResult) + "s");

// Set number of objects
int objects = Worker.getObjectsCount();
numberObjects.setText(String.valueOf(objects));

// Set number of light sources
int lightSource = Worker.getLightSourcesCount();
numberLightSource.setText(String.valueOf(lightSource));

// Set number of rays
int rays = Worker.getRaysCount();
numberRays.setText(String.valueOf(rays));


// Set quality information
qualtiyInfo.setText(width + "x" + height);

QGraphicsPixmapItem item = new QGraphicsPixmapItem(QPixmap.fromImage(image));
scene.clear();
scene.addItem(item);

// Switch to the result window
centralWidget.setCurrentIndex(3);

// Set the scene to the graphics view
resultGraphicsView.setScene(scene);
// Verwenden Sie die BoundingRect der Items
// Fit the view to the image
resultGraphicsView.setSceneRect(scene.itemsBoundingRect());
// Damit das Bild nicht verzerrt wird und vollständig sichtbar bleibt
// Set the resize anchor to the center of the view
resultGraphicsView.setResizeAnchor(QGraphicsView.ViewportAnchor.AnchorViewCenter);
resultGraphicsView.fitInView(scene.itemsBoundingRect(), AspectRatioMode.KeepAspectRatio);

// Enable the start button again and stop the progress bar
startButton.setEnabled(true);
stopProgressBar();
progressBar.setTextVisible(true);
Expand Down Expand Up @@ -567,7 +560,7 @@ private void startProgressBar() {
}

/**
* Stops the progress bar and sets the text to "Ready".
* Stops the progress bar.
*/

private void stopProgressBar() {
Expand All @@ -576,6 +569,11 @@ private void stopProgressBar() {
progressBar.setValue(0);
}

/**
* Returns the camera object with the entered values from the GUI, or default values.
* @return Camera
*/

private Camera getCameraFromGUI() {
double roll = cameraRoll.value();
double pitch = cameraPitch.value();
Expand Down
6 changes: 6 additions & 0 deletions raytracer/src/main/java/rayrangers/raytracer/gui/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
public class Worker {

// Variables for the results
private static double renderTime;
private static int objectCount;
private static int lightSourceCount;
Expand Down Expand Up @@ -68,6 +69,7 @@ public static QImage renderJSON(String filePath) {
QImage result;
long start = System.currentTimeMillis();

// Parse the scene from the JSON-file
Scene scene = null;
UUID cameraUUID = null;
try {
Expand All @@ -86,6 +88,7 @@ public static QImage renderJSON(String filePath) {
renderTime = (end - start) / 1000.0;
System.out.printf("Total runtime for rendering: %f s%n", renderTime);

// just for testing
//objectCount = entities.size();
//System.out.println("Number of objects: " + objectCount);

Expand Down Expand Up @@ -133,6 +136,7 @@ public static QImage invokePrototype(Camera camera) throws Exception {
TrafoMatrix tmTea = new TrafoMatrix(-50, -100, 10, -90, 10, -33, 1, 1, 1);
teapot.transform(tmTea);

// Add camera to the scene
scene.addCamera(camera);

// Add light sources to the scene
Expand Down Expand Up @@ -162,6 +166,7 @@ public static QImage invokePrototype(Camera camera) throws Exception {
System.out.println("Teapot not added to the list of entities.");
}

// Render the scene
Renderer renderer = new Renderer(scene, camera.getUuid());
ViewPane viewPane = camera.getViewPane();
renderer.render();
Expand All @@ -179,6 +184,7 @@ public static QImage invokePrototype(Camera camera) throws Exception {
}
}

// Print out the results in console
long end = System.currentTimeMillis();
renderTime = (end - start) / 1000.0;
System.out.printf("Total runtime for rendering: %f s%n", renderTime);
Expand Down

0 comments on commit 0a7d837

Please sign in to comment.