From 526ab00c33530aebf964e81c5a24e35fa749229d Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 25 Sep 2020 18:57:44 +0200 Subject: [PATCH] Finished Planet editing GUI --- src/CMakeLists.txt | 2 +- src/MainWindow.cpp | 22 +++++++- src/MainWindow.hpp | 5 ++ src/Planet.cpp | 20 ++++--- src/Planet.hpp | 4 ++ src/PlanetConfig.cpp | 38 ++++++++++++- src/PlanetConfig.hpp | 10 ++++ ui/PlanetConfig.ui | 128 +++++++++++++++++++++++++------------------ 8 files changed, 163 insertions(+), 66 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6e323c7..3bbe2aa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,7 +7,7 @@ cmake_minimum_required (VERSION 3.8) add_executable (GraviSim "main.cpp" "MainWindow.cpp" - "Screen.hpp" "Screen.cpp" "Planet.hpp" "Planet.cpp" "PlanetConfig.cpp") + "Screen.hpp" "Screen.cpp" "Planet.hpp" "Planet.cpp" "PlanetConfig.cpp" ) target_link_libraries(GraviSim Qt5::Widgets diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index d752ec2..7d88fa7 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -31,8 +31,9 @@ void MainWindow::OpenPlanetDialog(Planet* planet) activePlanet = nullptr; ui.config->Enable(); - ui.config->SetTitle("Planets don't have names yet"); + ui.config->SetTitle(planet->name); ui.config->SetRadius(planet->radius); + ui.config->SetColor(planet->GetColor()); ui.config->SetX(planet->position.rx()); ui.config->SetY(planet->position.ry()); @@ -45,17 +46,30 @@ void MainWindow::ClosePlanetDialog() ui.config->SetTitle("No planet selected"); ui.config->Disable(); + ui.config->SetColor(QColor(0, 0, 0, 0)); ui.config->SetRadius(0.f); ui.config->SetX(0.f); ui.config->SetY(0.f); } +void MainWindow::OnNameChanged(const QString& name) +{ + if (activePlanet != nullptr) + activePlanet->name = name; +} + void MainWindow::OnRadiusChanged(double radius) { if(activePlanet != nullptr) activePlanet->radius = radius; } +void MainWindow::OnColourChanged(const QColor& color) +{ + if (activePlanet != nullptr) + activePlanet->SetColor(color); +} + void MainWindow::OnXChanged(double x) { if (activePlanet != nullptr) @@ -67,3 +81,9 @@ void MainWindow::OnYChanged(double y) if (activePlanet != nullptr) activePlanet->position.ry() = y; } + +void MainWindow::OnToggle() +{ + isSimulating = !isSimulating; + ui.config->SetButtonLabel((isSimulating) ? "Stop Simulation" : "Start Simulation"); +} diff --git a/src/MainWindow.hpp b/src/MainWindow.hpp index 84fd373..55af7c0 100644 --- a/src/MainWindow.hpp +++ b/src/MainWindow.hpp @@ -18,9 +18,12 @@ public: void ClosePlanetDialog(); public slots: + void OnNameChanged(const QString& name); void OnRadiusChanged(double radius); + void OnColourChanged(const QColor& color); void OnXChanged(double x); void OnYChanged(double y); + void OnToggle(); private: MainWindow(); @@ -29,6 +32,8 @@ private: private: Ui::MainWindow ui; + bool isSimulating = false; + Planet* activePlanet; static MainWindow* instance; }; diff --git a/src/Planet.cpp b/src/Planet.cpp index f2ae0d3..069bb8b 100644 --- a/src/Planet.cpp +++ b/src/Planet.cpp @@ -20,6 +20,17 @@ void Planet::Select(bool select) outline.setColor(Qt::transparent); } +void Planet::SetColor(const QColor& color) +{ + circle = QBrush(color); + outline = QPen(Qt::transparent); + outline.setWidthF(4.f); + + int h, s, v, a; + color.getHsv(&h, &s, &v, &a); + selectionColor = QColor::fromHsv((h + 180) % 360, s, v, a); +} + void Planet::Draw(QPainter* painter) { painter->setBrush(circle); @@ -35,11 +46,6 @@ bool Planet::IsInside(QPointF point) const void Planet::Initialize(QColor color) { - circle = QBrush(color); - outline = QPen(Qt::transparent); - outline.setWidthF(4.f); - - int h, s, v, a; - color.getHsv(&h, &s, &v, &a); - selectionColor = QColor::fromHsv((h + 180) % 360, s, v, a); + name = "Unnamed"; + SetColor(color); } diff --git a/src/Planet.hpp b/src/Planet.hpp index b8a5047..2b8d8a9 100644 --- a/src/Planet.hpp +++ b/src/Planet.hpp @@ -13,6 +13,9 @@ public: void Resize(float newRadius) { radius = newRadius; } void Select(bool select); + void SetColor(const QColor& color); + const QColor& GetColor() { return circle.color(); } + void Draw(QPainter* painter); bool IsInside(QPointF point) const; @@ -22,6 +25,7 @@ private: public: QPointF position; + QString name; float radius; private: diff --git a/src/PlanetConfig.cpp b/src/PlanetConfig.cpp index af8afef..e9e043c 100644 --- a/src/PlanetConfig.cpp +++ b/src/PlanetConfig.cpp @@ -1,31 +1,41 @@ #include "PlanetConfig.hpp" +#include + #include #include "MainWindow.hpp" PlanetConfig::PlanetConfig(QWidget* parent) : - QWidget(parent) + QWidget(parent), temp(nullptr) { ui.setupUi(this); - ui.color = new QColorDialog(this); - MainWindow* instance = MainWindow::Instance(); + connect(ui.name, SIGNAL(returnPressed()), + this, SLOT(TextBoxSlot())); + connect(ui.radius, QOverload::of(&QDoubleSpinBox::valueChanged), instance, [instance](double d) { instance->OnRadiusChanged(d); }); + connect(ui.colour, SIGNAL(returnPressed()), + this, SLOT(ColourTBSlot())); + connect(ui.xPos, QOverload::of(&QDoubleSpinBox::valueChanged), instance, [instance](double d) { instance->OnXChanged(d); }); connect(ui.yPos, QOverload::of(&QDoubleSpinBox::valueChanged), instance, [instance](double d) { instance->OnYChanged(d); }); + connect(ui.toggle, SIGNAL(clicked()), + instance, SLOT(OnToggle())); + } void PlanetConfig::Disable() { ui.radius->setDisabled(true); + ui.colour->setDisabled(true); ui.xPos->setDisabled(true); ui.yPos->setDisabled(true); } @@ -33,6 +43,7 @@ void PlanetConfig::Disable() void PlanetConfig::Enable() { ui.radius->setDisabled(false); + ui.colour->setDisabled(false); ui.xPos->setDisabled(false); ui.yPos->setDisabled(false); } @@ -47,6 +58,11 @@ void PlanetConfig::SetRadius(double radius) ui.radius->setValue(radius); } +void PlanetConfig::SetColor(const QColor& color) +{ + ui.colour->setText(QString::number(color.rgb() << 8, 16)); +} + void PlanetConfig::SetX(double x) { ui.xPos->setValue(x); @@ -56,3 +72,19 @@ void PlanetConfig::SetY(double y) { ui.yPos->setValue(y); } + +void PlanetConfig::SetButtonLabel(const QString& label) +{ + ui.toggle->setText(label); +} + +void PlanetConfig::ColourTBSlot() +{ + // std::cout << ui.colour->text().toStdString() << std::endl; + MainWindow::Instance()->OnColourChanged(QColor::fromRgb(ui.colour->text().toUInt(NULL, 16))); +} + +void PlanetConfig::TextBoxSlot() +{ + MainWindow::Instance()->OnNameChanged(ui.name->text()); +} diff --git a/src/PlanetConfig.hpp b/src/PlanetConfig.hpp index 021f8eb..4f21ae2 100644 --- a/src/PlanetConfig.hpp +++ b/src/PlanetConfig.hpp @@ -6,6 +6,8 @@ class PlanetConfig : public QWidget { + Q_OBJECT + public: PlanetConfig(QWidget* parent); @@ -14,9 +16,17 @@ public: void SetTitle(const QString& title); void SetRadius(double radius); + void SetColor(const QColor& color); void SetX(double x); void SetY(double y); + void SetButtonLabel(const QString& label); + +public slots: + void TextBoxSlot(); + void ColourTBSlot(); + private: Ui::Form ui; + QWidget* temp; }; \ No newline at end of file diff --git a/ui/PlanetConfig.ui b/ui/PlanetConfig.ui index 3e613eb..8b87d8e 100644 --- a/ui/PlanetConfig.ui +++ b/ui/PlanetConfig.ui @@ -6,8 +6,8 @@ 0 0 - 436 - 567 + 308 + 380 @@ -17,38 +17,32 @@ false - background-color: rgb(222, 222, 222); + - - + + + + 12 + + + QLayout::SetNoConstraint + - - - - 0 - 0 - - + - Arial - 12 + Courier New + 10 75 true - - TextLabel + + background-color: rgb(255, 255, 255); - - Qt::AutoText - - - Qt::AlignHCenter|Qt::AlignTop - - - true + + 24 @@ -72,20 +66,23 @@ + + background-color: rgb(255, 255, 255); + 1000.000000000000000 - 0.100000000000000 + 1.000000000000000 - + - + Courier New @@ -101,25 +98,45 @@ - + + + + 0 + 0 + + + + Hhhhhh + + - - - - - Courier New - 12 - + + + + background-color: rgb(255, 255, 255); - - X + + 10000.000000000000000 - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + 1.000000000000000 + + + + + + + background-color: rgb(255, 255, 255); + + + 10000.989999999999782 + + + 1.000000000000000 @@ -139,28 +156,31 @@ - - - - 1000.000000000000000 + + + + + Courier New + 12 + - - 0.100000000000000 + + X - - - - - - 10000.000000000000000 - - - 0.100000000000000 + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + Start Simulation + + +