File indexing completed on 2024-05-05 16:49:21

0001 /*
0002  * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com>
0003  * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 #include "hourlyweatherforecast.h"
0008 namespace KWeatherCore
0009 {
0010 class HourlyWeatherForecast::HourlyWeatherForecastPrivate
0011 {
0012 public:
0013     QDateTime date = QDateTime::currentDateTime();
0014     QString weatherDescription = QStringLiteral("Unknown");
0015     QString weatherIcon = QStringLiteral("weather-none-available");
0016     // weather icon without time of day
0017     QString neutralWeatherIcon = QStringLiteral("weather-none-available");
0018     QString symbolCode;
0019     double temperature = 0; // celsius
0020     double pressure = 0; // hPa
0021     WindDirection windDirection = WindDirection::E;
0022     double windSpeed = 0; // m/s
0023     double humidity = 0; // %
0024     double fog = 0; // %
0025     double uvIndex = 0; // 0-1
0026     double precipitationAmount = 0; // mm
0027 };
0028 HourlyWeatherForecast::HourlyWeatherForecast(const QDateTime &date)
0029     : d(std::make_unique<HourlyWeatherForecastPrivate>())
0030 {
0031     d->date = date;
0032 }
0033 HourlyWeatherForecast::HourlyWeatherForecast(HourlyWeatherForecast &&other) =
0034     default;
0035 HourlyWeatherForecast::~HourlyWeatherForecast() = default;
0036 HourlyWeatherForecast::HourlyWeatherForecast(const HourlyWeatherForecast &other)
0037     : d(std::make_unique<HourlyWeatherForecastPrivate>())
0038 {
0039     *d = *other.d;
0040 }
0041 HourlyWeatherForecast &
0042 HourlyWeatherForecast::operator=(HourlyWeatherForecast &&other) = default;
0043 QJsonObject HourlyWeatherForecast::toJson() const
0044 {
0045     QJsonObject obj;
0046     obj[QStringLiteral("date")] = date().toString(Qt::ISODate);
0047     obj[QStringLiteral("weatherDescription")] = weatherDescription();
0048     obj[QStringLiteral("weatherIcon")] = weatherIcon();
0049     obj[QStringLiteral("neutralWeatherIcon")] = neutralWeatherIcon();
0050     obj[QStringLiteral("temperature")] = temperature();
0051     obj[QStringLiteral("pressure")] = pressure();
0052     obj[QStringLiteral("windDirection")] = static_cast<int>(windDirection());
0053     obj[QStringLiteral("windSpeed")] = windSpeed();
0054     obj[QStringLiteral("humidity")] = humidity();
0055     obj[QStringLiteral("fog")] = fog();
0056     obj[QStringLiteral("uvIndex")] = uvIndex();
0057     obj[QStringLiteral("precipitationAmount")] = precipitationAmount();
0058     return obj;
0059 }
0060 HourlyWeatherForecast HourlyWeatherForecast::fromJson(QJsonObject obj)
0061 {
0062     HourlyWeatherForecast ret(QDateTime::fromString(
0063         obj[QStringLiteral("date")].toString(), Qt::ISODate));
0064     ret.setWeatherDescription(
0065         obj[QStringLiteral("weatherDescription")].toString());
0066     ret.setWeatherIcon(obj[QStringLiteral("weatherIcon")].toString());
0067     ret.setNeutralWeatherIcon(
0068         obj[QStringLiteral("neutralWeatherIcon")].toString());
0069     ret.setTemperature(obj[QStringLiteral("temperature")].toDouble());
0070     ret.setPressure(obj[QStringLiteral("pressure")].toDouble());
0071     ret.setWindDirection(static_cast<WindDirection>(
0072         obj[QStringLiteral("windDirection")].toInt()));
0073     ret.setWindSpeed(obj[QStringLiteral("windSpeed")].toDouble());
0074     ret.setHumidity(obj[QStringLiteral("humidity")].toDouble());
0075     ret.setFog(obj[QStringLiteral("fog")].toDouble());
0076     ret.setUvIndex(obj[QStringLiteral("uvIndex")].toDouble());
0077     ret.setPrecipitationAmount(
0078         obj[QStringLiteral("precipitationAmount")].toDouble());
0079     return ret;
0080 }
0081 const QDateTime &HourlyWeatherForecast::date() const
0082 {
0083     return d->date;
0084 }
0085 void HourlyWeatherForecast::setDate(const QDateTime &date)
0086 {
0087     d->date = std::move(date);
0088 }
0089 const QString &HourlyWeatherForecast::weatherDescription() const
0090 {
0091     return d->weatherDescription;
0092 }
0093 void HourlyWeatherForecast::setWeatherDescription(
0094     const QString &weatherDescription)
0095 {
0096     d->weatherDescription = weatherDescription;
0097 }
0098 const QString &HourlyWeatherForecast::weatherIcon() const
0099 {
0100     return d->weatherIcon;
0101 }
0102 void HourlyWeatherForecast::setWeatherIcon(const QString &weatherIcon)
0103 {
0104     d->weatherIcon = weatherIcon;
0105 }
0106 const QString &HourlyWeatherForecast::neutralWeatherIcon() const
0107 {
0108     return d->neutralWeatherIcon;
0109 }
0110 void HourlyWeatherForecast::setNeutralWeatherIcon(
0111     const QString &neutralWeatherIcon)
0112 {
0113     d->neutralWeatherIcon = neutralWeatherIcon;
0114 }
0115 const QString &HourlyWeatherForecast::symbolCode() const
0116 {
0117     return d->symbolCode;
0118 }
0119 void HourlyWeatherForecast::setSymbolCode(const QString &symbolCode)
0120 {
0121     d->symbolCode = symbolCode;
0122 }
0123 double HourlyWeatherForecast::temperature() const
0124 {
0125     return d->temperature;
0126 }
0127 void HourlyWeatherForecast::setTemperature(double temperature)
0128 {
0129     d->temperature = temperature;
0130 }
0131 double HourlyWeatherForecast::pressure() const
0132 {
0133     return d->pressure;
0134 }
0135 void HourlyWeatherForecast::setPressure(double pressure)
0136 {
0137     d->pressure = pressure;
0138 }
0139 WindDirection HourlyWeatherForecast::windDirection() const
0140 {
0141     return d->windDirection;
0142 }
0143 QString HourlyWeatherForecast::windDirectionStr() const
0144 {
0145     switch (windDirection()) {
0146     case WindDirection::E:
0147         return QStringLiteral("E");
0148     case WindDirection::N:
0149         return QStringLiteral("N");
0150     case WindDirection::NE:
0151         return QStringLiteral("NE");
0152     case WindDirection::NW:
0153         return QStringLiteral("NW");
0154     case WindDirection::S:
0155         return QStringLiteral("S");
0156     case WindDirection::SE:
0157         return QStringLiteral("SE");
0158     case WindDirection::SW:
0159         return QStringLiteral("SW");
0160     case WindDirection::W:
0161         return QStringLiteral("W");
0162     }
0163     return QStringLiteral("E");
0164 }
0165 void HourlyWeatherForecast::setWindDirection(WindDirection windDirection)
0166 {
0167     d->windDirection = windDirection;
0168 }
0169 void HourlyWeatherForecast::setWindDirectionStr(const QString &windDirection)
0170 {
0171     if (windDirection == QStringLiteral("E"))
0172         setWindDirection(WindDirection::E);
0173     else if (windDirection == QStringLiteral("N"))
0174         setWindDirection(WindDirection::N);
0175     else if (windDirection == QStringLiteral("NE"))
0176         setWindDirection(WindDirection::NE);
0177     else if (windDirection == QStringLiteral("NW"))
0178         setWindDirection(WindDirection::NW);
0179     else if (windDirection == QStringLiteral("S"))
0180         setWindDirection(WindDirection::S);
0181     else if (windDirection == QStringLiteral("SE"))
0182         setWindDirection(WindDirection::SE);
0183     else if (windDirection == QStringLiteral("SW"))
0184         setWindDirection(WindDirection::SW);
0185     else if (windDirection == QStringLiteral("W"))
0186         setWindDirection(WindDirection::W);
0187 }
0188 double HourlyWeatherForecast::windSpeed() const
0189 {
0190     return d->windSpeed;
0191 }
0192 void HourlyWeatherForecast::setWindSpeed(double windSpeed)
0193 {
0194     d->windSpeed = windSpeed;
0195 }
0196 double HourlyWeatherForecast::humidity() const
0197 {
0198     return d->humidity;
0199 }
0200 void HourlyWeatherForecast::setHumidity(double humidity)
0201 {
0202     d->humidity = humidity;
0203 }
0204 double HourlyWeatherForecast::fog() const
0205 {
0206     return d->fog;
0207 }
0208 void HourlyWeatherForecast::setFog(double fog)
0209 {
0210     d->fog = fog;
0211 }
0212 double HourlyWeatherForecast::uvIndex() const
0213 {
0214     return d->uvIndex;
0215 }
0216 void HourlyWeatherForecast::setUvIndex(double uvIndex)
0217 {
0218     d->uvIndex = uvIndex;
0219 }
0220 double HourlyWeatherForecast::precipitationAmount() const
0221 {
0222     return d->precipitationAmount;
0223 }
0224 void HourlyWeatherForecast::setPrecipitationAmount(double precipitationAmount)
0225 {
0226     d->precipitationAmount = precipitationAmount;
0227 }
0228 bool HourlyWeatherForecast::operator==(
0229     const KWeatherCore::HourlyWeatherForecast &rhs) const
0230 {
0231     return (weatherDescription() == rhs.weatherDescription() &&
0232             weatherIcon() == rhs.weatherIcon() && date() == rhs.date());
0233 }
0234 
0235 HourlyWeatherForecast &
0236 HourlyWeatherForecast::operator=(const HourlyWeatherForecast &other)
0237 {
0238     *d = *other.d;
0239     return *this;
0240 }
0241 }