KWeatherCore

kweathercore.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020-2021 Han Young <[email protected]>
3  * SPDX-FileCopyrightText: 2020-2021 Devin Lin <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 
8 #include "kweathercore_p.h"
9 
10 namespace KWeatherCore
11 {
12 KWeatherCorePrivate::KWeatherCorePrivate(QObject *parent)
13  : QObject(parent)
14 {
15 }
16 
17 Polygon KWeatherCorePrivate::stringToPolygon(const QString &str)
18 {
19  Polygon res;
20  const auto pairList = str.split(QLatin1Char(' '));
21  for (auto &pair : pairList) {
22  auto coordinate = pair.split(QLatin1Char(','));
23  res.push_back({coordinate.front().toFloat(), coordinate.back().toFloat()});
24  }
25  return res;
26 }
27 
28 QString KWeatherCorePrivate::toFixedString(double num)
29 {
30  std::ostringstream oss;
31  oss << std::fixed << std::setprecision(2) << num;
32  return QString::fromStdString(oss.str());
33 }
34 
35 QDir KWeatherCorePrivate::getCacheDirectory(double latitude, double longitude)
36 {
37 #ifdef Q_OS_ANDROID
38  QDir dir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/cache/") + toFixedString(latitude) + QStringLiteral("/")
39  + toFixedString(longitude));
40  if (!dir.exists())
41  dir.mkpath(QStringLiteral("."));
42  return dir;
43 #else
44  QDir dir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/kweather/cache/") + toFixedString(latitude)
45  + QStringLiteral("/") + toFixedString(longitude));
46  if (!dir.exists()) {
47  dir.mkpath(QStringLiteral("."));
48  }
49  return dir;
50 #endif
51 }
52 
53 AlertInfo::Urgency KWeatherCorePrivate::urgencyStringToEnum(const QString &str)
54 {
55  if (str == QStringLiteral("Immediate")) {
56  return AlertInfo::Urgency::Immediate;
57  } else if (str == QStringLiteral("Expected")) {
58  return AlertInfo::Urgency::Expected;
59  } else if (str == QStringLiteral("Future")) {
60  return AlertInfo::Urgency::Future;
61  } else if (str == QStringLiteral("Past")) {
62  return AlertInfo::Urgency::Past;
63  } else {
64  return AlertInfo::Urgency::Unknown;
65  }
66 }
67 
68 AlertInfo::Severity KWeatherCorePrivate::severityStringToEnum(const QString &str)
69 {
70  if (str == QStringLiteral("Extreme")) {
71  return AlertInfo::Severity::Extreme;
72  } else if (str == QStringLiteral("Severe")) {
73  return AlertInfo::Severity::Severe;
74  } else if (str == QStringLiteral("Moderate")) {
75  return AlertInfo::Severity::Moderate;
76  } else if (str == QStringLiteral("Minor")) {
77  return AlertInfo::Severity::Minor;
78  } else {
79  return AlertInfo::Severity::Unknown;
80  }
81 }
82 
83 AlertInfo::Certainty KWeatherCorePrivate::certaintyStringToEnum(const QString &str)
84 {
85  if (str == QStringLiteral("Observed")) {
86  return AlertInfo::Certainty::Observed;
87  } else if (str == QStringLiteral("Likely")) {
88  return AlertInfo::Certainty::Likely;
89  } else if (str == QStringLiteral("Possible")) {
90  return AlertInfo::Certainty::Possible;
91  } else if (str == QStringLiteral("Unlikely")) {
92  return AlertInfo::Certainty::Unlikely;
93  } else {
94  return AlertInfo::Certainty::Unknown;
95  }
96 }
97 
98 QString KWeatherCorePrivate::urgencyToString(AlertInfo::Urgency urgency)
99 {
100  QString res;
101  switch (urgency) {
102  case AlertInfo::Urgency::Immediate:
103  res = i18n("Immediate");
104  break;
105  case AlertInfo::Urgency::Expected:
106  res = i18n("Expected");
107  break;
108  case AlertInfo::Urgency::Future:
109  res = i18n("Future");
110  break;
111  case AlertInfo::Urgency::Past:
112  res = i18n("Past");
113  break;
114  case AlertInfo::Urgency::Unknown:
115  res = i18n("Unknown");
116  }
117  return res;
118 }
119 
120 QString KWeatherCorePrivate::severityToString(AlertInfo::Severity severity)
121 {
122  QString res;
123  switch (severity) {
124  case AlertInfo::Severity::Extreme:
125  res = i18n("Extreme");
126  break;
127  case AlertInfo::Severity::Severe:
128  res = i18n("Severe");
129  break;
130  case AlertInfo::Severity::Moderate:
131  res = i18n("Moderate");
132  break;
133  case AlertInfo::Severity::Minor:
134  res = i18n("Minor");
135  break;
136  case AlertInfo::Severity::Unknown:
137  res = i18n("Unknown");
138  }
139  return res;
140 }
141 
142 QString KWeatherCorePrivate::certaintyToString(AlertInfo::Certainty certainty)
143 {
144  QString res;
145  switch (certainty) {
146  case AlertInfo::Certainty::Observed:
147  res = i18n("Observed");
148  break;
149  case AlertInfo::Certainty::Likely:
150  res = i18n("Likely");
151  break;
152  case AlertInfo::Certainty::Possible:
153  res = i18n("Possible");
154  break;
155  case AlertInfo::Certainty::Unlikely:
156  res = i18n("Unlikely");
157  break;
158  case AlertInfo::Certainty::Unknown:
159  res = i18n("Unknown");
160  }
161  return res;
162 }
163 
164 int KWeatherCorePrivate::weatherIconPriorityRank(const QString &icon)
165 {
166  return WEATHER_ICON_PRIORITY_RANK[icon];
167 }
168 
169 ResolvedWeatherDesc KWeatherCorePrivate::resolveAPIWeatherDesc(const QString &desc)
170 {
171  return WEATHER_API_DESC_MAP[desc];
172 }
173 
174 }
QString writableLocation(QStandardPaths::StandardLocation type)
void push_back(const T &value)
QString fromStdString(const std::string &str)
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString i18n(const char *text, const TYPE &arg...)
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:05:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.