mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 00:16:51 +08:00
refs #212, methods for parsing values
* PQ, set by other PQ * PQ, set NULL * PQ, CPqTime, implemented parse to string * CPqTime to QTime
This commit is contained in:
@@ -151,8 +151,8 @@ namespace BlackMisc
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiply operator
|
||||
*/
|
||||
* Multiply operator
|
||||
*/
|
||||
template <class MU, class PQ> PQ CPhysicalQuantity<MU, PQ>::operator *(double factor) const
|
||||
{
|
||||
PQ copy = *derived();
|
||||
@@ -306,6 +306,16 @@ namespace BlackMisc
|
||||
this->m_value = json.value("value").toDouble();
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse from string
|
||||
*/
|
||||
template <class MU, class PQ> void CPhysicalQuantity<MU, PQ>::parseFromString(const QString &value)
|
||||
{
|
||||
PQ parsed = CPqString::parse<PQ>(value);
|
||||
this->m_value = parsed.m_value;
|
||||
this->m_unit = parsed.m_unit;
|
||||
}
|
||||
|
||||
/*
|
||||
* metaTypeId
|
||||
*/
|
||||
@@ -320,7 +330,6 @@ namespace BlackMisc
|
||||
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::isA(int metaTypeId) const
|
||||
{
|
||||
if (metaTypeId == qMetaTypeId<PQ>()) { return true; }
|
||||
|
||||
return this->CValueObject::isA(metaTypeId);
|
||||
}
|
||||
|
||||
|
||||
@@ -94,6 +94,9 @@ namespace BlackMisc
|
||||
//! Is quantity null?
|
||||
bool isNull() const { return this->m_unit.isNull(); }
|
||||
|
||||
//! Set null
|
||||
void setNull() { this->m_unit = MU::nullUnit(); }
|
||||
|
||||
//! Value in given unit
|
||||
double value(const MU &unit) const;
|
||||
|
||||
@@ -116,7 +119,13 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
//! \brief Rounded value in given unit
|
||||
//! Set by other PQ
|
||||
void set(const PQ &pq)
|
||||
{
|
||||
this->m_value = pq.m_value;
|
||||
this->m_unit = pq.m_unit;
|
||||
}
|
||||
|
||||
//! Rounded value in given unit
|
||||
double valueRounded(const MU &unit, int digits = -1) const;
|
||||
|
||||
@@ -225,6 +234,9 @@ namespace BlackMisc
|
||||
//! \copydoc CValueObject::fromJson
|
||||
virtual void fromJson(const QJsonObject &json) override;
|
||||
|
||||
//! \copydoc CValueObject::parseFromString
|
||||
virtual void parseFromString(const QString &value) override;
|
||||
|
||||
//! Register metadata of unit and quantity
|
||||
static void registerMetadata();
|
||||
};
|
||||
|
||||
49
src/blackmisc/pqtime.cpp
Normal file
49
src/blackmisc/pqtime.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "pqtime.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace PhysicalQuantities
|
||||
{
|
||||
CTime::CTime(const QTime &time) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
|
||||
{
|
||||
int seconds = QTime().secsTo(time);
|
||||
CTime converted(seconds, CTimeUnit::s());
|
||||
converted.setUnit(CTimeUnit::hms());
|
||||
this->set(converted);
|
||||
}
|
||||
|
||||
CTime::CTime(const QString &time) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
|
||||
{
|
||||
CTime parsed;
|
||||
parsed.parseFromString(time);
|
||||
this->set(parsed);
|
||||
}
|
||||
|
||||
void CTime::parseFromString(const QString &time)
|
||||
{
|
||||
Q_ASSERT(time.length() == 5 || time.length() == 8);
|
||||
QTime t;
|
||||
if (time.length() == 5)
|
||||
t = QTime::fromString("hh:mm");
|
||||
else if (time.length() == 8)
|
||||
t = QTime::fromString("hh:mm:ss");
|
||||
CTime parsed(t);
|
||||
if (time.length() == 5)
|
||||
parsed.setUnit(CTimeUnit::hrmin());
|
||||
else if (time.length() == 8)
|
||||
parsed.setUnit(CTimeUnit::hms());
|
||||
else
|
||||
parsed.setUnit(CTimeUnit::nullUnit()); // indicates invalid
|
||||
this->set(parsed);
|
||||
}
|
||||
|
||||
QTime CTime::toQTime() const
|
||||
{
|
||||
CTime copy(*this);
|
||||
copy.setUnit(CTimeUnit::hms());
|
||||
QString s = copy.toQString(false);
|
||||
QTime t = QTime::fromString(s, "hh:mm:ss");
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,13 @@ namespace BlackMisc
|
||||
|
||||
//! Destructor
|
||||
virtual ~CTime() {}
|
||||
|
||||
//! From string hh:mm, or hh:mm:ss
|
||||
void parseFromString(const QString &time);
|
||||
|
||||
//! To Qt time
|
||||
QTime toQTime() const;
|
||||
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user