This commit is contained in:
2026-05-04 16:24:48 +02:00
parent 55a76be186
commit 0549437776
7 changed files with 610 additions and 60 deletions

View File

@@ -0,0 +1,129 @@
#pragma once
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include "services/textureCache.hpp"
#include "widgets/weather.hpp"
#include "gtkmm/box.h"
#include "gtkmm/enums.h"
#include "gtkmm/image.h"
std::string formatTemperature(double temp) {
return std::to_string(static_cast<int>(std::round(temp))) + "°C";
}
class WeatherInfo : public Gtk::Box {
private:
Gtk::Image getImageFromWeatherCode(int weather_code, bool is_daytime = true) {
Gtk::Image pic;
auto file = std::ifstream("resources/weatherCodes.json");
if (!file.is_open()) {
auto homeDir = std::getenv("HOME");
std::cout << "Failed to open local weatherCodes.json, trying " << std::string(homeDir) + "/.config/bar/weatherCodes.json" << std::endl;
file = std::ifstream(std::string(homeDir) + "/.config/bar/weatherCodes.json");
if (!file.is_open()) {
std::cerr << "Failed to open weatherCodes.json" << std::endl;
return pic;
}
}
auto json = nlohmann::json::parse(file);
file.close();
std::string image_path = json[std::to_string(weather_code)][is_daytime ? "day" : "night"]["image"].get<std::string>();
auto texture = TextureCacheService::getInstance()->getTexture(image_path);
if (texture) {
pic.set(texture);
}
pic.set_pixel_size(64);
return pic;
}
public:
WeatherInfo(WeatherWidget::CurrentWeather current_weather) {
set_orientation(Gtk::Orientation::HORIZONTAL);
this->set_valign(Gtk::Align::CENTER);
this->set_halign(Gtk::Align::FILL);
auto image = getImageFromWeatherCode(current_weather.weather_code);
auto temp = Gtk::Label();
temp.set_markup("<span weight='bold'>" + formatTemperature(current_weather.temperature) + "</span>" + "<span color='gray' size='small'> (" + formatTemperature(current_weather.apparent_temperature) + ")</span>");
temp.set_halign(Gtk::Align::START);
auto temp_box = Gtk::Box(Gtk::Orientation::VERTICAL);
temp_box.set_valign(Gtk::Align::CENTER);
temp_box.set_halign(Gtk::Align::START);
temp_box.append(temp);
append(image);
append(temp_box);
}
WeatherInfo(WeatherWidget::HourlyWeather hourly_weather) {
set_orientation(Gtk::Orientation::HORIZONTAL);
this->set_valign(Gtk::Align::CENTER);
add_css_class("weather-info");
auto image = getImageFromWeatherCode(hourly_weather.weather_code);
auto temp = Gtk::Label();
temp.set_markup("<span weight='bold'>" + formatTemperature(hourly_weather.temperature_2m) + "</span>" + "<span color='gray' size='small'> (" + formatTemperature(hourly_weather.apparent_temperature) + ")</span>");
temp.set_halign(Gtk::Align::START);
Gtk::Box temp_box(Gtk::Orientation::VERTICAL);
temp_box.set_halign(Gtk::Align::START);
temp_box.set_valign(Gtk::Align::CENTER);
temp_box.append(temp);
auto timeLabel = Gtk::Label();
timeLabel.set_markup("<span size='small'>" + hourly_weather.time + "</span>");
timeLabel.set_halign(Gtk::Align::START);
temp_box.append(timeLabel);
append(image);
append(temp_box);
}
WeatherInfo(WeatherWidget::DailyWeather daily_weather) {
set_orientation(Gtk::Orientation::HORIZONTAL);
this->set_valign(Gtk::Align::CENTER);
add_css_class("weather-info");
auto image = getImageFromWeatherCode(daily_weather.weather_code);
auto max_label = Gtk::Label();
max_label.set_markup("<span weight='bold'>H: " + formatTemperature(daily_weather.temperature_2m_max) + "</span>" + "<span color='gray' size='small'> (" + formatTemperature(daily_weather.apparent_temperature_max) + ")</span>");
max_label.set_halign(Gtk::Align::START);
auto min_label = Gtk::Label();
min_label.set_markup("<span size='small'>L: " + formatTemperature(daily_weather.temperature_2m_min) + "</span>" + "<span color='gray' size='small'> (" + formatTemperature(daily_weather.apparent_temperature_min) + ")</span>");
min_label.set_halign(Gtk::Align::START);
Gtk::Box temp_box(Gtk::Orientation::VERTICAL);
temp_box.set_halign(Gtk::Align::START);
temp_box.set_valign(Gtk::Align::CENTER);
temp_box.append(max_label);
temp_box.append(min_label);
auto timeLabel = Gtk::Label();
timeLabel.set_markup("<span size='small'>" + daily_weather.time + "</span>");
timeLabel.set_halign(Gtk::Align::START);
temp_box.append(timeLabel);
append(image);
append(temp_box);
}
};