From 0bf630e57661a5739cbe97f8ac69c72454affbff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Sat, 2 Feb 2019 14:47:20 +0100 Subject: [PATCH] Further seperate MapWindow from MapApplication Rename MapAppAPIWrapper to MapWindowWrapper and MapApp to MapApplication --- pi2-demo/include/Bicycle.hpp | 4 +- pi2-demo/include/Car.hpp | 4 +- pi2-demo/include/Junction.hpp | 4 +- pi2-demo/include/Lane.hpp | 4 +- pi2-demo/include/Map.hpp | 8 +-- ...AppAPIWrapper.hpp => MapWindowWrapper.hpp} | 10 ++-- pi2-demo/include/Vehicle.hpp | 6 +-- pi2-demo/meson.build | 10 ++-- pi2-demo/src/Bicycle.cpp | 8 +-- pi2-demo/src/Car.cpp | 8 +-- pi2-demo/src/Junction.cpp | 6 +-- pi2-demo/src/Lane.cpp | 6 +-- pi2-demo/src/Map.cpp | 21 ++++---- pi2-demo/src/MapAppAPIWrapper.cpp | 44 --------------- pi2-demo/src/MapWindowWrapper.cpp | 44 +++++++++++++++ pi2-view/include/MapApp.hpp | 35 ------------ pi2-view/include/MapApplication.hpp | 20 +++++++ pi2-view/include/pi2-view.h | 48 ++++++++--------- pi2-view/meson.build | 4 +- pi2-view/src/MapApp.cpp | 53 ------------------ pi2-view/src/MapApplication.cpp | 28 ++++++++++ pi2-view/src/MapWindow.cpp | 6 +-- pi2-view/src/pi2-view.cpp | 54 +++++++++++-------- 23 files changed, 202 insertions(+), 233 deletions(-) rename pi2-demo/include/{MapAppAPIWrapper.hpp => MapWindowWrapper.hpp} (82%) delete mode 100644 pi2-demo/src/MapAppAPIWrapper.cpp create mode 100644 pi2-demo/src/MapWindowWrapper.cpp delete mode 100644 pi2-view/include/MapApp.hpp create mode 100644 pi2-view/include/MapApplication.hpp delete mode 100644 pi2-view/src/MapApp.cpp create mode 100644 pi2-view/src/MapApplication.cpp diff --git a/pi2-demo/include/Bicycle.hpp b/pi2-demo/include/Bicycle.hpp index 71e0036..f52e840 100644 --- a/pi2-demo/include/Bicycle.hpp +++ b/pi2-demo/include/Bicycle.hpp @@ -3,13 +3,13 @@ #include "Vehicle.hpp" #include #include -class MapAppAPIWrapper; +class MapWindowWrapper; class Bicycle : public Vehicle { public: Bicycle(std::string name, double topSpeed, double timeOfStart, double time); - void draw(MapAppAPIWrapper &mapApp, std::string_view laneName, + void draw(MapWindowWrapper &mapWindow, std::string_view laneName, double laneLength, double speedLimit) const override; double getSpeed(double speedLimit) const override; diff --git a/pi2-demo/include/Car.hpp b/pi2-demo/include/Car.hpp index b8b071e..c591eff 100644 --- a/pi2-demo/include/Car.hpp +++ b/pi2-demo/include/Car.hpp @@ -3,7 +3,7 @@ #include "Vehicle.hpp" #include #include -class MapAppAPIWrapper; +class MapWindowWrapper; class Car : public Vehicle { public: @@ -11,7 +11,7 @@ public: Car(std::string name, double topSpeed, double timeOfStart, double fuelConsumption, double fuelCapacity, double time); - void draw(MapAppAPIWrapper &mapApp, std::string_view laneName, + void draw(MapWindowWrapper &mapWindow, std::string_view laneName, double laneLength, double speedLimit) const override; double getSpeed(double speedLimit) const override; diff --git a/pi2-demo/include/Junction.hpp b/pi2-demo/include/Junction.hpp index 72d7ec5..83c09a5 100644 --- a/pi2-demo/include/Junction.hpp +++ b/pi2-demo/include/Junction.hpp @@ -8,7 +8,7 @@ #include #include class Vehicle; -class MapAppAPIWrapper; +class MapWindowWrapper; class Junction : public Named { public: @@ -25,7 +25,7 @@ public: void processVehicles(double time); - void drawVehicles(MapAppAPIWrapper &mapApp) const; + void drawVehicles(MapWindowWrapper &mapWindow) const; void createCar(std::string name, double topSpeed, double fuelConsumption, double fuelCapacity, double timeOfStart, double time); diff --git a/pi2-demo/include/Lane.hpp b/pi2-demo/include/Lane.hpp index e3413ff..8f85f9c 100644 --- a/pi2-demo/include/Lane.hpp +++ b/pi2-demo/include/Lane.hpp @@ -5,7 +5,7 @@ #include #include #include -class MapAppAPIWrapper; +class MapWindowWrapper; class Junction; class Lane : public Named { @@ -16,7 +16,7 @@ public: double speedLimit, bool noPassing, std::weak_ptr destinationJunction); - void drawVehicles(MapAppAPIWrapper &mapApp) const; + void drawVehicles(MapWindowWrapper &mapWindow) const; void processVehicles(double time); diff --git a/pi2-demo/include/Map.hpp b/pi2-demo/include/Map.hpp index fb9d6d8..c9e334f 100644 --- a/pi2-demo/include/Map.hpp +++ b/pi2-demo/include/Map.hpp @@ -1,6 +1,6 @@ #pragma once -#include "MapAppAPIWrapper.hpp" +#include "MapWindowWrapper.hpp" #include #include #include @@ -18,19 +18,19 @@ public: private: double time; std::vector> junctions; - MapAppAPIWrapper mapApp; + MapWindowWrapper mapWindow; std::weak_ptr getJunction(std::string_view name); /// Required format: /// `name` `fuelAmount` `x` `y` - void extractJunctionAndDraw(std::istream &is, MapAppAPIWrapper &mapApp); + void extractJunctionAndDraw(std::istream &is, MapWindowWrapper &mapWindow); /// Required format: /// `nameJunctionA` `nameJunctionB` `nameLaneAToB` `nameLaneBToA` /// `length in km` `speedLimit in {1,2,3}` `noPassing` `coordinateCount` /// `coordinates...` - void extractRoadAndDraw(std::istream &is, MapAppAPIWrapper &mapApp); + void extractRoadAndDraw(std::istream &is, MapWindowWrapper &mapWindow); /// Required format: /// `name` `topSpeed` `nameStartJunction` `timeOfStart` diff --git a/pi2-demo/include/MapAppAPIWrapper.hpp b/pi2-demo/include/MapWindowWrapper.hpp similarity index 82% rename from pi2-demo/include/MapAppAPIWrapper.hpp rename to pi2-demo/include/MapWindowWrapper.hpp index 84388eb..e2e5399 100644 --- a/pi2-demo/include/MapAppAPIWrapper.hpp +++ b/pi2-demo/include/MapWindowWrapper.hpp @@ -3,12 +3,12 @@ #include #include #include -struct MapApp; +struct MapWindow; -class MapAppAPIWrapper { +class MapWindowWrapper { public: - MapAppAPIWrapper(std::string_view windowTitle); - virtual ~MapAppAPIWrapper(); + MapWindowWrapper(std::string_view windowTitle); + virtual ~MapWindowWrapper(); void addJunction(double x, double y); void addRoad(std::string_view laneThereName, std::string_view laneBackName, const std::vector &coordinates); @@ -21,5 +21,5 @@ public: void setTimeLabel(std::chrono::minutes time); private: - MapApp *mapApp; + MapWindow *mapWindow; }; diff --git a/pi2-demo/include/Vehicle.hpp b/pi2-demo/include/Vehicle.hpp index c22c97e..88f4179 100644 --- a/pi2-demo/include/Vehicle.hpp +++ b/pi2-demo/include/Vehicle.hpp @@ -3,13 +3,13 @@ #include "Named.hpp" #include #include -class MapAppAPIWrapper; +class MapWindowWrapper; class Vehicle : public Named { public: - // Draws the vehicle on the provided \p mapApp on the specified \p laneName + // Draws the vehicle on the provided \p mapWindow on the specified \p laneName // which has a \p laneLength and a \p speedLimit - virtual void draw(MapAppAPIWrapper &mapApp, std::string_view laneName, + virtual void draw(MapWindowWrapper &mapWindow, std::string_view laneName, double laneLength, double speedLimit) const = 0; /// \returns The current speed in km/h diff --git a/pi2-demo/meson.build b/pi2-demo/meson.build index 1d6b9cf..559c223 100644 --- a/pi2-demo/meson.build +++ b/pi2-demo/meson.build @@ -1,14 +1,14 @@ pi2_demo_inc = include_directories('include') pi2_demo_src = files( - 'src/Named.cpp', 'src/Bicycle.cpp', - 'src/Vehicle.cpp', - 'src/Junction.cpp', 'src/Car.cpp', + 'src/Junction.cpp', 'src/Lane.cpp', - 'src/Map.cpp', 'src/main.cpp', - 'src/MapAppAPIWrapper.cpp' + 'src/Map.cpp', + 'src/MapWindowWrapper.cpp', + 'src/Named.cpp', + 'src/Vehicle.cpp', ) boost_po_dep = dependency('boost', modules : ['program_options']) executable('pi2-demo', pi2_demo_src, diff --git a/pi2-demo/src/Bicycle.cpp b/pi2-demo/src/Bicycle.cpp index 920801a..99ff11c 100644 --- a/pi2-demo/src/Bicycle.cpp +++ b/pi2-demo/src/Bicycle.cpp @@ -1,5 +1,5 @@ #include "Bicycle.hpp" -#include "MapAppAPIWrapper.hpp" +#include "MapWindowWrapper.hpp" #include #include #include @@ -8,11 +8,11 @@ Bicycle::Bicycle(std::string name, const double topSpeed, const double timeOfStart, const double time) : Vehicle(std::move(name), topSpeed, timeOfStart, time) {} -void Bicycle::draw(MapAppAPIWrapper &mapApp, std::string_view laneName, +void Bicycle::draw(MapWindowWrapper &mapWindow, std::string_view laneName, const double laneLength, const double speedLimit) const { const auto positionOnLane = getDistanceOnLane() / laneLength; - mapApp.addOrReplaceBicycle(getName(), laneName, positionOnLane, - getSpeed(speedLimit)); + mapWindow.addOrReplaceBicycle(getName(), laneName, positionOnLane, + getSpeed(speedLimit)); } double Bicycle::getSpeed(const double speedLimit) const { diff --git a/pi2-demo/src/Car.cpp b/pi2-demo/src/Car.cpp index 1619ba9..7294065 100644 --- a/pi2-demo/src/Car.cpp +++ b/pi2-demo/src/Car.cpp @@ -1,5 +1,5 @@ #include "Car.hpp" -#include "MapAppAPIWrapper.hpp" +#include "MapWindowWrapper.hpp" #include #include @@ -10,11 +10,11 @@ Car::Car(std::string name, const double topSpeed, const double timeOfStart, fuelConsumption(fuelConsumption), fuelCapacity(fuelCapacity), fuel(fuelCapacity / 2) {} -void Car::draw(MapAppAPIWrapper &mapApp, std::string_view laneName, +void Car::draw(MapWindowWrapper &mapWindow, std::string_view laneName, const double laneLength, const double speedLimit) const { const auto positionOnLane = getDistanceOnLane() / laneLength; - mapApp.addOrReplaceCar(getName(), laneName, positionOnLane, - getSpeed(speedLimit), fuel); + mapWindow.addOrReplaceCar(getName(), laneName, positionOnLane, + getSpeed(speedLimit), fuel); } double Car::getSpeed(const double speedLimit) const { diff --git a/pi2-demo/src/Junction.cpp b/pi2-demo/src/Junction.cpp index 50703ac..0033fe9 100644 --- a/pi2-demo/src/Junction.cpp +++ b/pi2-demo/src/Junction.cpp @@ -5,7 +5,7 @@ #include #include #include -class MapAppAPIWrapper; +class MapWindowWrapper; void Junction::connect(const std::weak_ptr &junctionA, const std::weak_ptr &junctionB, @@ -33,9 +33,9 @@ void Junction::processVehicles(const double time) { } } -void Junction::drawVehicles(MapAppAPIWrapper &mapApp) const { +void Junction::drawVehicles(MapWindowWrapper &mapWindow) const { for (auto &&weg : outboundLanes) { - weg.drawVehicles(mapApp); + weg.drawVehicles(mapWindow); } } diff --git a/pi2-demo/src/Lane.cpp b/pi2-demo/src/Lane.cpp index ae99252..2415f54 100644 --- a/pi2-demo/src/Lane.cpp +++ b/pi2-demo/src/Lane.cpp @@ -6,7 +6,7 @@ #include #include #include -class MapAppAPIWrapper; +class MapWindowWrapper; // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) Lane::Lane(Lane &&other) noexcept = default; @@ -19,9 +19,9 @@ Lane::Lane(std::string name, std::string oppositeLaneName, const double length, oppositeLaneName(std::move(oppositeLaneName)), speedLimit(speedLimit), noPassing(noPassing) {} -void Lane::drawVehicles(MapAppAPIWrapper &mapApp) const { +void Lane::drawVehicles(MapWindowWrapper &mapWindow) const { for (auto &&fahrzeug : vehicles) { - fahrzeug->draw(mapApp, getName(), length, speedLimit); + fahrzeug->draw(mapWindow, getName(), length, speedLimit); } } diff --git a/pi2-demo/src/Map.cpp b/pi2-demo/src/Map.cpp index 3a93663..c43a1fc 100644 --- a/pi2-demo/src/Map.cpp +++ b/pi2-demo/src/Map.cpp @@ -1,6 +1,6 @@ #include "Map.hpp" #include "Junction.hpp" -#include "MapAppAPIWrapper.hpp" +#include "MapWindowWrapper.hpp" #include #include #include @@ -15,13 +15,13 @@ #include Map::Map(std::istream &is, std::string_view windowTitle) - : time(0), mapApp(windowTitle) { + : time(0), mapWindow(windowTitle) { std::string type; while (is >> type) { if (type == "KREUZUNG") { - extractJunctionAndDraw(is, mapApp); + extractJunctionAndDraw(is, mapWindow); } else if (type == "STRASSE") { - extractRoadAndDraw(is, mapApp); + extractRoadAndDraw(is, mapWindow); } else if (type == "PKW") { extractCar(is); } else if (type == "FAHRRAD") { @@ -38,9 +38,9 @@ void Map::simulate(const std::chrono::seconds duration, time += static_cast(duration.count()) / 60 / 60 / count; for (auto &&junction : junctions) { junction->processVehicles(time); - junction->drawVehicles(mapApp); + junction->drawVehicles(mapWindow); } - mapApp.setTimeLabel(std::chrono::minutes(static_cast(time * 60))); + mapWindow.setTimeLabel(std::chrono::minutes(static_cast(time * 60))); std::this_thread::sleep_for(std::chrono::nanoseconds( static_cast(std::nano::den / frequency))); } @@ -52,7 +52,8 @@ std::weak_ptr Map::getJunction(std::string_view name) { [&name](const auto &junction) { return junction->getName() == name; }); } -void Map::extractJunctionAndDraw(std::istream &is, MapAppAPIWrapper &mapApp) { +void Map::extractJunctionAndDraw(std::istream &is, + MapWindowWrapper &mapWindow) { std::string name; double fuelAmount; is >> name >> fuelAmount; @@ -61,10 +62,10 @@ void Map::extractJunctionAndDraw(std::istream &is, MapAppAPIWrapper &mapApp) { int x; int y; is >> x >> y; - mapApp.addJunction(x, y); + mapWindow.addJunction(x, y); } -void Map::extractRoadAndDraw(std::istream &is, MapAppAPIWrapper &mapApp) { +void Map::extractRoadAndDraw(std::istream &is, MapWindowWrapper &mapWindow) { std::string nameJunctionA; std::string nameJunctionB; std::string nameLaneAToB; @@ -103,7 +104,7 @@ void Map::extractRoadAndDraw(std::istream &is, MapAppAPIWrapper &mapApp) { is >> x; return x; }); - mapApp.addRoad(nameLaneAToB, nameLaneBToA, coordinates); + mapWindow.addRoad(nameLaneAToB, nameLaneBToA, coordinates); } void Map::extractBicycle(std::istream &is) { diff --git a/pi2-demo/src/MapAppAPIWrapper.cpp b/pi2-demo/src/MapAppAPIWrapper.cpp deleted file mode 100644 index e2b5b5e..0000000 --- a/pi2-demo/src/MapAppAPIWrapper.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "MapAppAPIWrapper.hpp" -#include -#include -#include - -MapAppAPIWrapper::MapAppAPIWrapper(std::string_view windowTitle) - : mapApp(mapAppCreate(windowTitle.data())) {} - -MapAppAPIWrapper::~MapAppAPIWrapper() { - mapAppDestroy(mapApp); - mapApp = nullptr; -} - -void MapAppAPIWrapper::addJunction(const double x, const double y) { - mapAppAddJunction(mapApp, x, y); -} - -void MapAppAPIWrapper::addRoad(std::string_view laneThereName, - std::string_view laneBackName, - const std::vector &coordinates) { - mapAppAddRoad(mapApp, laneThereName.data(), laneBackName.data(), - coordinates.data(), coordinates.size()); -} - -void MapAppAPIWrapper::addOrReplaceBicycle(std::string_view vehicleName, - std::string_view laneName, - const double positionOnLane, - const double speed) { - mapAppAddOrReplaceBicycle(mapApp, vehicleName.data(), laneName.data(), - positionOnLane, speed); -} - -void MapAppAPIWrapper::addOrReplaceCar(std::string_view vehicleName, - std::string_view laneName, - const double positionOnLane, - const double speed, - const double remainingFuel) { - mapAppAddOrReplaceCar(mapApp, vehicleName.data(), laneName.data(), - positionOnLane, speed, remainingFuel); -} - -void MapAppAPIWrapper::setTimeLabel(const std::chrono::minutes time) { - mapAppSetTimeLabel(mapApp, time.count()); -} diff --git a/pi2-demo/src/MapWindowWrapper.cpp b/pi2-demo/src/MapWindowWrapper.cpp new file mode 100644 index 0000000..1e6f1c0 --- /dev/null +++ b/pi2-demo/src/MapWindowWrapper.cpp @@ -0,0 +1,44 @@ +#include "MapWindowWrapper.hpp" +#include +#include +#include + +MapWindowWrapper::MapWindowWrapper(std::string_view windowTitle) + : mapWindow(mapWindowCreate(windowTitle.data())) {} + +MapWindowWrapper::~MapWindowWrapper() { + mapWindowDestroy(mapWindow); + mapWindow = nullptr; +} + +void MapWindowWrapper::addJunction(const double x, const double y) { + mapWindowAddJunction(mapWindow, x, y); +} + +void MapWindowWrapper::addRoad(std::string_view laneThereName, + std::string_view laneBackName, + const std::vector &coordinates) { + mapWindowAddRoad(mapWindow, laneThereName.data(), laneBackName.data(), + coordinates.data(), coordinates.size()); +} + +void MapWindowWrapper::addOrReplaceBicycle(std::string_view vehicleName, + std::string_view laneName, + const double positionOnLane, + const double speed) { + mapWindowAddOrReplaceBicycle(mapWindow, vehicleName.data(), laneName.data(), + positionOnLane, speed); +} + +void MapWindowWrapper::addOrReplaceCar(std::string_view vehicleName, + std::string_view laneName, + const double positionOnLane, + const double speed, + const double remainingFuel) { + mapWindowAddOrReplaceCar(mapWindow, vehicleName.data(), laneName.data(), + positionOnLane, speed, remainingFuel); +} + +void MapWindowWrapper::setTimeLabel(const std::chrono::minutes time) { + mapWindowSetTimeLabel(mapWindow, time.count()); +} diff --git a/pi2-view/include/MapApp.hpp b/pi2-view/include/MapApp.hpp deleted file mode 100644 index cca16d6..0000000 --- a/pi2-view/include/MapApp.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include "MapWindow.hpp" -#include -#include -#include -#include -#include -namespace Glib { -template class RefPtr; -} -namespace Gtk { -class Application; -} - -class MapApp { -public: - MapApp(std::string windowTitle); - virtual ~MapApp(); - void addJunction(double x, double y); - void addRoad(std::string laneThereName, std::string laneBackName, - std::vector> coordinates); - void addOrReplaceBicycle(const std::string &vehicleName, - const std::string &laneName, double positionOnLane, - double speed); - void addOrReplaceCar(const std::string &vehicleName, - const std::string &laneName, double positionOnLane, - double speed, double remainingFuel); - void setTimeLabel(std::chrono::minutes time); - -private: - static Glib::RefPtr app; - static std::thread thread; - MapWindow mapWindow; -}; diff --git a/pi2-view/include/MapApplication.hpp b/pi2-view/include/MapApplication.hpp new file mode 100644 index 0000000..1879b4f --- /dev/null +++ b/pi2-view/include/MapApplication.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +namespace Glib { +template class RefPtr; +} +namespace Gtk { +class Application; +class Window; +} // namespace Gtk + +class MapApplication { +public: + static void addWindow(Gtk::Window &window); + static void removeWindow(Gtk::Window &window); + +private: + static Glib::RefPtr application; + static std::thread thread; +}; diff --git a/pi2-view/include/pi2-view.h b/pi2-view/include/pi2-view.h index ca6e247..9bbab92 100644 --- a/pi2-view/include/pi2-view.h +++ b/pi2-view/include/pi2-view.h @@ -8,67 +8,67 @@ extern "C" { #endif /** - * The application to show a map, a list of vehicles and additional information + * A window showing a map, a list of vehicles and additional information */ -struct MapApp; +struct MapWindow; /** - * Creates a MapApp and opens the corresponding window. + * Creates and opens a MapWindow. * - * You *have* to call mapAppDestroy(MapApp *) to avoid memory leaks. + * You *have* to call mapWindowDestroy(MapWindow *) to avoid memory leaks. * - * \returns A pointer to the created MapApp + * \returns A pointer to the created MapWindow */ -MapApp *mapAppCreate(const char *windowTitle); +MapWindow *mapWindowCreate(const char *windowTitle); /** - * Destroys \p mapApp and frees its memory. + * Closes and destroys \p mapWindow and frees its memory. */ -void mapAppDestroy(MapApp *mapApp); +void mapWindowDestroy(MapWindow *mapWindow); /** - * Adds a junction to \p mapApp + * Adds a junction to \p mapWindow * * The coordinates \p x and \p y mark the middle of the junction and represent * amounts of pixels. */ -void mapAppAddJunction(MapApp *mapApp, double x, double y); +void mapWindowAddJunction(MapWindow *mapWindow, double x, double y); /** - * Adds a road to \p mapApp + * Adds a road to \p mapWindow * * \param coordinates A pointer to the begin of an array of the coordinates * The coordinates have to be alternating values of x and y in pixels. * \param coordinateCount The count of \p coordinates */ -void mapAppAddRoad(MapApp *mapApp, const char *laneThereName, - const char *laneBackName, const double *coordinates, - size_t coordinateCount); +void mapWindowAddRoad(MapWindow *mapWindow, const char *laneThereName, + const char *laneBackName, const double *coordinates, + size_t coordinateCount); /** - * Adds or replaces a bicycle to/in \p mapApp + * Adds or replaces a bicycle to/in \p mapWindow * * \param positionOnLane The relative position of the vehicle on its lane * has to be between 0 and 1. */ -void mapAppAddOrReplaceBicycle(MapApp *mapApp, const char *vehicleName, - const char *roadName, double positionOnLane, - double speed); +void mapWindowAddOrReplaceBicycle(MapWindow *mapWindow, const char *vehicleName, + const char *roadName, double positionOnLane, + double speed); /** - * Adds or replaces a car to/in \p mapApp + * Adds or replaces a car to/in \p mapWindow * * \param positionOnLane The relative position of the vehicle on its lane * has to be between 0 and 1. */ -void mapAppAddOrReplaceCar(MapApp *mapApp, const char *vehicleName, - const char *roadName, double positionOnLane, - double speed, double remainingFuel); +void mapWindowAddOrReplaceCar(MapWindow *mapWindow, const char *vehicleName, + const char *roadName, double positionOnLane, + double speed, double remainingFuel); /** - * Sets the time label of \p mapApp to \p minutes + * Sets the time label of \p mapWindow to \p minutes */ -void mapAppSetTimeLabel(MapApp *mapApp, int64_t minutes); +void mapWindowSetTimeLabel(MapWindow *mapWindow, int64_t minutes); #ifdef __cplusplus } diff --git a/pi2-view/meson.build b/pi2-view/meson.build index 9283ced..ea05dd9 100644 --- a/pi2-view/meson.build +++ b/pi2-view/meson.build @@ -4,10 +4,10 @@ pi2_view_src = files( 'src/DrawableJunction.cpp', 'src/DrawableRoad.cpp', 'src/DrawableVehicle.cpp', - 'src/MapApp.cpp', + 'src/MapApplication.cpp', 'src/MapArea.cpp', 'src/MapWindow.cpp', - 'src/pi2-view.cpp' + 'src/pi2-view.cpp', ) gnome = import('gnome') ui_resources = gnome.compile_resources( diff --git a/pi2-view/src/MapApp.cpp b/pi2-view/src/MapApp.cpp deleted file mode 100644 index 484e0e8..0000000 --- a/pi2-view/src/MapApp.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "MapApp.hpp" -#include -#include - -Glib::RefPtr MapApp::app = Gtk::Application::create(); -std::thread MapApp::thread; - -MapApp::MapApp(std::string windowTitle) : mapWindow(windowTitle) { - if (app->get_windows().empty()) { - thread = std::thread([this]() { - app = Gtk::Application::create(); - app->run(mapWindow); - }); - } else { - app->add_window(mapWindow); - mapWindow.show(); - } -} - -MapApp::~MapApp() { - app->remove_window(mapWindow); - if (thread.joinable() && app->get_windows().empty()) { - thread.join(); - } -} - -void MapApp::setTimeLabel(const std::chrono::minutes minutes) { - mapWindow.setTimeLabel(minutes); -} - -void MapApp::addJunction(const double x, const double y) { - mapWindow.addJunction(x, y); -} - -void MapApp::addRoad(std::string laneThereName, std::string laneBackName, - std::vector> coordinates) { - mapWindow.addRoad(laneThereName, laneBackName, coordinates); -} - -void MapApp::addOrReplaceBicycle(const std::string &vehicleName, - const std::string &laneName, - const double positionOnLane, - const double speed) { - mapWindow.addOrReplaceBicycle(vehicleName, laneName, positionOnLane, speed); -} - -void MapApp::addOrReplaceCar(const std::string &vehicleName, - const std::string &laneName, - const double positionOnLane, const double speed, - const double remainingFuel) { - mapWindow.addOrReplaceCar(vehicleName, laneName, positionOnLane, speed, - remainingFuel); -} diff --git a/pi2-view/src/MapApplication.cpp b/pi2-view/src/MapApplication.cpp new file mode 100644 index 0000000..7d4f891 --- /dev/null +++ b/pi2-view/src/MapApplication.cpp @@ -0,0 +1,28 @@ +#include "MapApplication.hpp" +#include +#include +#include +#include + +Glib::RefPtr MapApplication::application = + Gtk::Application::create(); +std::thread MapApplication::thread; + +void MapApplication::addWindow(Gtk::Window &window) { + if (application->get_windows().empty()) { + thread = std::thread([&window]() { + application = Gtk::Application::create(); + application->run(window); + }); + } else { + application->add_window(window); + window.show(); + } +} + +void MapApplication::removeWindow(Gtk::Window &window) { + application->remove_window(window); + if (thread.joinable() && application->get_windows().empty()) { + thread.join(); + } +} diff --git a/pi2-view/src/MapWindow.cpp b/pi2-view/src/MapWindow.cpp index 77a5b9c..2a4d639 100644 --- a/pi2-view/src/MapWindow.cpp +++ b/pi2-view/src/MapWindow.cpp @@ -129,11 +129,11 @@ void MapWindow::refreshVehicleLabels(const DrawableVehicle &vehicle) { remainingFuelLabel->set_text(remainingFuelBuffer.str()); } -void MapWindow::setTimeLabel(const std::chrono::minutes minutes) { +void MapWindow::setTimeLabel(const std::chrono::minutes time) { std::ostringstream timeBuffer; timeBuffer << std::setfill('0') << std::setw(2) - << std::chrono::duration_cast(minutes).count() - << ':' << std::setw(2) << minutes.count() % 60; + << std::chrono::duration_cast(time).count() + << ':' << std::setw(2) << time.count() % 60; Glib::signal_idle().connect_once( [this, timeString = timeBuffer.str()]() mutable { timeLabel->set_text(std::move(timeString)); diff --git a/pi2-view/src/pi2-view.cpp b/pi2-view/src/pi2-view.cpp index 4a4aeb4..e9d9366 100644 --- a/pi2-view/src/pi2-view.cpp +++ b/pi2-view/src/pi2-view.cpp @@ -1,8 +1,9 @@ #include "pi2-view.h" #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wmismatched-tags" -#include "MapApp.hpp" +#include "MapWindow.hpp" #pragma clang diagnostic pop +#include "MapApplication.hpp" #include #include #include @@ -12,19 +13,25 @@ #include #include -gsl::owner mapAppCreate(gsl::czstring<> windowTitle) { - return new MapApp(windowTitle); +gsl::owner mapWindowCreate(gsl::czstring<> windowTitle) { + auto mapWindow = new MapWindow(windowTitle); + MapApplication::addWindow(*mapWindow); + return mapWindow; } -void mapAppDestroy(gsl::owner mapApp) { delete mapApp; } +void mapWindowDestroy(gsl::owner mapWindow) { + MapApplication::removeWindow(*mapWindow); + delete mapWindow; +} -void mapAppAddJunction(MapApp *mapApp, const double x, const double y) { - mapApp->addJunction(x, y); +void mapWindowAddJunction(MapWindow *mapWindow, const double x, + const double y) { + mapWindow->addJunction(x, y); } -void mapAppAddRoad(MapApp *mapApp, gsl::czstring<> laneThereName, - gsl::czstring<> laneBackName, const double coordinates[], - const size_t coordinateCount) { +void mapWindowAddRoad(MapWindow *mapWindow, gsl::czstring<> laneThereName, + gsl::czstring<> laneBackName, const double coordinates[], + const size_t coordinateCount) { const gsl::span coordinatesSpan(coordinates, coordinateCount); std::vector> coordinateVector(coordinateCount / 2); std::generate(coordinateVector.begin(), coordinateVector.end(), @@ -32,24 +39,25 @@ void mapAppAddRoad(MapApp *mapApp, gsl::czstring<> laneThereName, return std::valarray( {coordinatesSpan.at(i++), coordinatesSpan(i++)}); }); - mapApp->addRoad(laneThereName, laneBackName, coordinateVector); + mapWindow->addRoad(laneThereName, laneBackName, coordinateVector); } -void mapAppAddOrReplaceBicycle(MapApp *mapApp, gsl::czstring<> vehicleName, - gsl::czstring<> roadName, - const double positionOnLane, - const double speed) { - mapApp->addOrReplaceBicycle(vehicleName, roadName, positionOnLane, speed); +void mapWindowAddOrReplaceBicycle(MapWindow *mapWindow, + gsl::czstring<> vehicleName, + gsl::czstring<> roadName, + const double positionOnLane, + const double speed) { + mapWindow->addOrReplaceBicycle(vehicleName, roadName, positionOnLane, speed); } -void mapAppAddOrReplaceCar(MapApp *mapApp, gsl::czstring<> vehicleName, - gsl::czstring<> roadName, - const double positionOnLane, const double speed, - const double remainingFuel) { - mapApp->addOrReplaceCar(vehicleName, roadName, positionOnLane, speed, - remainingFuel); +void mapWindowAddOrReplaceCar(MapWindow *mapWindow, gsl::czstring<> vehicleName, + gsl::czstring<> roadName, + const double positionOnLane, const double speed, + const double remainingFuel) { + mapWindow->addOrReplaceCar(vehicleName, roadName, positionOnLane, speed, + remainingFuel); } -void mapAppSetTimeLabel(MapApp *mapApp, int64_t minutes) { - mapApp->setTimeLabel(std::chrono::minutes(minutes)); +void mapWindowSetTimeLabel(MapWindow *mapWindow, int64_t minutes) { + mapWindow->setTimeLabel(std::chrono::minutes(minutes)); } -- 2.22.0