swift
cloudlayer.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QHash>
7 
8 using namespace swift::misc::aviation;
9 
10 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::weather, CCloudLayer)
11 
12 namespace swift::misc::weather
13 {
14 
15  CCloudLayer::CCloudLayer(const CAltitude &base, const CAltitude &top, Coverage coverage) : m_base(base), m_top(top)
16  {
17  setCoverage(coverage);
18  }
19 
20  CCloudLayer::CCloudLayer(const CAltitude &base, const CAltitude &top, double precipitationRate,
21  Precipitation precipitation, Clouds clouds, Coverage coverage)
22  : m_base(base), m_top(top), m_precipitationRate(precipitationRate), m_precipitation(precipitation),
23  m_clouds(clouds)
24  {
25  setCoverage(coverage);
26  }
27 
28  void CCloudLayer::setCoverage(Coverage coverage) { m_coveragePercent = 100 * coverage / 4; }
29 
31  {
32  if (m_coveragePercent > 85) { return Overcast; }
33  if (m_coveragePercent > 60 && m_coveragePercent <= 85) { return Broken; }
34  if (m_coveragePercent > 30 && m_coveragePercent <= 60) { return Scattered; }
35  if (m_coveragePercent > 10 && m_coveragePercent <= 30) { return Few; }
36  return None;
37  }
38 
40  {
41  if (index.isMyself()) { return QVariant::fromValue(*this); }
42  switch (index.frontCasted<ColumnIndex>())
43  {
44  case IndexBase: return QVariant::fromValue(m_base);
45  case IndexTop: return QVariant::fromValue(m_top);
46  case IndexPrecipitationRate: return QVariant::fromValue(m_precipitationRate);
47  case IndexPrecipitation: return QVariant::fromValue(m_precipitation);
48  case IndexClouds: return QVariant::fromValue(m_clouds);
49  case IndexCoveragePercent: return QVariant::fromValue(m_coveragePercent);
50  default: return CValueObject::propertyByIndex(index);
51  }
52  }
53 
54  void CCloudLayer::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
55  {
56  if (index.isMyself())
57  {
58  (*this) = variant.value<CCloudLayer>();
59  return;
60  }
61  switch (index.frontCasted<ColumnIndex>())
62  {
63  case IndexBase: setBase(variant.value<CAltitude>()); break;
64  case IndexTop: setTop(variant.value<CAltitude>()); break;
65  case IndexPrecipitationRate: setPrecipitationRate(variant.value<int>()); break;
66  case IndexPrecipitation: setPrecipitation(variant.value<Precipitation>()); break;
67  case IndexClouds: setClouds(variant.value<Clouds>()); break;
68  case IndexCoveragePercent: setCoveragePercent(variant.value<int>()); break;
69  default: CValueObject::setPropertyByIndex(index, variant); break;
70  }
71  }
72 
73  QString CCloudLayer::convertToQString(bool ) const
74  {
75  static const QHash<Coverage, QString> hash = {
76  { None, "" }, { Few, "few" }, { Scattered, "scattered" }, { Broken, "broken" }, { Overcast, "overcast" }
77  };
78 
79  return QStringLiteral("%1 from %2 to %3").arg(hash.value(getCoverage()), m_base.toQString(), m_top.toQString());
80  }
81 
82 } // namespace swift::misc::weather
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
Altitude as used in aviation, can be AGL or MSL altitude.
Definition: altitude.h:52
ColumnIndex
Base class enums.
Definition: mixinindex.h:44
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Value object for a cloud layer.
Definition: cloudlayer.h:27
Precipitation
Precipitation Type.
Definition: cloudlayer.h:53
void setBase(const misc::aviation::CAltitude &base)
Set base.
Definition: cloudlayer.h:82
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: cloudlayer.cpp:73
CCloudLayer()=default
Default constructor.
void setCoverage(Coverage coverage)
Set coverage.
Definition: cloudlayer.cpp:28
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: cloudlayer.cpp:54
Coverage getCoverage() const
Get coverage.
Definition: cloudlayer.cpp:30
void setPrecipitationRate(double rate)
Set precipitation rate in mm/h.
Definition: cloudlayer.h:94
void setClouds(Clouds type)
Set cloud type.
Definition: cloudlayer.h:106
void setCoveragePercent(int coverage)
Set coverage in %.
Definition: cloudlayer.h:118
void setPrecipitation(Precipitation type)
Set precipitation.
Definition: cloudlayer.h:100
void setTop(const misc::aviation::CAltitude &top)
Set layer top.
Definition: cloudlayer.h:88
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition: cloudlayer.cpp:39
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67