diff --git a/.gitignore b/.gitignore index ac1fe06b3..200d4d055 100644 --- a/.gitignore +++ b/.gitignore @@ -87,3 +87,5 @@ desktop.ini # Editors temporary files *~ /client.pro.user.2.7pre1 +/client.pro.user.3.0-pre1 +/client.pro.user.15 diff --git a/externals.pri b/externals.pri index 5ea07510f..902b60790 100644 --- a/externals.pri +++ b/externals.pri @@ -8,7 +8,6 @@ isEmpty(EXTERNALDIR) { EXTERNALDIR = $$(VATSIM_EXTERNAL_DIR) - } # if no env variable is set, we use the standard path in the client folder. @@ -52,10 +51,3 @@ linux-g++-32 { linux-g++-64 { LIBS *= -L$$EXTERNALDIR/linux64/lib } - - - - - - - diff --git a/samples/blackmiscquantities/samplesaviation.cpp b/samples/blackmiscquantities/samplesaviation.cpp index a29f30791..436c47f04 100644 --- a/samples/blackmiscquantities/samplesaviation.cpp +++ b/samples/blackmiscquantities/samplesaviation.cpp @@ -4,6 +4,13 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "samplesaviation.h" +#include "blackmisc/pqconstants.h" +#include "blackmisc/avheading.h" +#include "blackmisc/aviocomsystem.h" +#include "blackmisc/avionavsystem.h" +#include "blackmisc/aviotransponder.h" + +#include using namespace BlackMisc::Aviation; using namespace BlackMisc::PhysicalQuantities; @@ -11,51 +18,54 @@ using namespace BlackMisc::PhysicalQuantities; namespace BlackMiscTest { -/* - * Samples - */ -int CSamplesAviation::samples() -{ - CHeading h1(180, CHeading::Magnetic, CAngleUnit::deg()); - CHeading h2(180, CHeading::True, CAngleUnit::deg()); - - qDebug() << h1; - qDebug() << h1 << h2 << (h1 == h2) << (h1 != h2) << (h1 == h1); - - CComSystem c1 = CComSystem::getCom1System(125.3); - qDebug() << c1; - c1.setActiveUnicom(); - qDebug() << c1; - - // CComSystem *c1p = new CComSystem("Test", CFrequency(125.3,CFrequencyUnit::MHz())); - if (!CComSystem::tryGetComSystem(c1, "Test", -1.0)) - qDebug() << c1 << "is reset to default as expected"; - else - qDebug() << "Something is wrong here"; - - try + /* + * Samples + */ + int CSamplesAviation::samples() { - // uncomment to test assert - // CFrequency f1(-1.0, CFrequencyUnit::MHz()); - // c1 = CComSystem("ups", f1, f1); - // qDebug() << "Why do I get here??"; + CHeading h1(180, CHeading::Magnetic, CAngleUnit::deg()); + CHeading h2(180, CHeading::True, CAngleUnit::deg()); + + qDebug() << h1; + qDebug() << h1 << h2 << (h1 == h2) << (h1 != h2) << (h1 == h1); + + // COM system + CComSystem c1 = CComSystem::getCom1System(125.3); + qDebug() << c1; + c1.setActiveUnicom(); + qDebug() << c1; + + if (!CComSystem::tryGetComSystem(c1, "Test", -1.0)) + qDebug() << c1 << "is reset to default as expected"; + else + qDebug() << "Something is wrong here"; + + try + { + // uncomment to test assert + // CFrequency f1(-1.0, CFrequencyUnit::MHz()); + // c1 = CComSystem("ups", f1, f1); + // qDebug() << "Why do I get here??"; + } + catch (std::range_error &ex) + { + qDebug() << "As expected" << ex.what(); + } + + // NAV system + CNavSystem nav1; + CNavSystem::tryGetNav1System(nav1, 110.0); + qDebug() << nav1; + + // Transponder tests + CTransponder tr1("T1", 7000, CTransponder::StateStandby); + CTransponder tr2("T2", "4532", CTransponder::ModeMil3); + qDebug() << tr1 << tr2; + + + + qDebug() << "-----------------------------------------------"; + return 0; } - catch (std::range_error &ex) - { - qDebug() << "As expected" << ex.what(); - } - - CNavSystem nav1; - CNavSystem::tryGetNav1System(nav1, 110.0); - qDebug() << nav1; - - CTransponder tr1("T1", 7000, CTransponder::StateStandby); - CTransponder tr2("T2", "4532", CTransponder::ModeMil3); - qDebug() << tr1 << tr2; - - // bye - qDebug() << "-----------------------------------------------"; - return 0; -} } // namespace diff --git a/samples/blackmiscquantities/samplesaviation.h b/samples/blackmiscquantities/samplesaviation.h index f4ebe94af..a02e5f0f6 100644 --- a/samples/blackmiscquantities/samplesaviation.h +++ b/samples/blackmiscquantities/samplesaviation.h @@ -6,13 +6,6 @@ #ifndef BLACKMISCTEST_SAMPLESAVIATION_H #define BLACKMISCTEST_SAMPLESAVIATION_H -#include "blackmisc/pqconstants.h" -#include "blackmisc/avheading.h" -#include "blackmisc/aviocomsystem.h" -#include "blackmisc/avionavsystem.h" -#include "blackmisc/aviotransponder.h" -#include - namespace BlackMiscTest { /*! diff --git a/samples/blackmiscquantities/samplesphysicalquantities.cpp b/samples/blackmiscquantities/samplesphysicalquantities.cpp index 6c24add7f..d1e1a94c0 100644 --- a/samples/blackmiscquantities/samplesphysicalquantities.cpp +++ b/samples/blackmiscquantities/samplesphysicalquantities.cpp @@ -10,109 +10,108 @@ using namespace BlackMisc::PhysicalQuantities; namespace BlackMiscTest { -/** - * Running the quantities - */ -int CSamplesPhysicalQuantities::samples() -{ - // cases which must not work - // CLengthUnit du1(CAngleUnit::rad()); + /** + * Running the quantities + */ + int CSamplesPhysicalQuantities::samples() + { + // cases which must not work + // CLengthUnit du1(CAngleUnit::rad()); - CLengthUnit lu1(CLengthUnit::cm()); - CLengthUnit lu2(CLengthUnit::ft()); - QString lu1s = lu1.toQString(true); - QString lu2s = lu2.toQString(true); - qDebug() << lu1 << lu2 << lu1s << lu2s << lu1.getName(true) << lu2.getName(true); - const CLength l1(5.0, CLengthUnit::ft()); // 5 ft - CLength l2(1, CLengthUnit::NM()); // 1NM - CLength l3(1, CLengthUnit::km()); - CLength l4(l3); + CLengthUnit lu1(CLengthUnit::cm()); + CLengthUnit lu2(CLengthUnit::ft()); + QString lu1s = lu1.toQString(true); + QString lu2s = lu2.toQString(true); + qDebug() << lu1 << lu2 << lu1s << lu2s << lu1.getName(true) << lu2.getName(true); + const CLength l1(5.0, CLengthUnit::ft()); // 5 ft + CLength l2(1, CLengthUnit::NM()); // 1NM + CLength l3(1, CLengthUnit::km()); + CLength l4(l3); - qDebug() << CLengthUnit::ft(); - qDebug() << l1 << l2 << l3 << l4; - qDebug() << l1.valueRoundedWithUnit(CLengthUnit::ft(), 5) - << l2.valueRoundedWithUnit(CLengthUnit::km()); - qDebug() << l3.getUnit(); + qDebug() << CLengthUnit::ft(); + qDebug() << l1 << l2 << l3 << l4; + qDebug() << l1.valueRoundedWithUnit(CLengthUnit::ft(), 5) + << l2.valueRoundedWithUnit(CLengthUnit::km()); + qDebug() << l3.getUnit(); + l2.switchUnit(CLengthUnit::ft()); // now in ft + l3 += l3; // 2km now + l3 *= 1.5;// 3km now + qDebug() << l2 << l3; - l2.switchUnit(CLengthUnit::ft()); // now in ft - l3 += l3; // 2km now - l3 *= 1.5;// 3km now - qDebug() << l2 << l3; + l3 = l3 * 2; + qDebug() << "doubled l3:" << l3; - l3 = l3 * 2; - qDebug() << "doubled l3:" << l3; + // more tests + CFrequency f1(1E6, CFrequencyUnit::Hz()); // 1MHz + qDebug() << f1 << f1.valueRoundedWithUnit(CFrequencyUnit::MHz()) << f1.valueRoundedWithUnit(CFrequencyUnit::GHz(), 3); + CSpeed s1 = CSpeed(100, CSpeedUnit::km_h()); + CSpeed s2 = CSpeed(1000, CSpeedUnit::ft_min()); + CSpeed s3 = CSpeed(s2); + s3.switchUnit(CSpeedUnit::m_s()); + qDebug() << s1 << s1.valueRoundedWithUnit(CSpeedUnit::defaultUnit()) << s1.valueRoundedWithUnit(CSpeedUnit::NM_h()); + qDebug() << s2 << s3; - CFrequency f1(1E6, CFrequencyUnit::Hz()); // 1MHz - qDebug() << f1 << f1.valueRoundedWithUnit(CFrequencyUnit::MHz()) << f1.valueRoundedWithUnit(CFrequencyUnit::GHz(), 3); + CAngle a1(180, CAngleUnit::deg()); + CAngle a2(1.5 * CAngle::PI(), CAngleUnit::rad()); + CAngle a3(180.5, CAngleUnit::deg()); + CAngle a4(35.4336, CAngleUnit::sexagesimalDeg()); // 35.72666 + a1 += a2; + // a1 = d2; // must not work + qDebug() << a1; + a1.switchUnit(CAngleUnit::deg()); + // a2 += d1; // must not work + a2 = a1 + a1; - CSpeed s1 = CSpeed(100, CSpeedUnit::km_h()); - CSpeed s2 = CSpeed(1000, CSpeedUnit::ft_min()); - CSpeed s3 = CSpeed(s2); - s3.switchUnit(CSpeedUnit::m_s()); - qDebug() << s1 << s1.valueRoundedWithUnit(CSpeedUnit::defaultUnit()) << s1.valueRoundedWithUnit(CSpeedUnit::NM_h()); - qDebug() << s2 << s3; + a2.switchUnit(CAngleUnit::deg()); + qDebug() << a1.valueRoundedWithUnit() << a1.piFactor(); + qDebug() << a2; + a3.switchUnit(CAngleUnit::sexagesimalDeg()); + a4.switchUnit(CAngleUnit::deg()); + qDebug() << a3 << a4; - CAngle a1(180, CAngleUnit::deg()); - CAngle a2(1.5 * CAngle::PI(), CAngleUnit::rad()); - CAngle a3(180.5, CAngleUnit::deg()); - CAngle a4(35.4336, CAngleUnit::sexagesimalDeg()); // 35.72666 - a1 += a2; - // a1 = d2; // must not work - qDebug() << a1; - a1.switchUnit(CAngleUnit::deg()); - // a2 += d1; // must not work - a2 = a1 + a1; + CMass w1(1, CMassUnit::tonne()); + CMass w2(w1); + w2.switchUnit(CMassUnit::lb()); + qDebug() << w1 << w1.valueRoundedWithUnit(CMassUnit::kg()) << w2; - a2.switchUnit(CAngleUnit::deg()); - qDebug() << a1.valueRoundedWithUnit() << a1.piFactor(); - qDebug() << a2; - a3.switchUnit(CAngleUnit::sexagesimalDeg()); - a4.switchUnit(CAngleUnit::deg()); - qDebug() << a3 << a4; + CPressure p1(1013.25, CPressureUnit::hPa()); + qDebug() << p1 << p1.valueRoundedWithUnit(CPressureUnit::psi()) << p1.valueRoundedWithUnit(CPressureUnit::inHg()); - CMass w1(1, CMassUnit::tonne()); - CMass w2(w1); - w2.switchUnit(CMassUnit::lb()); - qDebug() << w1 << w1.valueRoundedWithUnit(CMassUnit::kg()) << w2; + CTemperature t1; + CTemperature t2(20, CTemperatureUnit::C()); + CTemperature t3(1, CTemperatureUnit::F()); + qDebug() << t1 << t2 << t2.valueRoundedWithUnit(CTemperatureUnit::defaultUnit(), -1, true); + qDebug() << t3.valueRoundedWithUnit(CTemperatureUnit::F(), -1, true) << t3.valueRoundedWithUnit(CTemperatureUnit::C(), -1, true) << "I18N/UTF"; - CPressure p1(1013.25, CPressureUnit::hPa()); - qDebug() << p1 << p1.valueRoundedWithUnit(CPressureUnit::psi()) << p1.valueRoundedWithUnit(CPressureUnit::inHg()); + // some logging with CLogMessage + // bDebug << p1; + // bDebug << p1.getUnit() << p1.getUnit().getMultiplier(); - CTemperature t1; - CTemperature t2(20, CTemperatureUnit::C()); - CTemperature t3(1, CTemperatureUnit::F()); - qDebug() << t1 << t2 << t2.valueRoundedWithUnit(CTemperatureUnit::defaultUnit(), -1, true); - qDebug() << t3.valueRoundedWithUnit(CTemperatureUnit::F(), -1, true) << t3.valueRoundedWithUnit(CTemperatureUnit::C(), -1, true) << "I18N/UTF"; + // some of the faults Mathew has pointed out, not longer possible + // CAngleUnit::rad() = CAngleUnit::deg(); + // qDebug() << CAngleUnit::rad(); // wrong - // some logging with CLogMessage - // bDebug << p1; - // bDebug << p1.getUnit() << p1.getUnit().getMultiplier(); + (t1 - t2).switchUnit(CTemperatureUnit::F()); // was not working since wrong return type const + // CLengthUnit duA(CSpeedUnit::ft_min()); // no longer possible + CLengthUnit duB(CLengthUnit::cm()); + qDebug() << duB; - // some of the faults Mathew has pointed out, not longer possible - // CAngleUnit::rad() = CAngleUnit::deg(); - // qDebug() << CAngleUnit::rad(); // wrong + CTime ti1(1, CTimeUnit::h()); + CTime ti2(ti1); + ti2.switchUnit(CTimeUnit::ms()); + CTime ti3(1.0101, CTimeUnit::hms()); + CTime ti4(1.15, CTimeUnit::hrmin()); + CTime ti5(1.30, CTimeUnit::minsec()); + qDebug() << ti1 << ti2 << ti3 << ti4 << ti5; - (t1 - t2).switchUnit(CTemperatureUnit::F()); // was not working since wrong return type const - // CLengthUnit duA(CSpeedUnit::ft_min()); // no longer possible - CLengthUnit duB(CLengthUnit::cm()); - qDebug() << duB; + CAcceleration ac1(10, CAccelerationUnit::m_s2()); + qDebug() << ac1 << ac1.toQString(true) << ac1.valueRoundedWithUnit(-1, true) << "I18N/UTF"; - CTime ti1(1, CTimeUnit::h()); - CTime ti2(ti1); - ti2.switchUnit(CTimeUnit::ms()); - CTime ti3(1.0101, CTimeUnit::hms()); - CTime ti4(1.15, CTimeUnit::hrmin()); - CTime ti5(1.30, CTimeUnit::minsec()); - qDebug() << ti1 << ti2 << ti3 << ti4 << ti5; - - CAcceleration ac1(10, CAccelerationUnit::m_s2()); - qDebug() << ac1 << ac1.valueRoundedWithUnit(-1, true) << "I18N/UTF"; - - // bye - qDebug() << "-----------------------------------------------"; - return 0; -} + // bye + qDebug() << "-----------------------------------------------"; + return 0; + } } // namespace diff --git a/samples/blackmiscvectorgeo/samplesgeo.cpp b/samples/blackmiscvectorgeo/samplesgeo.cpp index 578f9b0cf..426be5213 100644 --- a/samples/blackmiscvectorgeo/samplesgeo.cpp +++ b/samples/blackmiscvectorgeo/samplesgeo.cpp @@ -11,43 +11,43 @@ using namespace BlackMisc::PhysicalQuantities; namespace BlackMiscTest { -/* - * Samples - */ -int CSamplesGeo::samples() -{ - CLatitude lat1(20.0, CAngleUnit::deg()); - CLatitude lat2 = lat1; - CLatitude lat3 = lat1 - lat2; + /* + * Samples + */ + int CSamplesGeo::samples() + { + CLatitude lat1(20.0, CAngleUnit::deg()); + CLatitude lat2 = lat1; + CLatitude lat3 = lat1 - lat2; - qDebug() << lat1 << lat2 << lat3; - qDebug() << (lat1 + lat2) << (lat1 - lat2); + qDebug() << lat1 << lat2 << lat3; + qDebug() << (lat1 + lat2) << (lat1 - lat2); - lat3 += lat1; - CLongitude lon1(33.0, CAngleUnit::deg()); - qDebug() << lon1 << lat3; + lat3 += lat1; + CLongitude lon1(33.0, CAngleUnit::deg()); + qDebug() << lon1 << lat3; // lat3 += lon1; // must not work // lat3 = lon1; //must not work // CGeoLongitude lonx(lat2); // must notwork - CCoordinateGeodetic cg(10.0, 20.0, 1000); - CCoordinateEcef ce = CCoordinateTransformation::toEcef(cg); - CCoordinateGeodetic cg2 = CCoordinateTransformation::toGeodetic(ce); - cg2.switchUnit(CAngleUnit::deg()); - qDebug() << cg << ce << cg2; + CCoordinateGeodetic cg(10.0, 20.0, 1000); + CCoordinateEcef ce = CCoordinateTransformation::toEcef(cg); + CCoordinateGeodetic cg2 = CCoordinateTransformation::toGeodetic(ce); + cg2.switchUnit(CAngleUnit::deg()); + qDebug() << cg << ce << cg2; - CCoordinateNed cned = CCoordinateTransformation::toNed(ce, cg); - CCoordinateEcef ce2 = CCoordinateTransformation::toEcef(cned); - qDebug() << ce << cned << ce2; - qDebug() << (cned + cned) << (ce + ce); + CCoordinateNed cned = CCoordinateTransformation::toNed(ce, cg); + CCoordinateEcef ce2 = CCoordinateTransformation::toEcef(cned); + qDebug() << ce << cned << ce2; + qDebug() << (cned + cned) << (ce + ce); // cned += ce2; // must not work - // bye - qDebug() << "-----------------------------------------------"; - return 0; + // bye + qDebug() << "-----------------------------------------------"; + return 0; -} + } } // namespace diff --git a/samples/cli_client/client.cpp b/samples/cli_client/client.cpp index 7caff6c5d..aac90ad5f 100644 --- a/samples/cli_client/client.cpp +++ b/samples/cli_client/client.cpp @@ -90,7 +90,7 @@ Client::Client(BlackMisc::IContext &ctx) void Client::command(QString line) { - QTextStream stream (&line, QIODevice::ReadOnly); + QTextStream stream(&line, QIODevice::ReadOnly); QString cmd; stream >> cmd; stream.skipWhiteSpace(); @@ -110,7 +110,7 @@ void Client::command(QString line) /************ Commands *************/ /****************************************************************************/ -void Client::help(QTextStream&) +void Client::help(QTextStream &) { std::cout << "Commands:" << std::endl; auto keys = m_commands.keys(); @@ -120,17 +120,17 @@ void Client::help(QTextStream&) } } -void Client::echo(QTextStream& line) +void Client::echo(QTextStream &line) { std::cout << "echo: " << line.readAll().toStdString() << std::endl; } -void Client::exit(QTextStream&) +void Client::exit(QTextStream &) { emit quit(); } -void Client::setServerDetailsCmd(QTextStream& args) +void Client::setServerDetailsCmd(QTextStream &args) { QString hostname; quint16 port; @@ -138,7 +138,7 @@ void Client::setServerDetailsCmd(QTextStream& args) emit setServerDetails(hostname, port); } -void Client::setUserCredentialsCmd(QTextStream& args) +void Client::setUserCredentialsCmd(QTextStream &args) { QString username; QString password; @@ -146,29 +146,29 @@ void Client::setUserCredentialsCmd(QTextStream& args) emit setUserCredentials(username, password); } -void Client::setCallsignCmd(QTextStream& args) +void Client::setCallsignCmd(QTextStream &args) { QString callsign; args >> callsign; emit setCallsign(callsign); } -void Client::setRealNameCmd(QTextStream& args) +void Client::setRealNameCmd(QTextStream &args) { emit setRealName(args.readAll()); } -void Client::initiateConnectionCmd(QTextStream&) +void Client::initiateConnectionCmd(QTextStream &) { emit initiateConnection(); } -void Client::terminateConnectionCmd(QTextStream&) +void Client::terminateConnectionCmd(QTextStream &) { emit terminateConnection(); } -void Client::sendPrivateTextMessageCmd(QTextStream& args) +void Client::sendPrivateTextMessageCmd(QTextStream &args) { QString callsign; args >> callsign; @@ -176,7 +176,7 @@ void Client::sendPrivateTextMessageCmd(QTextStream& args) emit sendPrivateTextMessage(callsign, args.readAll()); } -void Client::sendRadioTextMessageCmd(QTextStream& args) +void Client::sendRadioTextMessageCmd(QTextStream &args) { QString freqsBlob; args >> freqsBlob; @@ -189,63 +189,63 @@ void Client::sendRadioTextMessageCmd(QTextStream& args) emit sendRadioTextMessage(freqs, args.readAll()); } -void Client::sendIpQueryCmd(QTextStream&) +void Client::sendIpQueryCmd(QTextStream &) { emit sendIpQuery(); } -void Client::sendFreqQueryCmd(QTextStream& args) +void Client::sendFreqQueryCmd(QTextStream &args) { QString callsign; args >> callsign; emit sendFreqQuery(callsign); } -void Client::sendServerQueryCmd(QTextStream& args) +void Client::sendServerQueryCmd(QTextStream &args) { QString callsign; args >> callsign; emit sendServerQuery(callsign); } -void Client::sendAtcQueryCmd(QTextStream& args) +void Client::sendAtcQueryCmd(QTextStream &args) { QString callsign; args >> callsign; emit sendAtcQuery(callsign); } -void Client::sendAtisQueryCmd(QTextStream& args) +void Client::sendAtisQueryCmd(QTextStream &args) { QString callsign; args >> callsign; emit sendAtisQuery(callsign); } -void Client::sendNameQueryCmd(QTextStream& args) +void Client::sendNameQueryCmd(QTextStream &args) { QString callsign; args >> callsign; emit sendNameQuery(callsign); } -void Client::sendCapabilitiesQueryCmd(QTextStream& args) +void Client::sendCapabilitiesQueryCmd(QTextStream &args) { QString callsign; args >> callsign; emit sendCapabilitiesQuery(callsign); } -void Client::replyToFreqQueryCmd(QTextStream& args) +void Client::replyToFreqQueryCmd(QTextStream &args) { QString callsign; double num; args >> callsign >> num; - BlackMisc::PhysicalQuantities::CFrequency freq (num, BlackMisc::PhysicalQuantities::CFrequencyUnit::kHz()); + BlackMisc::PhysicalQuantities::CFrequency freq(num, BlackMisc::PhysicalQuantities::CFrequencyUnit::kHz()); emit replyToFreqQuery(callsign, freq); } -void Client::replyToNameQueryCmd(QTextStream& args) +void Client::replyToNameQueryCmd(QTextStream &args) { QString callsign; QString realname; @@ -270,21 +270,21 @@ void Client::sendPlaneInfoCmd(QTextStream& args) emit sendPlaneInfo(callsign, acTypeICAO, airlineICAO, livery); } -void Client::pingCmd(QTextStream& args) +void Client::pingCmd(QTextStream &args) { QString callsign; args >> callsign; emit ping(callsign); } -void Client::requestMetarCmd(QTextStream& args) +void Client::requestMetarCmd(QTextStream &args) { QString airportICAO; args >> airportICAO; emit requestMetar(airportICAO); } -void Client::requestWeatherDataCmd(QTextStream& args) +void Client::requestWeatherDataCmd(QTextStream &args) { QString airportICAO; args >> airportICAO; @@ -331,7 +331,7 @@ void Client::connectionStatusError() std::cout << "CONN_STATUS_ERROR" << std::endl; } -void Client::ipQueryReplyReceived(const QString& ip) +void Client::ipQueryReplyReceived(const QString &ip) { std::cout << "IP_REPLY " << ip.toStdString() << std::endl; } @@ -376,12 +376,12 @@ void Client::nameQueryRequestReceived(const QString& callsign) std::cout << "NAME_QUERY " << callsign.toStdString() << std::endl; } -void Client::kicked(const QString& msg) +void Client::kicked(const QString &msg) { std::cout << "KICKED " << msg.toStdString() << std::endl; } -void Client::metarReceived(const QString& data) +void Client::metarReceived(const QString &data) { std::cout << "METAR " << data.toStdString() << std::endl; } diff --git a/samples/cli_client/client.h b/samples/cli_client/client.h index 6c149ebbd..06987c554 100644 --- a/samples/cli_client/client.h +++ b/samples/cli_client/client.h @@ -27,37 +27,37 @@ public slots: void command(QString line); private: //commands - void help(QTextStream& args); - void echo(QTextStream& args); - void exit(QTextStream& args); - void setServerDetailsCmd(QTextStream& args); - void setUserCredentialsCmd(QTextStream& args); - void setCallsignCmd(QTextStream& args); - void setRealNameCmd(QTextStream& args); - void initiateConnectionCmd(QTextStream& args); - void terminateConnectionCmd(QTextStream& args); - void sendPrivateTextMessageCmd(QTextStream& args); - void sendRadioTextMessageCmd(QTextStream& args); - void sendIpQueryCmd(QTextStream& args); - void sendFreqQueryCmd(QTextStream& args); - void sendServerQueryCmd(QTextStream& args); - void sendAtcQueryCmd(QTextStream& args); - void sendAtisQueryCmd(QTextStream& args); - void sendNameQueryCmd(QTextStream& args); - void sendCapabilitiesQueryCmd(QTextStream& args); - void replyToFreqQueryCmd(QTextStream& args); - void replyToNameQueryCmd(QTextStream& args); void requestPlaneInfoCmd(QTextStream& args); void sendPlaneInfoCmd(QTextStream& args); - void pingCmd(QTextStream& args); - void requestMetarCmd(QTextStream& args); - void requestWeatherDataCmd(QTextStream& args); + void help(QTextStream &args); + void echo(QTextStream &args); + void exit(QTextStream &args); + void setServerDetailsCmd(QTextStream &args); + void setUserCredentialsCmd(QTextStream &args); + void setCallsignCmd(QTextStream &args); + void setRealNameCmd(QTextStream &args); + void initiateConnectionCmd(QTextStream &args); + void terminateConnectionCmd(QTextStream &args); + void sendPrivateTextMessageCmd(QTextStream &args); + void sendRadioTextMessageCmd(QTextStream &args); + void sendIpQueryCmd(QTextStream &args); + void sendFreqQueryCmd(QTextStream &args); + void sendServerQueryCmd(QTextStream &args); + void sendAtcQueryCmd(QTextStream &args); + void sendAtisQueryCmd(QTextStream &args); + void sendNameQueryCmd(QTextStream &args); + void sendCapabilitiesQueryCmd(QTextStream &args); + void replyToFreqQueryCmd(QTextStream &args); + void replyToNameQueryCmd(QTextStream &args); + void pingCmd(QTextStream &args); + void requestMetarCmd(QTextStream &args); + void requestWeatherDataCmd(QTextStream &args); signals: //to send to INetwork - void setServerDetails(const QString& hostname, quint16 port); - void setUserCredentials(const QString& username, const QString& password); void setCallsign(const QString& callsign); - void setRealName(const QString& name); + void setServerDetails(const QString &hostname, quint16 port); + void setUserCredentials(const QString &username, const QString &password); + void setRealName(const QString &name); void initiateConnection(); void terminateConnection(); void sendPrivateTextMessage(const QString& callsign, const QString& msg); @@ -86,7 +86,6 @@ public slots: //to receive from INetwork void connectionStatusConnected(); void connectionStatusDisconnected(); void connectionStatusError(); - void ipQueryReplyReceived(const QString& ip); void freqQueryReplyReceived(const QString& callsign, const BlackMisc::PhysicalQuantities::CFrequency& freq); void serverQueryReplyReceived(const QString& callsign, const QString& hostname); void atcQueryReplyReceived(const QString& callsign, bool isATC); @@ -95,18 +94,19 @@ public slots: //to receive from INetwork void capabilitiesQueryReplyReceived(const QString& callsign, quint32 flags); void freqQueryRequestReceived(const QString& callsign); void nameQueryRequestReceived(const QString& callsign); - void kicked(const QString& msg); - void metarReceived(const QString& data); void pilotDisconnected(const QString& callsign); void planeInfoReceived(const QString& callsign, const QString& acTypeICAO, const QString& airlineICAO, const QString& livery); void planeInfoRequestReceived(const QString& callsign); void pong(const QString& callsign, const BlackMisc::PhysicalQuantities::CTime& elapsedTime); void radioTextMessageReceived(const QString& callsign, const QString& msg, const QVector& freqs); void privateTextMessageReceived(const QString& fromCallsign, const QString& toCallsign, const QString& msg); + void ipQueryReplyReceived(const QString &ip); + void kicked(const QString &msg); + void metarReceived(const QString &data); private: - QMap> m_commands; - BlackCore::INetwork* m_net; + QMap> m_commands; + BlackCore::INetwork *m_net; }; #endif //__BLACKSAMPLE_CLI_CLIENT_H__ diff --git a/src/blackcore/atclistmgr.h b/src/blackcore/atclistmgr.h index e8a551b2c..545fa32ed 100644 --- a/src/blackcore/atclistmgr.h +++ b/src/blackcore/atclistmgr.h @@ -47,7 +47,7 @@ namespace BlackCore /*! * Concrete ATC list manager. Implementation of IAtcListManager. * - * Has a dependency on INetwork. An INetwork must be available through the IContext. + * Has a dependency on INetwork. An INetwork must be available through the IContext singleton. */ class CAtcListManager : public IAtcListManager { @@ -65,13 +65,13 @@ namespace BlackCore public slots: /*! * CAtcListManager is responsible for connecting these slots. - * @{ + * \{ */ void update(const QString& callsign, const BlackMisc::PhysicalQuantities::CFrequency& freq, const BlackMisc::Geo::CCoordinateGeodetic& pos, const BlackMisc::PhysicalQuantities::CLength& range); void remove(const QString& callsign); void clear(); - /*! @} */ + /*! \} */ private: BlackMisc::CAtcList m_list; diff --git a/src/blackcore/blackcore.pro b/src/blackcore/blackcore.pro index a880791cb..6839867b6 100644 --- a/src/blackcore/blackcore.pro +++ b/src/blackcore/blackcore.pro @@ -10,11 +10,8 @@ CONFIG += staticlib c++11 INCLUDEPATH += .. DEPENDPATH += . .. -linux-g++* { - QMAKE_CXXFLAGS += -std=c++0x -} - -#PRECOMPILED_HEADER = stdpch.h +# linux-g++* { QMAKE_CXXFLAGS += -std=c++0x } +# PRECOMPILED_HEADER = stdpch.h precompile_header:!isEmpty(PRECOMPILED_HEADER) { DEFINES += USING_PCH diff --git a/src/blackcore/dbus_server.cpp b/src/blackcore/dbus_server.cpp index e40a1d515..9af3b339f 100644 --- a/src/blackcore/dbus_server.cpp +++ b/src/blackcore/dbus_server.cpp @@ -7,7 +7,8 @@ #include "dbus_server.h" -namespace BlackCore { +namespace BlackCore +{ CDBusServer::CDBusServer(const QString &address, QObject *parent) : QObject(parent), m_busServer(address, parent) diff --git a/src/blackcore/dbus_server.h b/src/blackcore/dbus_server.h index 95b15e030..3c9068cd8 100644 --- a/src/blackcore/dbus_server.h +++ b/src/blackcore/dbus_server.h @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef DBUSSERVER_H -#define DBUSSERVER_H +#ifndef BLACKCORE_DBUSSERVER_H +#define BLACKCORE_DBUSSERVER_H #include #include diff --git a/src/blackcore/network.h b/src/blackcore/network.h index 3b66196ba..9b569dc90 100644 --- a/src/blackcore/network.h +++ b/src/blackcore/network.h @@ -23,7 +23,7 @@ namespace BlackCore { /*! - * Interface to a connection to a multi-user flight simulation and ATC network. + * Interface for a connection to a multi-user flight simulation and ATC network. * * \warning If an INetwork signal is connected to a slot, and that slot emits a signal * which is connected to an INetwork slot, then at least one of those connections @@ -40,18 +40,21 @@ namespace BlackCore { AcceptsAtisResponses = 1 << 0, SupportsInterimPosUpdates = 1 << 1, - SupportsModelDescriptions = 1 << 2, + SupportsModelDescriptions = 1 << 2 }; public slots: - virtual void setServerDetails(const QString& hostname, quint16 port) = 0; - virtual void setUserCredentials(const QString& username, const QString& password) = 0; virtual void setCallsign(const QString& callsign) = 0; - virtual void setRealName(const QString& name) = 0; + + // Network + virtual void setServerDetails(const QString &hostname, quint16 port) = 0; + virtual void setUserCredentials(const QString &username, const QString &password) = 0; + virtual void setRealName(const QString &name) = 0; virtual void initiateConnection() = 0; virtual void terminateConnection() = 0; virtual void sendPrivateTextMessage(const QString& callsign, const QString& msg) = 0; virtual void sendRadioTextMessage(const QVector& freqs, const QString& msg) = 0; + virtual void sendIpQuery() = 0; virtual void sendFreqQuery(const QString& callsign) = 0; virtual void sendServerQuery(const QString& callsign) = 0; @@ -66,20 +69,25 @@ namespace BlackCore //TODO virtual void sendFlightPlan(...) = 0; virtual void sendPlaneInfo(const QString& callsign, const QString& acTypeICAO, const QString& airlineICAO, const QString& livery) = 0; virtual void ping(const QString& callsign) = 0; - virtual void requestMetar(const QString& airportICAO) = 0; - virtual void requestWeatherData(const QString& airportICAO) = 0; + + // Weather / flight plan + virtual void requestMetar(const QString &airportICAO) = 0; + virtual void requestWeatherData(const QString &airportICAO) = 0; signals: void atcPositionUpdate(const QString& callsign, const BlackMisc::PhysicalQuantities::CFrequency& freq, const BlackMisc::Geo::CCoordinateGeodetic& pos, const BlackMisc::PhysicalQuantities::CLength& range); void atcDisconnected(const QString& callsign); //TODO void cloudDataReceived(...); + void metarReceived(const QString &data); + + // Connection / Network in general + void kicked(const QString &msg); void connectionStatusIdle(); void connectionStatusConnecting(); void connectionStatusConnected(); void connectionStatusDisconnected(); void connectionStatusError(); - void ipQueryReplyReceived(const QString& ip); void freqQueryReplyReceived(const QString& callsign, const BlackMisc::PhysicalQuantities::CFrequency& freq); void serverQueryReplyReceived(const QString& callsign, const QString& hostname); void atcQueryReplyReceived(const QString& callsign, bool isATC); @@ -89,8 +97,6 @@ namespace BlackCore void freqQueryRequestReceived(const QString& callsign); void nameQueryRequestReceived(const QString& callsign); //TODO void interimPilotPositionUpdate(...); - void kicked(const QString& msg); - void metarReceived(const QString& data); void pilotDisconnected(const QString& callsign); void planeInfoReceived(const QString& callsign, const QString& acTypeICAO, const QString& airlineICAO, const QString& livery); void planeInfoRequestReceived(const QString& callsign); @@ -98,6 +104,7 @@ namespace BlackCore void pong(const QString& callsign, const BlackMisc::PhysicalQuantities::CTime& elapsedTime); void radioTextMessageReceived(const QString& callsign, const QString& msg, const QVector& freqs); void privateTextMessageReceived(const QString& fromCallsign, const QString& toCallsign, const QString& msg); + void ipQueryReplyReceived(const QString &ip); //TODO void temperatureDataReceived(...); //TODO void windDataReceived(...); }; @@ -132,6 +139,6 @@ namespace BlackCore virtual void requestWeatherData(const QString&) {} }; -} //namespace BlackCore +} // namespace -#endif //BLACKCORE_NETWORK_H +#endif // guard diff --git a/src/blackcore/network_vatlib.cpp b/src/blackcore/network_vatlib.cpp index 3a03661b5..93013c261 100644 --- a/src/blackcore/network_vatlib.cpp +++ b/src/blackcore/network_vatlib.cpp @@ -47,7 +47,7 @@ namespace BlackCore capabilities += "=1"; m_net->CreateNetworkSession(CLIENT_NAME_VERSION, CLIENT_VERSION_MAJOR, CLIENT_VERSION_MINOR, - CLIENT_SIMULATOR_NAME, CLIENT_PUBLIC_ID, CLIENT_PRIVATE_KEY, toFSD(capabilities)); + CLIENT_SIMULATOR_NAME, CLIENT_PUBLIC_ID, CLIENT_PRIVATE_KEY, toFSD(capabilities)); m_net->InstallOnConnectionStatusChangedEvent(onConnectionStatusChanged, this); m_net->InstallOnTextMessageReceivedEvent(onTextMessageReceived, this); @@ -95,7 +95,7 @@ namespace BlackCore catch (...) { exceptionDispatcher(Q_FUNC_INFO); } } - void NetworkVatlib::timerEvent(QTimerEvent*) + void NetworkVatlib::timerEvent(QTimerEvent *) { try { @@ -112,27 +112,28 @@ namespace BlackCore return m_fsdTextCodec->fromUnicode(qstr); } - QString NetworkVatlib::fromFSD(const char* cstr) const + + QString NetworkVatlib::fromFSD(const char *cstr) const { return m_fsdTextCodec->toUnicode(cstr); } - void exceptionDispatcher(const char* caller) + void exceptionDispatcher(const char *caller) { try { throw; } - catch (const NetworkNotConnectedException& e) + catch (const NetworkNotConnectedException &e) { // this could be caused by a race condition during normal operation, so not an error qDebug() << "NetworkNotConnectedException caught in " << caller << "\n" << e.what(); } - catch (const VatlibException& e) + catch (const VatlibException &e) { qFatal("VatlibException caught in %s\n%s", caller, e.what()); } - catch (const std::exception& e) + catch (const std::exception &e) { qFatal("std::exception caught in %s\n%s", caller, e.what()); } @@ -146,7 +147,7 @@ namespace BlackCore /********************************** INetwork slots ************************************/ /********************************** * * * * * * * * * * * * * * * * * * * ************************************/ - void NetworkVatlib::setServerDetails(const QString& host, quint16 port) + void NetworkVatlib::setServerDetails(const QString &host, quint16 port) { Q_ASSERT_X(isDisconnected(), "NetworkVatlib", "Can't change server details while still connected"); @@ -154,7 +155,7 @@ namespace BlackCore m_serverPort = port; } - void NetworkVatlib::setUserCredentials(const QString& username, const QString& password) + void NetworkVatlib::setUserCredentials(const QString &username, const QString &password) { Q_ASSERT_X(isDisconnected(), "NetworkVatlib", "Can't change login details while still connected"); @@ -169,7 +170,7 @@ namespace BlackCore m_callsign = toFSD(callsign); } - void NetworkVatlib::setRealName(const QString& name) + void NetworkVatlib::setRealName(const QString &name) { Q_ASSERT_X(isDisconnected(), "NetworkVatlib", "Can't change name while still connected"); @@ -182,16 +183,14 @@ namespace BlackCore try { - m_status = Cvatlib_Network::connStatus_Connecting; //paranoia - + m_status = Cvatlib_Network::connStatus_Connecting; // paranoia Cvatlib_Network::PilotConnectionInfo info; info.callsign = m_callsign.data(); info.name = m_realname.data(); info.rating = Cvatlib_Network::pilotRating_Student; //TODO info.sim = Cvatlib_Network::simType_XPlane; //TODO - m_net->SetPilotLoginInfo(toFSD(m_serverHost).data(), m_serverPort, - toFSD(m_username).data(), toFSD(m_password).data(), info); + toFSD(m_username).data(), toFSD(m_password).data(), info); m_net->ConnectAndLogon(); } catch (...) @@ -361,7 +360,7 @@ namespace BlackCore catch (...) { exceptionDispatcher(Q_FUNC_INFO); } } - void NetworkVatlib::requestMetar(const QString& airportICAO) + void NetworkVatlib::requestMetar(const QString &airportICAO) { try { @@ -370,7 +369,7 @@ namespace BlackCore catch (...) { exceptionDispatcher(Q_FUNC_INFO); } } - void NetworkVatlib::requestWeatherData(const QString& airportICAO) + void NetworkVatlib::requestWeatherData(const QString &airportICAO) { try { @@ -383,15 +382,14 @@ namespace BlackCore /********************************** shimlib callbacks ************************************/ /********************************** * * * * * * * * * * * * * * * * * * * ************************************/ - NetworkVatlib* cbvar_cast(void* cbvar) + NetworkVatlib *cbvar_cast(void *cbvar) { - return static_cast(cbvar); + return static_cast(cbvar); } - void NetworkVatlib::onConnectionStatusChanged(Cvatlib_Network*, Cvatlib_Network::connStatus, Cvatlib_Network::connStatus newStatus, void* cbvar) + void NetworkVatlib::onConnectionStatusChanged(Cvatlib_Network *, Cvatlib_Network::connStatus oldStatus, Cvatlib_Network::connStatus newStatus, void *cbvar) { cbvar_cast(cbvar)->m_status = newStatus; - switch (newStatus) { case Cvatlib_Network::connStatus_Idle: emit cbvar_cast(cbvar)->connectionStatusIdle(); break; @@ -402,12 +400,12 @@ namespace BlackCore } } - void NetworkVatlib::onTextMessageReceived(Cvatlib_Network*, const char* from, const char* to, const char* msg, void* cbvar) + void NetworkVatlib::onTextMessageReceived(Cvatlib_Network *, const char *from, const char *to, const char *msg, void *cbvar) { emit cbvar_cast(cbvar)->privateTextMessageReceived(cbvar_cast(cbvar)->fromFSD(from), cbvar_cast(cbvar)->fromFSD(to), cbvar_cast(cbvar)->fromFSD(msg)); } - void NetworkVatlib::onRadioMessageReceived(Cvatlib_Network*, const char* from, INT numFreq, INT* freqList, const char* msg, void* cbvar) + void NetworkVatlib::onRadioMessageReceived(Cvatlib_Network *, const char *from, INT numFreq, INT *freqList, const char *msg, void *cbvar) { QVector freqs; for (int i = 0; i < numFreq; ++i) @@ -417,7 +415,7 @@ namespace BlackCore emit cbvar_cast(cbvar)->radioTextMessageReceived(cbvar_cast(cbvar)->fromFSD(from), cbvar_cast(cbvar)->fromFSD(msg), freqs); } - void NetworkVatlib::onPilotDisconnected(Cvatlib_Network*, const char* callsign, void* cbvar) + void NetworkVatlib::onPilotDisconnected(Cvatlib_Network *, const char *callsign, void *cbvar) { emit cbvar_cast(cbvar)->pilotDisconnected(cbvar_cast(cbvar)->fromFSD(callsign)); } @@ -443,22 +441,22 @@ namespace BlackCore CCoordinateGeodetic(pos.lat, pos.lon, 0), CLength(pos.visibleRange, CLengthUnit::NM())); } - void NetworkVatlib::onKicked(Cvatlib_Network*, const char* reason, void* cbvar) + void NetworkVatlib::onKicked(Cvatlib_Network *, const char *reason, void *cbvar) { emit cbvar_cast(cbvar)->kicked(cbvar_cast(cbvar)->fromFSD(reason)); } - void NetworkVatlib::onPong(Cvatlib_Network*, const char* callsign, INT elapsedTime, void* cbvar) + void NetworkVatlib::onPong(Cvatlib_Network *, const char *callsign, INT elapsedTime, void *cbvar) { emit cbvar_cast(cbvar)->pong(cbvar_cast(cbvar)->fromFSD(callsign), CTime(elapsedTime, CTimeUnit::s())); } - void NetworkVatlib::onMetarReceived(Cvatlib_Network*, const char* data, void* cbvar) + void NetworkVatlib::onMetarReceived(Cvatlib_Network *, const char *data, void *cbvar) { emit cbvar_cast(cbvar)->metarReceived(cbvar_cast(cbvar)->fromFSD(data)); } - void NetworkVatlib::onInfoQueryRequestReceived(Cvatlib_Network*, const char* callsign, Cvatlib_Network::infoQuery type, const char*, void* cbvar) + void NetworkVatlib::onInfoQueryRequestReceived(Cvatlib_Network *, const char *callsign, Cvatlib_Network::infoQuery type, const char *, void *cbvar) { switch (type) { @@ -467,7 +465,7 @@ namespace BlackCore } } - void NetworkVatlib::onInfoQueryReplyReceived(Cvatlib_Network*, const char* callsign, Cvatlib_Network::infoQuery type, const char* data, const char* data2, void* cbvar) + void NetworkVatlib::onInfoQueryReplyReceived(Cvatlib_Network *, const char *callsign, Cvatlib_Network::infoQuery type, const char *data, const char *data2, void *cbvar) { switch (type) { @@ -479,13 +477,13 @@ namespace BlackCore } } - void NetworkVatlib::onCapabilitiesReplyReceived(Cvatlib_Network* net, const char* callsign, const char** keysValues, void* cbvar) + void NetworkVatlib::onCapabilitiesReplyReceived(Cvatlib_Network *net, const char *callsign, const char **keysValues, void *cbvar) { quint32 flags = 0; while (*keysValues) { - const char* key = keysValues[0]; - const char* value = keysValues[1]; + const char *key = keysValues[0]; + const char *value = keysValues[1]; if (*value == '1') { if (key == net->capability_AtcInfo) { flags |= AcceptsAtisResponses; } @@ -497,7 +495,7 @@ namespace BlackCore emit cbvar_cast(cbvar)->capabilitiesQueryReplyReceived(cbvar_cast(cbvar)->fromFSD(callsign), flags); } - void NetworkVatlib::onAtisReplyReceived(Cvatlib_Network*, const char* callsign, Cvatlib_Network::atisLineType, const char* data, void* cbvar) + void NetworkVatlib::onAtisReplyReceived(Cvatlib_Network *, const char *callsign, Cvatlib_Network::atisLineType, const char *data, void *cbvar) { emit cbvar_cast(cbvar)->atisQueryReplyReceived(cbvar_cast(cbvar)->fromFSD(callsign), cbvar_cast(cbvar)->fromFSD(data)); } @@ -507,7 +505,7 @@ namespace BlackCore //TODO } - void NetworkVatlib::onErrorReceived(Cvatlib_Network*, Cvatlib_Network::error type, const char* msg, const char* data, void* cbvar) + void NetworkVatlib::onErrorReceived(Cvatlib_Network *, Cvatlib_Network::error type, const char *msg, const char *data, void *cbvar) { switch (type) { @@ -536,7 +534,7 @@ namespace BlackCore } return; - terminate: + terminate: emit cbvar_cast(cbvar)->terminate(); } @@ -550,12 +548,12 @@ namespace BlackCore //TODO } - void NetworkVatlib::onPilotInfoRequestReceived(Cvatlib_Network*, const char* callsign, void* cbvar) + void NetworkVatlib::onPilotInfoRequestReceived(Cvatlib_Network *, const char *callsign, void *cbvar) { emit cbvar_cast(cbvar)->planeInfoRequestReceived(cbvar_cast(cbvar)->fromFSD(callsign)); } - void NetworkVatlib::onPilotInfoReceived(Cvatlib_Network* net, const char* callsign, const char** keysValues, void* cbvar) + void NetworkVatlib::onPilotInfoReceived(Cvatlib_Network *net, const char *callsign, const char **keysValues, void *cbvar) { const char* acTypeICAO = nullptr; const char* airlineICAO = nullptr; @@ -564,7 +562,7 @@ namespace BlackCore { QString key (*keysValues); keysValues++; - if (key == net->acinfo_Equipment) { acTypeICAO = *keysValues; } + if (key == net->acinfo_Equipment) { acTypeICAO = *keysValues; } else if (key == net->acinfo_Airline) { airlineICAO = *keysValues; } else if (key == net->acinfo_Livery) { livery = *keysValues; } keysValues++; @@ -573,4 +571,4 @@ namespace BlackCore cbvar_cast(cbvar)->fromFSD(airlineICAO), cbvar_cast(cbvar)->fromFSD(livery)); } -} //namespace BlackCore +} // namespace diff --git a/src/blackcore/network_vatlib.h b/src/blackcore/network_vatlib.h index 413a7c3d8..d9bf84556 100644 --- a/src/blackcore/network_vatlib.h +++ b/src/blackcore/network_vatlib.h @@ -22,20 +22,22 @@ namespace BlackCore class NetworkVatlib : public INetwork { - Q_OBJECT; + Q_OBJECT public: NetworkVatlib(); virtual ~NetworkVatlib(); - protected: //QObject overrides - virtual void timerEvent(QTimerEvent*); + protected: // QObject overrides + virtual void timerEvent(QTimerEvent *); - public: //INetwork slots overrides - virtual void setServerDetails(const QString& hostname, quint16 port); - virtual void setUserCredentials(const QString& username, const QString& password); + public: // INetwork slots overrides + + // Network virtual void setCallsign(const QString& callsign); - virtual void setRealName(const QString& name); + virtual void setServerDetails(const QString &hostname, quint16 port); + virtual void setUserCredentials(const QString &username, const QString &password); + virtual void setRealName(const QString &name); virtual void initiateConnection(); virtual void terminateConnection(); virtual void sendPrivateTextMessage(const QString& callsign, const QString& msg); @@ -52,31 +54,35 @@ namespace BlackCore virtual void requestPlaneInfo(const QString& callsign); virtual void sendPlaneInfo(const QString& callsign, const QString& acTypeICAO, const QString& airlineICAO, const QString& livery); virtual void ping(const QString& callsign); - virtual void requestMetar(const QString& airportICAO); - virtual void requestWeatherData(const QString& airportICAO); + // Weather + virtual void requestWeatherData(const QString &airportICAO); + + + // ATC + virtual void requestMetar(const QString &airportICAO); private: //shimlib callbacks - static void onConnectionStatusChanged(Cvatlib_Network*, Cvatlib_Network::connStatus oldStatus, Cvatlib_Network::connStatus newStatus, void* cbvar); - static void onTextMessageReceived(Cvatlib_Network*, const char* from, const char* to, const char* msg, void* cbvar); - static void onRadioMessageReceived(Cvatlib_Network*, const char* from, INT numFreq, INT* freqList, const char* msg, void* cbvar); - static void onPilotDisconnected(Cvatlib_Network*, const char* callsign, void* cbvar); - static void onControllerDisconnected(Cvatlib_Network*, const char* callsign, void* cbvar); - static void onPilotPositionUpdate(Cvatlib_Network*, const char* callsign, Cvatlib_Network::PilotPosUpdate pos, void* cbvar); - static void onInterimPilotPositionUpdate(Cvatlib_Network*, const char* callsign, Cvatlib_Network::PilotPosUpdate pos, void* cbvar); - static void onAtcPositionUpdate(Cvatlib_Network*, const char* callsign, Cvatlib_Network::ATCPosUpdate pos, void* cbvar); - static void onKicked(Cvatlib_Network*, const char* reason, void* cbvar); - static void onPong(Cvatlib_Network*, const char* callsign, INT elapsedTime, void* cbvar); - static void onMetarReceived(Cvatlib_Network*, const char* data, void* cbvar); - static void onInfoQueryRequestReceived(Cvatlib_Network*, const char* callsign, Cvatlib_Network::infoQuery type, const char* data, void* cbvar); - static void onInfoQueryReplyReceived(Cvatlib_Network*, const char* callsign, Cvatlib_Network::infoQuery type, const char* data, const char* data2, void* cbvar); - static void onCapabilitiesReplyReceived(Cvatlib_Network*, const char* callsign, const char** keysValues, void* cbvar); - static void onAtisReplyReceived(Cvatlib_Network*, const char* callsign, Cvatlib_Network::atisLineType type, const char* data, void* cbvar); - static void onTemperatureDataReceived(Cvatlib_Network*, Cvatlib_Network::TempLayer layers[4], INT pressure, void* cbvar); - static void onErrorReceived(Cvatlib_Network*, Cvatlib_Network::error type, const char* msg, const char* data, void* cbvar); - static void onWindDataReceived(Cvatlib_Network*, Cvatlib_Network::WindLayer layers[4], void* cbvar); - static void onCloudDataReceived(Cvatlib_Network*, Cvatlib_Network::CloudLayer layers[2], Cvatlib_Network::StormLayer storm, float vis, void* cbvar); - static void onPilotInfoRequestReceived(Cvatlib_Network*, const char* callsign, void* cbvar); - static void onPilotInfoReceived(Cvatlib_Network*, const char* callsign, const char** keysValues, void* cbvar); + static void onConnectionStatusChanged(Cvatlib_Network *, Cvatlib_Network::connStatus oldStatus, Cvatlib_Network::connStatus newStatus, void *cbvar); + static void onTextMessageReceived(Cvatlib_Network *, const char *from, const char *to, const char *msg, void *cbvar); + static void onRadioMessageReceived(Cvatlib_Network *, const char *from, INT numFreq, INT *freqList, const char *msg, void *cbvar); + static void onControllerDisconnected(Cvatlib_Network *, const char *callsign, void *cbvar); + static void onInterimPilotPositionUpdate(Cvatlib_Network *, const char *callsign, Cvatlib_Network::PilotPosUpdate pos, void *cbvar); + static void onAtcPositionUpdate(Cvatlib_Network *, const char *callsign, Cvatlib_Network::ATCPosUpdate pos, void *cbvar); + static void onKicked(Cvatlib_Network *, const char *reason, void *cbvar); + static void onPong(Cvatlib_Network *, const char *callsign, INT elapsedTime, void *cbvar); + static void onMetarReceived(Cvatlib_Network *, const char *data, void *cbvar); + static void onInfoQueryRequestReceived(Cvatlib_Network *, const char *callsign, Cvatlib_Network::infoQuery type, const char *data, void *cbvar); + static void onInfoQueryReplyReceived(Cvatlib_Network *, const char *callsign, Cvatlib_Network::infoQuery type, const char *data, const char *data2, void *cbvar); + static void onCapabilitiesReplyReceived(Cvatlib_Network *, const char *callsign, const char **keysValues, void *cbvar); + static void onAtisReplyReceived(Cvatlib_Network *, const char *callsign, Cvatlib_Network::atisLineType type, const char *data, void *cbvar); + static void onTemperatureDataReceived(Cvatlib_Network *, Cvatlib_Network::TempLayer layers[4], INT pressure, void *cbvar); + static void onErrorReceived(Cvatlib_Network *, Cvatlib_Network::error type, const char *msg, const char *data, void *cbvar); + static void onWindDataReceived(Cvatlib_Network *, Cvatlib_Network::WindLayer layers[4], void *cbvar); + static void onCloudDataReceived(Cvatlib_Network *, Cvatlib_Network::CloudLayer layers[2], Cvatlib_Network::StormLayer storm, float vis, void *cbvar); + static void onPilotDisconnected(Cvatlib_Network *, const char *callsign, void *cbvar); + static void onPilotInfoRequestReceived(Cvatlib_Network *, const char *callsign, void *cbvar); + static void onPilotInfoReceived(Cvatlib_Network *, const char *callsign, const char **keysValues, void *cbvar); + static void onPilotPositionUpdate(Cvatlib_Network *, const char *callsign, Cvatlib_Network::PilotPosUpdate pos, void *cbvar); private: QByteArray toFSD(QString qstr) const; @@ -109,9 +115,9 @@ namespace BlackCore QByteArray m_callsign; QByteArray m_realname; - QTextCodec* m_fsdTextCodec; + QTextCodec *m_fsdTextCodec; }; } //namespace BlackCore -#endif //BLACKCORE_NETWORK_VATLIB_H +#endif // guard diff --git a/src/blackmisc/avaltitude.cpp b/src/blackmisc/avaltitude.cpp index d0b7becb8..f4944b4b3 100644 --- a/src/blackmisc/avaltitude.cpp +++ b/src/blackmisc/avaltitude.cpp @@ -10,42 +10,62 @@ using BlackMisc::PhysicalQuantities::CLengthUnit; namespace BlackMisc { -namespace Aviation -{ + namespace Aviation + { -/* - * Own implementation for streaming - */ -QString CAltitude::convertToQString(bool /* i18n */) const -{ - QString s = this->CLength::convertToQString(); - return s.append(this->isMeanSeaLevel() ? " MSL" : " AGL"); -} + /* + * Own implementation for streaming + */ + QString CAltitude::convertToQString(bool /* i18n */) const + { + QString s = this->CLength::convertToQString(); + return s.append(this->isMeanSeaLevel() ? " MSL" : " AGL"); + } -/* - * Equal? - */ -bool CAltitude::operator ==(const CAltitude &other) -{ - return other.m_datum == this->m_datum && this->CLength::operator ==(other); -} + /* + * Marshall to DBus + */ + void CAltitude::marshallToDbus(QDBusArgument &argument) const + { + this->CLength::marshallToDbus(argument); + argument << qint32(this->m_datum); + } -/* - * Unequal? - */ -bool CAltitude::operator !=(const CAltitude &other) -{ - return !((*this) == other); -} + /* + * Unmarshall from DBus + */ + void CAltitude::unmarshallFromDbus(const QDBusArgument &argument) + { + this->CLength::unmarshallFromDbus(argument); + qint32 datum; + argument >> datum; + this->m_datum = static_cast(datum); + } -/* - * Register metadata - */ -void CAltitude::registerMetadata() -{ - qRegisterMetaType(typeid(CAltitude).name()); - qDBusRegisterMetaType(); -} + /* + * Equal? + */ + bool CAltitude::operator ==(const CAltitude &other) const + { + return other.m_datum == this->m_datum && this->CLength::operator ==(other); + } -} // namespace + /* + * Unequal? + */ + bool CAltitude::operator !=(const CAltitude &other) const + { + return !((*this) == other); + } + + /* + * Register metadata + */ + void CAltitude::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + } // namespace } // namespace diff --git a/src/blackmisc/avaltitude.h b/src/blackmisc/avaltitude.h index cf74a0311..87e5b6fbd 100644 --- a/src/blackmisc/avaltitude.h +++ b/src/blackmisc/avaltitude.h @@ -9,124 +9,117 @@ namespace BlackMisc { -namespace Aviation -{ - -/*! - * \brief Altitude as used in aviation, can be AGL or MSL altitude - * \remarks Intentionally allowing +/- CLength , and >= / <= CLength. - */ -class CAltitude : public BlackMisc::PhysicalQuantities::CLength -{ -public: - /*! - * Enum type to distinguish between MSL and AGL - */ - enum ReferenceDatum + namespace Aviation { - MeanSeaLevel = 0, //!< MSL - AboveGround = 1, //!< AGL - }; -private: - ReferenceDatum m_datum; //!< MSL or AGL? + /*! + * \brief Altitude as used in aviation, can be AGL or MSL altitude + * \remarks Intentionally allowing +/- CLength , and >= / <= CLength. + */ + class CAltitude : public BlackMisc::PhysicalQuantities::CLength + { + public: + /*! + * Enum type to distinguish between MSL and AGL + */ + enum ReferenceDatum + { + MeanSeaLevel = 0, //!< MSL + AboveGround = 1 //!< AGL + }; -protected: - /*! - * \brief Specific stream operation for Altitude - * \param i18n - * \return - */ - virtual QString convertToQString(bool i18n) const; + private: + ReferenceDatum m_datum; //!< MSL or AGL? - /*! - * \brief Stream to DBus << - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - this->CLength::marshallToDbus(argument); - argument << qint32(this->m_datum); - } + protected: + /*! + * \brief Specific stream operation for Altitude + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n) const; - /*! - * \brief Stream from DBus >> - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - this->CLength::unmarshallFromDbus(argument); - qint32 datum; - argument >> datum; - this->m_datum = static_cast(datum); - } + /*! + * \brief Stream to DBus << + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; -public: - /*! - * \brief Default constructor: 0 Altitude true - */ - CAltitude() : BlackMisc::PhysicalQuantities::CLength(0, BlackMisc::PhysicalQuantities::CLengthUnit::m()), m_datum(MeanSeaLevel) {} + /*! + * \brief Stream from DBus >> + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); - /*! - * \brief Constructor - * \param value - * \param datum MSL or AGL? - * \param unit - */ - CAltitude(double value, ReferenceDatum datum, const BlackMisc::PhysicalQuantities::CLengthUnit &unit) : BlackMisc::PhysicalQuantities::CLength(value, unit), m_datum(datum) {} + public: + /*! + * \brief Default constructor: 0 Altitude true + */ + CAltitude() : BlackMisc::PhysicalQuantities::CLength(0, BlackMisc::PhysicalQuantities::CLengthUnit::m()), m_datum(MeanSeaLevel) {} - /*! - * \brief Constructor by CLength - * \param altitude - * \param datum - */ - CAltitude(BlackMisc::PhysicalQuantities::CLength altitude, ReferenceDatum datum) : BlackMisc::PhysicalQuantities::CLength(altitude), m_datum(datum) {} + /*! + * \brief Constructor + * \param value + * \param datum MSL or AGL? + * \param unit + */ + CAltitude(double value, ReferenceDatum datum, const BlackMisc::PhysicalQuantities::CLengthUnit &unit) : BlackMisc::PhysicalQuantities::CLength(value, unit), m_datum(datum) {} - /*! - * \brief Equal operator == - * \param other - * @return - */ - bool operator ==(const CAltitude &other); + /*! + * \brief Constructor by CLength + * \param altitude + * \param datum + */ + CAltitude(BlackMisc::PhysicalQuantities::CLength altitude, ReferenceDatum datum) : BlackMisc::PhysicalQuantities::CLength(altitude), m_datum(datum) {} - /*! - * \brief Unequal operator == - * \param other - * @return - */ - bool operator !=(const CAltitude &other); + /*! + * \brief Equal operator == + * \param other + * @return + */ + bool operator ==(const CAltitude &other) const; - /*! - * \brief AGL Above ground level? - * \return - */ - bool isAboveGroundLevel() const - { - return AboveGround == this->m_datum; - } + /*! + * \brief Unequal operator == + * \param other + * @return + */ + bool operator !=(const CAltitude &other) const; - /*! - * \brief MSL Mean sea level? - * \return - */ - bool isMeanSeaLevel() const - { - return MeanSeaLevel == this->m_datum; - } + /*! + * \brief AGL Above ground level? + * \return + */ + bool isAboveGroundLevel() const + { + return AboveGround == this->m_datum; + } - /*! - * \brief Get reference datum (MSL or AGL) - * \return - */ - ReferenceDatum getReferenceDatum() const { return m_datum; } + /*! + * \brief MSL Mean sea level? + * \return + */ + bool isMeanSeaLevel() const + { + return MeanSeaLevel == this->m_datum; + } - /*! - * \brief Register metadata - */ - static void registerMetadata(); -}; + /*! + * \brief Get reference datum (MSL or AGL) + * \return + */ + ReferenceDatum getReferenceDatum() const + { + return m_datum; + } -} // namespace + /*! + * \brief Register metadata + */ + static void registerMetadata(); + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Aviation::CAltitude) diff --git a/src/blackmisc/avheading.cpp b/src/blackmisc/avheading.cpp index ffaddbbd9..c122e9cda 100644 --- a/src/blackmisc/avheading.cpp +++ b/src/blackmisc/avheading.cpp @@ -10,42 +10,71 @@ using BlackMisc::PhysicalQuantities::CAngleUnit; namespace BlackMisc { -namespace Aviation -{ + namespace Aviation + { -/* - * Own implementation for streaming - */ -QString CHeading::convertToQString(bool i18n) const -{ - QString s = CAngle::convertToQString(i18n); - return s.append(this->isMagneticHeading() ? " magnetic" : " true"); -} + /* + * Own implementation for streaming + */ + QString CHeading::convertToQString(bool i18n) const + { + QString s = CAngle::convertToQString(i18n).append(" "); + if (i18n) + { + return s.append(this->isMagneticHeading() ? + QCoreApplication::translate("Aviation", "magnetic") : + QCoreApplication::translate("Aviation", "true")); + } + else + { + return s.append(this->isMagneticHeading() ? "magnetic" : "true"); + } + } -/* - * Equal? - */ -bool CHeading::operator ==(const CHeading &other) -{ - return other.m_north == this->m_north && this->CAngle::operator ==(other); -} + /* + * Marshall to DBus + */ + void CHeading::marshallToDbus(QDBusArgument &argument) const + { + this->CAngle::marshallToDbus(argument); + argument << qint32(this->m_north); + } -/* - * Unequal? - */ -bool CHeading::operator !=(const CHeading &other) -{ - return !((*this) == other); -} + /* + * Unmarshall from DBus + */ + void CHeading::unmarshallFromDbus(const QDBusArgument &argument) + { + this->CAngle::unmarshallFromDbus(argument); + qint32 north; + argument >> north; + this->m_north = static_cast(north); + } -/*! - * \brief Register metadata of unit and quantity - */ -void CHeading::registerMetadata() -{ - qRegisterMetaType(typeid(CHeading).name()); - qDBusRegisterMetaType(); -} + /* + * Equal? + */ + bool CHeading::operator ==(const CHeading &other) const + { + return other.m_north == this->m_north && this->CAngle::operator ==(other); + } -} // namespace + /* + * Unequal? + */ + bool CHeading::operator !=(const CHeading &other) const + { + return !((*this) == other); + } + + /*! + * \brief Register metadata of unit and quantity + */ + void CHeading::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + } // namespace } // namespace diff --git a/src/blackmisc/avheading.h b/src/blackmisc/avheading.h index e187143ff..e8d0c7468 100644 --- a/src/blackmisc/avheading.h +++ b/src/blackmisc/avheading.h @@ -9,118 +9,108 @@ namespace BlackMisc { -namespace Aviation -{ - -/*! - * \brief Heading as used in aviation, can be true or magnetic heading - * \remarks Intentionally allowing +/- CAngle , and >= / <= CAngle. - */ -class CHeading : public BlackMisc::PhysicalQuantities::CAngle -{ -public: - /*! - * Enum type to distinguish between true north and magnetic north - */ - enum ReferenceNorth + namespace Aviation { - Magnetic = 0, //!< magnetic north - True = 1, //!< true north - }; -private: - ReferenceNorth m_north; //!< magnetic or true? + /*! + * \brief Heading as used in aviation, can be true or magnetic heading + * \remarks Intentionally allowing +/- CAngle , and >= / <= CAngle. + */ + class CHeading : public BlackMisc::PhysicalQuantities::CAngle + { + public: + /*! + * Enum type to distinguish between true north and magnetic north + */ + enum ReferenceNorth + { + Magnetic = 0, //!< magnetic north + True = 1 //!< true north + }; -protected: - /*! - * \brief Specific stream operation for heading - * \param i18n - * \return - */ - virtual QString convertToQString(bool i18n = false) const; + private: + ReferenceNorth m_north; //!< magnetic or true? - /*! - * \brief Stream to DBus << - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - this->CAngle::marshallToDbus(argument); - argument << qint32(this->m_north); - } + protected: + /*! + * \brief Specific stream operation for heading + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n = false) const; - /*! - * \brief Stream from DBus >> - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - this->CAngle::unmarshallFromDbus(argument); - qint32 north; - argument >> north; - this->m_north = static_cast(north); - } + /*! + * \brief Stream to DBus << + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; -public: - /*! - * \brief Default constructor: 0 heading true - */ - CHeading() : CAngle(0, BlackMisc::PhysicalQuantities::CAngleUnit::rad()), m_north(Magnetic) {} + /*! + * \brief Stream from DBus >> + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); - /*! - * \brief Constructor - * \param value - * \param north - * \param unit - */ - CHeading(double value, ReferenceNorth north, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : CAngle(value, unit), m_north(north) {} + public: + /*! + * \brief Default constructor: 0 heading true + */ + CHeading() : CAngle(0, BlackMisc::PhysicalQuantities::CAngleUnit::rad()), m_north(Magnetic) {} - /*! - * \brief Constructor by CAngle - * \param north - * \param magnetic - */ - CHeading(CAngle heading, ReferenceNorth north) : CAngle(heading), m_north(north) {} + /*! + * \brief Constructor + * \param value + * \param north + * \param unit + */ + CHeading(double value, ReferenceNorth north, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : CAngle(value, unit), m_north(north) {} - /*! - * \brief Equal operator == - * \param other - * @return - */ - bool operator ==(const CHeading &other); + /*! + * \brief Constructor by CAngle + * \param north + * \param magnetic + */ + CHeading(CAngle heading, ReferenceNorth north) : CAngle(heading), m_north(north) {} - /*! - * \brief Unequal operator == - * \param other - * @return - */ - bool operator !=(const CHeading &other); + /*! + * \brief Equal operator == + * \param other + * @return + */ + bool operator ==(const CHeading &other) const; - /*! - * \brief Magnetic heading? - * \return - */ - bool isMagneticHeading() const { return Magnetic == this->m_north; } + /*! + * \brief Unequal operator == + * \param other + * @return + */ + bool operator !=(const CHeading &other) const; - /*! - * \brief True heading? - * \return - */ - bool isTrueHeading() const { return True == this->m_north; } + /*! + * \brief Magnetic heading? + * \return + */ + bool isMagneticHeading() const { return Magnetic == this->m_north; } - /*! - * \brief Get reference north (magnetic or true) - * \return - */ - ReferenceNorth getReferenceNorth() const { return m_north; } + /*! + * \brief True heading? + * \return + */ + bool isTrueHeading() const { return True == this->m_north; } - /*! - * \brief Register metadata - */ - static void registerMetadata(); -}; + /*! + * \brief Get reference north (magnetic or true) + * \return + */ + ReferenceNorth getReferenceNorth() const { return m_north; } -} // namespace + /*! + * \brief Register metadata + */ + static void registerMetadata(); + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Aviation::CHeading) diff --git a/src/blackmisc/avioadfsystem.h b/src/blackmisc/avioadfsystem.h index 6dc056e82..47f6c1adf 100644 --- a/src/blackmisc/avioadfsystem.h +++ b/src/blackmisc/avioadfsystem.h @@ -11,264 +11,265 @@ namespace BlackMisc { -namespace Aviation -{ - -/*! - * \brief ADF system ("for NDBs") - */ -class CAdfSystem : public CModulator -{ -private: - /*! - * \brief Valid civil aviation frequency? - * \param f - * \return - */ - bool isValidFrequency(PhysicalQuantities::CFrequency f) const + namespace Aviation { - double fr = f.valueRounded(PhysicalQuantities::CFrequencyUnit::kHz(), this->m_digits); - return fr >= 190.0 && fr <= 1750.0; - } - /*! - * \brief Constructor - * \param validate - * \param name - * \param activeFrequency - * \param standbyFrequency - * \param digits - * - */ - CAdfSystem(bool validate, const QString &name, const PhysicalQuantities::CFrequency &activeFrequency, const PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): - CModulator(name, activeFrequency, standbyFrequency, digits) - { - this->validate(validate); - } + /*! + * \brief ADF system ("for NDBs") + */ + class CAdfSystem : public CModulator + { + private: + /*! + * \brief Valid civil aviation frequency? + * \param f + * \return + */ + bool isValidFrequency(PhysicalQuantities::CFrequency f) const + { + double fr = f.valueRounded(PhysicalQuantities::CFrequencyUnit::kHz(), this->m_digits); + return fr >= 190.0 && fr <= 1750.0; + } -protected: - /*! - * \brief Are the set values valid / in range? - * \return - */ - bool validValues() const - { - if (this->isDefaultValue()) return true; // special case - return - this->isValidFrequency(this->getFrequencyActive()) && - this->isValidFrequency(this->getFrequencyStandby()); - } + /*! + * \brief Constructor + * \param validate + * \param name + * \param activeFrequency + * \param standbyFrequency + * \param digits + * + */ + CAdfSystem(bool validate, const QString &name, const PhysicalQuantities::CFrequency &activeFrequency, const PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): + CModulator(name, activeFrequency, standbyFrequency, digits) + { + this->validate(validate); + } - /*! - * \brief Validate values by assert and exception - * \param strict - * \throws std::range_error - * \remarks Cannot be virtualsince already used in constructor - * \return - */ - bool validate(bool strict = true) const - { - if (this->isDefaultValue()) return true; - bool valid = this->validValues(); - if (!strict) return valid; - Q_ASSERT_X(valid, "CAdfSystem::validate", "illegal values"); - if (!valid) throw std::range_error("Illegal values in CAdfSystem::validate"); - return true; - } + protected: + /*! + * \brief Are the set values valid / in range? + * \return + */ + bool validValues() const + { + if (this->isDefaultValue()) return true; // special case + return + this->isValidFrequency(this->getFrequencyActive()) && + this->isValidFrequency(this->getFrequencyStandby()); + } -public: - /*! - * Default constructor - */ - CAdfSystem() : CModulator() {} + /*! + * \brief Validate values by assert and exception + * \param strict + * \throws std::range_error + * \remarks Cannot be virtualsince already used in constructor + * \return + */ + bool validate(bool strict = true) const + { + if (this->isDefaultValue()) return true; + bool valid = this->validValues(); + if (!strict) return valid; + Q_ASSERT_X(valid, "CAdfSystem::validate", "illegal values"); + if (!valid) throw std::range_error("Illegal values in CAdfSystem::validate"); + return true; + } - /*! - * \brief Copy constructor - * \param other - */ - CAdfSystem(const CAdfSystem &other) : CModulator(other) {} + public: + /*! + * Default constructor + */ + CAdfSystem() : CModulator() {} - /*! - * \brief Constructor - * \param name - * \param activeFrequency - * \param standbyFrequency - * \param digits - */ - CAdfSystem(const QString &name, const PhysicalQuantities::CFrequency &activeFrequency, const PhysicalQuantities::CFrequency &standbyFrequency = CModulator::FrequencyNotSet(), int digits = 3): - CModulator(name, activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency, digits) - { - this->validate(true); - } + /*! + * \brief Copy constructor + * \param other + */ + CAdfSystem(const CAdfSystem &other) : CModulator(other) {} - /*! - * \brief Set active frequency - * \param frequencyKHz - */ - void setFrequencyActiveKHz(double frequencyKHz) - { - this->CModulator::setFrequencyActiveKHz(frequencyKHz); - this->validate(true); - } + /*! + * \brief Constructor + * \param name + * \param activeFrequency + * \param standbyFrequency + * \param digits + */ + CAdfSystem(const QString &name, const PhysicalQuantities::CFrequency &activeFrequency, const PhysicalQuantities::CFrequency &standbyFrequency = CModulator::FrequencyNotSet(), int digits = 3): + CModulator(name, activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency, digits) + { + this->validate(true); + } - /*! - * \brief Set standby frequency - * \param frequencyKHz - */ - void setFrequencyStandbyKHz(double frequencyKHz) - { - this->CModulator::setFrequencyStandbyKHz(frequencyKHz); - this->validate(true); - } - /*! - * \brief operator == - * \param otherSystem - * \return - */ - bool operator ==(const CAdfSystem &otherSystem) const - { - return this->CModulator::operator ==(otherSystem); - } + /*! + * \brief Set active frequency + * \param frequencyKHz + */ + void setFrequencyActiveKHz(double frequencyKHz) + { + this->CModulator::setFrequencyActiveKHz(frequencyKHz); + this->validate(true); + } - /*! - * \brief operator == - * \param otherSystem - * \return - */ - bool operator !=(const CAdfSystem &otherSystem) const - { - return this->CModulator::operator !=(otherSystem); - } + /*! + * \brief Set standby frequency + * \param frequencyKHz + */ + void setFrequencyStandbyKHz(double frequencyKHz) + { + this->CModulator::setFrequencyStandbyKHz(frequencyKHz); + this->validate(true); + } - /*! - * Try to get a ADF unit with given name and frequency. Returns true in case an object - * has been sucessfully created, otherwise returns a default object and false. - * \param[out] o_adfSystem - * \param name - * \param activeFrequencyKHz - * \param standbyFrequencyKHz - * \return - */ - static bool tryGetAdfSystem(CAdfSystem &o_adfSystem, const QString &name, double activeFrequencyKHz, double standbyFrequencyKHz = -1) - { - o_adfSystem = CAdfSystem(false, name, PhysicalQuantities::CFrequency(activeFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz()), PhysicalQuantities::CFrequency(standbyFrequencyKHz < 0 ? activeFrequencyKHz : standbyFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz())); - bool s; - if (!(s = o_adfSystem.validate(false))) o_adfSystem = CAdfSystem(); // reset to default - return s; - } + /*! + * \brief operator == + * \param otherSystem + * \return + */ + bool operator ==(const CAdfSystem &otherSystem) const + { + return this->CModulator::operator ==(otherSystem); + } - /*! - * Try to get a ADF unit with given name and frequency. Returns true in case an object - * has been sucessfully created, otherwise returns a default object. - * \param[out] o_adfSystem - * \param name - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetAdfSystem(CAdfSystem &o_adfSystem, const QString &name, PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - o_adfSystem = CAdfSystem(false, name, activeFrequency, standbyFrequency); - bool s; - if (!(s = o_adfSystem.validate(false))) o_adfSystem = CAdfSystem(); // reset to default - return s; - } + /*! + * \brief operator == + * \param otherSystem + * \return + */ + bool operator !=(const CAdfSystem &otherSystem) const + { + return this->CModulator::operator !=(otherSystem); + } - /*! - * \brief ADF1 unit - * \param activeFrequencyKHz - * \param standbyFrequencyKHz - * \return - */ - static CAdfSystem GetAdf1System(double activeFrequencyKHz, double standbyFrequencyKHz = -1) - { - return CAdfSystem(CModulator::NameCom1(), PhysicalQuantities::CFrequency(activeFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz()), PhysicalQuantities::CFrequency(standbyFrequencyKHz < 0 ? activeFrequencyKHz : standbyFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz())); - } + /*! + * Try to get a ADF unit with given name and frequency. Returns true in case an object + * has been sucessfully created, otherwise returns a default object and false. + * \param[out] o_adfSystem + * \param name + * \param activeFrequencyKHz + * \param standbyFrequencyKHz + * \return + */ + static bool tryGetAdfSystem(CAdfSystem &o_adfSystem, const QString &name, double activeFrequencyKHz, double standbyFrequencyKHz = -1) + { + o_adfSystem = CAdfSystem(false, name, PhysicalQuantities::CFrequency(activeFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz()), PhysicalQuantities::CFrequency(standbyFrequencyKHz < 0 ? activeFrequencyKHz : standbyFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz())); + bool s; + if (!(s = o_adfSystem.validate(false))) o_adfSystem = CAdfSystem(); // reset to default + return s; + } - /*! - * \brief ADF1 unit - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static CAdfSystem GetAdf1System(PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CAdfSystem(CModulator::NameCom1(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); - } + /*! + * Try to get a ADF unit with given name and frequency. Returns true in case an object + * has been sucessfully created, otherwise returns a default object. + * \param[out] o_adfSystem + * \param name + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetAdfSystem(CAdfSystem &o_adfSystem, const QString &name, PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + o_adfSystem = CAdfSystem(false, name, activeFrequency, standbyFrequency); + bool s; + if (!(s = o_adfSystem.validate(false))) o_adfSystem = CAdfSystem(); // reset to default + return s; + } - /*! - * \brief Try to get ADF unit - * \param[out] o_adfSystem - * \param activeFrequencyKHz - * \param standbyFrequencyKHz - * \return - */ - static bool tryGetAdf1Unit(CAdfSystem &o_adfSystem, double activeFrequencyKHz, double standbyFrequencyKHz = -1) - { - return CAdfSystem::tryGetAdfSystem(o_adfSystem, CModulator::NameCom1(), activeFrequencyKHz, standbyFrequencyKHz); - } + /*! + * \brief ADF1 unit + * \param activeFrequencyKHz + * \param standbyFrequencyKHz + * \return + */ + static CAdfSystem GetAdf1System(double activeFrequencyKHz, double standbyFrequencyKHz = -1) + { + return CAdfSystem(CModulator::NameCom1(), PhysicalQuantities::CFrequency(activeFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz()), PhysicalQuantities::CFrequency(standbyFrequencyKHz < 0 ? activeFrequencyKHz : standbyFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz())); + } - /*! - * \brief Try to get ADF unit - * \param[out] o_adfSystem - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetAdf1Unit(CAdfSystem &o_adfSystem, PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CAdfSystem::tryGetAdfSystem(o_adfSystem, CModulator::NameCom1(), activeFrequency, standbyFrequency); - } + /*! + * \brief ADF1 unit + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static CAdfSystem GetAdf1System(PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CAdfSystem(CModulator::NameCom1(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); + } - /*! - * \brief ADF2 unit - * \param activeFrequencyKHz - * \param standbyFrequencyKHz - * \return - */ - static CAdfSystem GetAdf2System(double activeFrequencyKHz, double standbyFrequencyKHz = -1) - { - return CAdfSystem(CModulator::NameCom2(), PhysicalQuantities::CFrequency(activeFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz()), PhysicalQuantities::CFrequency(standbyFrequencyKHz < 0 ? activeFrequencyKHz : standbyFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz())); - } + /*! + * \brief Try to get ADF unit + * \param[out] o_adfSystem + * \param activeFrequencyKHz + * \param standbyFrequencyKHz + * \return + */ + static bool tryGetAdf1Unit(CAdfSystem &o_adfSystem, double activeFrequencyKHz, double standbyFrequencyKHz = -1) + { + return CAdfSystem::tryGetAdfSystem(o_adfSystem, CModulator::NameCom1(), activeFrequencyKHz, standbyFrequencyKHz); + } - /*! - * \brief ADF2 unit - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static CAdfSystem GetAdf2System(PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CAdfSystem(CModulator::NameCom2(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); - } + /*! + * \brief Try to get ADF unit + * \param[out] o_adfSystem + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetAdf1Unit(CAdfSystem &o_adfSystem, PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CAdfSystem::tryGetAdfSystem(o_adfSystem, CModulator::NameCom1(), activeFrequency, standbyFrequency); + } - /*! - * \brief Try to get ADF unit - * \param[out] o_adfSystem - * \param activeFrequencyKHz - * \param standbyFrequencyKHz - * \return - */ - static bool tryGetAdf2System(CAdfSystem &o_adfSystem, double activeFrequencyKHz, double standbyFrequencyKHz = -1) - { - return CAdfSystem::tryGetAdfSystem(o_adfSystem, CModulator::NameCom2(), activeFrequencyKHz, standbyFrequencyKHz); - } + /*! + * \brief ADF2 unit + * \param activeFrequencyKHz + * \param standbyFrequencyKHz + * \return + */ + static CAdfSystem GetAdf2System(double activeFrequencyKHz, double standbyFrequencyKHz = -1) + { + return CAdfSystem(CModulator::NameCom2(), PhysicalQuantities::CFrequency(activeFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz()), PhysicalQuantities::CFrequency(standbyFrequencyKHz < 0 ? activeFrequencyKHz : standbyFrequencyKHz, PhysicalQuantities::CFrequencyUnit::MHz())); + } - /*! - * \brief Try to get ADF unit - * \param[out] o_adfSystem - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetAdf2System(CAdfSystem &o_adfSystem, PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CAdfSystem::tryGetAdfSystem(o_adfSystem, CModulator::NameCom2(), activeFrequency, standbyFrequency); - } -}; + /*! + * \brief ADF2 unit + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static CAdfSystem GetAdf2System(PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CAdfSystem(CModulator::NameCom2(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); + } -} // namespace + /*! + * \brief Try to get ADF unit + * \param[out] o_adfSystem + * \param activeFrequencyKHz + * \param standbyFrequencyKHz + * \return + */ + static bool tryGetAdf2System(CAdfSystem &o_adfSystem, double activeFrequencyKHz, double standbyFrequencyKHz = -1) + { + return CAdfSystem::tryGetAdfSystem(o_adfSystem, CModulator::NameCom2(), activeFrequencyKHz, standbyFrequencyKHz); + } + + /*! + * \brief Try to get ADF unit + * \param[out] o_adfSystem + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetAdf2System(CAdfSystem &o_adfSystem, PhysicalQuantities::CFrequency activeFrequency, PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CAdfSystem::tryGetAdfSystem(o_adfSystem, CModulator::NameCom2(), activeFrequency, standbyFrequency); + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Aviation::CAdfSystem) diff --git a/src/blackmisc/aviobase.h b/src/blackmisc/aviobase.h index fa77b57d3..0594c43dc 100644 --- a/src/blackmisc/aviobase.h +++ b/src/blackmisc/aviobase.h @@ -13,86 +13,86 @@ namespace BlackMisc { -namespace Aviation -{ + namespace Aviation + { -/*! - * \brief Base class for avionics - */ class CAvionicsBase : public BlackMisc::CStreamable -{ -protected: - QString m_name; //!< name of the unit + /*! + * \brief Base class for avionics + */ + { + protected: + QString m_name; //!< name of the unit - /*! - * \brief Constructor - */ - CAvionicsBase(const QString &name) : m_name(name) {} + /*! + * \brief Constructor + */ + CAvionicsBase(const QString &name) : m_name(name) {} - /*! - * \brief Are the set values valid / in range - * \return - */ - virtual bool validValues() - { - return true; - } + /*! + * \brief Are the set values valid / in range + * \return + */ + virtual bool validValues() + { + return true; + } - /*! - * \brief Set name - * \param name - */ - void setName(const QString &name) - { - this->m_name = name; - } + /*! + * \brief Set name + * \param name + */ + void setName(const QString &name) + { + this->m_name = name; + } - /*! - * \brief operator == - * \param other - * \return - */ - bool operator ==(const CAvionicsBase &other) const - { - if (this == &other) return true; - return this->m_name == other.m_name; - } + /*! + * \brief operator == + * \param other + * \return + */ + bool operator ==(const CAvionicsBase &other) const + { + if (this == &other) return true; + return this->m_name == other.m_name; + } - /*! - * \brief Stream to DBus << - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - argument << this->m_name; - } + /*! + * \brief Stream to DBus << + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const + { + argument << this->m_name; + } - /*! - * \brief Stream from DBus >> - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - argument >> this->m_name; - } + /*! + * \brief Stream from DBus >> + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + argument >> this->m_name; + } -public: - /*! - * \brief Virtual destructor - */ - virtual ~CAvionicsBase() {} + public: + /*! + * \brief Virtual destructor + */ + virtual ~CAvionicsBase() {} - /*! - * \brief Name - * \return - */ - QString getName() const - { - return this->m_name; - } -}; + /*! + * \brief Name + * \return + */ + QString getName() const + { + return this->m_name; + } + }; -} // namespace + } // namespace } // namespace #endif // guard diff --git a/src/blackmisc/aviocomsystem.h b/src/blackmisc/aviocomsystem.h index ab10eab81..97a02e55d 100644 --- a/src/blackmisc/aviocomsystem.h +++ b/src/blackmisc/aviocomsystem.h @@ -10,341 +10,341 @@ namespace BlackMisc { -namespace Aviation -{ - -/*! - * \brief COM system (aka "radio") - */ -class CComSystem : public CModulator -{ -private: - /*! - * \brief Valid civil aviation frequency? - * \param f - * \return - */ - bool isValidCivilAviationFrequency(BlackMisc::PhysicalQuantities::CFrequency f) const + namespace Aviation { - double fr = f.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz(), this->m_digits); - return fr >= 118.0 && fr <= 136.975; - } - /*! - * \brief Valid military aviation frequency? - * \param f - * \return - */ - bool isValidMilitaryFrequency(BlackMisc::PhysicalQuantities::CFrequency f) const - { - double fr = f.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz(), this->m_digits); - return fr >= 220.0 && fr <= 399.95; - } + /*! + * \brief COM system (aka "radio") + */ + class CComSystem : public CModulator + { + private: + /*! + * \brief Constructor + * \param validate + * \param name + * \param activeFrequency + * \param standbyFrequency + * \param digits + * + */ + CComSystem(bool validate, const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): + CModulator(name, activeFrequency, standbyFrequency, digits) + { + this->validate(validate); + } - /*! - * \brief Constructor - * \param validate - * \param name - * \param activeFrequency - * \param standbyFrequency - * \param digits - * - */ - CComSystem(bool validate, const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): - CModulator(name, activeFrequency, standbyFrequency, digits) - { - this->validate(validate); - } + protected: + /*! + * \brief Are the set values valid / in range? + * \return + */ + bool validValues() const + { + if (this->isDefaultValue()) return true; // special case + return + (CComSystem::isValidCivilAviationFrequency(this->getFrequencyActive()) || + CComSystem::isValidMilitaryFrequency(this->getFrequencyActive())) && + (CComSystem::isValidCivilAviationFrequency(this->getFrequencyStandby()) || + CComSystem::isValidMilitaryFrequency(this->getFrequencyStandby())); + } -protected: - /*! - * \brief Are the set values valid / in range? - * \return - */ - bool validValues() const - { - if (this->isDefaultValue()) return true; // special case - return - (this->isValidCivilAviationFrequency(this->getFrequencyActive()) || - this->isValidMilitaryFrequency(this->getFrequencyActive())) && - (this->isValidCivilAviationFrequency(this->getFrequencyStandby()) || - this->isValidMilitaryFrequency(this->getFrequencyStandby())); - } + /*! + * \brief Validate values by assert and exception + * \param strict + * \throws std::range_error + * \remarks Cannot be virtual because used in constructor + * \return + */ + bool validate(bool strict = true) const + { + if (this->isDefaultValue()) return true; + bool valid = this->validValues(); + if (!strict) return valid; + Q_ASSERT_X(valid, "CComSystem::validate", "illegal values"); + if (!valid) throw std::range_error("Illegal values in CComSystem::validate"); + return true; + } - /*! - * \brief Validate values by assert and exception - * \param strict - * \throws std::range_error - * \remarks Cannot be virtual because used in constructor - * \return - */ - bool validate(bool strict = true) const - { - if (this->isDefaultValue()) return true; - bool valid = this->validValues(); - if (!strict) return valid; - Q_ASSERT_X(valid, "CComSystem::validate", "illegal values"); - if (!valid) throw std::range_error("Illegal values in CComSystem::validate"); - return true; - } + public: + /*! + * Default constructor + */ + CComSystem() : CModulator() {} -public: - /*! - * Default constructor - */ - CComSystem() : CModulator() {} + /*! + * \brief Copy constructor + * \param other + */ + CComSystem(const CComSystem &other) : CModulator(other) {} - /*! - * \brief Copy constructor - * \param other - */ - CComSystem(const CComSystem &other) : CModulator(other) {} + /*! + * \brief Constructor + * \param name + * \param activeFrequency + * \param standbyFrequency + * \param digits + */ + CComSystem(const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency = CModulator::FrequencyNotSet(), int digits = 3): + CModulator(name, activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency, digits) + { + this->validate(true); + } - /*! - * \brief Constructor - * \param name - * \param activeFrequency - * \param standbyFrequency - * \param digits - */ - CComSystem(const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency = CModulator::FrequencyNotSet(), int digits = 3): - CModulator(name, activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency, digits) - { - this->validate(true); - } + /*! + * \brief Set active frequency + * \param frequencyMHz + */ + void setFrequencyActiveMHz(double frequencyMHz) + { + this->CModulator::setFrequencyActiveMHz(frequencyMHz); + this->validate(true); + } - /*! - * \brief Set active frequency - * \param frequencyMHz - */ - void setFrequencyActiveMHz(double frequencyMHz) - { - this->CModulator::setFrequencyActiveMHz(frequencyMHz); - this->validate(true); - } + /*! + * \brief Set standby frequency + * \param frequencyMHz + */ + void setFrequencyStandbyMHz(double frequencyMHz) + { + this->CModulator::setFrequencyStandbyMHz(frequencyMHz); + this->validate(true); + } - /*! - * \brief Set standby frequency - * \param frequencyMHz - */ - void setFrequencyStandbyMHz(double frequencyMHz) - { - this->CModulator::setFrequencyStandbyMHz(frequencyMHz); - this->validate(true); - } + /*! + * \brief Set UNICOM frequency as active + */ + void setActiveUnicom() + { + this->toggleActiveStandby(); + this->setFrequencyActive(BlackMisc::PhysicalQuantities::CPhysicalQuantitiesConstants::FrequencyUnicom()); + } - /*! - * \brief Set UNICOM frequency as active - */ - void setActiveUnicom() - { - this->toggleActiveStandby(); - this->setFrequencyActive(BlackMisc::PhysicalQuantities::CPhysicalQuantitiesConstants::FrequencyUnicom()); - } + /*! + * \brief Set International Air Distress 121.5MHz + */ + void setActiveInternationalAirDistress() + { + this->toggleActiveStandby(); + this->setFrequencyActive(BlackMisc::PhysicalQuantities::CPhysicalQuantitiesConstants::FrequencyInternationalAirDistress()); + } - /*! - * \brief Set International Air Distress 121.5MHz - */ - void setActiveInternationalAirDistress() - { - this->toggleActiveStandby(); - this->setFrequencyActive(BlackMisc::PhysicalQuantities::CPhysicalQuantitiesConstants::FrequencyInternationalAirDistress()); - } + /*! + * \brief operator == + * \param other + * \return + */ + bool operator ==(const CComSystem &other) const + { + return this->CModulator::operator ==(other); + } - /*! - * \brief operator == - * \param other - * \return - */ - bool operator ==(const CComSystem &other) const - { - return this->CModulator::operator ==(other); - } + /*! + * \brief operator != + * \param other + * \return + */ + bool operator !=(const CComSystem &other) const + { + return this->CModulator::operator !=(other); + } - /*! - * \brief operator != - * \param other - * \return - */ - bool operator !=(const CComSystem &other) const - { - return this->CModulator::operator !=(other); - } + /*! + * Try to get a COM unit with given name and frequency. Returns true in case an object + * has been sucessfully created, otherwise returns a default object. + * \param[out] o_comSystem + * \param name + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static bool tryGetComSystem(CComSystem &o_comSystem, const QString &name, double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + o_comSystem = CComSystem(false, name, BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); + bool s; + if (!(s = o_comSystem.validate(false))) o_comSystem = CComSystem(); // reset to default + return s; + } - /*! - * Try to get a COM unit with given name and frequency. Returns true in case an object - * has been sucessfully created, otherwise returns a default object. - * \param[out] o_comSystem - * \param name - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static bool tryGetComSystem(CComSystem &o_comSystem, const QString &name, double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - o_comSystem = CComSystem(false, name, BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); - bool s; - if (!(s = o_comSystem.validate(false))) o_comSystem = CComSystem(); // reset to default - return s; - } + /*! + * Try to get a COM unit with given name and frequency. Returns true in case an object + * has been sucessfully created, otherwise returns a default object. + * \param[out] o_comSystem + * \param name + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetComSystem(CComSystem &o_comSystem, const QString &name, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + o_comSystem = CComSystem(false, name, activeFrequency, standbyFrequency); + bool s; + if (!(s = o_comSystem.validate(false))) o_comSystem = CComSystem(); // reset to default + return s; + } - /*! - * Try to get a COM unit with given name and frequency. Returns true in case an object - * has been sucessfully created, otherwise returns a default object. - * \param[out] o_comSystem - * \param name - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetComSystem(CComSystem &o_comSystem, const QString &name, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - o_comSystem = CComSystem(false, name, activeFrequency, standbyFrequency); - bool s; - if (!(s = o_comSystem.validate(false))) o_comSystem = CComSystem(); // reset to default - return s; - } + /*! + * \brief COM1 unit + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static CComSystem getCom1System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CComSystem(CModulator::NameCom1(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); + } - /*! - * \brief COM1 unit - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static CComSystem getCom1System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CComSystem(CModulator::NameCom1(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); - } + /*! + * \brief COM1 unit + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static CComSystem getCom1System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CComSystem(CModulator::NameCom1(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); + } - /*! - * \brief COM1 unit - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static CComSystem getCom1System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CComSystem(CModulator::NameCom1(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); - } + /*! + * \brief Try to get COM unit + * \param[out] o_comSystem + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static bool tryGetCom1Unit(CComSystem &o_comSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom1(), activeFrequencyMHz, standbyFrequencyMHz); + } - /*! - * \brief Try to get COM unit - * \param[out] o_comSystem - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static bool tryGetCom1Unit(CComSystem &o_comSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom1(), activeFrequencyMHz, standbyFrequencyMHz); - } + /*! + * \brief Try to get COM unit + * \param[out] o_comSystem + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetCom1Unit(CComSystem &o_comSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom1(), activeFrequency, standbyFrequency); + } - /*! - * \brief Try to get COM unit - * \param[out] o_comSystem - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetCom1Unit(CComSystem &o_comSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom1(), activeFrequency, standbyFrequency); - } + /*! + * \brief COM2 unit + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static CComSystem getCom2System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CComSystem(CModulator::NameCom2(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); + } - /*! - * \brief COM2 unit - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static CComSystem getCom2System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CComSystem(CModulator::NameCom2(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); - } + /*! + * \brief COM2 unit + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static CComSystem getCom2System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CComSystem(CModulator::NameCom2(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); + } - /*! - * \brief COM2 unit - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static CComSystem getCom2System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CComSystem(CModulator::NameCom2(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); - } + /*! + * \brief Try to get COM unit + * \param[out] o_comSystem + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static bool tryGetCom2System(CComSystem &o_comSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom2(), activeFrequencyMHz, standbyFrequencyMHz); + } - /*! - * \brief Try to get COM unit - * \param[out] o_comSystem - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static bool tryGetCom2System(CComSystem &o_comSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom2(), activeFrequencyMHz, standbyFrequencyMHz); - } + /*! + * \brief Try to get COM unit + * \param[out] o_comSystem + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetCom2System(CComSystem &o_comSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom2(), activeFrequency, standbyFrequency); + } - /*! - * \brief Try to get COM unit - * \param[out] o_comSystem - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetCom2System(CComSystem &o_comSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom2(), activeFrequency, standbyFrequency); - } + /*! + * \brief COM3 unit + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static CComSystem getCom3System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CComSystem(CModulator::NameCom3(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); + } - /*! - * \brief COM3 unit - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static CComSystem getCom3System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CComSystem(CModulator::NameCom3(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); - } + /*! + * \brief COM3 unit + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static CComSystem getCom3System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CComSystem(CModulator::NameCom3(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); + } - /*! - * \brief COM3 unit - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static CComSystem getCom3System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CComSystem(CModulator::NameCom3(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); - } + /*! + * \brief Try to get COM unit + * \param[out] o_comSystem + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static bool tryGetCom3System(CComSystem &o_comSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom3(), activeFrequencyMHz, standbyFrequencyMHz); + } - /*! - * \brief Try to get COM unit - * \param[out] o_comSystem - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static bool tryGetCom3System(CComSystem &o_comSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom3(), activeFrequencyMHz, standbyFrequencyMHz); - } + /*! + * \brief Try to get COM unit + * \param[out] o_comSystem + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetCom3System(CComSystem &o_comSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom3(), activeFrequency, standbyFrequency); + } - /*! - * \brief Try to get COM unit - * \param[out] o_comSystem - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetCom3System(CComSystem &o_comSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CComSystem::tryGetComSystem(o_comSystem, CModulator::NameCom3(), activeFrequency, standbyFrequency); - } -}; + /*! + * \brief Valid civil aviation frequency? + * \param f + * \return + */ + static bool isValidCivilAviationFrequency(BlackMisc::PhysicalQuantities::CFrequency f) + { + double fr = f.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz(), 3); + return fr >= 118.0 && fr <= 136.975; + } -} // namespace + /*! + * \brief Valid military aviation frequency? + * \param f + * \return + */ + static bool isValidMilitaryFrequency(BlackMisc::PhysicalQuantities::CFrequency f) + { + double fr = f.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz(), 3); + return fr >= 220.0 && fr <= 399.95; + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Aviation::CComSystem) diff --git a/src/blackmisc/aviomodulator.cpp b/src/blackmisc/aviomodulator.cpp index c7de9a2df..b69265fa4 100644 --- a/src/blackmisc/aviomodulator.cpp +++ b/src/blackmisc/aviomodulator.cpp @@ -13,52 +13,74 @@ using BlackMisc::PhysicalQuantities::CFrequencyUnit; namespace BlackMisc { -namespace Aviation -{ + namespace Aviation + { -/* - * Toggle standby <-> active - */ -template void CModulator::toggleActiveStandby() -{ - CFrequency a = this->m_frequencyActive; - this->m_frequencyActive = this->m_frequencyStandby; - this->m_frequencyStandby = a; -} + /* + * Toggle standby <-> active + */ + template void CModulator::toggleActiveStandby() + { + CFrequency a = this->m_frequencyActive; + this->m_frequencyActive = this->m_frequencyStandby; + this->m_frequencyStandby = a; + } -/* - * Register metadata - */ -template void CModulator::registerMetadata() -{ - qRegisterMetaType(typeid(AVIO).name()); - qDBusRegisterMetaType(); -} + /* + * Register metadata + */ + template void CModulator::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } -/* - * Equal operator == - */ -template bool CModulator::operator ==(const CModulator &other) const -{ - if (this == &other) return true; - return (this->getName() == other.getName() && - this->m_frequencyActive == other.m_frequencyActive && - this->m_frequencyStandby == other.m_frequencyStandby); -} + /* + * Equal operator == + */ + template bool CModulator::operator ==(const CModulator &other) const + { + if (this == &other) return true; + return (this->getName() == other.getName() && + this->m_frequencyActive == other.m_frequencyActive && + this->m_frequencyStandby == other.m_frequencyStandby); + } -/* - * Equal operator != - */ -template bool CModulator::operator !=(const CModulator &other) const -{ - return !(other == (*this)); -} + /* + * Equal operator != + */ + template bool CModulator::operator !=(const CModulator &other) const + { + return !(other == (*this)); + } -// see here for the reason of thess forward instantiations -// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html -template class CModulator; -template class CModulator; -template class CModulator; + /* + * To DBus + */ + template void CModulator::marshallToDbus(QDBusArgument &argument) const + { + this->CAvionicsBase::marshallToDbus(argument); + argument << this->m_frequencyActive; + argument << this->m_frequencyStandby; + argument << this->m_digits; + } -} // namespace + /* + * From DBuss + */ + template void CModulator::unmarshallFromDbus(const QDBusArgument &argument) + { + this->CAvionicsBase::unmarshallFromDbus(argument); + argument >> this->m_frequencyActive; + argument >> this->m_frequencyStandby; + argument >> this->m_digits; + } + + // see here for the reason of thess forward instantiations + // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html + template class CModulator; + template class CModulator; + template class CModulator; + + } // namespace } // namespace diff --git a/src/blackmisc/aviomodulator.h b/src/blackmisc/aviomodulator.h index 3a39ee661..cd36fb0db 100644 --- a/src/blackmisc/aviomodulator.h +++ b/src/blackmisc/aviomodulator.h @@ -11,284 +11,272 @@ namespace BlackMisc { -namespace Aviation -{ - -/*! - * \brief Base class for COM, NAV, Squawk units. - */ -template class CModulator : public CAvionicsBase -{ -private: - BlackMisc::PhysicalQuantities::CFrequency m_frequencyActive; //!< active frequency - BlackMisc::PhysicalQuantities::CFrequency m_frequencyStandby; //!< standby frequency - -protected: - int m_digits; //!< digits used - -protected: - /*! - * \brief Default constructor - */ - CModulator() : CAvionicsBase("default") {} - - /*! - * \brief Copy constructor - * \param other - */ - CModulator(const CModulator &other) : CAvionicsBase(other.getName()), - m_frequencyActive(other.m_frequencyActive), m_frequencyStandby(other.m_frequencyStandby), m_digits(other.m_digits) {} - - /*! - * \brief Constructor - * \param name - * \param activeFrequency - * \param standbyFrequency - * \param digits - */ - CModulator(const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits) : - CAvionicsBase(name), m_frequencyActive(activeFrequency), m_frequencyStandby(standbyFrequency), m_digits(digits) {} - - /*! - * \brief String for converter - * \param i18n - * \return - */ - virtual QString convertToQString(bool i18n = false) const + namespace Aviation { - QString s(this->getName()); - s.append(" Active: ").append(this->m_frequencyActive.valueRoundedWithUnit(3, i18n)); - s.append(" Standby: ").append(this->m_frequencyStandby.valueRoundedWithUnit(3, i18n)); - return s; - } - /*! - * \brief Set active frequency - * \param frequencyKHz - */ - void setFrequencyActiveKHz(double frequencyKHz) - { - this->m_frequencyActive = BlackMisc::PhysicalQuantities::CFrequency(frequencyKHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::kHz()); - } + /*! + * \brief Base class for COM, NAV, Squawk units. + */ + template class CModulator : public CAvionicsBase + { + private: + BlackMisc::PhysicalQuantities::CFrequency m_frequencyActive; //!< active frequency + BlackMisc::PhysicalQuantities::CFrequency m_frequencyStandby; //!< standby frequency - /*! - * \brief Set standby frequency - * \param frequencyKHz - */ - void setFrequencyStandbyKHz(double frequencyKHz) - { - this->m_frequencyStandby = BlackMisc::PhysicalQuantities::CFrequency(frequencyKHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::kHz()); - } + protected: + int m_digits; //!< digits used - /*! - * \brief Set active frequency - * \param frequencyMHz - */ - void setFrequencyActiveMHz(double frequencyMHz) - { - this->m_frequencyActive = BlackMisc::PhysicalQuantities::CFrequency(frequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()); - } + protected: + /*! + * \brief Default constructor + */ + CModulator() : CAvionicsBase("default") {} - /*! - * \brief Set standby frequency - * \param frequencyMHz - */ - void setFrequencyStandbyMHz(double frequencyMHz) - { - this->m_frequencyStandby = BlackMisc::PhysicalQuantities::CFrequency(frequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()); - } + /*! + * \brief Copy constructor + * \param other + */ + CModulator(const CModulator &other) : CAvionicsBase(other.getName()), + m_frequencyActive(other.m_frequencyActive), m_frequencyStandby(other.m_frequencyStandby), m_digits(other.m_digits) {} - /*! - * \brief operator == - * \param other - * \return - */ - bool operator ==(const CModulator &other) const; + /*! + * \brief Constructor + * \param name + * \param activeFrequency + * \param standbyFrequency + * \param digits + */ + CModulator(const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits) : + CAvionicsBase(name), m_frequencyActive(activeFrequency), m_frequencyStandby(standbyFrequency), m_digits(digits) {} - /*! - * \brief operator != - * \param other - * \return - */ - bool operator !=(const CModulator &other) const; + /*! + * \brief String for converter + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n = false) const + { + QString s(this->getName()); + s.append(" Active: ").append(this->m_frequencyActive.valueRoundedWithUnit(3, i18n)); + s.append(" Standby: ").append(this->m_frequencyStandby.valueRoundedWithUnit(3, i18n)); + return s; + } - /*! - * \brief COM1 - * \return - */ - static const QString &NameCom1() - { - static QString n("COM1"); - return n; - } + /*! + * \brief Set active frequency + * \param frequencyKHz + */ + void setFrequencyActiveKHz(double frequencyKHz) + { + this->m_frequencyActive = BlackMisc::PhysicalQuantities::CFrequency(frequencyKHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::kHz()); + } - /*! - * \brief COM2 - * \return - */ - static const QString &NameCom2() - { - static QString n("COM2"); - return n; - } + /*! + * \brief Set standby frequency + * \param frequencyKHz + */ + void setFrequencyStandbyKHz(double frequencyKHz) + { + this->m_frequencyStandby = BlackMisc::PhysicalQuantities::CFrequency(frequencyKHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::kHz()); + } - /*! - * \brief COM3 - * \return - */ - static const QString &NameCom3() - { - static QString n("COM3"); - return n; - } + /*! + * \brief Set active frequency + * \param frequencyMHz + */ + void setFrequencyActiveMHz(double frequencyMHz) + { + this->m_frequencyActive = BlackMisc::PhysicalQuantities::CFrequency(frequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()); + } - /*! - * \brief NAV1 - * \return - */ - static const QString &NameNav1() - { - static QString n("NAV1"); - return n; - } + /*! + * \brief Set standby frequency + * \param frequencyMHz + */ + void setFrequencyStandbyMHz(double frequencyMHz) + { + this->m_frequencyStandby = BlackMisc::PhysicalQuantities::CFrequency(frequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()); + } - /*! - * \brief NAV2 - * \return - */ - static const QString &NameNav2() - { - static QString n("NAV2"); - return n; - } + /*! + * \brief operator == + * \param other + * \return + */ + bool operator ==(const CModulator &other) const; - /*! - * \brief NAV2 - * \return - */ - static const QString &NameNav3() - { - static QString n("NAV3"); - return n; - } + /*! + * \brief operator != + * \param other + * \return + */ + bool operator !=(const CModulator &other) const; - /*! - * \brief ADF1 - * \return - */ - static const QString &NameAdf1() - { - static QString n("ADF1"); - return n; - } + /*! + * \brief COM1 + * \return + */ + static const QString &NameCom1() + { + static QString n("COM1"); + return n; + } - /*! - * \brief ADF2 - * \return - */ - static const QString &NameAdf2() - { - static QString n("ADF2"); - return n; - } + /*! + * \brief COM2 + * \return + */ + static const QString &NameCom2() + { + static QString n("COM2"); + return n; + } - /*! - * \brief Frequency not set - * \return - */ - static const BlackMisc::PhysicalQuantities::CFrequency &FrequencyNotSet() - { - static BlackMisc::PhysicalQuantities::CFrequency f; - return f; - } + /*! + * \brief COM3 + * \return + */ + static const QString &NameCom3() + { + static QString n("COM3"); + return n; + } - /*! - * \brief Stream to DBus << - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - this->CAvionicsBase::marshallToDbus(argument); - argument << this->m_frequencyActive; - argument << this->m_frequencyStandby; - argument << this->m_digits; - } + /*! + * \brief NAV1 + * \return + */ + static const QString &NameNav1() + { + static QString n("NAV1"); + return n; + } - /*! - * \brief Stream from DBus >> - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - this->CAvionicsBase::unmarshallFromDbus(argument); - argument >> this->m_frequencyActive; - argument >> this->m_frequencyStandby; - argument >> this->m_digits; - } + /*! + * \brief NAV2 + * \return + */ + static const QString &NameNav2() + { + static QString n("NAV2"); + return n; + } -public: - /*! - * \brief Virtual destructor - */ - virtual ~CModulator() {} + /*! + * \brief NAV2 + * \return + */ + static const QString &NameNav3() + { + static QString n("NAV3"); + return n; + } - /*! - * \brief Default value - * \return - */ - virtual bool isDefaultValue() const - { - return this->m_frequencyActive == CModulator::FrequencyNotSet(); - } + /*! + * \brief ADF1 + * \return + */ + static const QString &NameAdf1() + { + static QString n("ADF1"); + return n; + } - /*! - * \brief Toggle active and standby frequencies - */ - void toggleActiveStandby(); + /*! + * \brief ADF2 + * \return + */ + static const QString &NameAdf2() + { + static QString n("ADF2"); + return n; + } - /*! - * \brief Active frequency - * \return - */ - BlackMisc::PhysicalQuantities::CFrequency getFrequencyActive() const - { - return this->m_frequencyActive; - } + /*! + * \brief Frequency not set + * \return + */ + static const BlackMisc::PhysicalQuantities::CFrequency &FrequencyNotSet() + { + static BlackMisc::PhysicalQuantities::CFrequency f; + return f; + } - /*! - * \brief Standby frequency - * \return - */ - BlackMisc::PhysicalQuantities::CFrequency getFrequencyStandby() const - { - return this->m_frequencyActive; - } + /*! + * \brief Stream to DBus << + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; - /*! - * \brief Set active frequency - * \param frequency - */ - void setFrequencyActive(const BlackMisc::PhysicalQuantities::CFrequency &frequency) - { - this->m_frequencyActive = frequency; - } + /*! + * \brief Stream from DBus >> + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); - /*! - * \brief Set standby frequency - * \param frequency - */ - void setFrequencyStandby(const BlackMisc::PhysicalQuantities::CFrequency &frequency) - { - this->m_frequencyStandby = frequency; - } + public: + /*! + * \brief Virtual destructor + */ + virtual ~CModulator() {} - /*! - * \brief Register metadata - */ - static void registerMetadata(); -}; + /*! + * \brief Default value + * \return + */ + virtual bool isDefaultValue() const + { + return this->m_frequencyActive == CModulator::FrequencyNotSet(); + } -} // namespace + /*! + * \brief Toggle active and standby frequencies + */ + void toggleActiveStandby(); + + /*! + * \brief Active frequency + * \return + */ + BlackMisc::PhysicalQuantities::CFrequency getFrequencyActive() const + { + return this->m_frequencyActive; + } + + /*! + * \brief Standby frequency + * \return + */ + BlackMisc::PhysicalQuantities::CFrequency getFrequencyStandby() const + { + return this->m_frequencyActive; + } + + /*! + * \brief Set active frequency + * \param frequency + */ + void setFrequencyActive(const BlackMisc::PhysicalQuantities::CFrequency &frequency) + { + this->m_frequencyActive = frequency; + } + + /*! + * \brief Set standby frequency + * \param frequency + */ + void setFrequencyStandby(const BlackMisc::PhysicalQuantities::CFrequency &frequency) + { + this->m_frequencyStandby = frequency; + } + + /*! + * \brief Register metadata + */ + static void registerMetadata(); + }; + + } // namespace } // namespace #endif // guard diff --git a/src/blackmisc/avionavsystem.h b/src/blackmisc/avionavsystem.h index 188a42502..c3d126ea0 100644 --- a/src/blackmisc/avionavsystem.h +++ b/src/blackmisc/avionavsystem.h @@ -9,277 +9,278 @@ namespace BlackMisc { -namespace Aviation -{ - -/*! - * \brief NAV system (radio navigation) - */ -class CNavSystem : public CModulator -{ -private: - /*! - * \brief Valid civil aviation frequency? - * \param f - * \return - */ - bool isValidCivilNavigationFrequency(BlackMisc::PhysicalQuantities::CFrequency f) const + namespace Aviation { - double fr = f.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz(), this->m_digits); - return fr >= 108.0 && fr <= 117.95; - } - /*! - * \brief Valid military aviation frequency? - * \param f - * \return - */ - bool isValidMilitaryNavigationFrequency(BlackMisc::PhysicalQuantities::CFrequency f) const - { - double fr = f.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz(), this->m_digits); - return fr >= 960.0 && fr <= 1215.0; // valid TACAN frequency - } + /*! + * \brief NAV system (radio navigation) + */ + class CNavSystem : public CModulator + { + private: + /*! + * \brief Valid civil aviation frequency? + * \param f + * \return + */ + bool isValidCivilNavigationFrequency(BlackMisc::PhysicalQuantities::CFrequency f) const + { + double fr = f.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz(), this->m_digits); + return fr >= 108.0 && fr <= 117.95; + } - /*! - * \brief Constructor - * \param validate - * \param name - * \param activeFrequency - * \param standbyFrequency - * \param digits - */ - CNavSystem(bool validate, const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): - CModulator(name, activeFrequency, standbyFrequency, digits) - { - this->validate(validate); - } + /*! + * \brief Valid military aviation frequency? + * \param f + * \return + */ + bool isValidMilitaryNavigationFrequency(BlackMisc::PhysicalQuantities::CFrequency f) const + { + double fr = f.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz(), this->m_digits); + return fr >= 960.0 && fr <= 1215.0; // valid TACAN frequency + } -protected: - /*! - * \brief Are the set values valid / in range? - * \return - */ - bool validValues() const - { - if (this->isDefaultValue()) return true; // special case - bool v = - (this->isValidCivilNavigationFrequency(this->getFrequencyActive()) || - this->isValidMilitaryNavigationFrequency(this->getFrequencyActive())) && - (this->isValidCivilNavigationFrequency(this->getFrequencyStandby()) || - this->isValidMilitaryNavigationFrequency(this->getFrequencyStandby())); - return v; - } + /*! + * \brief Constructor + * \param validate + * \param name + * \param activeFrequency + * \param standbyFrequency + * \param digits + */ + CNavSystem(bool validate, const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): + CModulator(name, activeFrequency, standbyFrequency, digits) + { + this->validate(validate); + } - /*! - * \brief Validate values by assert and exception - * \param strict - * \throws std::range_error - * \remarks Cannot be virtual because used in constructor - * \return - */ - bool validate(bool strict = true) const - { - if (this->isDefaultValue()) return true; - bool valid = this->validValues(); - if (!strict) return valid; - Q_ASSERT_X(valid, "CModulator::validate", "illegal values"); - if (!valid) throw std::range_error("Illegal values in CModulator::validate"); - return true; - } + protected: + /*! + * \brief Are the set values valid / in range? + * \return + */ + bool validValues() const + { + if (this->isDefaultValue()) return true; // special case + bool v = + (this->isValidCivilNavigationFrequency(this->getFrequencyActive()) || + this->isValidMilitaryNavigationFrequency(this->getFrequencyActive())) && + (this->isValidCivilNavigationFrequency(this->getFrequencyStandby()) || + this->isValidMilitaryNavigationFrequency(this->getFrequencyStandby())); + return v; + } -public: - /*! - * Default constructor - */ - CNavSystem() : CModulator() {} + /*! + * \brief Validate values by assert and exception + * \param strict + * \throws std::range_error + * \remarks Cannot be virtual because used in constructor + * \return + */ + bool validate(bool strict = true) const + { + if (this->isDefaultValue()) return true; + bool valid = this->validValues(); + if (!strict) return valid; + Q_ASSERT_X(valid, "CModulator::validate", "illegal values"); + if (!valid) throw std::range_error("Illegal values in CModulator::validate"); + return true; + } - /*! - * \brief Copy constructor - * \param other - */ - CNavSystem(const CNavSystem &other) : CModulator(other) {} + public: + /*! + * Default constructor + */ + CNavSystem() : CModulator() {} - /*! - * \brief Constructor - * \param name - * \param activeFrequency - * \param standbyFrequency - * \param digits - */ - CNavSystem(const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): - CModulator(name, activeFrequency, standbyFrequency, digits) - { - this->validate(true); - } + /*! + * \brief Copy constructor + * \param other + */ + CNavSystem(const CNavSystem &other) : CModulator(other) {} - /*! - * \brief Set active frequency - * \param frequencyMHz - */ - void setFrequencyActiveMHz(double frequencyMHz) - { - this->CModulator::setFrequencyActiveMHz(frequencyMHz); - this->validate(true); - } + /*! + * \brief Constructor + * \param name + * \param activeFrequency + * \param standbyFrequency + * \param digits + */ + CNavSystem(const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): + CModulator(name, activeFrequency, standbyFrequency, digits) + { + this->validate(true); + } - /*! - * \brief Set standby frequency - * \param frequencyMHz - */ - void setFrequencyStandbyMHz(double frequencyMHz) - { - this->CModulator::setFrequencyStandbyMHz(frequencyMHz); - this->validate(true); - } - /*! - * \brief operator == - * \param other - * \return - */ - bool operator ==(const CNavSystem &other) const - { - return this->CModulator::operator ==(other); - } + /*! + * \brief Set active frequency + * \param frequencyMHz + */ + void setFrequencyActiveMHz(double frequencyMHz) + { + this->CModulator::setFrequencyActiveMHz(frequencyMHz); + this->validate(true); + } - /*! - * \brief operator == - * \param other - * \return - */ - bool operator !=(const CNavSystem &other) const - { - return this->CModulator::operator !=(other); - } + /*! + * \brief Set standby frequency + * \param frequencyMHz + */ + void setFrequencyStandbyMHz(double frequencyMHz) + { + this->CModulator::setFrequencyStandbyMHz(frequencyMHz); + this->validate(true); + } - /*! - * Try to get a NAV unit with given name and frequency. Returns true in case an object - * has been sucessfully created,otherwise returns a default object. - * \param[out] o_navSystem - * \param name - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static bool tryGetNavSystem(CNavSystem &o_navSystem, const QString &name, double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - o_navSystem = CNavSystem(false, name, BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); - bool s; - if (!(s = o_navSystem.validate(false))) o_navSystem = CNavSystem(); // reset to default - return s; - } + /*! + * \brief operator == + * \param other + * \return + */ + bool operator ==(const CNavSystem &other) const + { + return this->CModulator::operator ==(other); + } - /*! - * Try to get a NAV unit with given name and frequency. Returns true in case an object - * has been sucessfully created, otherwise returns a default object. - * \param[out] o_navSystem - * \param name - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetNavSystem(CNavSystem &o_navSystem, const QString &name, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - o_navSystem = CNavSystem(false, name, activeFrequency, standbyFrequency); - bool s; - if (!(s = o_navSystem.validate(false))) o_navSystem = CNavSystem(); // reset to default - return s; - } + /*! + * \brief operator == + * \param other + * \return + */ + bool operator !=(const CNavSystem &other) const + { + return this->CModulator::operator !=(other); + } - /*! - * \brief NAV1 unit - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static CNavSystem getNav1System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CNavSystem(CModulator::NameNav1(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); - } + /*! + * Try to get a NAV unit with given name and frequency. Returns true in case an object + * has been sucessfully created,otherwise returns a default object. + * \param[out] o_navSystem + * \param name + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static bool tryGetNavSystem(CNavSystem &o_navSystem, const QString &name, double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + o_navSystem = CNavSystem(false, name, BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); + bool s; + if (!(s = o_navSystem.validate(false))) o_navSystem = CNavSystem(); // reset to default + return s; + } - /*! - * \brief NAV1 unit - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static CNavSystem getNav1System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CNavSystem(CModulator::NameNav1(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); - } + /*! + * Try to get a NAV unit with given name and frequency. Returns true in case an object + * has been sucessfully created, otherwise returns a default object. + * \param[out] o_navSystem + * \param name + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetNavSystem(CNavSystem &o_navSystem, const QString &name, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + o_navSystem = CNavSystem(false, name, activeFrequency, standbyFrequency); + bool s; + if (!(s = o_navSystem.validate(false))) o_navSystem = CNavSystem(); // reset to default + return s; + } - /*! - * \brief Try to get NAV unit - * \param[out] o_navSystem - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static bool tryGetNav1System(CNavSystem &o_navSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CNavSystem::tryGetNavSystem(o_navSystem, CModulator::NameNav1(), activeFrequencyMHz, standbyFrequencyMHz); - } + /*! + * \brief NAV1 unit + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static CNavSystem getNav1System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CNavSystem(CModulator::NameNav1(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); + } - /*! - * \brief Try to get NAV unit - * \param[out] o_navSystem - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetNav1System(CNavSystem &o_navSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CNavSystem::tryGetNavSystem(o_navSystem, CModulator::NameNav1(), activeFrequency, standbyFrequency); - } + /*! + * \brief NAV1 unit + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static CNavSystem getNav1System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CNavSystem(CModulator::NameNav1(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); + } - /*! - * \brief NAV2 unit - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static CNavSystem getNav2System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CNavSystem(CModulator::NameNav2(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); - } + /*! + * \brief Try to get NAV unit + * \param[out] o_navSystem + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static bool tryGetNav1System(CNavSystem &o_navSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CNavSystem::tryGetNavSystem(o_navSystem, CModulator::NameNav1(), activeFrequencyMHz, standbyFrequencyMHz); + } - /*! - * \brief NAV2 unit - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static CNavSystem getNav2System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CNavSystem(CModulator::NameNav2(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); - } + /*! + * \brief Try to get NAV unit + * \param[out] o_navSystem + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetNav1System(CNavSystem &o_navSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CNavSystem::tryGetNavSystem(o_navSystem, CModulator::NameNav1(), activeFrequency, standbyFrequency); + } - /*! - * \brief Try to get NAV unit - * \param[out] o_navSystem - * \param activeFrequencyMHz - * \param standbyFrequencyMHz - * \return - */ - static bool tryGetNav2System(CNavSystem &o_navSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) - { - return CNavSystem::tryGetNavSystem(o_navSystem, CModulator::NameNav2(), activeFrequencyMHz, standbyFrequencyMHz); - } + /*! + * \brief NAV2 unit + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static CNavSystem getNav2System(double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CNavSystem(CModulator::NameNav2(), BlackMisc::PhysicalQuantities::CFrequency(activeFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()), BlackMisc::PhysicalQuantities::CFrequency(standbyFrequencyMHz < 0 ? activeFrequencyMHz : standbyFrequencyMHz, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz())); + } - /*! - * \brief Try to get NAV unit - * \param[out] o_navSystem - * \param activeFrequency - * \param standbyFrequency - * \return - */ - static bool tryGetNav2System(CNavSystem &o_navSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) - { - return CNavSystem::tryGetNavSystem(o_navSystem, CModulator::NameNav2(), activeFrequency, standbyFrequency); - } -}; + /*! + * \brief NAV2 unit + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static CNavSystem getNav2System(BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CNavSystem(CModulator::NameNav2(), activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency); + } -} // namespace + /*! + * \brief Try to get NAV unit + * \param[out] o_navSystem + * \param activeFrequencyMHz + * \param standbyFrequencyMHz + * \return + */ + static bool tryGetNav2System(CNavSystem &o_navSystem, double activeFrequencyMHz, double standbyFrequencyMHz = -1) + { + return CNavSystem::tryGetNavSystem(o_navSystem, CModulator::NameNav2(), activeFrequencyMHz, standbyFrequencyMHz); + } + + /*! + * \brief Try to get NAV unit + * \param[out] o_navSystem + * \param activeFrequency + * \param standbyFrequency + * \return + */ + static bool tryGetNav2System(CNavSystem &o_navSystem, BlackMisc::PhysicalQuantities::CFrequency activeFrequency, BlackMisc::PhysicalQuantities::CFrequency standbyFrequency = CModulator::FrequencyNotSet()) + { + return CNavSystem::tryGetNavSystem(o_navSystem, CModulator::NameNav2(), activeFrequency, standbyFrequency); + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Aviation::CNavSystem) diff --git a/src/blackmisc/aviotransponder.cpp b/src/blackmisc/aviotransponder.cpp index 676508e4a..446a31fd8 100644 --- a/src/blackmisc/aviotransponder.cpp +++ b/src/blackmisc/aviotransponder.cpp @@ -7,130 +7,143 @@ namespace BlackMisc { -namespace Aviation -{ + namespace Aviation + { -/** - * Valid values? - */ -bool CTransponder::validValues() const -{ - if (this->isDefaultValue()) return true; // special case - if (this->m_transponderCode < 0 || this->m_transponderCode > 7777) return false; + /* + * Valid values? + */ + bool CTransponder::validValues() const + { + if (this->isDefaultValue()) return true; // special case + if (this->m_transponderCode < 0 || this->m_transponderCode > 7777) return false; - // check each digit - qint32 tc = this->m_transponderCode; - qint32 d; - while (tc > 7) { - d = (tc % 10); - if (d > 7) return false; - tc /= 10; - } - return true; -} + // check each digit + qint32 tc = this->m_transponderCode; + qint32 d; + while (tc > 7) + { + d = (tc % 10); + if (d > 7) return false; + tc /= 10; + } + return true; + } -/** - * Validate - */ -bool CTransponder::validate(bool strict) const -{ - if (this->isDefaultValue()) return true; - bool valid = this->validValues(); - if (!strict) return valid; - Q_ASSERT_X(valid, "CTransponder::validate", "illegal values"); - if (!valid) throw std::range_error("Illegal values in CTransponder::validate"); - return true; -} + /* + * Validate + */ + bool CTransponder::validate(bool strict) const + { + if (this->isDefaultValue()) return true; + bool valid = this->validValues(); + if (!strict) return valid; + Q_ASSERT_X(valid, "CTransponder::validate", "illegal values"); + if (!valid) throw std::range_error("Illegal values in CTransponder::validate"); + return true; + } -/** - * String representation - */ -QString CTransponder::convertToQString(bool /* i18n */) const -{ - QString s = this->getName(); - s = s.append(" ").append(this->getTransponderCodeFormatted()).append(" ").append(this->getModeAsString()); - return s; -} + /* + * String representation + */ + QString CTransponder::convertToQString(bool /* i18n */) const + { + QString s = this->getName(); + s = s.append(" ").append(this->getTransponderCodeFormatted()).append(" ").append(this->getModeAsString()); + return s; + } -/** - * Mode as readable string - */ -QString CTransponder::getModeAsString() const -{ - QString m; - switch (this->m_transponderMode) { - case StateIdent: - m = "Ident"; - break; - case StateStandby: - m = "Standby"; - break; - case ModeC: - m = "Mode C"; - break; - case ModeMil1: - m = "Mil.Mode 1"; - break; - case ModeMil2: - m = "Mil.Mode 2"; - break; - case ModeMil3: - m = "Mil.Mode 3"; - break; - case ModeMil4: - m = "Mil.Mode 4"; - break; - case ModeMil5: - m = "Mil.Mode 5"; - break; - default: - throw std::range_error("Illegal Transponder Mode"); - } - return m; -} + /* + * Mode as readable string + */ + QString CTransponder::getModeAsString() const + { + QString m; + switch (this->m_transponderMode) + { + case StateIdent: + m = "Ident"; + break; + case StateStandby: + m = "Standby"; + break; + case ModeC: + m = "Mode C"; + break; + case ModeS: + m = "Mode S"; + break; + case ModeMil1: + m = "Mil.Mode 1"; + break; + case ModeMil2: + m = "Mil.Mode 2"; + break; + case ModeMil3: + m = "Mil.Mode 3"; + break; + case ModeMil4: + m = "Mil.Mode 4"; + break; + case ModeMil5: + m = "Mil.Mode 5"; + break; + default: + throw std::range_error("Illegal Transponder Mode"); + } + return m; + } -/** - * Formatted transponder code - */ -QString CTransponder::getTransponderCodeFormatted() const -{ - QString f("0000"); - f = f.append(QString::number(this->m_transponderCode)); - return f.right(4); -} + /* + * Formatted transponder code + */ + QString CTransponder::getTransponderCodeFormatted() const + { + QString f("0000"); + f = f.append(QString::number(this->m_transponderCode)); + return f.right(4); + } -/*! - * \brief Stream to DBus << - * \param argument - */ -void CTransponder::marshallToDbus(QDBusArgument &argument) const -{ - this->CAvionicsBase::marshallToDbus(argument); - argument << this->m_transponderCode; - argument << static_cast(this->m_transponderMode); -} + /* + * Formatted transponder code + mode + */ + QString CTransponder::getTransponderCodeAndModeFormatted() const + { + QString s = this->getTransponderCodeFormatted(); + s.append(' ').append(this->getModeAsString()); + return s; + } -/*! - * \brief Stream from DBus >> - * \param argument - */ -void CTransponder::unmarshallFromDbus(const QDBusArgument &argument) -{ - this->CAvionicsBase::unmarshallFromDbus(argument); - qint32 tm; - argument >> this->m_transponderCode; - argument >> tm; - this->m_transponderMode = static_cast(tm); -} + /* + * Stream to DBus << + */ + void CTransponder::marshallToDbus(QDBusArgument &argument) const + { + this->CAvionicsBase::marshallToDbus(argument); + argument << this->m_transponderCode; + argument << static_cast(this->m_transponderMode); + } -/*! - * \brief Register metadata of unit and quantity - */ -void CTransponder::registerMetadata() -{ - qRegisterMetaType(typeid(CTransponder).name()); - qDBusRegisterMetaType(); -} + /*! + * Stream from DBus >> + */ + void CTransponder::unmarshallFromDbus(const QDBusArgument &argument) + { + this->CAvionicsBase::unmarshallFromDbus(argument); + qint32 tm; + argument >> this->m_transponderCode; + argument >> tm; + this->m_transponderMode = static_cast(tm); + } -} // namespace + /* + * Register metadata of unit and quantity + */ + void CTransponder::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + } // namespace } // namespace diff --git a/src/blackmisc/aviotransponder.h b/src/blackmisc/aviotransponder.h index 3d09cb1cc..86c4a7fad 100644 --- a/src/blackmisc/aviotransponder.h +++ b/src/blackmisc/aviotransponder.h @@ -10,320 +10,326 @@ namespace BlackMisc { -namespace Aviation -{ - -/*! - * \brief Transponder - */ -class CTransponder : public CAvionicsBase -{ -public: - /*! - * \brief Our transponder codes - */ - enum TransponderMode + namespace Aviation { - StateStandby = 0, // not a real mode, more a state - ModeMil1 = 1, ModeMil2 = 2, ModeMil3 = 3, ModeMil4 = 4, ModeMil5 = 5, - StateIdent = 10, // not a real mode, more a state - ModeA = 11, - ModeC = 12, - ModeS = 20 - }; + /*! + * \brief Transponder + */ + class CTransponder : public CAvionicsBase + { + public: + /*! + * \brief Our transponder codes + */ + enum TransponderMode + { + StateStandby = 0, // not a real mode, more a state + ModeMil1 = 1, ModeMil2 = 2, ModeMil3 = 3, ModeMil4 = 4, ModeMil5 = 5, + StateIdent = 10, // not a real mode, more a state + ModeA = 11, + ModeC = 12, + ModeS = 20 + }; -private: - qint32 m_transponderCode; //validate(validate); - } + /*! + * \brief Constructor for validation + * \param validate + * \param name + * \param transponderCode + * \param transponderMode + */ + CTransponder(bool validate, const QString &name, qint32 transponderCode, TransponderMode transponderMode) : + CAvionicsBase(name), m_transponderCode(transponderCode), m_transponderMode(transponderMode) + { + this->validate(validate); + } - /*! - * \brief Constructor for validation - * \param validate - * \param name - * \param transponderCode - * \param transponderMode - */ - CTransponder(bool validate, const QString &name, const QString transponderCode, TransponderMode transponderMode) : - CAvionicsBase(name), m_transponderCode(0), m_transponderMode(transponderMode) - { - bool ok = false; - this->m_transponderCode = transponderCode.toUInt(&ok); - if (!ok) this->m_transponderCode = -1; // will cause assert / exception - this->validate(validate); - } + /*! + * \brief Constructor for validation + * \param validate + * \param name + * \param transponderCode + * \param transponderMode + */ + CTransponder(bool validate, const QString &name, const QString transponderCode, TransponderMode transponderMode) : + CAvionicsBase(name), m_transponderCode(0), m_transponderMode(transponderMode) + { + bool ok = false; + this->m_transponderCode = transponderCode.toUInt(&ok); + if (!ok) this->m_transponderCode = -1; // will cause assert / exception + this->validate(validate); + } -protected: - /*! - * \brief Are the set values valid / in range? - * \return - */ - bool validValues() const; + protected: + /*! + * \brief Are the set values valid / in range? + * \return + */ + bool validValues() const; - /*! - * \brief Default value - * \return - */ - virtual bool isDefaultValue() const - { - return this->m_transponderCode == 0; - } + /*! + * \brief Default value + * \return + */ + virtual bool isDefaultValue() const + { + return this->m_transponderCode == 0; + } - /*! - * \brief Validate values by assert and exception - * \param strict - * \throws std::range_error - * \remarks Cannot be virtualsince already used in constructor - * \return - */ - bool validate(bool strict = true) const; + /*! + * \brief Validate values by assert and exception + * \param strict + * \throws std::range_error + * \remarks Cannot be virtualsince already used in constructor + * \return + */ + bool validate(bool strict = true) const; - /*! - * \brief Meaningful string representation - * \param i18n - * \return - */ - virtual QString convertToQString(bool i18n = false) const; + /*! + * \brief Meaningful string representation + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n = false) const; - /*! - * \brief Stream to DBus << - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const; + /*! + * \brief Stream to DBus << + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; - /*! - * \brief Stream from DBus >> - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument); + /*! + * \brief Stream from DBus >> + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); -public: - /*! - * Default constructor - */ - CTransponder() : CAvionicsBase("default"), m_transponderCode(0), m_transponderMode(StateStandby) {} + public: + /*! + * Default constructor + */ + CTransponder() : CAvionicsBase("transponder"), m_transponderCode(0), m_transponderMode(ModeS) {} - /*! - * \brief Copy constructor - * \param other - */ - CTransponder(const CTransponder &other) : CAvionicsBase(other.getName()), - m_transponderCode(other.m_transponderCode), m_transponderMode(other.m_transponderMode) {} + /*! + * \brief Copy constructor + * \param other + */ + CTransponder(const CTransponder &other) : CAvionicsBase(other.getName()), + m_transponderCode(other.m_transponderCode), m_transponderMode(other.m_transponderMode) {} - /*! - * \brief Constructor - * \param name - * \param transponderCode - * \param transponderMode - */ - CTransponder(const QString &name, qint32 transponderCode, TransponderMode transponderMode) : - CAvionicsBase(name), m_transponderCode(transponderCode), m_transponderMode(transponderMode) - { - this->validate(true); - } + /*! + * \brief Constructor + * \param name + * \param transponderCode + * \param transponderMode + */ + CTransponder(const QString &name, qint32 transponderCode, TransponderMode transponderMode) : + CAvionicsBase(name), m_transponderCode(transponderCode), m_transponderMode(transponderMode) + { + this->validate(true); + } - /*! - * \brief Constructor - * \param name - * \param transponderCode - * \param transponderMode - */ - CTransponder(const QString &name, const QString &transponderCode, TransponderMode transponderMode) : - CAvionicsBase(name), m_transponderCode(0), m_transponderMode(transponderMode) - { - bool ok = false; - this->m_transponderCode = transponderCode.toUInt(&ok); - if (!ok) this->m_transponderCode = -1; // will cause assert / exception - this->validate(true); - } + /*! + * \brief Constructor + * \param name + * \param transponderCode + * \param transponderMode + */ + CTransponder(const QString &name, const QString &transponderCode, TransponderMode transponderMode) : + CAvionicsBase(name), m_transponderCode(0), m_transponderMode(transponderMode) + { + bool ok = false; + this->m_transponderCode = transponderCode.toUInt(&ok); + if (!ok) this->m_transponderCode = -1; // will cause assert / exception + this->validate(true); + } - /*! - * \brief Transponder mode as string - * \return - * \throws std::range_erros - */ - QString getModeAsString() const; + /*! + * \brief Transponder mode as string + * \return + * \throws std::range_erros + */ + QString getModeAsString() const; - /*! - * \brief Transponder mode - * \return - */ - TransponderMode getTransponderMode() const - { - return this->m_transponderMode; - } + /*! + * \brief Transponder mode + * \return + */ + TransponderMode getTransponderMode() const + { + return this->m_transponderMode; + } - /*! - * \brief Transponder code - * \return - */ - qint32 getTransponderCode() const - { - return this->m_transponderCode; - } + /*! + * \brief Transponder code + * \return + */ + qint32 getTransponderCode() const + { + return this->m_transponderCode; + } - /*! - * \brief Transponder code - * \return - */ - QString getTransponderCodeFormatted() const; + /*! + * \brief Transponder code + * \return + */ + QString getTransponderCodeFormatted() const; - /*! - * \brief Set transponder code - * \param transponderCode - */ - void setTransponderCode(qint32 transponderCode) - { - this->m_transponderCode = transponderCode; - this->validate(true); - } + /*! + * \brief Formatted with code / mode + * \return + */ + QString getTransponderCodeAndModeFormatted() const; - /*! - * \brief Set transponder mode - * \param mode - */ - void setTransponderMode(TransponderMode mode) - { - this->m_transponderMode = mode ; - this->validate(true); - } - /*! - * \brief Set emergency - */ - void setEmergency() - { - this->m_transponderCode = 7700; - } + /*! + * \brief Set transponder code + * \param transponderCode + */ + void setTransponderCode(qint32 transponderCode) + { + this->m_transponderCode = transponderCode; + this->validate(true); + } - /*! - * \brief Set VFR - */ - void setVFR() - { - this->m_transponderCode = 7000; - } + /*! + * \brief Set transponder mode + * \param mode + */ + void setTransponderMode(TransponderMode mode) + { + this->m_transponderMode = mode ; + this->validate(true); + } - /*! - * \brief Set IFR - */ - void setIFR() - { - this->m_transponderCode = 2000; - } + /*! + * \brief Set emergency + */ + void setEmergency() + { + this->m_transponderCode = 7700; + } - /*! - * \brief operator == - * \param other - * \return - */ - bool operator ==(const CTransponder &other) const - { - return - this->m_transponderCode == other.m_transponderCode && - this->m_transponderMode == other.m_transponderMode && - this->CAvionicsBase::operator ==(other); - } + /*! + * \brief Set VFR + */ + void setVFR() + { + this->m_transponderCode = 7000; + } - /*! - * \brief operator =! - * \param other - * \return - */ - bool operator !=(const CTransponder &other) const - { - return !((*this) == other); - } + /*! + * \brief Set IFR + */ + void setIFR() + { + this->m_transponderCode = 2000; + } - /*! - * Try to get a Transponder unit with given name and code. Returns true in case an object - * has been sucessfully created, otherwise returns a default object. - * \param[out] o_transponder - * \param name - * \param transponderCode - * \param mode - * \return - */ - static bool tryGetTransponder(CTransponder &o_transponder, const QString &name, qint32 transponderCode, TransponderMode mode) - { - o_transponder = CTransponder(false, name, transponderCode, mode); - bool s; - if (!(s = o_transponder.validate(false))) o_transponder = CTransponder(); // reset to default - return s; - } + /*! + * \brief operator == + * \param other + * \return + */ + bool operator ==(const CTransponder &other) const + { + return + this->m_transponderCode == other.m_transponderCode && + this->m_transponderMode == other.m_transponderMode && + this->CAvionicsBase::operator ==(other); + } - /*! - * Try to get a Transponder unit with given name and code. Returns true in case an object - * has been sucessfully created, otherwise returns a default object. - * \param[out] o_transponder - * \param name - * \param transponderCode - * \param mode - * \return - */ - static bool tryGetTransponder(CTransponder &o_transponder, const QString &name, const QString &transponderCode, TransponderMode mode) - { - o_transponder = CTransponder(false, name, transponderCode, mode); - bool s; - if (!(s = o_transponder.validate(false))) o_transponder = CTransponder(); // reset to default - return s; - } + /*! + * \brief operator =! + * \param other + * \return + */ + bool operator !=(const CTransponder &other) const + { + return !((*this) == other); + } - /*! - * \brief Transponder unit - * \param transponderCode - * \param mode - * \return - */ - static CTransponder getStandardTransponder(qint32 transponderCode, TransponderMode mode) - { - return CTransponder("Transponder", transponderCode, mode); - } + /*! + * Try to get a Transponder unit with given name and code. Returns true in case an object + * has been sucessfully created, otherwise returns a default object. + * \param[out] o_transponder + * \param name + * \param transponderCode + * \param mode + * \return + */ + static bool tryGetTransponder(CTransponder &o_transponder, const QString &name, qint32 transponderCode, TransponderMode mode) + { + o_transponder = CTransponder(false, name, transponderCode, mode); + bool s; + if (!(s = o_transponder.validate(false))) o_transponder = CTransponder(); // reset to default + return s; + } - /*! - * \brief Try to get Transponder unit - * \param[out] o_transponder - * \param transponderCode - * \param mode - * \return - */ - static bool tryGetStandardTransponder(CTransponder &o_transponder, qint32 transponderCode, TransponderMode mode) - { - return CTransponder::tryGetTransponder(o_transponder, "Transponder", transponderCode, mode); - } + /*! + * Try to get a Transponder unit with given name and code. Returns true in case an object + * has been sucessfully created, otherwise returns a default object. + * \param[out] o_transponder + * \param name + * \param transponderCode + * \param mode + * \return + */ + static bool tryGetTransponder(CTransponder &o_transponder, const QString &name, const QString &transponderCode, TransponderMode mode) + { + o_transponder = CTransponder(false, name, transponderCode, mode); + bool s; + if (!(s = o_transponder.validate(false))) o_transponder = CTransponder(); // reset to default + return s; + } - /*! - * \brief Try to get Transponder unit - * \param[out] o_transponder - * \param transponderCode - * \param mode - * \return - */ - static bool tryGetStandardTransponder(CTransponder &o_transponder, const QString &transponderCode, TransponderMode mode) - { - return CTransponder::tryGetTransponder(o_transponder, "Transponder", transponderCode, mode); - } + /*! + * \brief Transponder unit + * \param transponderCode + * \param mode + * \return + */ + static CTransponder getStandardTransponder(qint32 transponderCode, TransponderMode mode) + { + return CTransponder("Transponder", transponderCode, mode); + } - /*! - * \brief Register metadata of unit and quantity - */ - static void registerMetadata(); + /*! + * \brief Try to get Transponder unit + * \param[out] o_transponder + * \param transponderCode + * \param mode + * \return + */ + static bool tryGetStandardTransponder(CTransponder &o_transponder, qint32 transponderCode, TransponderMode mode) + { + return CTransponder::tryGetTransponder(o_transponder, "Transponder", transponderCode, mode); + } -}; + /*! + * \brief Try to get Transponder unit + * \param[out] o_transponder + * \param transponderCode + * \param mode + * \return + */ + static bool tryGetStandardTransponder(CTransponder &o_transponder, const QString &transponderCode, TransponderMode mode) + { + return CTransponder::tryGetTransponder(o_transponder, "Transponder", transponderCode, mode); + } -} // namespace + /*! + * \brief Register metadata of unit and quantity + */ + static void registerMetadata(); + + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Aviation::CTransponder) diff --git a/src/blackmisc/avtrack.cpp b/src/blackmisc/avtrack.cpp index 0569216f1..4a9eaacf5 100644 --- a/src/blackmisc/avtrack.cpp +++ b/src/blackmisc/avtrack.cpp @@ -10,42 +10,70 @@ using BlackMisc::PhysicalQuantities::CAngleUnit; namespace BlackMisc { -namespace Aviation -{ + namespace Aviation + { + /* + * Own implementation for streaming + */ + QString CTrack::convertToQString(bool i18n) const + { + QString s = CAngle::convertToQString(i18n).append(" "); + if (i18n) + { + return s.append(this->isMagneticTrack() ? + QCoreApplication::translate("Aviation", "magnetic") : + QCoreApplication::translate("Aviation", "true")); + } + else + { + return s.append(this->isMagneticTrack() ? "magnetic" : "true"); + } + } -/* - * Own implementation for streaming - */ -QString CTrack::convertToQString(bool i18n) const -{ - QString s = CAngle::convertToQString(i18n); - return s.append(this->isMagneticTrack() ? " magnetic" : " true"); -} + /* + * Marshall to DBus + */ + void CTrack::marshallToDbus(QDBusArgument &argument) const + { + this->CAngle::marshallToDbus(argument); + argument << qint32(this->m_north); + } -/* - * Equal? - */ -bool CTrack::operator ==(const CTrack &other) -{ - return other.m_north == this->m_north && this->CAngle::operator ==(other); -} + /* + * Unmarshall from DBus + */ + void CTrack::unmarshallFromDbus(const QDBusArgument &argument) + { + this->CAngle::unmarshallFromDbus(argument); + qint32 north; + argument >> north; + this->m_north = static_cast(north); + } -/* - * Unequal? - */ -bool CTrack::operator !=(const CTrack &other) -{ - return !((*this) == other); -} + /* + * Equal? + */ + bool CTrack::operator ==(const CTrack &other) const + { + return other.m_north == this->m_north && this->CAngle::operator ==(other); + } -/*! - * \brief Register metadata of unit and quantity - */ -void CTrack::registerMetadata() -{ - qRegisterMetaType(typeid(CTrack).name()); - qDBusRegisterMetaType(); -} + /* + * Unequal? + */ + bool CTrack::operator !=(const CTrack &other) const + { + return !((*this) == other); + } -} // namespace + /*! + * \brief Register metadata of unit and quantity + */ + void CTrack::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + } // namespace } // namespace diff --git a/src/blackmisc/avtrack.h b/src/blackmisc/avtrack.h index 3600f71ba..758a5e182 100644 --- a/src/blackmisc/avtrack.h +++ b/src/blackmisc/avtrack.h @@ -9,127 +9,118 @@ namespace BlackMisc { -namespace Aviation -{ - -/*! - * \brief Track as used in aviation, can be true or magnetic Track - * \remarks Intentionally allowing +/- BlackMisc::PhysicalQuantities::CAngle , and >= / <= CAngle. - */ -class CTrack : public BlackMisc::PhysicalQuantities::CAngle -{ -public: - /*! - * Enum type to distinguish between true north and magnetic north - */ - enum ReferenceNorth + namespace Aviation { - Magnetic = 0, //!< magnetic north - True = 1, //!< true north - }; -private: - ReferenceNorth m_north; //!< magnetic or true? + /*! + * \brief Track as used in aviation, can be true or magnetic Track + * \remarks Intentionally allowing +/- BlackMisc::PhysicalQuantities::CAngle , + * and >= / <= CAngle. + */ + class CTrack : public BlackMisc::PhysicalQuantities::CAngle + { + public: + /*! + * Enum type to distinguish between true north and magnetic north + */ + enum ReferenceNorth + { + Magnetic = 0, //!< magnetic north + True = 1 //!< true north + }; -protected: - /*! - * \brief Specific stream operation for Track - * \return - */ - virtual QString convertToQString(bool i18n = false) const; + private: + ReferenceNorth m_north; //!< magnetic or true? - /*! - * \brief Stream to DBus << - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - this->CAngle::marshallToDbus(argument); - argument << qint32(this->m_north); - } + protected: + /*! + * \brief Specific stream operation for Track + * \return + */ + virtual QString convertToQString(bool i18n = false) const; - /*! - * \brief Stream from DBus >> - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - this->CAngle::unmarshallFromDbus(argument); - qint32 north; - argument >> north; - this->m_north = static_cast(north); - } + /*! + * \brief Stream to DBus << + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; -public: - /*! - * \brief Default constructor: 0 Track magnetic - */ - CTrack() : BlackMisc::PhysicalQuantities::CAngle(0, BlackMisc::PhysicalQuantities::CAngleUnit::rad()), m_north(Magnetic) {} + /*! + * \brief Stream from DBus >> + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); - /*! - * \brief Constructor - * \param value - * \param north - * \param unit - */ - CTrack(double value, ReferenceNorth north, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : BlackMisc::PhysicalQuantities::CAngle(value, unit), m_north(north) {} + public: + /*! + * \brief Default constructor: 0 Track magnetic + */ + CTrack() : BlackMisc::PhysicalQuantities::CAngle(0, BlackMisc::PhysicalQuantities::CAngleUnit::rad()), m_north(Magnetic) {} - /*! - * \brief Constructor by CAngle - * \param track - * \param north - */ - CTrack(BlackMisc::PhysicalQuantities::CAngle track, ReferenceNorth north) : BlackMisc::PhysicalQuantities::CAngle(track), m_north(north) {} + /*! + * \brief Constructor + * \param value + * \param north + * \param unit + */ + CTrack(double value, ReferenceNorth north, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : BlackMisc::PhysicalQuantities::CAngle(value, unit), m_north(north) {} - /*! - * \brief Equal operator == - * \param other - * \return - */ - bool operator ==(const CTrack &other); + /*! + * \brief Constructor by CAngle + * \param track + * \param north + */ + CTrack(BlackMisc::PhysicalQuantities::CAngle track, ReferenceNorth north) : BlackMisc::PhysicalQuantities::CAngle(track), m_north(north) {} - /*! - * \brief Unequal operator == - * \param other - * \return - */ - bool operator !=(const CTrack &other); + /*! + * \brief Equal operator == + * \param other + * \return + */ + bool operator ==(const CTrack &other) const; - /*! - * \brief Magnetic Track? - * \return - */ - bool isMagneticTrack() const - { - return Magnetic == this->m_north; - (void)QT_TRANSLATE_NOOP("Aviation", "magnetic"); - } + /*! + * \brief Unequal operator == + * \param other + * \return + */ + bool operator !=(const CTrack &other) const; - /*! - * \brief True Track? - * \return - */ - bool isTrueTrack() const - { - return True == this->m_north; - (void)QT_TRANSLATE_NOOP("Aviation", "true"); - } + /*! + * \brief Magnetic Track? + * \return + */ + bool isMagneticTrack() const + { + return Magnetic == this->m_north; + (void)QT_TRANSLATE_NOOP("Aviation", "magnetic"); + } - /*! - * \brief Get reference north (magnetic or true) - * \return - */ - ReferenceNorth getReferenceNorth() const { return m_north; } + /*! + * \brief True Track? + * \return + */ + bool isTrueTrack() const + { + return True == this->m_north; + (void)QT_TRANSLATE_NOOP("Aviation", "true"); + } - /*! - * \brief Register metadata - */ - static void registerMetadata(); -}; + /*! + * \brief Get reference north (magnetic or true) + * \return + */ + ReferenceNorth getReferenceNorth() const { return m_north; } -} // namespace + /*! + * \brief Register metadata + */ + static void registerMetadata(); + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Aviation::CTrack) -#endif // BLACKMISC_AVTRACK_H +#endif // guard diff --git a/src/blackmisc/blackmisc.pro b/src/blackmisc/blackmisc.pro index 071684d66..5b609124e 100644 --- a/src/blackmisc/blackmisc.pro +++ b/src/blackmisc/blackmisc.pro @@ -14,7 +14,7 @@ precompile_header:!isEmpty(PRECOMPILED_HEADER) { DEFINES += LOG_IN_FILE RESOURCES += blackmisc.qrc -# lupdate +# lupdate (from cmd, e.g. lupdate blackmisc.pro) CODECFORTR = UTF-8 TRANSLATIONS += translations/blackmisc_i18n_de.ts \ translations/blackmisc_i18n_fr.ts \ @@ -23,5 +23,4 @@ TRANSLATIONS += translations/blackmisc_i18n_de.ts \ HEADERS += *.h SOURCES += *.cpp DESTDIR = ../../lib -OTHER_FILES += $$TRANSLATIONS \ - readme.txt +OTHER_FILES += $$TRANSLATIONS readme.txt diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index 70020caa3..279e429c2 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -4,6 +4,10 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "blackmiscfreefunctions.h" +#include "avallclasses.h" +#include "pqallquantities.h" +#include "mathallclasses.h" +#include "geoallclasses.h" /* * Metadata for PQs diff --git a/src/blackmisc/blackmiscfreefunctions.h b/src/blackmisc/blackmiscfreefunctions.h index 4dce58ddd..2c8b91b48 100644 --- a/src/blackmisc/blackmiscfreefunctions.h +++ b/src/blackmisc/blackmiscfreefunctions.h @@ -5,75 +5,80 @@ #ifndef BLACKMISC_FREEFUNCTIONS_H #define BLACKMISC_FREEFUNCTIONS_H -#include "avallclasses.h" -#include "pqallquantities.h" -#include "mathallclasses.h" -#include "geoallclasses.h" + +#include // for Q_INIT_RESOURCE /*! - * \brief Workaround, to call initResource from namespace + * Workaround, to call initResource from namespace. Used in BlackMisc::initResources(). + * Q_INIT_RESOURCE adds resource, here the translation files. */ -// cannot be declare within namespace, see docu +// cannot be declared within namespace, see docu +// hence BlackMisc::initResources() calls this inline function inline void initBlackMiscResources() { Q_INIT_RESOURCE(blackmisc); } /*! * Free functions in BlackMisc */ -namespace BlackMisc { +namespace BlackMisc +{ + /*! + * Free functions in PQs + */ + namespace PhysicalQuantities + { -/*! - * Free functions in PQ - */ -namespace PhysicalQuantities { + /*! + * \brief Register all metadata for PQs + */ + void registerMetadata(); -/*! - * \brief Register all metadata for PQs - */ -void registerMetadata(); + } // PQ -} // PQ + /*! + * Free functions in aviation + */ + namespace Aviation + { -/*! - * Free functions in aviation - */ -namespace Aviation { + /*! + * \brief Register metadata for aviation + */ + void registerMetadata(); -/*! - * \brief Register metadata for aviation - */ -void registerMetadata(); + } // Aviation -} // Aviation + /*! + * Free functions in Math + */ + namespace Math + { + /*! + * \brief Register metadata for math (matrices, vectors) + */ + void registerMetadata(); -namespace Math { + } // Math -/*! - * \brief Register metadata for math (matrices, vectors) - */ -void registerMetadata(); + namespace Geo + { -} // Math + /*! + * \brief Register metadata for geo (coordinates) + */ + void registerMetadata(); -namespace Geo { + } // Geo -/*! - * \brief Register metadata for geo (coordinates) - */ -void registerMetadata(); - -} // Geo - -/*! - * \brief Register all relevant metadata in BlackMisc - */ -void registerMetadata(); - -/*! - * \brief Init resources - */ -void initResources(); + /*! + * \brief Register all relevant metadata in BlackMisc + */ + void registerMetadata(); + /*! + * \brief Init resources + */ + void initResources(); } // BlackMisc -#endif // BLACKMISC_FREEFUNCTIONS_H +#endif // guard diff --git a/src/blackmisc/coordinateecef.h b/src/blackmisc/coordinateecef.h index e660b72ef..01c91b786 100644 --- a/src/blackmisc/coordinateecef.h +++ b/src/blackmisc/coordinateecef.h @@ -9,113 +9,113 @@ namespace BlackMisc { -namespace Geo -{ - -/*! - * \brief Earth centered, earth fixed position - */ -class CCoordinateEcef : public BlackMisc::Math::CVector3DBase -{ -public: - /*! - * \brief Default constructor - */ - CCoordinateEcef() : CVector3DBase() {} - - /*! - * \brief Constructor by values - * \param x - * \param y - * \param z - */ - CCoordinateEcef(double x, double y, double z) : CVector3DBase(x, y, z) {} - - /*! - * \brief Constructor by math vector - * \param vector - */ - explicit CCoordinateEcef(const BlackMisc::Math::CVector3D vector) : CVector3DBase(vector.i(), vector.j(), vector.k()) {} - - /*! - * \brief x - * \return - */ - double x() const + namespace Geo { - return this->m_i; - } - /*! - * \brief y - * \return - */ - double y() const - { - return this->m_j; - } + /*! + * \brief Earth centered, earth fixed position + */ + class CCoordinateEcef : public BlackMisc::Math::CVector3DBase + { + public: + /*! + * \brief Default constructor + */ + CCoordinateEcef() : CVector3DBase() {} - /*! - * \brief z - * \return - */ - double z() const - { - return this->m_k; - } + /*! + * \brief Constructor by values + * \param x + * \param y + * \param z + */ + CCoordinateEcef(double x, double y, double z) : CVector3DBase(x, y, z) {} - /*! - * \brief Set x - * \param x - */ - void setX(double x) - { - this->m_i = x; - } + /*! + * \brief Constructor by math vector + * \param vector + */ + explicit CCoordinateEcef(const BlackMisc::Math::CVector3D vector) : CVector3DBase(vector.i(), vector.j(), vector.k()) {} - /*! - * \brief Set y - * \param y - */ - void setY(double y) - { - this->m_j = y; - } + /*! + * \brief x + * \return + */ + double x() const + { + return this->m_i; + } - /*! - * \brief Set z - * \param z - */ - void setZ(double z) - { - this->m_k = z; - } + /*! + * \brief y + * \return + */ + double y() const + { + return this->m_j; + } - /*! - * \brief Concrete implementation of a 3D vector - * \return - */ - BlackMisc::Math::CVector3D toMathVector() const - { - return BlackMisc::Math::CVector3D(this->x(), this->y(), this->z()); - } + /*! + * \brief z + * \return + */ + double z() const + { + return this->m_k; + } -protected: - /*! - * \brief String for converter - * \return - */ - virtual QString convertToQString() const - { - QString s = "ECEF: {x %1, y %2, z %3}"; - s = s.arg(QString::number(this->x(), 'f', 6)). - arg(QString::number(this->y(), 'f', 6)). - arg(QString::number(this->z(), 'f', 6)); - return s; - } -}; + /*! + * \brief Set x + * \param x + */ + void setX(double x) + { + this->m_i = x; + } -} // namespace + /*! + * \brief Set y + * \param y + */ + void setY(double y) + { + this->m_j = y; + } + + /*! + * \brief Set z + * \param z + */ + void setZ(double z) + { + this->m_k = z; + } + + /*! + * \brief Concrete implementation of a 3D vector + * \return + */ + BlackMisc::Math::CVector3D toMathVector() const + { + return BlackMisc::Math::CVector3D(this->x(), this->y(), this->z()); + } + + protected: + /*! + * \brief String for converter + * \return + */ + virtual QString convertToQString() const + { + QString s = "ECEF: {x %1, y %2, z %3}"; + s = s.arg(QString::number(this->x(), 'f', 6)). + arg(QString::number(this->y(), 'f', 6)). + arg(QString::number(this->z(), 'f', 6)); + return s; + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Geo::CCoordinateEcef) diff --git a/src/blackmisc/coordinategeodetic.cpp b/src/blackmisc/coordinategeodetic.cpp index b1d675b33..d72217b2a 100644 --- a/src/blackmisc/coordinategeodetic.cpp +++ b/src/blackmisc/coordinategeodetic.cpp @@ -7,46 +7,66 @@ namespace BlackMisc { -namespace Geo -{ + namespace Geo + { -/* - * String for converter - */ -QString CCoordinateGeodetic::convertToQString(bool i18n) const -{ - QString s = "Geodetic: {%1, %2, %3}"; - return s.arg(this->m_latitude.valueRoundedWithUnit(6, i18n)).arg(this->m_longitude.valueRoundedWithUnit(6, i18n)).arg(this->m_height.valueRoundedWithUnit(i18n)); -} + /* + * String for converter + */ + QString CCoordinateGeodetic::convertToQString(bool i18n) const + { + QString s = "Geodetic: {%1, %2, %3}"; + return s.arg(this->m_latitude.valueRoundedWithUnit(6, i18n)).arg(this->m_longitude.valueRoundedWithUnit(6, i18n)).arg(this->m_height.valueRoundedWithUnit(i18n)); + } -/* - * Marshall to Dbus - */ -void CCoordinateGeodetic::marshallToDbus(QDBusArgument &argument) const -{ - argument << this->m_latitude; - argument << this->m_longitude; - argument << this->m_height; -} + /* + * Marshall to Dbus + */ + void CCoordinateGeodetic::marshallToDbus(QDBusArgument &argument) const + { + argument << this->m_latitude; + argument << this->m_longitude; + argument << this->m_height; + } -/* - * Unmarshall from Dbus - */ -void CCoordinateGeodetic::unmarshallFromDbus(const QDBusArgument &argument) -{ - argument >> this->m_latitude; - argument >> this->m_longitude; - argument >> this->m_height; -} + /* + * Unmarshall from Dbus + */ + void CCoordinateGeodetic::unmarshallFromDbus(const QDBusArgument &argument) + { + argument >> this->m_latitude; + argument >> this->m_longitude; + argument >> this->m_height; + } -/* - * Register metadata - */ -void CCoordinateGeodetic::registerMetadata() -{ - qRegisterMetaType(typeid(CCoordinateGeodetic).name()); - qDBusRegisterMetaType(); -} + /* + * Same coordinate + */ + bool CCoordinateGeodetic::operator ==(const CCoordinateGeodetic &other) const + { + if (this == &other) return true; + return this->m_height == other.m_height && + this->m_latitude == other.m_latitude && + this->m_longitude == other.m_longitude; + } -} // namespace + /* + * Unequal? + */ + bool CCoordinateGeodetic::operator !=(const CCoordinateGeodetic &other) const + { + return !((*this) == other); + } + + /* + * Register metadata + */ + void CCoordinateGeodetic::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + + } // namespace } // namespace diff --git a/src/blackmisc/coordinategeodetic.h b/src/blackmisc/coordinategeodetic.h index 864acff1c..fa473b0b3 100644 --- a/src/blackmisc/coordinategeodetic.h +++ b/src/blackmisc/coordinategeodetic.h @@ -12,195 +12,202 @@ namespace BlackMisc { -namespace Geo -{ - -/*! - * Latitude and longitude interface - * \brief Interface for geodetic ccordinates - */ -class ICoordinateGeodetic -{ - /*! - * \brief Latitude - * \return - */ - virtual const CLatitude &latitude() const = 0; - - /*! - * \brief Longitude - * \return - */ - virtual const CLongitude &longitude() const = 0; -}; - -/*! - * \brief Geodetic coordinate - */ -class CCoordinateGeodetic : public CStreamable, public ICoordinateGeodetic -{ -private: - BlackMisc::Geo::CLatitude m_latitude; //!< Latitude - BlackMisc::Geo::CLongitude m_longitude; //!< Longitude - BlackMisc::PhysicalQuantities::CLength m_height; //!< height - -protected: - /*! - * \brief String for converter - * \param i18n - * \return - */ - virtual QString convertToQString(bool i18n = false) const; - - /*! - * \brief Stream to DBus - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const; - - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument); - -public: - /*! - * \brief Default constructor - */ - CCoordinateGeodetic() : m_latitude(), m_longitude(), m_height() {} - - /*! - * \brief Copy constructor - */ - CCoordinateGeodetic(const CCoordinateGeodetic &geoCoordinate) : - m_latitude(geoCoordinate.m_latitude), m_longitude(geoCoordinate.m_longitude), m_height(geoCoordinate.m_height) {} - - /*! - * \brief Constructor by values - * \param latitude - * \param longitude - * \param height - */ - CCoordinateGeodetic(CLatitude latitude, CLongitude longitude, BlackMisc::PhysicalQuantities::CLength height) : - m_latitude(latitude), m_longitude(longitude), m_height(height) {} - - /*! - * \brief Constructor by values - * \param latitudeDegrees - * \param longitudeDegrees - * \param heightMeters - */ - CCoordinateGeodetic(double latitudeDegrees, double longitudeDegrees, double heightMeters) : - m_latitude(latitudeDegrees, BlackMisc::PhysicalQuantities::CAngleUnit::deg()), m_longitude(longitudeDegrees, BlackMisc::PhysicalQuantities::CAngleUnit::deg()), m_height(heightMeters, BlackMisc::PhysicalQuantities::CLengthUnit::m()) {} - - /*! - * \brief Latitude - * \return - */ - const CLatitude &latitude() const + namespace Geo { - return this->m_latitude; - } - /*! - * \brief Longitude - * \return - */ - const CLongitude &longitude() const - { - return this->m_longitude; - } + /*! + * Latitude and longitude interface + * \brief Interface for geodetic ccordinates + */ + class ICoordinateGeodetic + { + public: - /*! - * \brief Height - * \return - */ - const BlackMisc::PhysicalQuantities::CLength &height() const - { - return this->m_height; - } + /*! + * \brief Latitude + * \return + */ + virtual const CLatitude &latitude() const = 0; - /*! - * \brief Switch unit of latitude / longitude - * \param unit - * \return - */ - CCoordinateGeodetic &switchUnit(const BlackMisc::PhysicalQuantities::CAngleUnit &unit) - { - this->m_latitude.switchUnit(unit); - this->m_longitude.switchUnit(unit); - return *this; - } + /*! + * \brief Longitude + * \return + */ + virtual const CLongitude &longitude() const = 0; - /*! - * \brief Switch unit of height - * \param unit - * \return - */ - CCoordinateGeodetic &switchUnit(const BlackMisc::PhysicalQuantities::CLengthUnit &unit) - { - this->m_height.switchUnit(unit); - return *this; - } + /*! + * \brief As string + * \return + */ + QString latitudeAsString() const + { + return this->latitude().toQString(true); + } - /*! - * \brief Set latitude - * \param latitude - */ - void setLatitude(const CLatitude &latitude) - { - this->m_latitude = latitude; - } + /*! + * \brief As string + * \return + */ + QString longitudeAsString() const + { + return this->longitude().toQString(true); + } - /*! - * \brief Set longitude - * \param longitude - */ - void setLongitude(const CLongitude &longitude) - { - this->m_longitude = longitude; - } + }; - /*! - * \brief Set height - * \param height - */ - void setHeight(const BlackMisc::PhysicalQuantities::CLength &height) - { - this->m_height = height; - } - /*! - * \brief Equal operator == - * \param other - * \return - */ - bool operator ==(const CCoordinateGeodetic &other) const - { - if (this == &other) return true; - return this->m_height == other.m_height && - this->m_latitude == other.m_latitude && - this->m_longitude == other.m_longitude; - } + /*! + * \brief Geodetic coordinate + */ + class CCoordinateGeodetic : public CStreamable + { + private: + BlackMisc::Geo::CLatitude m_latitude; //!< Latitude + BlackMisc::Geo::CLongitude m_longitude; //!< Longitude + BlackMisc::PhysicalQuantities::CLength m_height; //!< height - /*! - * \brief Unequal operator != - * \param other - * \return - */ - bool operator !=(const CCoordinateGeodetic &other) const - { - return !((*this) == other); - } + protected: + /*! + * \brief String for converter + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n = false) const; - /* - * Register metadata - */ - static void registerMetadata(); -}; + /*! + * \brief Stream to DBus + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; -} // namespace + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); + + public: + /*! + * \brief Default constructor + */ + CCoordinateGeodetic() : m_latitude(), m_longitude(), m_height() {} + + /*! + * \brief Constructor by values + * \param latitude + * \param longitude + * \param height + */ + CCoordinateGeodetic(CLatitude latitude, CLongitude longitude, BlackMisc::PhysicalQuantities::CLength height) : + m_latitude(latitude), m_longitude(longitude), m_height(height) {} + + /*! + * \brief Constructor by values + * \param latitudeDegrees + * \param longitudeDegrees + * \param heightMeters + */ + CCoordinateGeodetic(double latitudeDegrees, double longitudeDegrees, double heightMeters) : + m_latitude(latitudeDegrees, BlackMisc::PhysicalQuantities::CAngleUnit::deg()), m_longitude(longitudeDegrees, BlackMisc::PhysicalQuantities::CAngleUnit::deg()), m_height(heightMeters, BlackMisc::PhysicalQuantities::CLengthUnit::m()) {} + + /*! + * \brief Latitude + * \return + */ + const CLatitude &latitude() const + { + return this->m_latitude; + } + + /*! + * \brief Longitude + * \return + */ + const CLongitude &longitude() const + { + return this->m_longitude; + } + + /*! + * \brief Height + * \return + */ + const BlackMisc::PhysicalQuantities::CLength &height() const + { + return this->m_height; + } + + /*! + * \brief Switch unit of latitude / longitude + * \param unit + * \return + */ + CCoordinateGeodetic &switchUnit(const BlackMisc::PhysicalQuantities::CAngleUnit &unit) + { + this->m_latitude.switchUnit(unit); + this->m_longitude.switchUnit(unit); + return *this; + } + + /*! + * \brief Switch unit of height + * \param unit + * \return + */ + CCoordinateGeodetic &switchUnit(const BlackMisc::PhysicalQuantities::CLengthUnit &unit) + { + this->m_height.switchUnit(unit); + return *this; + } + + /*! + * \brief Set latitude + * \param latitude + */ + void setLatitude(const CLatitude &latitude) + { + this->m_latitude = latitude; + } + + /*! + * \brief Set longitude + * \param longitude + */ + void setLongitude(const CLongitude &longitude) + { + this->m_longitude = longitude; + } + + /*! + * \brief Set height + * \param height + */ + void setHeight(const BlackMisc::PhysicalQuantities::CLength &height) + { + this->m_height = height; + } + + /*! + * \brief Equal operator == + * \param other + * \return + */ + bool operator ==(const CCoordinateGeodetic &other) const; + + /*! + * \brief Unequal operator != + * \param other + * \return + */ + bool operator !=(const CCoordinateGeodetic &other) const; + + /*! + * Register metadata + */ + static void registerMetadata(); + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Geo::CCoordinateGeodetic) diff --git a/src/blackmisc/coordinatened.h b/src/blackmisc/coordinatened.h index 78c40638e..71a38e282 100644 --- a/src/blackmisc/coordinatened.h +++ b/src/blackmisc/coordinatened.h @@ -11,196 +11,196 @@ namespace BlackMisc { -namespace Geo -{ -/*! - * \brief North, East, Down - */ -class CCoordinateNed : public BlackMisc::Math::CVector3DBase -{ -private: - CCoordinateGeodetic m_referencePosition; //!< geodetic reference position - bool m_hasReferencePosition; //!< valid reference position? - -protected: - /*! - * \brief String for converter - * \return - */ - virtual QString convertToQString() const + namespace Geo { - QString s = "NED: {N %1, E %2, D %3}"; - s = s.arg(QString::number(this->north(), 'f', 6)). - arg(QString::number(this->east(), 'f', 6)). - arg(QString::number(this->down(), 'f', 6)); - return s; - } + /*! + * \brief North, East, Down + */ + class CCoordinateNed : public BlackMisc::Math::CVector3DBase + { + private: + CCoordinateGeodetic m_referencePosition; //!< geodetic reference position + bool m_hasReferencePosition; //!< valid reference position? -public: - /*! - * \brief Default constructor - */ - CCoordinateNed() : CVector3DBase(), m_referencePosition(), m_hasReferencePosition(false) {} + protected: + /*! + * \brief String for converter + * \return + */ + virtual QString convertToQString() const + { + QString s = "NED: {N %1, E %2, D %3}"; + s = s.arg(QString::number(this->north(), 'f', 6)). + arg(QString::number(this->east(), 'f', 6)). + arg(QString::number(this->down(), 'f', 6)); + return s; + } - /*! - * \brief Constructor with reference position - * \param referencePosition - */ - CCoordinateNed(const CCoordinateGeodetic &referencePosition) : CVector3DBase(), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} + public: + /*! + * \brief Default constructor + */ + CCoordinateNed() : CVector3DBase(), m_referencePosition(), m_hasReferencePosition(false) {} - /*! - * \brief Constructor by values - * \param referencePosition - * \param north - * \param east - * \param down - */ - CCoordinateNed(const CCoordinateGeodetic &referencePosition, double north, double east, double down) : CVector3DBase(north, east, down), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} + /*! + * \brief Constructor with reference position + * \param referencePosition + */ + CCoordinateNed(const CCoordinateGeodetic &referencePosition) : CVector3DBase(), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} - /*! - * \brief Constructor by values - * \param north - * \param east - * \param down - */ - CCoordinateNed(double north, double east, double down) : CVector3DBase(north, east, down), m_referencePosition(), m_hasReferencePosition(false) {} + /*! + * \brief Constructor by values + * \param referencePosition + * \param north + * \param east + * \param down + */ + CCoordinateNed(const CCoordinateGeodetic &referencePosition, double north, double east, double down) : CVector3DBase(north, east, down), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} - /*! - * \brief Copy constructor - * \param otherNed - */ - CCoordinateNed(const CCoordinateNed &other) : - CVector3DBase(other), m_referencePosition(other.m_referencePosition), m_hasReferencePosition(other.m_hasReferencePosition) {} + /*! + * \brief Constructor by values + * \param north + * \param east + * \param down + */ + CCoordinateNed(double north, double east, double down) : CVector3DBase(north, east, down), m_referencePosition(), m_hasReferencePosition(false) {} - /*! - * \brief Constructor by math vector - * \param vector - */ - explicit CCoordinateNed(const BlackMisc::Math::CVector3D &vector) : CVector3DBase(vector.i(), vector.j(), vector.k()), m_referencePosition(), m_hasReferencePosition(false) {} + /*! + * \brief Copy constructor + * \param otherNed + */ + CCoordinateNed(const CCoordinateNed &other) : + CVector3DBase(other), m_referencePosition(other.m_referencePosition), m_hasReferencePosition(other.m_hasReferencePosition) {} - /*! - * \brief Constructor by math vector and reference position - * \param referencePosition - * \param vector - */ - CCoordinateNed(const CCoordinateGeodetic &referencePosition, const BlackMisc::Math::CVector3D &vector) : CVector3DBase(vector.i(), vector.j(), vector.k()), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} + /*! + * \brief Constructor by math vector + * \param vector + */ + explicit CCoordinateNed(const BlackMisc::Math::CVector3D &vector) : CVector3DBase(vector.i(), vector.j(), vector.k()), m_referencePosition(), m_hasReferencePosition(false) {} - /*! - * \brief Equal operator == - * \param other - * \return - */ - bool operator ==(const CCoordinateNed &other) const - { - if (this == &other) return true; - return this->m_hasReferencePosition == other.m_hasReferencePosition && - this->m_referencePosition == other.m_referencePosition && - this->CVector3DBase::operator== (other); - } + /*! + * \brief Constructor by math vector and reference position + * \param referencePosition + * \param vector + */ + CCoordinateNed(const CCoordinateGeodetic &referencePosition, const BlackMisc::Math::CVector3D &vector) : CVector3DBase(vector.i(), vector.j(), vector.k()), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} - /*! - * \brief Unequal operator != - * \param other - * \return - */ - bool operator !=(const CCoordinateNed &other) const - { - return !((*this) == other); - } + /*! + * \brief Equal operator == + * \param other + * \return + */ + bool operator ==(const CCoordinateNed &other) const + { + if (this == &other) return true; + return this->m_hasReferencePosition == other.m_hasReferencePosition && + this->m_referencePosition == other.m_referencePosition && + this->CVector3DBase::operator== (other); + } - /*! - * \brief Corresponding reference position - * \return - */ - CCoordinateGeodetic referencePosition() const - { - return this->m_referencePosition; - } + /*! + * \brief Unequal operator != + * \param other + * \return + */ + bool operator !=(const CCoordinateNed &other) const + { + return !((*this) == other); + } - /*! - * \brief Corresponding reference position - * \return - */ - bool hasReferencePosition() const - { - return this->m_hasReferencePosition; - } + /*! + * \brief Corresponding reference position + * \return + */ + CCoordinateGeodetic referencePosition() const + { + return this->m_referencePosition; + } - /*! - * \brief North - * \return - */ - double north() const - { - return this->m_i; - } + /*! + * \brief Corresponding reference position + * \return + */ + bool hasReferencePosition() const + { + return this->m_hasReferencePosition; + } - /*! - * \brief East - * \return - */ - double east() const - { - return this->m_j; - } + /*! + * \brief North + * \return + */ + double north() const + { + return this->m_i; + } - /*! - * \brief Down - * \return - */ - double down() const - { - return this->m_k; - } + /*! + * \brief East + * \return + */ + double east() const + { + return this->m_j; + } - /*! - * \brief Set north - * \param north - */ - void setNorth(double north) - { - this->m_i = north; - } + /*! + * \brief Down + * \return + */ + double down() const + { + return this->m_k; + } - /*! - * \brief Set east - * \param east - */ - void setEast(double east) - { - this->m_j = east; - } + /*! + * \brief Set north + * \param north + */ + void setNorth(double north) + { + this->m_i = north; + } - /*! - * \brief Set down - * \param down - */ - void setDown(double down) - { - this->m_k = down; - } + /*! + * \brief Set east + * \param east + */ + void setEast(double east) + { + this->m_j = east; + } - /*! - * \brief Corresponding reference position - * \param referencePosition - */ - void setReferencePosition(const CCoordinateGeodetic &referencePosition) - { - this->m_referencePosition = referencePosition; - this->m_hasReferencePosition = true; - } + /*! + * \brief Set down + * \param down + */ + void setDown(double down) + { + this->m_k = down; + } - /*! - * \brief Concrete implementation of a 3D vector - * \return - */ - BlackMisc::Math::CVector3D toMathVector() const - { - return BlackMisc::Math::CVector3D(this->north(), this->east(), this->down()); - } -}; + /*! + * \brief Corresponding reference position + * \param referencePosition + */ + void setReferencePosition(const CCoordinateGeodetic &referencePosition) + { + this->m_referencePosition = referencePosition; + this->m_hasReferencePosition = true; + } -} // namespace + /*! + * \brief Concrete implementation of a 3D vector + * \return + */ + BlackMisc::Math::CVector3D toMathVector() const + { + return BlackMisc::Math::CVector3D(this->north(), this->east(), this->down()); + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Geo::CCoordinateNed) diff --git a/src/blackmisc/coordinatetransformation.cpp b/src/blackmisc/coordinatetransformation.cpp index 28c70fbc4..bcc3f07b4 100644 --- a/src/blackmisc/coordinatetransformation.cpp +++ b/src/blackmisc/coordinatetransformation.cpp @@ -15,233 +15,233 @@ using namespace BlackMisc::Math; namespace BlackMisc { -namespace Geo -{ - -/* - * NED to ECEF - */ -CCoordinateEcef CCoordinateTransformation::toEcef(const CCoordinateNed &ned) -{ - CLatitude lat = ned.referencePosition().latitude(); - CLongitude lon = ned.referencePosition().longitude(); - double angleRad = - (lat.value(CAngleUnit::rad())) - CMath::PI() / 2; - - CMatrix3x3 dcm1; - CMatrix3x3 dcm2; - CMatrix3x3 dcm3; - CMatrix3x3 dcm; - CMatrix3x3 invDcm; - dcm1.setToIdentity(); - dcm2.setZero(); - dcm3.setZero(); - - dcm2(0, 0) = cos(angleRad); - dcm2(0, 2) = -sin(angleRad); - dcm2(1, 1) = 1; - dcm2(2, 0) = sin(angleRad); - dcm2(2, 2) = cos(angleRad); - - angleRad = lon.value(CAngleUnit::rad()); - dcm3(0, 0) = cos(angleRad); - dcm3(0, 1) = sin(angleRad); - dcm3(1, 0) = -sin(angleRad); - dcm3(1, 1) = cos(angleRad); - dcm3(2, 2) = 1; - - dcm = dcm1 * dcm2 * dcm3; - - bool inverse; - invDcm.setZero(); - invDcm = dcm.inverse(inverse); - Q_ASSERT_X(inverse, "toEcef", "Inverse matrix could not be calculated"); - - CVector3D tempResult = invDcm * ned.toMathVector(); // to generic vector - CCoordinateEcef result(tempResult); - return result; -} - -/* - * Geodetic to ECEF - */ -CCoordinateEcef CCoordinateTransformation::toEcef(const CCoordinateGeodetic &geo) -{ - CLatitude lat = geo.latitude(); - CLongitude lon = geo.longitude(); - - double phi = lat.value(CAngleUnit::rad()); - double lambdaRad = lon.value(CAngleUnit::rad()); - double sphi = sin(phi); - double cphi = cos(phi); - - double n = EarthRadiusMeters() / sqrt(1 - e2() * CMath::square(sphi)); - - double slambda = sin(lambdaRad); - - double clambda = cos(lambdaRad); - - double h = geo.height().value(CLengthUnit::m()); - double x = (n + h) * cphi; - double y = x * slambda; - x *= clambda; - double z = (e2m() * n + h) * sphi; - CCoordinateEcef result(x, y, z); - return result; -} - -/* - * Convert to NED - */ -CCoordinateNed CCoordinateTransformation::toNed(const CCoordinateEcef &ecef, const CCoordinateGeodetic &referencePosition) -{ - CLatitude lat = referencePosition.latitude(); - CLongitude lon = referencePosition.longitude(); - double angleRad = - (lat.value(CAngleUnit::rad())) - CMath::PIHALF(); - - CMatrix3x3 dcm1; - CMatrix3x3 dcm2(0.0); - CMatrix3x3 dcm3(0.0); - CMatrix3x3 dcm(0.0); - dcm1.setToIdentity(); - - dcm2(0, 0) = cos(angleRad); - dcm2(0, 2) = -sin(angleRad); - dcm2(1, 1) = 1; - dcm2(2, 0) = sin(angleRad); - dcm2(2, 2) = cos(angleRad); - - angleRad = lon.value(CAngleUnit::rad()); - dcm3(0, 0) = cos(angleRad); - dcm3(0, 1) = sin(angleRad); - dcm3(1, 0) = -sin(angleRad); - dcm3(1, 1) = cos(angleRad); - dcm3(2, 2) = 1; - - dcm = dcm1 * dcm2 * dcm3; - - CVector3D tempResult = dcm * ecef.toMathVector(); // to generic vector - CCoordinateNed result(referencePosition, tempResult); - return result; -} - -/* - * ECEF to geodetic - */ -CCoordinateGeodetic CCoordinateTransformation::toGeodetic(const CCoordinateEcef &ecef) -{ - double R = CMath::hypot(ecef.x(), ecef.y()); - double slam = 0; - double clam = 1; - - if (R != 0) + namespace Geo { - slam = ecef.y() / R; - clam = ecef.x() / R; - } - // Calculate the distance to the earth - double h = CMath::hypot(R, ecef.z()); - - double sphi = 0; - double cphi = 0; - - double p = CMath::square(R / EarthRadiusMeters()); - double q = e2m() * CMath::square(ecef.z() / EarthRadiusMeters()); - double r = (p + q - e4()) / 6.0; - - if (!(e4() *q == 0 && r <= 0)) - { - // Avoid possible division by zero when r = 0 by multiplying - // equations for s and t by r^3 and r, resp. - - double S = e4() * p * q / 4; //! S = r^3 * s - double r2 = CMath::square(r); - double r3 = r * r2; - double disc = S * (2 * r3 + S); - double u = r; - - if (disc >= 0) + /* + * NED to ECEF + */ + CCoordinateEcef CCoordinateTransformation::toEcef(const CCoordinateNed &ned) { - double T3 = S + r3; - /* - Pick the sign on the sqrt to maximize abs(T3). This minimizes - loss of precision due to cancellation. The result is unchanged - because of the way the T is used in definition of u. - */ - T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3 + CLatitude lat = ned.referencePosition().latitude(); + CLongitude lon = ned.referencePosition().longitude(); + double angleRad = - (lat.value(CAngleUnit::rad())) - CMath::PI() / 2; - //!N.B. cubicRootReal always returns the real root. cubicRootReal(-8) = -2. - double T = CMath::cubicRootReal(T3); + CMatrix3x3 dcm1; + CMatrix3x3 dcm2; + CMatrix3x3 dcm3; + CMatrix3x3 dcm; + CMatrix3x3 invDcm; + dcm1.setToIdentity(); + dcm2.setZero(); + dcm3.setZero(); - // T can be zero; but then r2 / T -> 0. - u += T + (T != 0 ? r2 / T : 0); - } - else - { - // T is complex, but the way u is defined the result is real. - double ang = atan2(sqrt(-disc), -(S + r3)); - /* - There are three possible cube roots. We choose the root which - avoids cancellation. Note that disc < 0 implies that r < 0. - */ - u += 2 * r * cos(ang / 3); + dcm2(0, 0) = cos(angleRad); + dcm2(0, 2) = -sin(angleRad); + dcm2(1, 1) = 1; + dcm2(2, 0) = sin(angleRad); + dcm2(2, 2) = cos(angleRad); + + angleRad = lon.value(CAngleUnit::rad()); + dcm3(0, 0) = cos(angleRad); + dcm3(0, 1) = sin(angleRad); + dcm3(1, 0) = -sin(angleRad); + dcm3(1, 1) = cos(angleRad); + dcm3(2, 2) = 1; + + dcm = dcm1 * dcm2 * dcm3; + + bool inverse; + invDcm.setZero(); + invDcm = dcm.inverse(inverse); + Q_ASSERT_X(inverse, "toEcef", "Inverse matrix could not be calculated"); + + CVector3D tempResult = invDcm * ned.toMathVector(); // to generic vector + CCoordinateEcef result(tempResult); + return result; } - // This is garanteed positive - double V = sqrt(CMath::square(u) + e4() * q); + /* + * Geodetic to ECEF + */ + CCoordinateEcef CCoordinateTransformation::toEcef(const CCoordinateGeodetic &geo) + { + CLatitude lat = geo.latitude(); + CLongitude lon = geo.longitude(); + + double phi = lat.value(CAngleUnit::rad()); + double lambdaRad = lon.value(CAngleUnit::rad()); + double sphi = sin(phi); + double cphi = cos(phi); + + double n = EarthRadiusMeters() / sqrt(1 - e2() * CMath::square(sphi)); + + double slambda = sin(lambdaRad); + + double clambda = cos(lambdaRad); + + double h = geo.height().value(CLengthUnit::m()); + double x = (n + h) * cphi; + double y = x * slambda; + x *= clambda; + double z = (e2m() * n + h) * sphi; + CCoordinateEcef result(x, y, z); + return result; + } /* - Avoid loss of accuracy when u < 0. Underflow doesn't occur in - e4 * q / (v - u) because u ~ e^4 when q is small and u < 0. - */ - double uv = u < 0 ? e4() * q / (V - u) : u + V; //! u+v, guaranteed positive + * Convert to NED + */ + CCoordinateNed CCoordinateTransformation::toNed(const CCoordinateEcef &ecef, const CCoordinateGeodetic &referencePosition) + { + CLatitude lat = referencePosition.latitude(); + CLongitude lon = referencePosition.longitude(); + double angleRad = - (lat.value(CAngleUnit::rad())) - CMath::PIHALF(); - // Need to guard against w going negative due to roundoff in uv - q. - double w = std::max(double(0), e2abs() * (uv - q) / (2 * V)); + CMatrix3x3 dcm1; + CMatrix3x3 dcm2(0.0); + CMatrix3x3 dcm3(0.0); + CMatrix3x3 dcm(0.0); + dcm1.setToIdentity(); + + dcm2(0, 0) = cos(angleRad); + dcm2(0, 2) = -sin(angleRad); + dcm2(1, 1) = 1; + dcm2(2, 0) = sin(angleRad); + dcm2(2, 2) = cos(angleRad); + + angleRad = lon.value(CAngleUnit::rad()); + dcm3(0, 0) = cos(angleRad); + dcm3(0, 1) = sin(angleRad); + dcm3(1, 0) = -sin(angleRad); + dcm3(1, 1) = cos(angleRad); + dcm3(2, 2) = 1; + + dcm = dcm1 * dcm2 * dcm3; + + CVector3D tempResult = dcm * ecef.toMathVector(); // to generic vector + CCoordinateNed result(referencePosition, tempResult); + return result; + } /* - Rearrange expression for k to avoid loss of accuracy due to - subtraction. Division by 0 not possible because uv > 0, w >= 0. - */ - double k = uv / (sqrt(uv + CMath::square(w)) + w); - double k1 = k; - double k2 = k + e2(); - double d = k1 * R / k2; - double H = CMath::hypot((ecef.z()) / k1, R / k2); + * ECEF to geodetic + */ + CCoordinateGeodetic CCoordinateTransformation::toGeodetic(const CCoordinateEcef &ecef) + { + double R = CMath::hypot(ecef.x(), ecef.y()); + double slam = 0; + double clam = 1; - sphi = (ecef.z() / k1) / H; - cphi = (R / k2) / H; + if (R != 0) + { + slam = ecef.y() / R; + clam = ecef.x() / R; + } - h = (1 - e2m() / k1) * CMath::hypot(d, ecef.z()); - } - else // e4 * q == 0 && r <= 0 - { - /* - This leads to k = 0 (oblate, equatorial plane) and k + e^2 = 0 - (prolate, rotation axis) and the generation of 0/0 in the general - formulas for phi and h. using the general formula and division by 0 - in formula for h. So handle this case by taking the limits: - f > 0: z -> 0, k -> e2 * sqrt(q)/sqrt(e4 - p) - f < 0: R -> 0, k + e2 -> - e2 * sqrt(q)/sqrt(e4 - p) - */ - double zz = sqrt((e4() - p) / e2m()); - double xx = sqrt(p); - double H = CMath::hypot(zz, xx); - sphi = zz / H; - cphi = xx / H; - if (ecef.z() < 0) sphi = -sphi; // for tiny negative Z (not for prolate) - h = - EarthRadiusMeters() * (e2m()) * H / e2abs(); - } + // Calculate the distance to the earth + double h = CMath::hypot(R, ecef.z()); - double latRad = atan2(sphi, cphi); - double lonRad = -atan2(-slam, clam); // Negative signs return lon degrees [-180, 180) - CCoordinateGeodetic result( - CLatitude(latRad, CAngleUnit::rad()), - CLongitude(lonRad, CAngleUnit::rad()), - CLength(h, CLengthUnit::m())); - result.switchUnit(CAngleUnit::deg()); - return result; -} + double sphi = 0; + double cphi = 0; -} // namespace + double p = CMath::square(R / EarthRadiusMeters()); + double q = e2m() * CMath::square(ecef.z() / EarthRadiusMeters()); + double r = (p + q - e4()) / 6.0; + + if (!(e4() *q == 0 && r <= 0)) + { + // Avoid possible division by zero when r = 0 by multiplying + // equations for s and t by r^3 and r, resp. + + double S = e4() * p * q / 4; //! S = r^3 * s + double r2 = CMath::square(r); + double r3 = r * r2; + double disc = S * (2 * r3 + S); + double u = r; + + if (disc >= 0) + { + double T3 = S + r3; + /* + Pick the sign on the sqrt to maximize abs(T3). This minimizes + loss of precision due to cancellation. The result is unchanged + because of the way the T is used in definition of u. + */ + T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3 + + //!N.B. cubicRootReal always returns the real root. cubicRootReal(-8) = -2. + double T = CMath::cubicRootReal(T3); + + // T can be zero; but then r2 / T -> 0. + u += T + (T != 0 ? r2 / T : 0); + } + else + { + // T is complex, but the way u is defined the result is real. + double ang = atan2(sqrt(-disc), -(S + r3)); + /* + There are three possible cube roots. We choose the root which + avoids cancellation. Note that disc < 0 implies that r < 0. + */ + u += 2 * r * cos(ang / 3); + } + + // This is garanteed positive + double V = sqrt(CMath::square(u) + e4() * q); + + /* + Avoid loss of accuracy when u < 0. Underflow doesn't occur in + e4 * q / (v - u) because u ~ e^4 when q is small and u < 0. + */ + double uv = u < 0 ? e4() * q / (V - u) : u + V; //! u+v, guaranteed positive + + // Need to guard against w going negative due to roundoff in uv - q. + double w = std::max(double(0), e2abs() * (uv - q) / (2 * V)); + + /* + Rearrange expression for k to avoid loss of accuracy due to + subtraction. Division by 0 not possible because uv > 0, w >= 0. + */ + double k = uv / (sqrt(uv + CMath::square(w)) + w); + double k1 = k; + double k2 = k + e2(); + double d = k1 * R / k2; + double H = CMath::hypot((ecef.z()) / k1, R / k2); + + sphi = (ecef.z() / k1) / H; + cphi = (R / k2) / H; + + h = (1 - e2m() / k1) * CMath::hypot(d, ecef.z()); + } + else // e4 * q == 0 && r <= 0 + { + /* + This leads to k = 0 (oblate, equatorial plane) and k + e^2 = 0 + (prolate, rotation axis) and the generation of 0/0 in the general + formulas for phi and h. using the general formula and division by 0 + in formula for h. So handle this case by taking the limits: + f > 0: z -> 0, k -> e2 * sqrt(q)/sqrt(e4 - p) + f < 0: R -> 0, k + e2 -> - e2 * sqrt(q)/sqrt(e4 - p) + */ + double zz = sqrt((e4() - p) / e2m()); + double xx = sqrt(p); + double H = CMath::hypot(zz, xx); + sphi = zz / H; + cphi = xx / H; + if (ecef.z() < 0) sphi = -sphi; // for tiny negative Z (not for prolate) + h = - EarthRadiusMeters() * (e2m()) * H / e2abs(); + } + + double latRad = atan2(sphi, cphi); + double lonRad = -atan2(-slam, clam); // Negative signs return lon degrees [-180, 180) + CCoordinateGeodetic result( + CLatitude(latRad, CAngleUnit::rad()), + CLongitude(lonRad, CAngleUnit::rad()), + CLength(h, CLengthUnit::m())); + result.switchUnit(CAngleUnit::deg()); + return result; + } + + } // namespace } // namespace diff --git a/src/blackmisc/coordinatetransformation.h b/src/blackmisc/coordinatetransformation.h index 519eb586b..69f26ef2c 100644 --- a/src/blackmisc/coordinatetransformation.h +++ b/src/blackmisc/coordinatetransformation.h @@ -21,112 +21,112 @@ namespace BlackMisc { -namespace Geo -{ - -/*! - * \brief Coordinate transformation class between different systems - */ -class CCoordinateTransformation -{ -private: - /*! - * \brief Equatorial radius of WGS84 ellipsoid (6378137 m) - * \return - */ - static const double &EarthRadiusMeters() + namespace Geo { - static double erm = 6378137.0; - return erm; - } - /*! - * \brief Flattening of WGS84 ellipsoid (1/298.257223563) - * \return - */ - static const double &Flattening() - { - static double f = 1.0 / 298.257223563; - return f; - } + /*! + * \brief Coordinate transformation class between different systems + */ + class CCoordinateTransformation + { + private: + /*! + * \brief Equatorial radius of WGS84 ellipsoid (6378137 m) + * \return + */ + static const double &EarthRadiusMeters() + { + static double erm = 6378137.0; + return erm; + } - /*! - * \brief First eccentricity squared - * \return - */ - static const double &e2() - { - static double e2 = (Flattening() * (2 - Flattening())); - return e2; - } + /*! + * \brief Flattening of WGS84 ellipsoid (1/298.257223563) + * \return + */ + static const double &Flattening() + { + static double f = 1.0 / 298.257223563; + return f; + } - /*! - * \brief First eccentricity to power of four - * \return - */ - static const double &e4() - { - static double e4 = BlackMisc::Math::CMath::square(e2()); - return e4; - } + /*! + * \brief First eccentricity squared + * \return + */ + static const double &e2() + { + static double e2 = (Flattening() * (2 - Flattening())); + return e2; + } - /*! - * \brief First eccentricity squared absolute - * \return - */ - static const double &e2abs() - { - static double e2abs = qAbs(e2()); - return e2abs; - } + /*! + * \brief First eccentricity to power of four + * \return + */ + static const double &e4() + { + static double e4 = BlackMisc::Math::CMath::square(e2()); + return e4; + } - /*! - * \brief Eccentricity e2m - * \return - */ - static const double &e2m() - { - static double e2m = BlackMisc::Math::CMath::square(1.0 - Flattening()); - return e2m; - } + /*! + * \brief First eccentricity squared absolute + * \return + */ + static const double &e2abs() + { + static double e2abs = qAbs(e2()); + return e2abs; + } - /*! - * \brief Default constructor, deleted - */ - CCoordinateTransformation(); + /*! + * \brief Eccentricity e2m + * \return + */ + static const double &e2m() + { + static double e2m = BlackMisc::Math::CMath::square(1.0 - Flattening()); + return e2m; + } -public: - /*! - * \brief NED to ECEF - * \param ned - * \return - */ - static CCoordinateEcef toEcef(const CCoordinateNed &ned); + /*! + * \brief Default constructor, deleted + */ + CCoordinateTransformation(); - /*! - * \brief Geodetic to ECEF - * \param geo - * \return - */ - static CCoordinateEcef toEcef(const CCoordinateGeodetic &geo); + public: + /*! + * \brief NED to ECEF + * \param ned + * \return + */ + static CCoordinateEcef toEcef(const CCoordinateNed &ned); - /*! - * \brief ECEF via Geodetic to NED - * \param ecef - * \param referencePosition - * \return - */ - static CCoordinateNed toNed(const CCoordinateEcef &ecef, const CCoordinateGeodetic &referencePosition); + /*! + * \brief Geodetic to ECEF + * \param geo + * \return + */ + static CCoordinateEcef toEcef(const CCoordinateGeodetic &geo); - /*! - * \brief ECEF to Geodetic - * \param ecef - * \return - */ - static CCoordinateGeodetic toGeodetic(const CCoordinateEcef &ecef); -}; + /*! + * \brief ECEF via Geodetic to NED + * \param ecef + * \param referencePosition + * \return + */ + static CCoordinateNed toNed(const CCoordinateEcef &ecef, const CCoordinateGeodetic &referencePosition); -} // namespace + /*! + * \brief ECEF to Geodetic + * \param ecef + * \return + */ + static CCoordinateGeodetic toGeodetic(const CCoordinateEcef &ecef); + }; + + } // namespace } // namespace #endif // guard diff --git a/src/blackmisc/geoearthangle.cpp b/src/blackmisc/geoearthangle.cpp index 219bc85c0..b5a06481a 100644 --- a/src/blackmisc/geoearthangle.cpp +++ b/src/blackmisc/geoearthangle.cpp @@ -9,69 +9,73 @@ namespace BlackMisc { -namespace Geo -{ + namespace Geo + { -/* - * Register metadata - */ -template void CEarthAngle::registerMetadata() -{ - qRegisterMetaType(typeid(LATorLON).name()); - qDBusRegisterMetaType(); -} - -/* - * Latitude or Longitude from a WGS string - */ -template LATorLON CEarthAngle::fromWgs84(const QString &wgsCoordinate) -{ - // http://www.regular-expressions.info/floatingpoint.html - QRegExp rx("([-+]?[0-9]*\\.?[0-9]+)"); - qint32 deg = 0; - qint32 min = 0; - double sec = 0.0; - double secFragment = 0.0; - int fragmentLength = 0; - int c = 0; - int pos= 0; - while ((pos = rx.indexIn(wgsCoordinate, pos)) != -1) { - QString cap = rx.cap(1); - pos += rx.matchedLength(); - switch(c++) { - case 0: - deg = cap.toInt(); - break; - case 1: - min = cap.toInt(); - break; - case 2: - sec = cap.toDouble(); - break; - case 3: - secFragment = cap.toDouble(); - fragmentLength = cap.length(); - break; - default: - break; + /* + * Register metadata + */ + template void CEarthAngle::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); } - } - if (fragmentLength > 0) { - // we do have given ms - sec += secFragment / qPow(10,fragmentLength); - } - if (wgsCoordinate.contains('S', Qt::CaseInsensitive) || - wgsCoordinate.contains('W', Qt::CaseInsensitive)) deg *= -1; + /* + * Latitude or Longitude from a WGS string + */ + template LATorLON CEarthAngle::fromWgs84(const QString &wgsCoordinate) + { + // http://www.regular-expressions.info/floatingpoint.html + QRegExp rx("([-+]?[0-9]*\\.?[0-9]+)"); + qint32 deg = 0; + qint32 min = 0; + double sec = 0.0; + double secFragment = 0.0; + int fragmentLength = 0; + int c = 0; + int pos = 0; + while ((pos = rx.indexIn(wgsCoordinate, pos)) != -1) + { + QString cap = rx.cap(1); + pos += rx.matchedLength(); + switch (c++) + { + case 0: + deg = cap.toInt(); + break; + case 1: + min = cap.toInt(); + break; + case 2: + sec = cap.toDouble(); + break; + case 3: + secFragment = cap.toDouble(); + fragmentLength = cap.length(); + break; + default: + break; + } + } + if (fragmentLength > 0) + { + // we do have given ms + sec += secFragment / qPow(10, fragmentLength); + } - CAngle a(deg, min, sec); - return LATorLON(a); -} + if (wgsCoordinate.contains('S', Qt::CaseInsensitive) || + wgsCoordinate.contains('W', Qt::CaseInsensitive)) deg *= -1; -// see here for the reason of thess forward instantiations -// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html -template class CEarthAngle; -template class CEarthAngle; + CAngle a(deg, min, sec); + return LATorLON(a); + } -} // namespace + + // see here for the reason of thess forward instantiations + // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html + template class CEarthAngle; + template class CEarthAngle; + + } // namespace } // namespace diff --git a/src/blackmisc/geoearthangle.h b/src/blackmisc/geoearthangle.h index 7491af0de..97dc8385a 100644 --- a/src/blackmisc/geoearthangle.h +++ b/src/blackmisc/geoearthangle.h @@ -9,185 +9,195 @@ namespace BlackMisc { -namespace Geo -{ -/*! - * \brief Base class for latitude / longitude - */ -template class CEarthAngle : public BlackMisc::PhysicalQuantities::CAngle -{ -protected: - /*! - * \brief Default constructor - */ - CEarthAngle() : CAngle(0.0, BlackMisc::PhysicalQuantities::CAngleUnit::deg()) {} - - /*! - * \brief Init by double value - * \param value - * \param unit - */ - CEarthAngle(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : CAngle(value, unit) {} - - /*! - * \brief Init by double value - * \param angle - */ - CEarthAngle(const BlackMisc::PhysicalQuantities::CAngle &angle) : CAngle(angle) {} - - /*! - * \brief String for converter and streaming - * \return - */ - virtual QString convertToQString() const + namespace Geo { - return this->valueRoundedWithUnit(6); - } + /*! + * \brief Base class for latitude / longitude + */ + template class CEarthAngle : public BlackMisc::PhysicalQuantities::CAngle + { + protected: + /*! + * \brief Default constructor + */ + CEarthAngle() : CAngle(0.0, BlackMisc::PhysicalQuantities::CAngleUnit::deg()) {} - /*! - * \brief Stream to DBus << - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - this->CAngle::marshallToDbus(argument); - } + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CEarthAngle(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : CAngle(value, unit) {} - /*! - * \brief Stream from DBus >> - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - this->CAngle::unmarshallFromDbus(argument); - } + /*! + * \brief Init by double value + * \param angle + */ + CEarthAngle(const BlackMisc::PhysicalQuantities::CAngle &angle) : CAngle(angle) {} -public: - /*! - * \brief Virtual destructor - */ - virtual ~CEarthAngle() {} + /*! + * \brief String representation + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n = false) const + { + return this->valueRoundedWithUnit(BlackMisc::PhysicalQuantities::CAngleUnit::deg(), 6, i18n); + } - /*! - * \brief Equal operator == - * \param latOrLon - * \return - */ - bool operator==(const LATorLON &latOrLon) const - { - return this->CAngle::operator ==(latOrLon); - } + /*! + * \brief Stream to DBus << + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const + { + this->CAngle::marshallToDbus(argument); + } - /*! - * \brief Not equal operator != - * \param latOrLon - * \return - */ - bool operator!=(const LATorLON &latOrLon) const - { - return this->CAngle::operator !=(latOrLon); - } + /*! + * \brief Stream from DBus >> + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + this->CAngle::unmarshallFromDbus(argument); + } - /*! - * \brief Plus operator += - * \param latOrLon - * \return - */ - CEarthAngle &operator +=(const CEarthAngle &latOrLon) - { - this->CAngle::operator +=(latOrLon); - return *this; - } + public: + /*! + * \brief Virtual destructor + */ + virtual ~CEarthAngle() {} - /*! - * \brief Minus operator-= - * \param latOrLon - * \return - */ - CEarthAngle &operator -=(const CEarthAngle &latOrLon) - { - this->CAngle::operator -=(latOrLon); - return *this; - } + /*! + * \brief Equal operator == + * \param latOrLon + * \return + */ + bool operator==(const LATorLON &latOrLon) const + { + return this->CAngle::operator ==(latOrLon); + } - /*! - * \brief Greater operator > - * \param latOrLon - * \return - */ - bool operator >(const LATorLON &latOrLon) const - { - return this->CAngle::operator >(latOrLon); - } + /*! + * \brief Not equal operator != + * \param latOrLon + * \return + */ + bool operator!=(const LATorLON &latOrLon) const + { + return this->CAngle::operator !=(latOrLon); + } - /*! - * \brief Less operator < - * \param latOrLon - * \return - */ - bool operator <(const LATorLON &latOrLon) const - { - return this->CAngle::operator >(latOrLon); - } + /*! + * \brief Plus operator += + * \param latOrLon + * \return + */ + CEarthAngle &operator +=(const CEarthAngle &latOrLon) + { + this->CAngle::operator +=(latOrLon); + return *this; + } - /*! - * \brief Less equal operator <= - * \param latOrLon - * \return - */ - bool operator <=(const LATorLON &latOrLon) const - { - return this->CAngle::operator <=(latOrLon); - } + /*! + * \brief Minus operator-= + * \param latOrLon + * \return + */ + CEarthAngle &operator -=(const CEarthAngle &latOrLon) + { + this->CAngle::operator -=(latOrLon); + return *this; + } - /*! - * \brief Greater equal operator >= - * \param latOrLon - * \return - */ - bool operator >=(const LATorLON &latOrLon) const - { - return this->CAngle::operator >=(latOrLon); - } + /*! + * \brief Greater operator > + * \param latOrLon + * \return + */ + bool operator >(const LATorLON &latOrLon) const + { + return this->CAngle::operator >(latOrLon); + } - /*! - * \brief Plus operator + - * \param latOrLon - * \return - */ - LATorLON operator +(const LATorLON &latOrLon) const - { - LATorLON l(*this); - l += latOrLon; - return l; - } + /*! + * \brief Less operator < + * \param latOrLon + * \return + */ + bool operator <(const LATorLON &latOrLon) const + { + return this->CAngle::operator >(latOrLon); + } - /*! - * \brief Minus operator - - * \param latOrLon - * \return - */ - LATorLON operator -(const LATorLON &latOrLon) const - { - LATorLON l(*this); - l -= latOrLon; - return l; - } + /*! + * \brief Less equal operator <= + * \param latOrLon + * \return + */ + bool operator <=(const LATorLON &latOrLon) const + { + return this->CAngle::operator <=(latOrLon); + } - /*! - * Register metadata - */ - static void registerMetadata(); + /*! + * \brief Greater equal operator >= + * \param latOrLon + * \return + */ + bool operator >=(const LATorLON &latOrLon) const + { + return this->CAngle::operator >=(latOrLon); + } - /*! - * \brief Latitude / Longitude from a WGS string such as - * \param wgsCoordinate 50° 2′ 0″ N / 8° 34′ 14″ E - * \return - */ - static LATorLON fromWgs84(const QString &wgsCoordinate); -}; + /*! + * \brief Plus operator + + * \param latOrLon + * \return + */ + LATorLON operator +(const LATorLON &latOrLon) const + { + LATorLON l(*this); + l += latOrLon; + return l; + } -} // namespace + /*! + * \brief Minus operator - + * \param latOrLon + * \return + */ + LATorLON operator -(const LATorLON &latOrLon) const + { + LATorLON l(*this); + l -= latOrLon; + return l; + } + + /*! + * \brief To angle + * \return + */ + BlackMisc::PhysicalQuantities::CAngle toAngle() const + { + return BlackMisc::PhysicalQuantities::CAngle(static_cast(*this)); + } + + /*! + * Register metadata + */ + static void registerMetadata(); + + /*! + * \brief Latitude / Longitude from a WGS string such as + * \param wgsCoordinate 50° 2′ 0″ N / 8° 34′ 14″ E + * \return + */ + static LATorLON fromWgs84(const QString &wgsCoordinate); + }; + + } // namespace } // namespace #endif // guard diff --git a/src/blackmisc/geolatitude.h b/src/blackmisc/geolatitude.h index 82fb40285..7f805f94c 100644 --- a/src/blackmisc/geolatitude.h +++ b/src/blackmisc/geolatitude.h @@ -7,50 +7,50 @@ namespace BlackMisc { -namespace Geo -{ - -/*! - * \brief Latitude - */ -class CLatitude : public CEarthAngle -{ -protected: - /*! - * \brief Specific string representation - */ - virtual QString convertToQString() const + namespace Geo { - QString s = "latitude "; - return s.append(CEarthAngle::convertToQString()); - } -public: - /*! - * \brief Default constructor - */ - CLatitude() : CEarthAngle() {} + /*! + * \brief Latitude + */ + class CLatitude : public CEarthAngle + { + protected: + /*! + * \brief Specific string representation + */ + virtual QString convertToQString() const + { + QString s = "latitude "; + return s.append(CEarthAngle::convertToQString()); + } - /*! - * \brief Constructor - * \param angle - */ - explicit CLatitude(const BlackMisc::PhysicalQuantities::CAngle &angle) : CEarthAngle(angle) {} + public: + /*! + * \brief Default constructor + */ + CLatitude() : CEarthAngle() {} - /*! - * \brief Init by double value - * \param value - * \param unit - */ - CLatitude(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : CEarthAngle(value, unit) {} + /*! + * \brief Constructor + * \param angle + */ + explicit CLatitude(const BlackMisc::PhysicalQuantities::CAngle &angle) : CEarthAngle(angle) {} - /*! - * \brief Virtual destructor - */ - virtual ~CLatitude() {} -}; + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CLatitude(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : CEarthAngle(value, unit) {} -} // namespace + /*! + * \brief Virtual destructor + */ + virtual ~CLatitude() {} + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Geo::CLatitude) diff --git a/src/blackmisc/geolongitude.h b/src/blackmisc/geolongitude.h index 64880ef51..2f09018c0 100644 --- a/src/blackmisc/geolongitude.h +++ b/src/blackmisc/geolongitude.h @@ -4,50 +4,50 @@ namespace BlackMisc { -namespace Geo -{ - -/*! - * \brief Longitude - */ -class CLongitude : public CEarthAngle -{ -protected: - /*! - * \brief Specific string representation - */ - virtual QString convertToQString() const + namespace Geo { - QString s = "longitude "; - return s.append(CEarthAngle::convertToQString()); - } -public: - /*! - * \brief Default constructor - */ - CLongitude() : CEarthAngle() {} + /*! + * \brief Longitude + */ + class CLongitude : public CEarthAngle + { + protected: + /*! + * \brief Specific string representation + */ + virtual QString convertToQString() const + { + QString s = "longitude "; + return s.append(CEarthAngle::convertToQString()); + } - /*! - * \brief Constructor - * \param angle - */ - explicit CLongitude(const BlackMisc::PhysicalQuantities::CAngle &angle) : CEarthAngle(angle) {} + public: + /*! + * \brief Default constructor + */ + CLongitude() : CEarthAngle() {} - /*! - * \brief Init by double value - * \param value - * \param unit - */ - CLongitude(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : CEarthAngle(value, unit) {} + /*! + * \brief Constructor + * \param angle + */ + explicit CLongitude(const BlackMisc::PhysicalQuantities::CAngle &angle) : CEarthAngle(angle) {} - /*! - * \brief Virtual destructor - */ - virtual ~CLongitude() {} -}; + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CLongitude(double value, const BlackMisc::PhysicalQuantities::CAngleUnit &unit) : CEarthAngle(value, unit) {} -} // namespace + /*! + * \brief Virtual destructor + */ + virtual ~CLongitude() {} + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Geo::CLongitude) diff --git a/src/blackmisc/mathematics.cpp b/src/blackmisc/mathematics.cpp index 0dd1bed84..5cde31126 100644 --- a/src/blackmisc/mathematics.cpp +++ b/src/blackmisc/mathematics.cpp @@ -9,59 +9,59 @@ namespace BlackMisc { -namespace Math -{ + namespace Math + { + /* + * Hypotenuse + */ + double CMath::hypot(double x, double y) + { + x = qAbs(x); + y = qAbs(y); + double max = std::max(x, y); + double min = std::min(x, y); + double r = min / max; + return max * sqrt(1 + r * r); + } -/* - * Hypotenuse - */ -double CMath::hypot(double x, double y) -{ - x = qAbs(x); - y = qAbs(y); - double max = std::max(x, y); - double min = std::min(x, y); - double r = min / max; - return max * sqrt(1 + r * r); -} + /* + * Real part of cubic root + */ + double CMath::cubicRootReal(double x) + { + double result; + result = std::pow(qAbs(x), (double)1.0 / 3.0); + return x < 0 ? -result : result; + } -/* - * Real part of cubic root - */ -double CMath::cubicRootReal(double x) -{ - double result; - result = std::pow(qAbs(x), (double)1.0 / 3.0); - return x < 0 ? -result : result; -} + /* + * Round utility method + */ + double CMath::round(double value, int digits) + { + // gosh, is there no Qt method for this??? It's year 2013 + double fractpart, intpart; + fractpart = modf(value, &intpart); + if (fractpart == 0) return value; // do not mess any "integers" to the worse + double m = pow(10.0, digits); + qint64 ri = qRound64(value * m); // do not loose any range here + double rv = double(ri) / m; + return rv; + } -/* - * Round utility method - */ -double CMath::round(double value, int digits) -{ - // gosh, is there no Qt method for this??? It's year 2013 - double fractpart, intpart; - fractpart = modf(value, &intpart); - if (fractpart == 0) return value; // do not mess any "integers" to the worse - double m = pow(10.0, digits); - qint64 ri = qRound64(value * m); // do not loose any range here - double rv = double(ri) / m; - return rv; -} + /* + * Round by given epsilon value + */ + double CMath::roundEpsilon(double value, double epsilon) + { + double fractpart, intpart; + fractpart = modf(value, &intpart); + if (fractpart == 0) return value; // do not mess any "integers" to the worse + qint64 ri = qRound(value / epsilon); + double rv = double(ri) * epsilon; // do not loose any range here + return rv; + } -/* - * Round by given epsilon value - */ -double CMath::roundEpsilon(double value, double epsilon) -{ - double fractpart, intpart; - fractpart = modf(value, &intpart); - if (fractpart == 0) return value; // do not mess any "integers" to the worse - qint64 ri = qRound(value / epsilon); - double rv = double(ri) * epsilon; // do not loose any range here - return rv; -} -} // namespace + } // namespace } // namespace diff --git a/src/blackmisc/mathematics.h b/src/blackmisc/mathematics.h index fa0eeada8..94ac96d5b 100644 --- a/src/blackmisc/mathematics.h +++ b/src/blackmisc/mathematics.h @@ -10,124 +10,124 @@ namespace BlackMisc { -namespace Math -{ - -/*! - * \brief Math utils - */ -class CMath -{ -public: - - /*! - * \brief Calculates the hypotenuse of x and y without overflow - * \param x - * \param y - * \return - */ - static double hypot(double x, double y); - - /*! - * \brief Calculates the square of x - * \param x - * \return - */ - static inline double square(double x) + namespace Math { - return x * x; - } - /*! - * \brief Calculates x to the power of three - * \param x - * \return - */ - static inline double cubic(const double x) - { - return x * x * x; - } + /*! + * \brief Math utils + */ + class CMath + { + public: - /*! - * \brief Calculates the real cubic root - * \param x - * \return - */ - static double cubicRootReal(double x); + /*! + * \brief Calculates the hypotenuse of x and y without overflow + * \param x + * \param y + * \return + */ + static double hypot(double x, double y); - /*! - * \brief Utility round method - * \param value - * \param digits - * \return - */ - static double round(double value, int digits); + /*! + * \brief Calculates the square of x + * \param x + * \return + */ + static inline double square(double x) + { + return x * x; + } - /*! - * \brief Round by given epsilon, e.g. - * \param value - * \param epsilon - * \return - */ - static double roundEpsilon(double value, double epsilon); + /*! + * \brief Calculates x to the power of three + * \param x + * \return + */ + static inline double cubic(const double x) + { + return x * x * x; + } - /*! - * \brief Nearest integer not greater in magnitude than value, correcting for epsilon - * \param value - * \param epsilon - */ - static inline double trunc(double value, double epsilon = 1e-10) - { - return value < 0 ? ceil(value - epsilon) : floor(value + epsilon); - } + /*! + * \brief Calculates the real cubic root + * \param x + * \return + */ + static double cubicRootReal(double x); - /*! - * \brief Fractional part of value - * \param value - */ - static inline double fract(double value) - { - double unused; - return modf(value, &unused); - } + /*! + * \brief Utility round method + * \param value + * \param digits + * \return + */ + static double round(double value, int digits); - /*! - * \brief PI - * \return - */ - static const double &PIHALF() - { - static double pi = 2.0 * qAtan(1.0); - return pi; - } + /*! + * \brief Round by given epsilon, e.g. + * \param value + * \param epsilon + * \return + */ + static double roundEpsilon(double value, double epsilon); - /*! - * \brief PI - * \return - */ - static const double &PI() - { - static double pi = 4.0 * qAtan(1.0); - return pi; - } + /*! + * \brief Nearest integer not greater in magnitude than value, correcting for epsilon + * \param value + * \param epsilon + */ + static inline double trunc(double value, double epsilon = 1e-10) + { + return value < 0 ? ceil(value - epsilon) : floor(value + epsilon); + } - /*! - * \brief PI * 2 - * \return - */ - static const double &PI2() - { - static double pi2 = 8.0 * qAtan(1.0); - return pi2; - } + /*! + * \brief Fractional part of value + * \param value + */ + static inline double fract(double value) + { + double unused; + return modf(value, &unused); + } -private: - /*! - * \brief Deleted - */ - CMath(); -}; + /*! + * \brief PI + * \return + */ + static const double &PIHALF() + { + static double pi = 2.0 * qAtan(1.0); + return pi; + } -} // namespace + /*! + * \brief PI + * \return + */ + static const double &PI() + { + static double pi = 4.0 * qAtan(1.0); + return pi; + } + + /*! + * \brief PI * 2 + * \return + */ + static const double &PI2() + { + static double pi2 = 8.0 * qAtan(1.0); + return pi2; + } + + private: + /*! + * \brief No objects, just static + */ + CMath(); + }; + + } // namespace } // namespace #endif // guard diff --git a/src/blackmisc/mathmatrix1x3.h b/src/blackmisc/mathmatrix1x3.h index fede41204..872adf999 100644 --- a/src/blackmisc/mathmatrix1x3.h +++ b/src/blackmisc/mathmatrix1x3.h @@ -11,47 +11,47 @@ namespace BlackMisc { -namespace Math -{ - -/*! - * \brief Matrix 1x3 - */ -class CMatrix1x3 : public CMatrixBase -{ -public: - /*! - * \brief Constructor - */ - CMatrix1x3() : CMatrixBase() {} - - /*! - * \brief Copy constructor - * \param other - */ - CMatrix1x3(const CMatrix1x3 &other) : CMatrixBase(other) {} - - /*! - * \brief Init by fill value - * \param fillValue - */ - explicit CMatrix1x3(double fillValue) : CMatrixBase(fillValue) {} - - /*! - * \brief Constructor - * \param c1 - * \param c2 - * \param c3 - */ - CMatrix1x3(double c1, double c2, double c3) : CMatrixBase() + namespace Math { - this->m_matrix(0, 0) = c1; - this->m_matrix(0, 1) = c2; - this->m_matrix(0, 2) = c3; - } -}; -} // namespace + /*! + * \brief Matrix 1x3 + */ + class CMatrix1x3 : public CMatrixBase + { + public: + /*! + * \brief Constructor + */ + CMatrix1x3() : CMatrixBase() {} + + /*! + * \brief Copy constructor + * \param other + */ + CMatrix1x3(const CMatrix1x3 &other) : CMatrixBase(other) {} + + /*! + * \brief Init by fill value + * \param fillValue + */ + explicit CMatrix1x3(double fillValue) : CMatrixBase(fillValue) {} + + /*! + * \brief Constructor + * \param c1 + * \param c2 + * \param c3 + */ + CMatrix1x3(double c1, double c2, double c3) : CMatrixBase() + { + this->m_matrix(0, 0) = c1; + this->m_matrix(0, 1) = c2; + this->m_matrix(0, 2) = c3; + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Math::CMatrix1x3) diff --git a/src/blackmisc/mathmatrix3x1.h b/src/blackmisc/mathmatrix3x1.h index b28bbc979..d6578d1c9 100644 --- a/src/blackmisc/mathmatrix3x1.h +++ b/src/blackmisc/mathmatrix3x1.h @@ -11,71 +11,72 @@ namespace BlackMisc { -namespace Math -{ - -class CMatrix3x3; - -/*! - * \brief Matrix 3x1 - */ -class CMatrix3x1 : public CMatrixBase -{ - friend class CMatrix3x3; - -public: - /*! - * \brief Constructor - */ - CMatrix3x1() : CMatrixBase() {} - - /*! - * \brief Constructor - * \param r1 - * \param r2 - * \param r3 - */ - CMatrix3x1(double r1, double r2, double r3) : CMatrixBase() + namespace Math { - this->m_matrix(0, 0) = r1; - this->m_matrix(1, 0) = r2; - this->m_matrix(2, 0) = r3; - } - /*! - * \brief Copy constructor - * \param otherMatrix - */ - CMatrix3x1(const CMatrix3x1 &other) : CMatrixBase(other) {} + class CMatrix3x3; - /*! - * \brief Init by fill value - * \param fillValue - */ - explicit CMatrix3x1(double fillValue) : CMatrixBase(fillValue) {} + /*! + * \brief Matrix 3x1 + */ + class CMatrix3x1 : public CMatrixBase + { + friend class CMatrix3x3; - /*! - * \brief Convert to vector - * \return - */ - CVector3D toVector3D() const - { - return CVector3D(this->getElement(0, 0), this->getElement(1, 0), this->getElement(2, 0)); - } + public: + /*! + * \brief Constructor + */ + CMatrix3x1() : CMatrixBase() {} - /*! - * \brief Convert from vector - * \return - */ - void fromVector3D(const CVector3D &vector) - { - this->m_matrix(0, 0) = vector.i(); - this->m_matrix(1, 0) = vector.j(); - this->m_matrix(2, 0) = vector.k(); - } -}; + /*! + * \brief Constructor + * \param r1 + * \param r2 + * \param r3 + */ + CMatrix3x1(double r1, double r2, double r3) : CMatrixBase() + { + this->m_matrix(0, 0) = r1; + this->m_matrix(1, 0) = r2; + this->m_matrix(2, 0) = r3; + } -} // namespace + /*! + * \brief Copy constructor + * \param otherMatrix + */ + CMatrix3x1(const CMatrix3x1 &other) : CMatrixBase(other) {} + + /*! + * \brief Init by fill value + * \param fillValue + */ + explicit CMatrix3x1(double fillValue) : CMatrixBase(fillValue) {} + + + /*! + * \brief Convert to vector + * \return + */ + CVector3D toVector3D() const + { + return CVector3D(this->getElement(0, 0), this->getElement(1, 0), this->getElement(2, 0)); + } + + /*! + * \brief Convert from vector + * \return + */ + void fromVector3D(const CVector3D &vector) + { + this->m_matrix(0, 0) = vector.i(); + this->m_matrix(1, 0) = vector.j(); + this->m_matrix(2, 0) = vector.k(); + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Math::CMatrix3x1) diff --git a/src/blackmisc/mathmatrix3x3.cpp b/src/blackmisc/mathmatrix3x3.cpp index fed2ad73f..3f8e5e37a 100644 --- a/src/blackmisc/mathmatrix3x3.cpp +++ b/src/blackmisc/mathmatrix3x3.cpp @@ -7,73 +7,74 @@ namespace BlackMisc { -namespace Math -{ - -/* - * Determinant - */ -double CMatrix3x3::determinant() const -{ - return - this->m_matrix(0, 0) * this->m_matrix(1, 1) * this->m_matrix(2, 2) + - this->m_matrix(0, 1) * this->m_matrix(1, 2) * this->m_matrix(2, 0) + - this->m_matrix(0, 2) * this->m_matrix(1, 0) * this->m_matrix(2, 1) - - this->m_matrix(0, 1) * this->m_matrix(1, 0) * this->m_matrix(2, 2) - - this->m_matrix(0, 2) * this->m_matrix(1, 1) * this->m_matrix(2, 0) - - this->m_matrix(0, 0) * this->m_matrix(1, 2) * this->m_matrix(2, 1); -} - -/* - * Inverse - */ -CMatrix3x3 CMatrix3x3::inverse(bool &o_invertible) const -{ - CMatrix3x3 inverse; - double det = this->determinant(); - if (det == 0) + namespace Math { - o_invertible = false; - inverse.setZero(); - return inverse; - } - double invdet = 1.0 / det; - inverse.m_matrix(0, 0) = (this->m_matrix(1, 1) * this->m_matrix(2, 2) - this->m_matrix(1, 2) * this->m_matrix(2, 1)) * invdet; - inverse.m_matrix(0, 1) = (- this->m_matrix(0, 1) * this->m_matrix(2, 2) + this->m_matrix(0, 2) * this->m_matrix(2, 1)) * invdet; - inverse.m_matrix(0, 2) = (this->m_matrix(0, 1) * this->m_matrix(1, 2) - this->m_matrix(0, 2) * this->m_matrix(1, 1)) * invdet; - inverse.m_matrix(1, 0) = (- this->m_matrix(1, 0) * this->m_matrix(2, 2) + this->m_matrix(1, 2) * this->m_matrix(2, 0)) * invdet; - inverse.m_matrix(1, 1) = (this->m_matrix(0, 0) * this->m_matrix(2, 2) - this->m_matrix(0, 2) * this->m_matrix(2, 0)) * invdet; - inverse.m_matrix(1, 2) = (- this->m_matrix(0, 0) * this->m_matrix(1, 2) + this->m_matrix(0, 2) * this->m_matrix(1, 0)) * invdet; - inverse.m_matrix(2, 0) = (this->m_matrix(1, 0) * this->m_matrix(2, 1) - this->m_matrix(1, 1) * this->m_matrix(2, 0)) * invdet; - inverse.m_matrix(2, 1) = (- this->m_matrix(0, 0) * this->m_matrix(2, 1) + this->m_matrix(0, 1) * this->m_matrix(2, 0)) * invdet; - inverse.m_matrix(2, 2) = (this->m_matrix(0, 0) * this->m_matrix(1, 1) - this->m_matrix(0, 1) * this->m_matrix(1, 0)) * invdet; + /* + * Determinant + */ + double CMatrix3x3::determinant() const + { + return + this->m_matrix(0, 0) * this->m_matrix(1, 1) * this->m_matrix(2, 2) + + this->m_matrix(0, 1) * this->m_matrix(1, 2) * this->m_matrix(2, 0) + + this->m_matrix(0, 2) * this->m_matrix(1, 0) * this->m_matrix(2, 1) - + this->m_matrix(0, 1) * this->m_matrix(1, 0) * this->m_matrix(2, 2) - + this->m_matrix(0, 2) * this->m_matrix(1, 1) * this->m_matrix(2, 0) - + this->m_matrix(0, 0) * this->m_matrix(1, 2) * this->m_matrix(2, 1); + } - o_invertible = true; - return inverse; -} + /* + * Inverse + */ + CMatrix3x3 CMatrix3x3::inverse(bool &o_invertible) const + { + CMatrix3x3 inverse; + double det = this->determinant(); + if (det == 0) + { + o_invertible = false; + inverse.setZero(); + return inverse; + } + double invdet = 1.0 / det; -/* - * Get a row - */ -CMatrix1x3 CMatrix3x3::getRow(int row) const -{ - bool valid = row >= 0 && row <= 3; - Q_ASSERT_X(valid, "getRow", "invalid row"); - if (!valid) throw std::range_error("invalid row"); - return CMatrix1x3(this->getElement(row, 0), this->getElement(row, 1), this->getElement(row, 2)); -} + inverse.m_matrix(0, 0) = (this->m_matrix(1, 1) * this->m_matrix(2, 2) - this->m_matrix(1, 2) * this->m_matrix(2, 1)) * invdet; + inverse.m_matrix(0, 1) = (- this->m_matrix(0, 1) * this->m_matrix(2, 2) + this->m_matrix(0, 2) * this->m_matrix(2, 1)) * invdet; + inverse.m_matrix(0, 2) = (this->m_matrix(0, 1) * this->m_matrix(1, 2) - this->m_matrix(0, 2) * this->m_matrix(1, 1)) * invdet; + inverse.m_matrix(1, 0) = (- this->m_matrix(1, 0) * this->m_matrix(2, 2) + this->m_matrix(1, 2) * this->m_matrix(2, 0)) * invdet; + inverse.m_matrix(1, 1) = (this->m_matrix(0, 0) * this->m_matrix(2, 2) - this->m_matrix(0, 2) * this->m_matrix(2, 0)) * invdet; + inverse.m_matrix(1, 2) = (- this->m_matrix(0, 0) * this->m_matrix(1, 2) + this->m_matrix(0, 2) * this->m_matrix(1, 0)) * invdet; + inverse.m_matrix(2, 0) = (this->m_matrix(1, 0) * this->m_matrix(2, 1) - this->m_matrix(1, 1) * this->m_matrix(2, 0)) * invdet; + inverse.m_matrix(2, 1) = (- this->m_matrix(0, 0) * this->m_matrix(2, 1) + this->m_matrix(0, 1) * this->m_matrix(2, 0)) * invdet; + inverse.m_matrix(2, 2) = (this->m_matrix(0, 0) * this->m_matrix(1, 1) - this->m_matrix(0, 1) * this->m_matrix(1, 0)) * invdet; -/* - * Get a column - */ -CMatrix3x1 CMatrix3x3::getColumn(int column) const -{ - bool valid = column >= 0 && column <= 3; - Q_ASSERT_X(valid, "getColumn", "invalid column"); - if (!valid) throw new std::range_error("invalid column"); - return CMatrix3x1(this->getElement(0, column), this->getElement(1, column), this->getElement(2, column)); -} + o_invertible = true; + return inverse; + } -} // namespace + /* + * Get a row + */ + CMatrix1x3 CMatrix3x3::getRow(int row) const + { + bool valid = row >= 0 && row <= 3; + Q_ASSERT_X(valid, "getRow", "invalid row"); + if (!valid) throw std::range_error("invalid row"); + return CMatrix1x3(this->getElement(row, 0), this->getElement(row, 1), this->getElement(row, 2)); + } + + /* + * Get a column + */ + CMatrix3x1 CMatrix3x3::getColumn(int column) const + { + bool valid = column >= 0 && column <= 3; + Q_ASSERT_X(valid, "getColumn", "invalid column"); + if (!valid) throw new std::range_error("invalid column"); + return CMatrix3x1(this->getElement(0, column), this->getElement(1, column), this->getElement(2, column)); + } + + + } // namespace } // namespace diff --git a/src/blackmisc/mathmatrix3x3.h b/src/blackmisc/mathmatrix3x3.h index f21a1d7be..9160c8dbc 100644 --- a/src/blackmisc/mathmatrix3x3.h +++ b/src/blackmisc/mathmatrix3x3.h @@ -11,166 +11,166 @@ namespace BlackMisc { -namespace Math -{ - -/*! - * \brief 3x3 matrix - */ -class CMatrix3x3 : public CMatrixBase -{ -public: - /*! - * \brief CMatrix3D - */ - CMatrix3x3() : CMatrixBase() {} - - /*! - * \brief Copy constructor - * \param other - */ - CMatrix3x3(const CMatrix3x3 &other) : CMatrixBase(other) {} - - /*! - * \brief Init by fill value - * \param fillValue - */ - explicit CMatrix3x3(double fillValue) : CMatrixBase(fillValue) {} - - /*! - * \brief Stupid but handy constructor - * \param r1c1 - * \param r1c2 - * \param r1c3 - * \param r2c1 - * \param r2c2 - * \param r2c3 - * \param r3c1 - * \param r3c2 - * \param r3c3 - */ - explicit CMatrix3x3(double r1c1, double r1c2, double r1c3, - double r2c1, double r2c2, double r2c3, - double r3c1, double r3c2, double r3c3) : CMatrixBase() + namespace Math { - this->setElement(0, 0, r1c1); - this->setElement(0, 1, r1c2); - this->setElement(0, 2, r1c3); - this->setElement(1, 0, r2c1); - this->setElement(1, 1, r2c2); - this->setElement(1, 2, r2c3); - this->setElement(2, 0, r3c1); - this->setElement(2, 1, r3c2); - this->setElement(2, 2, r3c3); - } - /*! - * \brief Calculates the determinant of the matrix - * \return - */ - double determinant() const; + /*! + * \brief 3x3 matrix + */ + class CMatrix3x3 : public CMatrixBase + { + public: + /*! + * \brief CMatrix3D + */ + CMatrix3x3() : CMatrixBase() {} - /*! - * \brief Calculate the inverse - * \param[out] o_isInvertible - * \return - */ - CMatrix3x3 inverse(bool &o_isInvertible) const; + /*! + * \brief Copy constructor + * \param other + */ + CMatrix3x3(const CMatrix3x3 &other) : CMatrixBase(other) {} - /*! - * \brief Operator *= - * \param other - * \return - */ - CMatrix3x3 &operator *=(const CMatrix3x3 &other) - { - this->m_matrix = this->m_matrix * other.m_matrix; - return *this; - } - /*! - * \brief Operator * - * \param other - * \return - */ - CMatrix3x3 operator *(const CMatrix3x3 &other) const - { - CMatrix3x3 m(*this); - m *= other; - return m; - } + /*! + * \brief Init by fill value + * \param fillValue + */ + explicit CMatrix3x3(double fillValue) : CMatrixBase(fillValue) {} - /*! - * \brief Operator * - * \param other - * \return - */ - CMatrix3x1 operator *(const CMatrix3x1 &other) const - { - CMatrix3x1 m; - m.m_matrix = this->m_matrix * other.m_matrix; - return m; - } + /*! + * \brief Stupid but handy constructor + * \param r1c1 + * \param r1c2 + * \param r1c3 + * \param r2c1 + * \param r2c2 + * \param r2c3 + * \param r3c1 + * \param r3c2 + * \param r3c3 + */ + explicit CMatrix3x3(double r1c1, double r1c2, double r1c3, + double r2c1, double r2c2, double r2c3, + double r3c1, double r3c2, double r3c3) : CMatrixBase() + { + this->setElement(0, 0, r1c1); + this->setElement(0, 1, r1c2); + this->setElement(0, 2, r1c3); + this->setElement(1, 0, r2c1); + this->setElement(1, 1, r2c2); + this->setElement(1, 2, r2c3); + this->setElement(2, 0, r3c1); + this->setElement(2, 1, r3c2); + this->setElement(2, 2, r3c3); + } - /*! - * \brief Multiply this matrix with vector - * \param vector - * \return - */ - CVector3D operator *(const CVector3D &vector) const - { - return ((*this) * CMatrix3x1(vector.toMatrix3x1())).toVector3D(); - } + /*! + * \brief Calculates the determinant of the matrix + * \return + */ + double determinant() const; - /*! - * \brief Multiply with factor - * \param factor - * \return - */ - CMatrix3x3 &operator *=(double factor) - { - this->CMatrixBase::operator *=(factor); - return *this; - } + /*! + * \brief Calculate the inverse + * \param[out] o_isInvertible + * \return + */ + CMatrix3x3 inverse(bool &o_isInvertible) const; - /*! - * \brief Multiply with factor - * \param factor - * \return - */ - CMatrix3x3 operator *(double factor) const - { - CMatrix3x3 m(*this); - m *= factor; - return m; - } + /*! + * \brief Operator *= + * \param other + * \return + */ + CMatrix3x3 &operator *=(const CMatrix3x3 &other) + { + this->m_matrix = this->m_matrix * other.m_matrix; + return *this; + } + /*! + * \brief Operator * + * \param other + * \return + */ + CMatrix3x3 operator *(const CMatrix3x3 &other) const + { + CMatrix3x3 m(*this); + m *= other; + return m; + } - /*! - * \brief Transposed matrix - * \return - */ - CMatrix3x3 transposed() const - { - CMatrix3x3 m(0); - m.m_matrix = this->m_matrix.transposed(); - return m; - } + /*! + * \brief Operator * + * \param other + * \return + */ + CMatrix3x1 operator *(const CMatrix3x1 &other) const + { + CMatrix3x1 m; + m.m_matrix = this->m_matrix * other.m_matrix; + return m; + } - /*! - * \brief Get column - * \param column - * \return - */ - CMatrix3x1 getColumn(int column) const; + /*! + * \brief Multiply this matrix with vector + * \param vector + * \return + */ + CVector3D operator *(const CVector3D &vector) const + { + return ((*this) * CMatrix3x1(vector.toMatrix3x1())).toVector3D(); + } - /*! - * \brief Get row - * \param row - * \return - */ - CMatrix1x3 getRow(int row) const; -}; + /*! + * \brief Multiply with factor + * \param factor + * \return + */ + CMatrix3x3 &operator *=(double factor) + { + this->CMatrixBase::operator *=(factor); + return *this; + } -} // namespace + /*! + * \brief Multiply with factor + * \param factor + * \return + */ + CMatrix3x3 operator *(double factor) const + { + CMatrix3x3 m(*this); + m *= factor; + return m; + } + + /*! + * \brief Transposed matrix + * \return + */ + CMatrix3x3 transposed() const + { + CMatrix3x3 m(0); + m.m_matrix = this->m_matrix.transposed(); + return m; + } + + /*! + * \brief Get column + * \param column + * \return + */ + CMatrix3x1 getColumn(int column) const; + + /*! + * \brief Get row + * \param row + * \return + */ + CMatrix1x3 getRow(int row) const; + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Math::CMatrix3x3) diff --git a/src/blackmisc/mathmatrixbase.cpp b/src/blackmisc/mathmatrixbase.cpp index d0a7b01b8..82bbdaac2 100644 --- a/src/blackmisc/mathmatrixbase.cpp +++ b/src/blackmisc/mathmatrixbase.cpp @@ -5,152 +5,183 @@ #include "blackmisc/mathmatrix3x3.h" #include "blackmisc/mathmatrix3x1.h" +#include "blackmisc/blackmiscfreefunctions.h" #include namespace BlackMisc { -namespace Math -{ - -/* - * Get element by column / row - */ -template double CMatrixBase::getElement(size_t row, size_t column) const -{ - this->checkRange(row, column); - return this->m_matrix(row, column); -} - -/* - * Set element by column / row - */ -template void CMatrixBase::setElement(size_t row, size_t column, double value) -{ - this->checkRange(row, column); - this->m_matrix(row, column) = value; -} - -/* - * Check range - */ -template void CMatrixBase::checkRange(size_t row, size_t column) const -{ - bool valid = (row < Rows && column < Columns); - Q_ASSERT_X(valid, "getElement()", "Row or column invalid"); - if (!valid) throw std::range_error("Row or column invalid"); -} - -/* - * All values zero? - */ -template bool CMatrixBase::isZero() const -{ - for (int r = 0; r < Rows; r++) + namespace Math { - for (int c = 0; c < Columns; c++) + + /* + * Get element by column / row + */ + template double CMatrixBase::getElement(size_t row, size_t column) const { - if (this->m_matrix(r, c) != 0) return false; + this->checkRange(row, column); + return this->m_matrix(row, column); } - } - return true; -} - -/* - * Set cell index - */ -template void CMatrixBase::setCellIndex() -{ - for (int r = 0; r < Rows; r++) - { - for (int c = 0; c < Columns; c++) + /* + * Set element by column / row + */ + template void CMatrixBase::setElement(size_t row, size_t column, double value) { - this->m_matrix(r, c) = r + 0.1 * c; + this->checkRange(row, column); + this->m_matrix(row, column) = value; } - } -} -/* - * To list - */ -template QList CMatrixBase::toList() const -{ - QList list; - for (int r = 0; r < Rows; r++) - { - for (int c = 0; c < Columns; c++) + /* + * Check range + */ + template void CMatrixBase::checkRange(size_t row, size_t column) const { - list.append(this->m_matrix(r, c)); + bool valid = (row < Rows && column < Columns); + Q_ASSERT_X(valid, "getElement()", "Row or column invalid"); + if (!valid) throw std::range_error("Row or column invalid"); } - } - return list; -} -/* - * From list - */ -template void CMatrixBase::fromList(const QList &list) -{ - Q_ASSERT_X(Rows * Columns == list.count(), "fromList()", "Mismatch of elements in list"); - int ct = 0; - for (int r = 0; r < Rows; r++) - { - for (int c = 0; c < Columns; c++) + /* + * All values zero? + */ + template bool CMatrixBase::isZero() const { - this->m_matrix(r, c) = list.at(ct++); + for (int r = 0; r < Rows; r++) + { + for (int c = 0; c < Columns; c++) + { + if (this->m_matrix(r, c) != 0) return false; + } + } + return true; } - } -} -/* - * Round all values - */ -template void CMatrixBase::round() -{ - for (int r = 0; r < Rows; r++) - { - for (int c = 0; c < Columns; c++) + + /* + * Set cell index + */ + template void CMatrixBase::setCellIndex() { - this->m_matrix(r, c) = CMath::roundEpsilon(this->m_matrix(r, c), 1E-10); + for (int r = 0; r < Rows; r++) + { + for (int c = 0; c < Columns; c++) + { + this->m_matrix(r, c) = r + 0.1 * c; + } + } } - } -} -/* - * Convert to string - */ -template QString CMatrixBase::convertToQString(bool /* i18n */) const -{ - QString s = "{"; - for (int r = 0; r < Rows; r++) - { - s = s.append("{"); - for (int c = 0; c < Columns; c++) + /* + * To list + */ + template QList CMatrixBase::toList() const { - QString n = QString::number(this->m_matrix(r, c), 'f', 2); - if (c > 0) s = s.append(","); - s = s.append(n); + QList list; + for (int r = 0; r < Rows; r++) + { + for (int c = 0; c < Columns; c++) + { + list.append(this->m_matrix(r, c)); + } + } + return list; } - s = s.append("}"); - } - s = s.append("}"); - return s; -} -/* - * Register metadata - */ -template void CMatrixBase::registerMetadata() -{ - qRegisterMetaType(typeid(ImplMatrix).name()); - qDBusRegisterMetaType(); -} + /* + * From list + */ + template void CMatrixBase::fromList(const QList &list) + { + Q_ASSERT_X(Rows * Columns == list.count(), "fromList()", "Mismatch of elements in list"); + int ct = 0; + for (int r = 0; r < Rows; r++) + { + for (int c = 0; c < Columns; c++) + { + this->m_matrix(r, c) = list.at(ct++); + } + } + } -// see here for the reason of thess forward instantiations -// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html -template class CMatrixBase; -template class CMatrixBase; -template class CMatrixBase; + /* + * Round all values + */ + template void CMatrixBase::round() + { + for (int r = 0; r < Rows; r++) + { + for (int c = 0; c < Columns; c++) + { + this->m_matrix(r, c) = CMath::roundEpsilon(this->m_matrix(r, c), 1E-10); + } + } + } -} // namespace + /* + * Convert to string + */ + template QString CMatrixBase::convertToQString(bool /* i18n */) const + { + QString s = "{"; + for (int r = 0; r < Rows; r++) + { + s = s.append("{"); + for (int c = 0; c < Columns; c++) + { + QString n = QString::number(this->m_matrix(r, c), 'f', 2); + if (c > 0) s = s.append(","); + s = s.append(n); + } + s = s.append("}"); + } + s = s.append("}"); + return s; + } + + /* + * To DBus + */ + template void CMatrixBase::marshallToDbus(QDBusArgument &argument) const + { + const QList l = this->toList(); + + // there is an issue with the signature of QList, so I use + // individual values + foreach(double v, l) + { + argument << v; + } + } + + /* + * From DBus + */ + template void CMatrixBase::unmarshallFromDbus(const QDBusArgument &argument) + { + QList list; + double v; + while (!argument.atEnd()) + { + argument >> v; + list.append(v); + } + this->fromList(list); + } + + /* + * Register metadata + */ + template void CMatrixBase::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + // see here for the reason of thess forward instantiations + // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html + template class CMatrixBase; + template class CMatrixBase; + template class CMatrixBase; + + } // namespace } // namespace diff --git a/src/blackmisc/mathmatrixbase.h b/src/blackmisc/mathmatrixbase.h index 48a9e310b..35c942fde 100644 --- a/src/blackmisc/mathmatrixbase.h +++ b/src/blackmisc/mathmatrixbase.h @@ -13,369 +13,345 @@ namespace BlackMisc { -namespace Math -{ - -/*! - * \brief Base functionality of a matrix - */ -template class CMatrixBase : public BlackMisc::CStreamable -{ -private: - /*! - * \brief Easy access to derived class (CRTP template parameter) - * \return - */ - ImplMatrix const *derived() const + namespace Math { - return static_cast(this); - } - /*! - * \brief Easy access to derived class (CRTP template parameter) - * \return - */ - ImplMatrix *derived() - { - return static_cast(this); - } + /*! + * \brief Base functionality of a matrix + */ + template class CMatrixBase : public BlackMisc::CStreamable + { + private: + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + ImplMatrix const *derived() const + { + return static_cast(this); + } -protected: - // no bug, Qt expects columns rows - QGenericMatrix m_matrix; //!< backing data + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + ImplMatrix *derived() + { + return static_cast(this); + } - /*! - * \brief Conversion to string - * \param i18n - * \return - */ - QString convertToQString(bool i18n = false) const; + protected: + // no bug, Qt expects columns rows + QGenericMatrix m_matrix; //!< backing data - /*! - * \brief Stream to DBus - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - const QList l = this->toList(); + /*! + * \brief Conversion to string + * \param i18n + * \return + */ + QString convertToQString(bool i18n = false) const; - // there is an issue with the signature of QList, so I use - // individual values - foreach(double v, l) { - argument << v; - } - } + /*! + * \brief Stream to DBus + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QList list; - double v; - while(!argument.atEnd()) { - argument >> v; - list.append(v); - } - this->fromList(list); - } + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); -public: - /*! - * \brief Default constructor - */ - CMatrixBase() : m_matrix() {} + public: + /*! + * \brief Default constructor + */ + CMatrixBase() : m_matrix() {} - /*! - * \brief Copy constructor - * \param other - */ - CMatrixBase(const CMatrixBase &other) : m_matrix(other.m_matrix) {} + /*! + * \brief Fill with value + * \param fillValue + */ + explicit CMatrixBase(double fillValue) : m_matrix() + { + this->fill(fillValue); + } - /*! - * \brief Fill with value - * \param fillValue - */ - explicit CMatrixBase(double fillValue) : m_matrix() - { - this->fill(fillValue); - } + /*! + * \brief Virtual destructor + */ + virtual ~CMatrixBase() {} - /*! - * \brief Virtual destructor - */ - virtual ~CMatrixBase() {} + /*! + * \brief List of values + * \return + */ + QList toList() const; - /*! - * \brief List of values - * \return - */ - QList toList() const; + /*! + * \brief List of values + * \return + */ + void fromList(const QList &list); - /*! - * \brief List of values - * \return - */ - void fromList(const QList &list); + /*! + * \brief Equal operator == + * \param other + * \return + */ + bool operator ==(const ImplMatrix &other) const + { + if (this == &other) return true; + return this->m_matrix == other.m_matrix; + } - /*! - * \brief Equal operator == - * \param other - * \return - */ - bool operator ==(const ImplMatrix &other) const - { - if (this == &other) return true; - return this->m_matrix == other.m_matrix; - } + /*! + * \brief Unequal operator != + * \param other + * \return + */ + bool operator !=(const ImplMatrix &other) const + { + return !((*this) == other); + } - /*! - * \brief Unequal operator != - * \param other - * \return - */ - bool operator !=(const ImplMatrix &other) const - { - return !((*this) == other); - } + /*! + * \brief Operator *= + * \param factor + * \return + */ + CMatrixBase &operator *=(double factor) + { + this->m_matrix *= factor; + return *this; + } - /*! - * \brief Operator *= - * \param factor - * \return - */ - CMatrixBase &operator *=(double factor) - { - this->m_matrix *= factor; - return *this; - } + /*! + * \brief Operator * + * \param factor + * \return + */ + ImplMatrix operator *(double factor) const + { + ImplMatrix m = *derived(); + m *= factor; + return m; + } - /*! - * \brief Operator * - * \param factor - * \return - */ - ImplMatrix operator *(double factor) const - { - ImplMatrix m = *derived(); - m *= factor; - return m; - } + /*! + * \brief Operator to support commutative scalar multiplication + * \param factor + * \param other + * \return + */ + friend ImplMatrix operator *(double factor, const ImplMatrix &other) + { + return other * factor; + } - /*! - * \brief Operator to support commutative scalar multiplication - * \param factor - * \param other - * \return - */ - friend ImplMatrix operator *(double factor, const ImplMatrix &other) - { - return other * factor; - } + /*! + * \brief Operator /= + * \param factor + * \return + */ + CMatrixBase &operator /=(double factor) + { + this->m_matrix /= factor; + return *this; + } - /*! - * \brief Operator /= - * \param factor - * \return - */ - CMatrixBase &operator /=(double factor) - { - this->m_matrix /= factor; - return *this; - } + /*! + * \brief Operator / + * \param factor + * \return + */ + ImplMatrix operator /(double factor) const + { + ImplMatrix m = *derived(); + m /= factor; + return m; + } - /*! - * \brief Operator / - * \param factor - * \return - */ - ImplMatrix operator /(double factor) const - { - ImplMatrix m = *derived(); - m /= factor; - return m; - } + /*! + * \brief Operator += + * \param other + * \return + */ + CMatrixBase &operator +=(const CMatrixBase &other) + { + this->m_matrix += other.m_matrix; + return *this; + } - /*! - * \brief Operator += - * \param other - * \return - */ - CMatrixBase &operator +=(const CMatrixBase &other) - { - this->m_matrix += other.m_matrix; - return *this; - } + /*! + * \brief Operator + + * \param other + * \return + */ + ImplMatrix operator +(const ImplMatrix &other) const + { + ImplMatrix m = *derived(); + m += other; + return m; + } - /*! - * \brief Operator + - * \param other - * \return - */ - ImplMatrix operator +(const ImplMatrix &other) const - { - ImplMatrix m = *derived(); - m += other; - return m; - } + /*! + * \brief Operator -= + * \param other + * \return + */ + CMatrixBase &operator -=(const CMatrixBase &other) + { + this->m_matrix -= other.m_matrix; + return *this; + } - /*! - * \brief Operator -= - * \param other - * \return - */ - CMatrixBase &operator -=(const CMatrixBase &other) - { - this->m_matrix -= other.m_matrix; - return *this; - } + /*! + * \brief Operator - + * \param other + * \return + */ + ImplMatrix operator -(const ImplMatrix &other) const + { + ImplMatrix m = *derived(); + m -= other; + return m; + } - /*! - * \brief Operator - - * \param other - * \return - */ - ImplMatrix operator -(const ImplMatrix &other) const - { - ImplMatrix m = *derived(); - m -= other; - return m; - } + /*! + * \brief Is identity matrix? + * \return + */ + bool isIdentity() const + { + return this->m_matrix.isIdentity(); + } - /*! - * \brief Is identity matrix? - * \return - */ - bool isIdentity() const - { - return this->m_matrix.isIdentity(); - } + /*! + * \brief Is identity matrix? Epsilon considered. + * \return + */ + bool isIdentityEpsilon() const + { + ImplMatrix m = *derived(); + m.round(); + return m.isIdentity(); + } - /*! - * \brief Is identity matrix? Epsilon considered. - * \return - */ - bool isIdentityEpsilon() const - { - ImplMatrix m = *derived(); - m.round(); - return m.isIdentity(); - } + /*! + * \brief Set as identity matrix + * \return + */ + void setToIdentity() + { + this->m_matrix.setToIdentity(); + } - /*! - * \brief Set as identity matrix - * \return - */ - void setToIdentity() - { - this->m_matrix.setToIdentity(); - } + /*! + * \brief All values to zero + */ + void setZero() { this->m_matrix.fill(0.0); } - /*! - * \brief All values to zero - */ - void setZero() { this->m_matrix.fill(0.0); } + /*! + * \brief Is zero + * \return + */ + bool isZero() const; - /*! - * \brief Is zero - * \return - */ - bool isZero() const; + /*! + * \brief Each cell gets a unique index (used primarily for testing) + */ + void setCellIndex(); - /*! - * \brief Each cell gets a unique index (used primarily for testing) - */ - void setCellIndex(); + /*! + * \brief Is identity matrix? Epsilon considered. + * \return + */ + bool isZeroEpsilon() const + { + ImplMatrix m = *derived(); + m.round(); + return m.isZero(); + } - /*! - * \brief Is identity matrix? Epsilon considered. - * \return - */ - bool isZeroEpsilon() const - { - ImplMatrix m = *derived(); - m.round(); - return m.isZero(); - } + /*! + * \brief Set all elements the same + * \param value + */ + void fill(double value) { this->m_matrix.fill(value); } - /*! - * \brief Set all elements the same - * \param value - */ - void fill(double value) { this->m_matrix.fill(value); } + /*! + * \brief Round all values + */ + void round(); - /*! - * \brief Round all values - */ - void round(); + /*! + * \brief Return a rounded matrix + * \return + */ + ImplMatrix roundedMatrix() const + { + ImplMatrix m = *derived(); + m.round(); + return m; + } - /*! - * \brief Return a rounded matrix - * \return - */ - ImplMatrix roundedMatrix() const - { - ImplMatrix m = *derived(); - m.round(); - return m; - } + /*! + * \brief Get element + * \param row + * \param column + * \return + */ + double getElement(size_t row, size_t column) const; - /*! - * \brief Get element - * \param row - * \param column - * \return - */ - double getElement(size_t row, size_t column) const; + /*! + * \brief Get element + * \param row + * \param column + * \param value + */ + void setElement(size_t row, size_t column, double value); - /*! - * \brief Get element - * \param row - * \param column - * \param value - */ - void setElement(size_t row, size_t column, double value); + /*! + * \brief Get element by operator () modifying + * \param row + * \param column + * \return + */ + double &operator()(size_t row, size_t column) + { + this->checkRange(row, column); + return this->m_matrix(row, column); + } - /*! - * \brief Get element by operator () modifying - * \param row - * \param column - * \return - */ - double &operator()(size_t row, size_t column) - { - this->checkRange(row, column); - return this->m_matrix(row, column); - } + /*! + * \brief Get element by operator () read only + * \param row + * \param column + * \return + */ + double operator()(size_t row, size_t column) const + { + return this->getElement(row, column); + } - /*! - * \brief Get element by operator () read only - * \param row - * \param column - * \return - */ - double operator()(size_t row, size_t column) const - { - return this->getElement(row, column); - } + /*! + * \brief Register metadata + */ + static void registerMetadata(); - /*! - * \brief Register metadata - */ - static void registerMetadata(); + private: + /*! + * \brief Check range of row / column + * \param row + * \param column + * \throws std::range_error if index out of bounds + */ + void checkRange(size_t row, size_t column) const; + }; -private: - /*! - * \brief Check range of row / column - * \param row - * \param column - * \throws std::range_error if index out of bounds - */ - void checkRange(size_t row, size_t column) const; -}; - -} // namespace + } // namespace } // namespace #endif // guard diff --git a/src/blackmisc/mathvector3d.h b/src/blackmisc/mathvector3d.h index dd8520bd0..7c78fbc83 100644 --- a/src/blackmisc/mathvector3d.h +++ b/src/blackmisc/mathvector3d.h @@ -5,97 +5,97 @@ namespace BlackMisc { -namespace Math -{ - -/*! - * \brief Concrete vector implementation - */ -class CVector3D : public CVector3DBase -{ -public: - /*! - * \brief Default constructor - */ - CVector3D() : CVector3DBase() {} - - /*! - * \brief Constructor by value - * \param i - * \param j - * \param k - */ - CVector3D(double i, double j, double k) : CVector3DBase(i, j, k) {} - - /*! - * \brief Constructor by value - * \param value - */ - explicit CVector3D(double value) : CVector3DBase(value) {} - - /*! - * \brief Copy constructor - * \param other - */ - CVector3D(const CVector3D &other) : CVector3DBase(other) {} - - /*! - * \brief i - * \return - */ - double i() const + namespace Math { - return this->m_i; - } - /*! - * \brief j - * \return - */ - double j() const - { - return this->m_j; - } + /*! + * \brief Concrete vector implementation + */ + class CVector3D : public CVector3DBase + { + public: + /*! + * \brief Default constructor + */ + CVector3D() : CVector3DBase() {} - /*! - * \brief k - * \return - */ - double k() const - { - return this->m_k; - } + /*! + * \brief Constructor by value + * \param i + * \param j + * \param k + */ + CVector3D(double i, double j, double k) : CVector3DBase(i, j, k) {} - /*! - * \brief Set i - * \param i - */ - void setI(double i) - { - this->m_i = i; - } + /*! + * \brief Constructor by value + * \param value + */ + explicit CVector3D(double value) : CVector3DBase(value) {} - /*! - * \brief Set j - * \param j - */ - void setJ(double j) - { - this->m_j = j; - } + /*! + * \brief Copy constructor + * \param other + */ + CVector3D(const CVector3D &other) : CVector3DBase(other) {} - /*! - * \brief Set k - * \param k - */ - void setK(double k) - { - this->m_k = k; - } + /*! + * \brief i + * \return + */ + double i() const + { + return this->m_i; + } -}; + /*! + * \brief j + * \return + */ + double j() const + { + return this->m_j; + } -} // namespace + /*! + * \brief k + * \return + */ + double k() const + { + return this->m_k; + } + + /*! + * \brief Set i + * \param i + */ + void setI(double i) + { + this->m_i = i; + } + + /*! + * \brief Set j + * \param j + */ + void setJ(double j) + { + this->m_j = j; + } + + /*! + * \brief Set k + * \param k + */ + void setK(double k) + { + this->m_k = k; + } + + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::Math::CVector3D) diff --git a/src/blackmisc/mathvector3dbase.cpp b/src/blackmisc/mathvector3dbase.cpp index 6484e6ae9..08d4d7da4 100644 --- a/src/blackmisc/mathvector3dbase.cpp +++ b/src/blackmisc/mathvector3dbase.cpp @@ -7,158 +7,158 @@ #include "blackmisc/mathmatrix3x3.h" #include "blackmisc/coordinateecef.h" #include "blackmisc/coordinatened.h" +#include "blackmisc/blackmiscfreefunctions.h" namespace BlackMisc { -namespace Math -{ - -/* - * Convert to string - */ -template QString CVector3DBase::convertToQString(bool /* i18n */) const -{ - QString s = ("{%1, %2, %3}"); - s = s.arg(QString::number(this->m_i, 'f')). - arg(QString::number(this->m_j, 'f')). - arg(QString::number(this->m_k, 'f')); - return s; -} - -/* - * Vector to zero - */ -template void CVector3DBase::setZero() -{ - this->fill(0.0); -} - -/* - * Vector to zero - */ -template void CVector3DBase::fill(double value) -{ - this->m_i = value; - this->m_j = value; - this->m_k = value; -} - -/* - * Element (return by reference) - */ -template double &CVector3DBase::getElement(size_t row) -{ - switch (row) + namespace Math { - case 0: - return this->m_i; - case 1: - return this->m_j; - case 2: - return this->m_k; - default: - Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector"); - throw std::range_error("Detected invalid index in 3D vector"); - } -} -/* - * Element - */ -template double CVector3DBase::getElement(size_t row) const -{ - return const_cast*>(this)->getElement(row); -} + /* + * Convert to string + */ + template QString CVector3DBase::convertToQString(bool /* i18n */) const + { + QString s = ("{%1, %2, %3}"); + s = s.arg(QString::number(this->m_i, 'f')). + arg(QString::number(this->m_j, 'f')). + arg(QString::number(this->m_k, 'f')); + return s; + } -/* - * Set given element - */ -template void CVector3DBase::setElement(size_t row, double value) -{ - switch (row) - { - case 0: - this->m_i = value; - break; - case 1: - this->m_j = value; - break; - case 2: - this->m_k = value; - break; - default: - Q_ASSERT_X(true, "setElement", "Detected invalid index in 3D vector"); - throw std::range_error("Detected invalid index in 3D vector"); - break; - } -} + /* + * Vector to zero + */ + template void CVector3DBase::setZero() + { + this->fill(0.0); + } -/* - * Cross product - */ -template ImplVector CVector3DBase::crossProduct(const ImplVector &other) const -{ - ImplVector v(other); - v.m_i = this->m_j * other.m_k - this->m_k * other.m_j; - v.m_j = this->m_k * other.m_i - this->m_i * other.m_k; - v.m_k = this->m_i * other.m_j - this->m_j * other.m_i; - return v; -} + /* + * Vector to zero + */ + template void CVector3DBase::fill(double value) + { + this->m_i = value; + this->m_j = value; + this->m_k = value; + } -/* - * Cross product - */ -template double CVector3DBase::dotProduct(const ImplVector &other) const -{ - return this->m_i * other.m_i + this->m_j * other.m_j + this->m_k * other.m_k; -} + /* + * Element (return by reference) + */ + template double &CVector3DBase::getElement(size_t row) + { + switch (row) + { + case 0: + return this->m_i; + case 1: + return this->m_j; + case 2: + return this->m_k; + default: + Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector"); + throw std::range_error("Detected invalid index in 3D vector"); + } + } + + /* + * Element + */ + template double CVector3DBase::getElement(size_t row) const + { + return const_cast*>(this)->getElement(row); + } + + /* + * Set given element + */ + template void CVector3DBase::setElement(size_t row, double value) + { + switch (row) + { + case 0: + this->m_i = value; + break; + case 1: + this->m_j = value; + break; + case 2: + this->m_k = value; + break; + default: + Q_ASSERT_X(true, "setElement", "Detected invalid index in 3D vector"); + throw std::range_error("Detected invalid index in 3D vector"); + break; + } + } + + /* + * Cross product + */ + template ImplVector CVector3DBase::crossProduct(const ImplVector &other) const + { + ImplVector v(other); + v.m_i = this->m_j * other.m_k - this->m_k * other.m_j; + v.m_j = this->m_k * other.m_i - this->m_i * other.m_k; + v.m_k = this->m_i * other.m_j - this->m_j * other.m_i; + return v; + } + + /* + * Cross product + */ + template double CVector3DBase::dotProduct(const ImplVector &other) const + { + return this->m_i * other.m_i + this->m_j * other.m_j + this->m_k * other.m_k; + } + + /* + * Convert to matrix + */ + template CMatrix3x1 CVector3DBase::toMatrix3x1() const + { + return CMatrix3x1(this->m_i, this->m_j, this->m_k); + } + + /*! + * \brief Stream to DBus + * \param argument + */ + template void CVector3DBase::marshallToDbus(QDBusArgument &argument) const + { + argument << this->m_i; + argument << this->m_j; + argument << this->m_k; + } + + /*! + * \brief Stream from DBus + * \param argument + */ + template void CVector3DBase::unmarshallFromDbus(const QDBusArgument &argument) + { + argument >> this->m_i; + argument >> this->m_j; + argument >> this->m_k; + } -/* - * Convert to matrix - */ -template CMatrix3x1 CVector3DBase::toMatrix3x1() const -{ - return CMatrix3x1(this->m_i, this->m_j, this->m_k); -} + /* + * Register metadata + */ + template void CVector3DBase::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + // see here for the reason of thess forward instantiations + // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html + template class CVector3DBase; + template class CVector3DBase; + template class CVector3DBase; -/*! - * \brief Stream to DBus - * \param argument - */ -template void CVector3DBase::marshallToDbus(QDBusArgument &argument) const -{ - argument << this->m_i; - argument << this->m_j; - argument << this->m_k; -} - -/*! - * \brief Stream from DBus - * \param argument - */ -template void CVector3DBase::unmarshallFromDbus(const QDBusArgument &argument) -{ - argument >> this->m_i; - argument >> this->m_j; - argument >> this->m_k; -} - -/* - * Register metadata - */ -template void CVector3DBase::registerMetadata() -{ - qRegisterMetaType(typeid(ImplVector).name()); - qDBusRegisterMetaType(); -} - -// see here for the reason of thess forward instantiations -// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html -template class CVector3DBase; -template class CVector3DBase; -template class CVector3DBase; - -} // namespace + } // namespace } // namespace diff --git a/src/blackmisc/mathvector3dbase.h b/src/blackmisc/mathvector3dbase.h index bd0bec9d7..f6e98c2c9 100644 --- a/src/blackmisc/mathvector3dbase.h +++ b/src/blackmisc/mathvector3dbase.h @@ -11,362 +11,356 @@ namespace BlackMisc { -namespace Math -{ - -class CMatrix3x1; - -/*! - * \brief 3D vector base (x, y, z) - */ -template class CVector3DBase : public CStreamable -{ -private: - /*! - * \brief Easy access to derived class (CRTP template parameter) - * \return - */ - ImplVector const *derived() const + namespace Math { - return static_cast(this); - } - /*! - * \brief Easy access to derived class (CRTP template parameter) - * \return - */ - ImplVector *derived() - { - return static_cast(this); - } + class CMatrix3x1; -protected: - // using own value since Qt QVector3D stores internally as float - double m_i; //!< Vector data i - double m_j; //!< Vector data j - double m_k; //!< Vector data k + /*! + * \brief 3D vector base (x, y, z) + */ + template class CVector3DBase : public CStreamable + { + private: + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + ImplVector const *derived() const + { + return static_cast(this); + } - /*! - * \brief Default constructor - */ - CVector3DBase() : m_i(0.0), m_j(0.0), m_k(0.0) {} + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + ImplVector *derived() + { + return static_cast(this); + } - /*! - * \brief Constructor by values - * \param i - * \param j - * \param k - */ - CVector3DBase(double i, double j, double k) : m_i(i), m_j(j), m_k(k) {} + protected: + // using own value since Qt QVector3D stores internally as float + double m_i; //!< Vector data i + double m_j; //!< Vector data j + double m_k; //!< Vector data k - /*! - * \brief Constructor by value - * \param value - */ - explicit CVector3DBase(double value) : m_i(value), m_j(value), m_k(value) {} + /*! + * \brief Default constructor + */ + CVector3DBase() : m_i(0.0), m_j(0.0), m_k(0.0) {} - /*! - * \brief Copy constructor - * \param other - */ - CVector3DBase(const CVector3DBase &other) : m_i(other.m_i), m_j(other.m_j), m_k(other.m_k) {} + /*! + * \brief Constructor by values + * \param i + * \param j + * \param k + */ + CVector3DBase(double i, double j, double k) : m_i(i), m_j(j), m_k(k) {} - /*! - * \brief Get element - * \param row - * \return Mutable reference - */ - double &getElement(size_t row); + /*! + * \brief Constructor by value + * \param value + */ + explicit CVector3DBase(double value) : m_i(value), m_j(value), m_k(value) {} - /*! - * \brief String for converter - * \param i18n - * \return - */ - virtual QString convertToQString(bool i18n = false) const; + /*! + * \brief Get element + * \param row + * \return Mutable reference + */ + double &getElement(size_t row); - /*! - * \brief Unmarshall from Dbus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument); + /*! + * \brief String for converter + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n = false) const; - /*! - * \brief Marshall to Dbus - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const; + /*! + * \brief Unmarshall from Dbus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); -public: - // getter and setters are implemented in the derived classes - // as they have different names (x, i, north) + /*! + * \brief Marshall to Dbus + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; - /*! - * \brief Virtual destructor - */ - virtual ~CVector3DBase() {} + public: + // getter and setters are implemented in the derived classes + // as they have different names (x, i, north) - /*! - * \brief Set zeros - */ - void setZero(); + /*! + * \brief Virtual destructor + */ + virtual ~CVector3DBase() {} - /*! - * \brief Is zero - */ - bool isZero() const - { - return this->m_i == 0 && this->m_j == 0 && this->m_k == 0; - } + /*! + * \brief Set zeros + */ + void setZero(); - /*! - * \brief Is identity matrix? Epsilon considered. - * \return - */ - bool isZeroEpsilon() const - { - ImplVector v; - v += *this; - v.round(); - return v.isZero(); - } + /*! + * \brief Is zero + */ + bool isZero() const + { + return this->m_i == 0 && this->m_j == 0 && this->m_k == 0; + } - /*! - * \brief Set all elements the same - * \param value - */ - void fill(double value); + /*! + * \brief Is identity matrix? Epsilon considered. + * \return + */ + bool isZeroEpsilon() const + { + ImplVector v; + v += *this; + v.round(); + return v.isZero(); + } - /*! - * \brief Get element - * \param row - * \return - */ - double getElement(size_t row) const; + /*! + * \brief Set all elements the same + * \param value + */ + void fill(double value); - /*! - * \brief Set element - * \param row - * \param value - */ - void setElement(size_t row, double value); + /*! + * \brief Get element + * \param row + * \return + */ + double getElement(size_t row) const; - /*! - * \brief Operator [] - * \param row - * \return - */ - double operator[](size_t row) const { return this->getElement(row); } + /*! + * \brief Set element + * \param row + * \param value + */ + void setElement(size_t row, double value); - /*! - * \brief Operator [] - * \param row - * \return Mutable reference - */ - double &operator[](size_t row) { return this->getElement(row); } + /*! + * \brief Operator [] + * \param row + * \return + */ + double operator[](size_t row) const { return this->getElement(row); } - /*! - * \brief Equal operator == - * \param other - * \return - */ - bool operator ==(const CVector3DBase &other) const - { - if (this == &other) return true; - return this->m_i == other.m_i && - this->m_j == other.m_j && - this->m_k == other.m_k; - } + /*! + * \brief Operator [] + * \param row + * \return Mutable reference + */ + double &operator[](size_t row) { return this->getElement(row); } - /*! - * \brief Unequal operator != - * \param other - * \return - */ - bool operator !=(const CVector3DBase &other) const - { - return !((*this) == other); - } + /*! + * \brief Equal operator == + * \param other + * \return + */ + bool operator ==(const CVector3DBase &other) const + { + if (this == &other) return true; + return this->m_i == other.m_i && + this->m_j == other.m_j && + this->m_k == other.m_k; + } - /*! - * \brief Operator += - * \param other - * \return - */ - CVector3DBase &operator +=(const CVector3DBase &other) - { - this->m_i += other.m_i; - this->m_j += other.m_j; - this->m_k += other.m_k; - return *this; - } + /*! + * \brief Unequal operator != + * \param other + * \return + */ + bool operator !=(const CVector3DBase &other) const + { + return !((*this) == other); + } - /*! - * \brief Operator + - * \param other - * \return - */ - ImplVector operator +(const ImplVector &other) const - { - ImplVector v = *derived(); - v += other; - return v; - } + /*! + * \brief Operator += + * \param other + * \return + */ + CVector3DBase &operator +=(const CVector3DBase &other) + { + this->m_i += other.m_i; + this->m_j += other.m_j; + this->m_k += other.m_k; + return *this; + } - /*! - * \brief Operator -= - * \param other - * \return - */ - CVector3DBase &operator -=(const CVector3DBase &other) - { - this->m_i -= other.m_i; - this->m_j -= other.m_j; - this->m_k -= other.m_k; - return *this; - } + /*! + * \brief Operator + + * \param other + * \return + */ + ImplVector operator +(const ImplVector &other) const + { + ImplVector v = *derived(); + v += other; + return v; + } - /*! - * \brief Operator - - * \param other - * \return - */ - ImplVector operator -(const ImplVector &other) const - { - ImplVector v = *derived(); - v -= other; - return v; - } + /*! + * \brief Operator -= + * \param other + * \return + */ + CVector3DBase &operator -=(const CVector3DBase &other) + { + this->m_i -= other.m_i; + this->m_j -= other.m_j; + this->m_k -= other.m_k; + return *this; + } - /*! - * \brief Multiply with scalar - * \param factor - * \return - */ - CVector3DBase &operator *=(double factor) - { - this->m_i *= factor; - this->m_j *= factor; - this->m_k *= factor; - return *this; - } + /*! + * \brief Operator - + * \param other + * \return + */ + ImplVector operator -(const ImplVector &other) const + { + ImplVector v = *derived(); + v -= other; + return v; + } - /*! - * \brief Multiply with scalar - * \param factor - * \return - */ - ImplVector operator *(double factor) const - { - ImplVector v = *derived(); - v *= factor; - return v; - } + /*! + * \brief Multiply with scalar + * \param factor + * \return + */ + CVector3DBase &operator *=(double factor) + { + this->m_i *= factor; + this->m_j *= factor; + this->m_k *= factor; + return *this; + } - /*! - * \brief Operator to support commutative multiplication - * \param factor - * \param otherVector - * \return - */ - friend ImplVector operator *(double factor, const ImplVector &other) - { - return other * factor; - } + /*! + * \brief Multiply with scalar + * \param factor + * \return + */ + ImplVector operator *(double factor) const + { + ImplVector v = *derived(); + v *= factor; + return v; + } - /*! - * \brief Divide by scalar - * \param divisor - * \return - */ - CVector3DBase &operator /=(double divisor) - { - this->m_i /= divisor; - this->m_j /= divisor; - this->m_k /= divisor; - return *this; - } + /*! + * \brief Operator to support commutative multiplication + * \param factor + * \param otherVector + * \return + */ + friend ImplVector operator *(double factor, const ImplVector &other) + { + return other * factor; + } - /*! - * \brief Divide by scalar - * \param divisor - * \return - */ - ImplVector operator /(double divisor) const - { - ImplVector v = *derived(); - v /= divisor; - return v; - } + /*! + * \brief Divide by scalar + * \param divisor + * \return + */ + CVector3DBase &operator /=(double divisor) + { + this->m_i /= divisor; + this->m_j /= divisor; + this->m_k /= divisor; + return *this; + } - /*! - * \brief Dot product - * \param other - * \return - */ - double dotProduct(const ImplVector &other) const; + /*! + * \brief Divide by scalar + * \param divisor + * \return + */ + ImplVector operator /(double divisor) const + { + ImplVector v = *derived(); + v /= divisor; + return v; + } - /*! - * \brief Cross product - * \param other - * \return - */ - ImplVector crossProduct(const ImplVector &other) const; + /*! + * \brief Dot product + * \param other + * \return + */ + double dotProduct(const ImplVector &other) const; - /*! - * \brief Reciprocal value - * \return - */ - ImplVector reciprocalValues() const - { - ImplVector v(1 / this->m_i, 1 / this->m_j, 1 / this->m_j); - return v; - } + /*! + * \brief Cross product + * \param other + * \return + */ + ImplVector crossProduct(const ImplVector &other) const; - /*! - * \brief Converted to matrix - * \return - */ - CMatrix3x1 toMatrix3x1() const; + /*! + * \brief Reciprocal value + * \return + */ + ImplVector reciprocalValues() const + { + ImplVector v(1 / this->m_i, 1 / this->m_j, 1 / this->m_j); + return v; + } - /*! - * \brief length / magnitude - * \return - */ - double length() const - { - return sqrt(this->m_i * this->m_i + this->m_j * this->m_j + this->m_k * this->m_k); - } + /*! + * \brief Converted to matrix + * \return + */ + CMatrix3x1 toMatrix3x1() const; - /*! - * \brief Round this vector - */ - void round() - { - const double epsilon = 1E-10; - this->m_i = BlackMisc::Math::CMath::roundEpsilon(this->m_i, epsilon); - this->m_j = BlackMisc::Math::CMath::roundEpsilon(this->m_j, epsilon); - this->m_k = BlackMisc::Math::CMath::roundEpsilon(this->m_k, epsilon); - } + /*! + * \brief length / magnitude + * \return + */ + double length() const + { + return sqrt(this->m_i * this->m_i + this->m_j * this->m_j + this->m_k * this->m_k); + } - /*! - * \brief Rounded vector - * \return - */ - ImplVector rounded() const - { - ImplVector v = *derived(); - v.round(); - return v; - } + /*! + * \brief Round this vector + */ + void round() + { + const double epsilon = 1E-10; + this->m_i = BlackMisc::Math::CMath::roundEpsilon(this->m_i, epsilon); + this->m_j = BlackMisc::Math::CMath::roundEpsilon(this->m_j, epsilon); + this->m_k = BlackMisc::Math::CMath::roundEpsilon(this->m_k, epsilon); + } - /*! - * \brief Register metadata - */ - static void registerMetadata(); -}; + /*! + * \brief Rounded vector + * \return + */ + ImplVector rounded() const + { + ImplVector v = *derived(); + v.round(); + return v; + } -} // namespace + /*! + * \brief Register metadata + */ + static void registerMetadata(); + }; + + } // namespace } // namespace #endif // guard diff --git a/src/blackmisc/pqacceleration.h b/src/blackmisc/pqacceleration.h index 6d7c7718e..f40406d5f 100644 --- a/src/blackmisc/pqacceleration.h +++ b/src/blackmisc/pqacceleration.h @@ -9,40 +9,40 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { -/*! - * \brief Acceleration - */ -class CAcceleration : public CPhysicalQuantity -{ -public: - /*! - * \brief Default constructor - */ - CAcceleration() : CPhysicalQuantity(0, CAccelerationUnit::defaultUnit()) {} + /*! + * \brief Acceleration + */ + class CAcceleration : public CPhysicalQuantity + { + public: + /*! + * \brief Default constructor + */ + CAcceleration() : CPhysicalQuantity(0, CAccelerationUnit::defaultUnit()) {} - /*! - * \brief Init by double value - * \param value - * \param unit - */ - CAcceleration(double value, const CAccelerationUnit &unit) : CPhysicalQuantity(value, unit) {} + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CAcceleration(double value, const CAccelerationUnit &unit) : CPhysicalQuantity(value, unit) {} - /*! - * \brief Copy constructor by base type - * \param base - */ - CAcceleration(const CPhysicalQuantity &base) : CPhysicalQuantity(base) {} + /*! + * \brief Copy constructor by base type + * \param base + */ + CAcceleration(const CPhysicalQuantity &base) : CPhysicalQuantity(base) {} - /*! - * \brief Virtual destructor - */ - virtual ~CAcceleration() {} -}; + /*! + * \brief Virtual destructor + */ + virtual ~CAcceleration() {} + }; -} // namespace + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CAcceleration) diff --git a/src/blackmisc/pqangle.h b/src/blackmisc/pqangle.h index c8209d8cd..cdfc62b7c 100644 --- a/src/blackmisc/pqangle.h +++ b/src/blackmisc/pqangle.h @@ -10,77 +10,78 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ - -/*! - * \brief Physical unit angle (radians, degrees) - */ -class CAngle : public CPhysicalQuantity -{ -public: - /*! - * \brief Default constructor - */ - CAngle() : CPhysicalQuantity(0, CAngleUnit::defaultUnit()) {} - - /*! - * \brief Init by double value - * \param value - * \param unit - */ - CAngle(double value, const CAngleUnit &unit): CPhysicalQuantity(value, unit) {} - - /*! - * \brief Init as sexagesimal degrees, minutes, seconds - * The sign of all parameters must be the same, either all positive or all negative. - * \param degrees - * \param minutes - * \param seconds - */ - CAngle(int degrees, int minutes, double seconds) : - CPhysicalQuantity( - degrees + minutes / 100.0 + seconds / 10000.0, - CAngleUnit::sexagesimalDeg()) {} - - /*! - * \brief Init as sexagesimal degrees, minutes - * The sign of both parameters must be the same, either both positive or both negative. - * \param degrees - * \param minutes - */ - CAngle(int degrees, double minutes) : - CPhysicalQuantity( - degrees + minutes / 100.0, - CAngleUnit::sexagesimalDegMin()) {} - - /*! - * \brief Virtual destructor - */ - virtual ~CAngle() {} - - /*! - * \brief Value as factor of PI (e.g. 0.5PI) - * \return - */ - double piFactor() const + namespace PhysicalQuantities { - return BlackMisc::Math::CMath::round(this->value(CAngleUnit::rad()) / BlackMisc::Math::CMath::PI() , 6); - } - /*! - * \brief PI as convenience method - * \return - */ - static const double &PI() - { - return BlackMisc::Math::CMath::PI(); - } -}; + /*! + * \brief Physical unit angle (radians, degrees) + */ + class CAngle : public CPhysicalQuantity + { + public: + /*! + * \brief Default constructor + */ + CAngle() : CPhysicalQuantity(0, CAngleUnit::defaultUnit()) {} -} // namespace + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CAngle(double value, const CAngleUnit &unit): CPhysicalQuantity(value, unit) {} + + /*! + * \brief Init as sexagesimal degrees, minutes, seconds + * The sign of all parameters must be the same, either all positive or all negative. + * \param degrees + * \param minutes + * \param seconds + */ + CAngle(int degrees, int minutes, double seconds) : + CPhysicalQuantity( + degrees + minutes / 100.0 + seconds / 10000.0, + CAngleUnit::sexagesimalDeg()) {} + + /*! + * \brief Init as sexagesimal degrees, minutes + * The sign of both parameters must be the same, either both positive or both negative. + * \param degrees + * \param minutes + */ + CAngle(int degrees, double minutes) : + CPhysicalQuantity( + degrees + minutes / 100.0, + CAngleUnit::sexagesimalDegMin()) {} + + + /*! + * \brief Virtual destructor + */ + virtual ~CAngle() {} + + /*! + * \brief Value as factor of PI (e.g. 0.5PI) + * \return + */ + double piFactor() const + { + return BlackMisc::Math::CMath::round(this->value(CAngleUnit::rad()) / BlackMisc::Math::CMath::PI() , 6); + } + + /*! + * \brief PI as convenience method + * \return + */ + static const double &PI() + { + return BlackMisc::Math::CMath::PI(); + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CAngle) -#endif // BLACKMISC_PQANGLE_H +#endif // guard diff --git a/src/blackmisc/pqbase.cpp b/src/blackmisc/pqbase.cpp index 9ef5efc27..40240a255 100644 --- a/src/blackmisc/pqbase.cpp +++ b/src/blackmisc/pqbase.cpp @@ -10,69 +10,69 @@ using namespace BlackMisc::Math; namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { // ----------------------------------------------------------------------- // --- Measurement unit -------------------------------------------------- // ----------------------------------------------------------------------- -/* - * Equal operator - */ -bool CMeasurementUnit::operator ==(const CMeasurementUnit &other) const -{ - if (this == &other) return true; - return this->m_name == other.m_name; -} + /* + * Equal operator + */ + bool CMeasurementUnit::operator ==(const CMeasurementUnit &other) const + { + if (this == &other) return true; + return this->m_name == other.m_name; + } -/* - * Unequal operator - */ -bool CMeasurementUnit::operator !=(const CMeasurementUnit &other) const -{ - return !(other == *this); -} + /* + * Unequal operator + */ + bool CMeasurementUnit::operator !=(const CMeasurementUnit &other) const + { + return !(other == *this); + } -/* - * Conversion - */ -double CMeasurementUnit::convertFrom(double value, const CMeasurementUnit &unit) const -{ - Q_ASSERT(this->m_converter); - Q_ASSERT(unit.m_converter); - if (this->m_converter == unit.m_converter) return value; - return this->m_converter->fromDefault(unit.m_converter->toDefault(value)); -} + /* + * Conversion + */ + double CMeasurementUnit::convertFrom(double value, const CMeasurementUnit &unit) const + { + Q_ASSERT(this->m_converter); + Q_ASSERT(unit.m_converter); + if (this->m_converter == unit.m_converter) return value; + return this->m_converter->fromDefault(unit.m_converter->toDefault(value)); + } -/* - * Value to QString with unit, e.g. "5.00m" - * @return - */ -QString CMeasurementUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const -{ - return this->makeRoundedQString(value, digits).append(this->getSymbol(i18n)); -} + /* + * Value to QString with unit, e.g. "5.00m" + * @return + */ + QString CMeasurementUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const + { + return this->makeRoundedQString(value, digits).append(this->getSymbol(i18n)); + } -/* - * Value rounded - */ -double CMeasurementUnit::roundValue(double value, int digits) const -{ - if (digits < 0) digits = this->m_displayDigits; - return CMath::round(value, digits); -} + /* + * Value rounded + */ + double CMeasurementUnit::roundValue(double value, int digits) const + { + if (digits < 0) digits = this->m_displayDigits; + return CMath::round(value, digits); + } -/* - * Rounded to QString - */ -QString CMeasurementUnit::makeRoundedQString(double value, int digits, bool /* i18n */) const -{ - if (digits < 0) digits = this->m_displayDigits; - double v = CMath::round(value, digits); - QString s = QLocale::system().toString(v, 'f', digits); - return s; -} + /* + * Rounded to QString + */ + QString CMeasurementUnit::makeRoundedQString(double value, int digits, bool /* i18n */) const + { + if (digits < 0) digits = this->m_displayDigits; + double v = CMath::round(value, digits); + QString s = QLocale::system().toString(v, 'f', digits); + return s; + } -} // namespace + } // namespace } // namespace diff --git a/src/blackmisc/pqbase.h b/src/blackmisc/pqbase.h index fd45e17a9..7c9d65553 100644 --- a/src/blackmisc/pqbase.h +++ b/src/blackmisc/pqbase.h @@ -19,353 +19,375 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ - -// --------------------------------------------------------------------------------- -// --- Unit -// --------------------------------------------------------------------------------- - -/*! - * \brief Base class for all units, such as meter, hertz. - */ -class CMeasurementUnit : public CStreamable -{ -protected: - /*! - * Abstract strategy pattern that encapsulates a unit conversion strategy. - */ - class Converter : public QSharedData + namespace PhysicalQuantities { - public: /*! - * Virtual destructor. + * \brief Base class for all units, such as meter, hertz. */ - virtual ~Converter() {} - /*! - * Convert from this unit to default unit. - * \param factor - * \return - */ - virtual double toDefault(double factor) const = 0; - /*! - * Convert from default unit to this unit. - * \param factor - * \return - */ - virtual double fromDefault(double factor) const = 0; - }; - - /*! - * Concrete strategy pattern for converting unit that does nothing. - */ - struct IdentityConverter : public Converter - { - virtual double toDefault(double factor) const { return factor; } - virtual double fromDefault(double factor) const { return factor; } - }; - - /*! - * Concrete strategy pattern for converting unit with linear conversion. - * \tparam Policy a policy class with static method factor() returning double - */ - template - struct LinearConverter : public Converter - { - virtual double toDefault(double factor) const { return factor * Policy::factor(); } - virtual double fromDefault(double factor) const { return factor / Policy::factor(); } - }; - - /*! - * Concrete strategy pattern for converting unit with offset linear conversion. - * \tparam Policy a policy class with static methods factor() and offset() returning double - */ - template - struct AffineConverter : public Converter - { - virtual double toDefault(double factor) const { return (factor - Policy::offset()) * Policy::factor(); } - virtual double fromDefault(double factor) const { return factor / Policy::factor() + Policy::offset(); } - }; - - /*! - * Concrete strategy pattern for converting unit with one subdivision conversion. - * \tparam FactorPolicy a policy class with static method factor() returning double - * \tparam SubdivPolicy a policy class with static methods fraction() and subfactor() returning double - */ - template - struct SubdivisionConverter : public Converter - { - virtual double toDefault(double factor) const { using BlackMisc::Math::CMath; - double part2 = CMath::fract(factor) * SubdivPolicy::fraction(); - factor = CMath::trunc(factor) + part2 / SubdivPolicy::subfactor(); - return factor * FactorPolicy::factor(); } - virtual double fromDefault(double factor) const { using BlackMisc::Math::CMath; - factor /= FactorPolicy::factor(); - double part2 = CMath::fract(factor) * SubdivPolicy::subfactor(); - return CMath::trunc(factor) + part2 / SubdivPolicy::fraction(); } - }; - - /*! - * Concrete strategy pattern for converting unit with two subdivision conversions. - * \tparam FactorPolicy a policy class with static method factor() returning double - * \tparam SubdivPolicy a policy class with static methods fraction() and subfactor() returning double - */ - template - struct SubdivisionConverter2 : public Converter - { - virtual double toDefault(double factor) const { using BlackMisc::Math::CMath; - double part2 = CMath::fract(factor) * SubdivPolicy::fraction(); - double part3 = CMath::fract(part2) * SubdivPolicy::fraction(); - factor = CMath::trunc(factor) + (CMath::trunc(part2) + part3 / SubdivPolicy::subfactor()) / SubdivPolicy::subfactor(); - return factor * FactorPolicy::factor(); } - virtual double fromDefault(double factor) const { using BlackMisc::Math::CMath; - factor /= FactorPolicy::factor(); - double part2 = CMath::fract(factor) * SubdivPolicy::subfactor(); - double part3 = CMath::fract(part2) * SubdivPolicy::subfactor(); - return CMath::trunc(factor) + (CMath::trunc(part2) + part3 / SubdivPolicy::fraction()) / SubdivPolicy::fraction(); } - }; - - //! Metapolicy that can be used to modify template parameters of converters - //! @{ - struct One { - static double factor() { return 1; } //!< factor \return - }; - template - struct Two { - static double factor() { return Policy::factor() * 2.0; } //!< factor \return - }; - template - struct Milli { - static double factor() { return Policy::factor() / 1000.0; } //!< factor \return - }; - template - struct Centi { - static double factor() { return Policy::factor() / 100.0; } //!< factor \return - }; - template - struct Hecto { - static double factor() { return Policy::factor() * 100.0; } //!< factor \return - }; - template - struct Kilo { - static double factor() { return Policy::factor() * 1000.0; } //!< factor \return - }; - template - struct Mega { - static double factor() { return Policy::factor() * 1e+6; } //!< factor \return - }; - template - struct Giga { - static double factor() { return Policy::factor() * 1e+9; } //!< factor \return - }; - template - struct InEachHundred { - static double fraction() { return 100.0f; } //!< fraction \return - static double subfactor() { return float(Subfactor); } //!< subfactor \return - }; - //! @} - -private: - QString m_name; //!< name, e.g. "meter" - QString m_symbol; //!< unit name, e.g. "m" - double m_epsilon; //!< values with differences below epsilon are the equal - int m_displayDigits; //!< standard rounding for string conversions - QSharedDataPointer m_converter; //!< strategy pattern allows an arbitrary conversion method as per object - -protected: - /*! - * Construct a unit with custom conversion - * \param name - * \param symbol - * \param displayDigits - * \param epsilon - */ - template - CMeasurementUnit(const QString &name, const QString &symbol, const Converter &, int displayDigits, double epsilon) - : m_name(name), m_symbol(symbol), m_epsilon(epsilon), m_displayDigits(displayDigits), m_converter(new Converter) - {} - - /*! - * \brief Copy constructor - * \param other - */ - CMeasurementUnit(const CMeasurementUnit &other) - : m_name(other.m_name), m_symbol(other.m_symbol), m_epsilon(other.m_epsilon), m_displayDigits(other.m_displayDigits), m_converter(other.m_converter) - {} - - /*! - * \brief String for streaming operators is full name - * \return - */ - virtual QString stringForStreaming() const - { - return this->getName(false); - } - - /*! - * \brief Name as stringification - * \param i18n - * \return - */ - virtual QString convertToQString(bool i18n = false) const - { - return this->getSymbol(i18n); - } - - /*! - * \brief Stream to DBus - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - argument << this->m_symbol; - } - - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &) - { - // the concrete implementations will override this default - // this is required so I can also stream None - (*this) = CMeasurementUnit::None(); - } - - /*! - * \brief Unit from symbol - * \param symbol must be a valid unit symbol (without i18n) or empty string (empty means default unit) - * \return - */ - template static const U &unitFromSymbol(const QString &symbol) - { - if (symbol.isEmpty()) return U::defaultUnit(); - const QList &units = U::allUnits(); - for (int i = 0; i < units.size(); ++i) { - if (units.at(i).getSymbol() == symbol) return units.at(i); - } - qFatal("Illegal unit name"); - return U::defaultUnit(); // just suppress "not all control paths return a value" - } - -public: - /*! - * \brief Equal operator == - * \param other - * \return - */ - bool operator == (const CMeasurementUnit &other) const; - - /*! - * \brief Unequal operator != - * \param other - * \return - */ - bool operator != (const CMeasurementUnit &other) const; - - /*! - * \brief Name such as "meter" - * \param i18n - * \return - */ - QString getName(bool i18n = false) const - { - return i18n ? QCoreApplication::translate("CMeasurementUnit", this->m_name.toStdString().c_str()) : this->m_name; - } - - /*! - * \brief Unit name such as "m" - * \param i18n - * \return - */ - QString getSymbol(bool i18n = false) const - { - return i18n ? QCoreApplication::translate("CMeasurementUnit", this->m_symbol.toStdString().c_str()) : this->m_symbol; - } - - /*! - * \brief Rounded value - * \param value - * \param digits - * \return - */ - double roundValue(double value, int digits = -1) const; - - /*! - * Rounded string utility method, virtual so units can have - * specialized formatting - * \param value - * \param digits - * \param i18n - * \return - */ - virtual QString makeRoundedQString(double value, int digits = -1, bool i18n = false) const; - - /*! - * \brief Value rounded with unit, e.g. "5.00m", "30kHz" - * \param value - * \param digits - * \param i18n - * \return - */ - virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const; - - /*! - * \brief Threshold for rounding - * \return - */ - double getEpsilon() const - { - return this->m_epsilon; - } - - /*! - * \brief getDisplayDigits - * \return - */ - int getDisplayDigits() const - { - return this->m_displayDigits; - } - - /*! - * Convert from other unit to this unit. - * \param - */ - double convertFrom(double value, const CMeasurementUnit &unit) const; - - /*! - * \brief Is given value <= epsilon? - * \param value - * \return - */ - bool isEpsilon(double value) const - { - if (value == 0) return true; - return abs(value) <= this->m_epsilon; - } - - // -------------------------------------------------------------------- - // -- static - // -------------------------------------------------------------------- - - /*! - * \brief Dimensionless unit - * \return - */ - static CMeasurementUnit &None() - { - struct NilConverter : public Converter + class CMeasurementUnit : public CStreamable { - virtual double toDefault(double) const { return 0.0; } - virtual double fromDefault(double) const { return 0.0; } + protected: + /*! + * Abstract strategy pattern that encapsulates a unit conversion strategy. + */ + class Converter : public QSharedData + { + public: + /*! + * Virtual destructor. + */ + virtual ~Converter() {} + /*! + * Convert from this unit to default unit. + * \param factor + * \return + */ + virtual double toDefault(double factor) const = 0; + /*! + * Convert from default unit to this unit. + * \param factor + * \return + */ + virtual double fromDefault(double factor) const = 0; + }; + + /*! + * \brief Converter for default values, such as None, used with public constructor + */ + struct NilConverter : public Converter + { + virtual double toDefault(double) const { return 0.0; } + virtual double fromDefault(double) const { return 0.0; } + }; + + /*! + * Concrete strategy pattern for converting unit that does nothing. + */ + struct IdentityConverter : public Converter + { + virtual double toDefault(double factor) const { return factor; } + virtual double fromDefault(double factor) const { return factor; } + }; + + /*! + * Concrete strategy pattern for converting unit with linear conversion. + * \tparam Policy a policy class with static method factor() returning double + */ + template + struct LinearConverter : public Converter + { + virtual double toDefault(double factor) const { return factor * Policy::factor(); } + virtual double fromDefault(double factor) const { return factor / Policy::factor(); } + }; + + /*! + * Concrete strategy pattern for converting unit with offset linear conversion. + * \tparam Policy a policy class with static methods factor() and offset() returning double + */ + template + struct AffineConverter : public Converter + { + virtual double toDefault(double factor) const { return (factor - Policy::offset()) * Policy::factor(); } + virtual double fromDefault(double factor) const { return factor / Policy::factor() + Policy::offset(); } + }; + + /*! + * Concrete strategy pattern for converting unit with one subdivision conversion. + * \tparam FactorPolicy a policy class with static method factor() returning double + * \tparam SubdivPolicy a policy class with static methods fraction() and subfactor() returning double + */ + template + struct SubdivisionConverter : public Converter + { + virtual double toDefault(double factor) const + { + using BlackMisc::Math::CMath; + double part2 = CMath::fract(factor) * SubdivPolicy::fraction(); + factor = CMath::trunc(factor) + part2 / SubdivPolicy::subfactor(); + return factor * FactorPolicy::factor(); + } + virtual double fromDefault(double factor) const + { + using BlackMisc::Math::CMath; + factor /= FactorPolicy::factor(); + double part2 = CMath::fract(factor) * SubdivPolicy::subfactor(); + return CMath::trunc(factor) + part2 / SubdivPolicy::fraction(); + } + }; + + /*! + * Concrete strategy pattern for converting unit with two subdivision conversions. + * \tparam FactorPolicy a policy class with static method factor() returning double + * \tparam SubdivPolicy a policy class with static methods fraction() and subfactor() returning double + */ + template + struct SubdivisionConverter2 : public Converter + { + virtual double toDefault(double factor) const + { + using BlackMisc::Math::CMath; + double part2 = CMath::fract(factor) * SubdivPolicy::fraction(); + double part3 = CMath::fract(part2) * SubdivPolicy::fraction(); + factor = CMath::trunc(factor) + (CMath::trunc(part2) + part3 / SubdivPolicy::subfactor()) / SubdivPolicy::subfactor(); + return factor * FactorPolicy::factor(); + } + virtual double fromDefault(double factor) const + { + using BlackMisc::Math::CMath; + factor /= FactorPolicy::factor(); + double part2 = CMath::fract(factor) * SubdivPolicy::subfactor(); + double part3 = CMath::fract(part2) * SubdivPolicy::subfactor(); + return CMath::trunc(factor) + (CMath::trunc(part2) + part3 / SubdivPolicy::fraction()) / SubdivPolicy::fraction(); + } + }; + + //! Metapolicy that can be used to modify template parameters of converters + //! @{ + struct One + { + static double factor() { return 1; } //!< factor \return + }; + template + struct Two + { + static double factor() { return Policy::factor() * 2.0; } //!< factor \return + }; + template + struct Milli + { + static double factor() { return Policy::factor() / 1000.0; } //!< factor \return + }; + template + struct Centi + { + static double factor() { return Policy::factor() / 100.0; } //!< factor \return + }; + template + struct Hecto + { + static double factor() { return Policy::factor() * 100.0; } //!< factor \return + }; + template + struct Kilo + { + static double factor() { return Policy::factor() * 1000.0; } //!< factor \return + }; + template + struct Mega + { + static double factor() { return Policy::factor() * 1e+6; } //!< factor \return + }; + template + struct Giga + { + static double factor() { return Policy::factor() * 1e+9; } //!< factor \return + }; + template + struct InEachHundred + { + static double fraction() { return 100.0f; } //!< fraction \return + static double subfactor() { return float(Subfactor); } //!< subfactor \return + }; + //! @} + + private: + QString m_name; //!< name, e.g. "meter" + QString m_symbol; //!< unit name, e.g. "m" + double m_epsilon; //!< values with differences below epsilon are the equal + int m_displayDigits; //!< standard rounding for string conversions + QSharedDataPointer m_converter; //!< strategy pattern allows an arbitrary conversion method as per object + + protected: + /*! + * Construct a unit with custom conversion + * \param name + * \param symbol + * \param displayDigits + * \param epsilon + */ + template + CMeasurementUnit(const QString &name, const QString &symbol, const Converter &, int displayDigits, double epsilon) + : m_name(name), m_symbol(symbol), m_epsilon(epsilon), m_displayDigits(displayDigits), m_converter(new Converter) + {} + + /*! + * \brief String for streaming operators is full name + * \return + */ + virtual QString stringForStreaming() const + { + return this->getName(false); + } + + /*! + * \brief Name as stringification + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n = false) const + { + return this->getSymbol(i18n); + } + + /*! + * \brief Stream to DBus + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const + { + argument << this->m_symbol; + } + + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &) + { + // the concrete implementations will override this default + // this is required so I can also stream None + (*this) = CMeasurementUnit::None(); + } + + public: + /*! + * \brief Default constructor for meta system + * \remarks Only public because the need, to use this with the metasystem + */ + CMeasurementUnit() : m_name("none"), m_symbol(""), m_epsilon(0), m_displayDigits(0), m_converter(new NilConverter()) + {} + + /*! + * \brief Equal operator == + * \param other + * \return + */ + bool operator == (const CMeasurementUnit &other) const; + + /*! + * \brief Unequal operator != + * \param other + * \return + */ + bool operator != (const CMeasurementUnit &other) const; + + /*! + * \brief Name such as "meter" + * \param i18n + * \return + */ + QString getName(bool i18n = false) const + { + return i18n ? QCoreApplication::translate("CMeasurementUnit", this->m_name.toStdString().c_str()) : this->m_name; + } + + /*! + * \brief Unit name such as "m" + * \param i18n + * \return + */ + QString getSymbol(bool i18n = false) const + { + return i18n ? QCoreApplication::translate("CMeasurementUnit", this->m_symbol.toStdString().c_str()) : this->m_symbol; + } + + + /*! + * \brief Rounded value + * \param value + * \param digits + * \return + */ + double roundValue(double value, int digits = -1) const; + + /*! + * Rounded string utility method, virtual so units can have + * specialized formatting + * \param value + * \param digits + * \param i18n + * \return + */ + virtual QString makeRoundedQString(double value, int digits = -1, bool i18n = false) const; + + /*! + * \brief Value rounded with unit, e.g. "5.00m", "30kHz" + * \param value + * \param digits + * \param i18n + * \return + */ + virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const; + + /*! + * \brief Threshold for rounding + * \return + */ + double getEpsilon() const + { + return this->m_epsilon; + } + + /*! + * \brief getDisplayDigits + * \return + */ + int getDisplayDigits() const + { + return this->m_displayDigits; + } + + /*! + * Convert from other unit to this unit. + * \param + */ + double convertFrom(double value, const CMeasurementUnit &unit) const; + + /*! + * \brief Is given value <= epsilon? + * \param value + * \return + */ + bool isEpsilon(double value) const + { + if (value == 0) return true; + return abs(value) <= this->m_epsilon; + } + + // -------------------------------------------------------------------- + // -- static + // -------------------------------------------------------------------- + + /*! + * \brief Unit from symbol + * \param symbol must be a valid unit symbol (without i18n) or empty string (empty means default unit) + * \return + */ + template static const U &unitFromSymbol(const QString &symbol) + { + if (symbol.isEmpty()) return U::defaultUnit(); + const QList &units = U::allUnits(); + for (int i = 0; i < units.size(); ++i) + { + if (units.at(i).getSymbol() == symbol) return units.at(i); + } + qFatal("Illegal unit name"); + return U::defaultUnit(); // just suppress "not all control paths return a value" + } + + /*! + * \brief Dimensionless unit + * \return + */ + static CMeasurementUnit &None() + { + static CMeasurementUnit none("none", "", NilConverter(), 0, 0); + return none; + } }; - static CMeasurementUnit none("none", "", NilConverter(), 0, 0); - return none; - } -}; -} // namespace + } // namespace } // namespace -#endif // BLACKMISC_PQBASE_H + +#endif // guard diff --git a/src/blackmisc/pqfrequency.h b/src/blackmisc/pqfrequency.h index d86a89f76..00882f40e 100644 --- a/src/blackmisc/pqfrequency.h +++ b/src/blackmisc/pqfrequency.h @@ -9,35 +9,33 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { + /*! + * \brief Physical unit frequency + */ + class CFrequency : public CPhysicalQuantity + { + public: + /*! + * \brief Default constructor + */ + CFrequency() : CPhysicalQuantity(0, CFrequencyUnit::defaultUnit()) {} -/*! - * \brief Physical unit distance - * \author KWB - */ -class CFrequency : public CPhysicalQuantity -{ -public: - /*! - * \brief Default constructor - */ - CFrequency() : CPhysicalQuantity(0, CFrequencyUnit::defaultUnit()) {} + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CFrequency(double value, const CFrequencyUnit &unit) : CPhysicalQuantity(value, unit) {} - /*! - * \brief Init by double value - * \param value - * \param unit - */ - CFrequency(double value, const CFrequencyUnit &unit) : CPhysicalQuantity(value, unit) {} + /*! + * \brief Virtual destructor + */ + virtual ~CFrequency() {} + }; - /*! - * \brief Virtual destructor - */ - virtual ~CFrequency() {} -}; - -} // namespace + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CFrequency) diff --git a/src/blackmisc/pqlength.h b/src/blackmisc/pqlength.h index 15cecf3ef..75f44d801 100644 --- a/src/blackmisc/pqlength.h +++ b/src/blackmisc/pqlength.h @@ -9,37 +9,36 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { -/*! - * \brief Physical unit length (length) - * \author KWB - */ -class CLength : public CPhysicalQuantity -{ -public: - /*! - * \brief Default constructor - */ - CLength() : CPhysicalQuantity(0, CLengthUnit::defaultUnit()) {} + /*! + * \brief Physical unit length (length) + */ + class CLength : public CPhysicalQuantity + { + public: + /*! + * \brief Default constructor + */ + CLength() : CPhysicalQuantity(0, CLengthUnit::defaultUnit()) {} - /*! - *\brief Init by double value - * \param value - * \param unit - */ - CLength(double value, const CLengthUnit &unit) : CPhysicalQuantity(value, unit) {} + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CLength(double value, const CLengthUnit &unit) : CPhysicalQuantity(value, unit) {} - /*! - * \brief Virtual destructor - */ - virtual ~CLength() {} -}; + /*! + * \brief Virtual destructor + */ + virtual ~CLength() {} + }; -} // namespace + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CLength) -#endif // BLACKMISC_PQLENGTH_H +#endif // guard diff --git a/src/blackmisc/pqmass.h b/src/blackmisc/pqmass.h index 00483b3b1..e17002188 100644 --- a/src/blackmisc/pqmass.h +++ b/src/blackmisc/pqmass.h @@ -9,34 +9,34 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { -/*! - * \brief Mass - */ -class CMass : public CPhysicalQuantity -{ -public: - /*! - * \brief Default constructor - */ - CMass() : CPhysicalQuantity(0, CMassUnit::defaultUnit()) {} + /*! + * \brief Mass + */ + class CMass : public CPhysicalQuantity + { + public: + /*! + * \brief Default constructor + */ + CMass() : CPhysicalQuantity(0, CMassUnit::defaultUnit()) {} - /*! - * \brief Init by double value - * \param value - * \param unit - */ - CMass(double value, const CMassUnit &unit) : CPhysicalQuantity(value, unit) {} + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CMass(double value, const CMassUnit &unit) : CPhysicalQuantity(value, unit) {} - /*! - * \brief Virtual destructor - */ - virtual ~CMass() {} -}; + /*! + * \brief Virtual destructor + */ + virtual ~CMass() {} + }; -} // namespace + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CMass) diff --git a/src/blackmisc/pqphysicalquantity.cpp b/src/blackmisc/pqphysicalquantity.cpp index 9d2105484..ef82f5cec 100644 --- a/src/blackmisc/pqphysicalquantity.cpp +++ b/src/blackmisc/pqphysicalquantity.cpp @@ -4,223 +4,267 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "blackmisc/pqallquantities.h" +#include "blackmiscfreefunctions.h" namespace BlackMisc { -namespace PhysicalQuantities -{ - -/* - * Constructor by double - */ -template CPhysicalQuantity::CPhysicalQuantity(double value, const MU &unit) : - m_value(value), m_unit(unit) -{ -} - -/* - * Equal operator == - */ -template bool CPhysicalQuantity::operator ==(const CPhysicalQuantity &other) const -{ - if (this == &other) return true; - double diff = std::abs(this->m_value - other.value(this->m_unit)); - return diff <= this->m_unit.getEpsilon(); -} - -/* - * Not equal - */ -template bool CPhysicalQuantity::operator !=(const CPhysicalQuantity &other) const -{ - return !((*this) == other); -} - -/* - * Plus operator - */ -template CPhysicalQuantity &CPhysicalQuantity::operator +=(const CPhysicalQuantity &other) -{ - this->m_value += other.value(this->m_unit); - return *this; -} - -/* - * Plus operator - */ -template PQ CPhysicalQuantity::operator +(const PQ &other) const -{ - PQ copy(other); - copy += *this; - return copy; -} - -/* - * Explicit plus - */ -template void CPhysicalQuantity::addValueSameUnit(double value) -{ - this->m_value += value; -} - -/* - * Explicit minus - */ -template void CPhysicalQuantity::substractValueSameUnit(double value) -{ - this->m_value -= value; -} - -/* - * Minus operator - */ -template CPhysicalQuantity &CPhysicalQuantity::operator -=(const CPhysicalQuantity &other) -{ - this->m_value -= other.value(this->m_unit); - return *this; -} - -/* - * Minus operator - */ -template PQ CPhysicalQuantity::operator -(const PQ &other) const -{ - PQ copy = *derived(); - copy -= other; - return copy; -} - -/* - * Multiply operator - */ -template CPhysicalQuantity &CPhysicalQuantity::operator *=(double factor) -{ - this->m_value *= factor; - return *this; -} - -/* - * Multiply operator - */ -template PQ CPhysicalQuantity::operator *(double factor) const -{ - PQ copy = *derived(); - copy *= factor; - return copy; -} - -/* - * Divide operator /= - */ -template CPhysicalQuantity &CPhysicalQuantity::operator /=(double divisor) -{ - this->m_value /= divisor; - return *this; -} - -/* - * Divide operator / - */ -template PQ CPhysicalQuantity::operator /(double divisor) const -{ - PQ copy = *derived(); - copy /= divisor; - return copy; -} - -/* - * Less operator < - */ -template bool CPhysicalQuantity::operator <(const CPhysicalQuantity &other) const -{ - if ((*this) == other) return false; - - return (this->m_value < other.value(this->m_unit)); -} - -/* - * Greater than - */ -template bool CPhysicalQuantity::operator >(const CPhysicalQuantity &other) const -{ - if (this == &other) return false; - return other < *this; -} - -/* - * Greater / Equal - */ -template bool CPhysicalQuantity::operator >=(const CPhysicalQuantity &other) const -{ - if (this == &other) return true; - return !(*this < other); -} - -/* - * Less equal - */ -template bool CPhysicalQuantity::operator <=(const CPhysicalQuantity &other) const -{ - if (this == &other) return true; - return !(*this > other); -} - -/* - * Switch to another unit - */ -template PQ &CPhysicalQuantity::switchUnit(const MU &newUnit) -{ - if (this->m_unit != newUnit) + namespace PhysicalQuantities { - this->m_value = newUnit.convertFrom(this->m_value, this->m_unit); - this->m_unit = newUnit; - } - return *derived(); -} -/* - * Init by double - */ -template void CPhysicalQuantity::setValueSameUnit(double baseValue) -{ - this->m_value = baseValue; -} + /* + * Constructor by double + */ + template CPhysicalQuantity::CPhysicalQuantity(double value, const MU &unit) : + m_value(value), m_unit(unit) + { + // void + } -/* - * Value rounded in unit - */ -template QString CPhysicalQuantity::valueRoundedWithUnit(const MU &unit, int digits, bool i18n) const -{ - return unit.makeRoundedQStringWithUnit(this->value(unit), digits, i18n); -} + /* + * Equal operator == + */ + template bool CPhysicalQuantity::operator ==(const CPhysicalQuantity &other) const + { + if (this == &other) return true; + double diff = std::abs(this->m_value - other.value(this->m_unit)); + return diff <= this->m_unit.getEpsilon(); + } -/* - * Value rounded in unit - */ -template double CPhysicalQuantity::valueRounded(const MU &unit, int digits) const -{ - return unit.roundValue(this->value(unit), digits); -} + /* + * Not equal + */ + template bool CPhysicalQuantity::operator !=(const CPhysicalQuantity &other) const + { + return !((*this) == other); + } -/* - * Value in unit - */ -template double CPhysicalQuantity::value(const MU &unit) const -{ - return unit.convertFrom(this->m_value, this->m_unit); -} + /* + * Plus operator + */ + template CPhysicalQuantity &CPhysicalQuantity::operator +=(const CPhysicalQuantity &other) + { + this->m_value += other.value(this->m_unit); + return *this; + } -// see here for the reason of thess forward instantiations -// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html -template class CPhysicalQuantity; -template class CPhysicalQuantity; -template class CPhysicalQuantity; -template class CPhysicalQuantity; -template class CPhysicalQuantity; -template class CPhysicalQuantity; -template class CPhysicalQuantity; -template class CPhysicalQuantity; -template class CPhysicalQuantity; + /* + * Plus operator + */ + template PQ CPhysicalQuantity::operator +(const PQ &other) const + { + PQ copy(other); + copy += *this; + return copy; + } -} // namespace + /* + * Explicit plus + */ + template void CPhysicalQuantity::addValueSameUnit(double value) + { + this->m_value += value; + } + + /* + * Explicit minus + */ + template void CPhysicalQuantity::substractValueSameUnit(double value) + { + this->m_value -= value; + } + + /* + * Minus operator + */ + template CPhysicalQuantity &CPhysicalQuantity::operator -=(const CPhysicalQuantity &other) + { + this->m_value -= other.value(this->m_unit); + return *this; + } + + /* + * Minus operator + */ + template PQ CPhysicalQuantity::operator -(const PQ &other) const + { + PQ copy = *derived(); + copy -= other; + return copy; + } + + /* + * Marshall + */ + template void CPhysicalQuantity::marshallToDbus(QDBusArgument &argument) const + { + argument << this->value(UnitClass::defaultUnit()); + argument << this->m_value; + argument << this->m_unit; + } + + /* + * Unmarshall + */ + template void CPhysicalQuantity::unmarshallFromDbus(const QDBusArgument &argument) + { + double ignore; + argument >> ignore; + argument >> this->m_value; + argument >> this->m_unit; + } + + /* + * Register metatype + */ + template void CPhysicalQuantity::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + qDBusRegisterMetaType >(); + qRegisterMetaType(); + qDBusRegisterMetaType(); + qDBusRegisterMetaType >(); + } + + /* + * Multiply operator + */ + template CPhysicalQuantity &CPhysicalQuantity::operator *=(double factor) + { + this->m_value *= factor; + return *this; + } + + /* + * Multiply operator + */ + template PQ CPhysicalQuantity::operator *(double factor) const + { + PQ copy = *derived(); + copy *= factor; + return copy; + } + + /* + * Divide operator /= + */ + template CPhysicalQuantity &CPhysicalQuantity::operator /=(double divisor) + { + this->m_value /= divisor; + return *this; + } + + /* + * Divide operator / + */ + template PQ CPhysicalQuantity::operator /(double divisor) const + { + PQ copy = *derived(); + copy /= divisor; + return copy; + } + + /* + * Less operator < + */ + template bool CPhysicalQuantity::operator <(const CPhysicalQuantity &other) const + { + if ((*this) == other) return false; + + return (this->m_value < other.value(this->m_unit)); + } + + /* + * Greater than + */ + template bool CPhysicalQuantity::operator >(const CPhysicalQuantity &other) const + { + if (this == &other) return false; + return other < *this; + } + + /* + * Greater / Equal + */ + template bool CPhysicalQuantity::operator >=(const CPhysicalQuantity &other) const + { + if (this == &other) return true; + return !(*this < other); + } + + /* + * Less equal + */ + template bool CPhysicalQuantity::operator <=(const CPhysicalQuantity &other) const + { + if (this == &other) return true; + return !(*this > other); + } + + /* + * Switch to another unit + */ + template PQ &CPhysicalQuantity::switchUnit(const MU &newUnit) + { + if (this->m_unit != newUnit) + { + this->m_value = newUnit.convertFrom(this->m_value, this->m_unit); + this->m_unit = newUnit; + } + return *derived(); + } + + /* + * Init by double + */ + template void CPhysicalQuantity::setValueSameUnit(double baseValue) + { + this->m_value = baseValue; + } + + /* + * Value rounded in unit + */ + template QString CPhysicalQuantity::valueRoundedWithUnit(const MU &unit, int digits, bool i18n) const + { + return unit.makeRoundedQStringWithUnit(this->value(unit), digits, i18n); + } + + /* + * Value rounded in unit + */ + template double CPhysicalQuantity::valueRounded(const MU &unit, int digits) const + { + return unit.roundValue(this->value(unit), digits); + } + + /* + * Value in unit + */ + template double CPhysicalQuantity::value(const MU &unit) const + { + return unit.convertFrom(this->m_value, this->m_unit); + } + + /* + * Convert to string + */ + template QString CPhysicalQuantity::convertToQString(bool i18n) const + { + return this->valueRoundedWithUnit(this->getUnit(), -1, i18n); + } + + // see here for the reason of thess forward instantiations + // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html + template class CPhysicalQuantity; + template class CPhysicalQuantity; + template class CPhysicalQuantity; + template class CPhysicalQuantity; + template class CPhysicalQuantity; + template class CPhysicalQuantity; + template class CPhysicalQuantity; + template class CPhysicalQuantity; + template class CPhysicalQuantity; + + } // namespace } // namespace diff --git a/src/blackmisc/pqphysicalquantity.h b/src/blackmisc/pqphysicalquantity.h index 87e7f62e2..1a24d2fa5 100644 --- a/src/blackmisc/pqphysicalquantity.h +++ b/src/blackmisc/pqphysicalquantity.h @@ -18,327 +18,343 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ - -/*! - * \brief A physical quantity such as "5m", "20s", "1500ft/s" - */ -template class CPhysicalQuantity : public BlackMisc::CStreamable -{ -private: - double m_value; //!< numeric part - MU m_unit; //!< unit part - - /*! - * Which subclass of CMeasurementUnit does this quantity use? - */ - typedef MU UnitClass; - - /*! - * \brief Easy access to derived class (CRTP template parameter) - * \return - */ - PQ const* derived() const + namespace PhysicalQuantities { - return static_cast(this); - } + /*! + * \brief A physical quantity such as "5m", "20s", "1500ft/s" + */ + template class CPhysicalQuantity : public BlackMisc::CStreamable + { - /*! - * \brief Easy access to derived class (CRTP template parameter) - * \return - */ - PQ* derived() - { - return static_cast(this); - } + private: + double m_value; //!< numeric part + MU m_unit; //!< unit part -protected: - /*! - * \brief Constructor with double - * \param value - * \param unit - */ - CPhysicalQuantity(double value, const MU &unit); + /*! + * Which subclass of CMeasurementUnit does this quantity use? + */ + typedef MU UnitClass; - /*! - * \brief Name as string - * \param i18n - * \return - */ - virtual QString convertToQString(bool i18n = false) const - { - return this->valueRoundedWithUnit(this->getUnit(), -1, i18n); - } + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + PQ const *derived() const + { + return static_cast(this); + } -public: - /*! - * \brief Virtual destructor - */ - virtual ~CPhysicalQuantity() {} + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + PQ *derived() + { + return static_cast(this); + } - /*! - * \brief Unit of the distance - * \return - */ - MU getUnit() const - { - return this->m_unit; - } + protected: + /*! + * \brief Constructor with double + * \param value + * \param unit + */ + CPhysicalQuantity(double value, const MU &unit); - /*! - * \brief Change unit, and convert value to maintain the same quantity - * \param newUnit - * \return - */ - PQ &switchUnit(const MU &newUnit); + /*! + * \brief Rounded value as string + * \param i18n + * \return + */ + virtual QString convertToQString(bool i18n = false) const; - /*! - * \brief Value in given unit - * \param unit - * \return - */ - double value(const MU &unit) const; + public: + /*! + * \brief Virtual destructor + */ + virtual ~CPhysicalQuantity() {} - /*! - * \brief Value in current unit - * \return - */ - double value() const - { - return this->m_value; - } + /*! + * \brief Unit + * \return + */ + MU getUnit() const + { + return this->m_unit; + } - /*! - * \brief Rounded value in given unit - * \param unit - * \param digits - * \return - */ - double valueRounded(const MU &unit, int digits = -1) const; + /*! + * \brief Simply set unit, do no calclulate conversion + * \param unit + * \sa switchUnit + */ + void setUnit(const MU &unit) + { + this->m_unit = unit; + } - /*! - * \brief Rounded value in current unit - * \param digits - * \return - */ - double valueRounded(int digits = -1) const - { - return this->valueRounded(this->m_unit, digits); - } + /*! + * \brief Set unit by string + * \param unitName + */ + void setUnitByString(const QString &unitName) + { + this->m_unit = CMeasurementUnit::unitFromSymbol(unitName); + } - /*! - * \brief Value to QString with the given unit, e.g. "5.00m" - * \param unit - * \param digits - * \param i18n - * \return - */ - QString valueRoundedWithUnit(const MU &unit, int digits = -1, bool i18n = false) const; + /*! + * \brief Unit + * \return + */ + QString getUnitSymbol() const + { + return this->m_unit.getSymbol(true); + } - /*! - * \brief Value to QString with the current unit, e.g. "5.00m" - * \param digits - * \param i18n - * \return - */ - QString valueRoundedWithUnit(int digits = -1, bool i18n = false) const - { - return this->valueRoundedWithUnit(this->m_unit, digits, i18n); - } + /*! + * \brief Change unit, and convert value to maintain the same quantity + * \param newUnit + * \return + */ + PQ &switchUnit(const MU &newUnit); - /*! - * \brief Change value without changing unit - * \param value - */ - void setValueSameUnit(double value); + /*! + * \brief Value in given unit + * \param unit + * \return + */ + double value(const MU &unit) const; - /*! - * \brief Add to the value in the current unit. - * \param value - */ - void addValueSameUnit(double value); + /*! + * \brief Value in current unit + * \return + */ + double value() const + { + return this->m_value; + } - /*! - * \brief Substract from the value in the current unit. - * \param value - */ - void substractValueSameUnit(double value); + /*! + * \brief Set value in current unit + * \param value + */ + void setCurrentUnitValue(double value) + { + this->m_value = value; + } - /*! - * \brief Multiply operator *= - * \param multiply - * \return - */ - CPhysicalQuantity &operator *=(double multiply); + /*! + * \brief Rounded value in given unit + * \param unit + * \param digits + * \return + */ + double valueRounded(const MU &unit, int digits = -1) const; - /*! - * \brief Divide operator /= - * \param divide - * \return - */ - CPhysicalQuantity &operator /=(double divide); + /*! + * \brief Rounded value in current unit + * \param digits + * \return + */ + double valueRounded(int digits = -1) const + { + return this->valueRounded(this->m_unit, digits); + } - /*! - * \brief Operator * - * \param multiply - * \return - */ - PQ operator *(double multiply) const; + /*! + * \brief Value to QString with the given unit, e.g. "5.00m" + * \param unit + * \param digits + * \param i18n + * \return + */ + QString valueRoundedWithUnit(const MU &unit, int digits = -1, bool i18n = false) const; - /*! - * \brief Operator to support commutative multiplication - * \param factor - * \param other - * \return - */ - friend PQ operator *(double factor, const PQ &other) - { - return other * factor; - } + /*! + * \brief Value to QString with the current unit, e.g. "5.00m" + * \param digits + * \param i18n + * \return + */ + QString valueRoundedWithUnit(int digits = -1, bool i18n = false) const + { + return this->valueRoundedWithUnit(this->m_unit, digits, i18n); + } - /*! - * \brief Operator / - * \param divide - * \return - */ - PQ operator /(double divide) const; + /*! + * \brief Change value without changing unit + * \param value + */ + void setValueSameUnit(double value); - /*! - * \brief Equal operator == - * \param other - * \return - */ - bool operator==(const CPhysicalQuantity &other) const; + /*! + * \brief Add to the value in the current unit. + * \param value + */ + void addValueSameUnit(double value); - /*! - * \brief Not equal operator != - * \param other - * \return - */ - bool operator!=(const CPhysicalQuantity &other) const; + /*! + * \brief Substract from the value in the current unit. + * \param value + */ + void substractValueSameUnit(double value); - /*! - * \brief Plus operator += - * \param other - * \return - */ - CPhysicalQuantity &operator +=(const CPhysicalQuantity &other); + /*! + * \brief Multiply operator *= + * \param multiply + * \return + */ + CPhysicalQuantity &operator *=(double multiply); - /*! - * \brief Minus operator-= - * \param other - * \return - */ - CPhysicalQuantity &operator -=(const CPhysicalQuantity &other); + /*! + * \brief Divide operator /= + * \param divide + * \return + */ + CPhysicalQuantity &operator /=(double divide); - /*! - * \brief Greater operator > - * \param other - * \return - */ - bool operator >(const CPhysicalQuantity &other) const; + /*! + * \brief Operator * + * \param multiply + * \return + */ + PQ operator *(double multiply) const; - /*! - * \brief Less operator < - * \param other - * \return - */ - bool operator <(const CPhysicalQuantity &other) const; + /*! + * \brief Operator to support commutative multiplication + * \param factor + * \param other + * \return + */ + friend PQ operator *(double factor, const PQ &other) + { + return other * factor; + } - /*! - * \brief Less equal operator <= - * \param other - * \return - */ - bool operator <=(const CPhysicalQuantity &other) const; + /*! + * \brief Operator / + * \param divide + * \return + */ + PQ operator /(double divide) const; - /*! - * \brief Greater equal operator >= - * \param other - * \return - */ - bool operator >=(const CPhysicalQuantity &other) const; + /*! + * \brief Equal operator == + * \param other + * \return + */ + bool operator==(const CPhysicalQuantity &other) const; - /*! - * \brief Plus operator + - * \param other - * \return - */ - PQ operator +(const PQ &other) const; + /*! + * \brief Not equal operator != + * \param other + * \return + */ + bool operator!=(const CPhysicalQuantity &other) const; - /*! - * \brief Minus operator - - * \param other - * \return - */ - PQ operator -(const PQ &other) const; + /*! + * \brief Plus operator += + * \param other + * \return + */ + CPhysicalQuantity &operator +=(const CPhysicalQuantity &other); - /*! - * \brief Quantity value <= epsilon - * \return - */ - bool isZeroEpsilon() const - { - return this->m_unit.isEpsilon(this->m_value); - } + /*! + * \brief Minus operator-= + * \param other + * \return + */ + CPhysicalQuantity &operator -=(const CPhysicalQuantity &other); - /*! - * \brief Value >= 0 epsilon considered - * \return - */ - bool isNonNegativeEpsilon() const - { - return this->isZeroEpsilon() || this->m_value > 0; - } + /*! + * \brief Greater operator > + * \param other + * \return + */ + bool operator >(const CPhysicalQuantity &other) const; - /*! - * \brief Value <= 0 epsilon considered - * \return - */ - bool isNonPositiveEpsilon() const - { - return this->isZeroEpsilon() || this->m_value < 0; - } + /*! + * \brief Less operator < + * \param other + * \return + */ + bool operator <(const CPhysicalQuantity &other) const; - /*! - * \brief Stream to DBus << - * \param argument - */ - virtual void marshallToDbus(QDBusArgument &argument) const - { - argument << this->value(UnitClass::defaultUnit()); - argument << this->m_value; - argument << this->m_unit; - } + /*! + * \brief Less equal operator <= + * \param other + * \return + */ + bool operator <=(const CPhysicalQuantity &other) const; - /*! - * \brief Stream from DBus >> - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - double ignore; - argument >> ignore; - argument >> this->m_value; - argument >> this->m_unit; - } + /*! + * \brief Greater equal operator >= + * \param other + * \return + */ + bool operator >=(const CPhysicalQuantity &other) const; - /*! - * \brief Register metadata of unit and quantity - */ - static void registerMetadata() - { - qRegisterMetaType(typeid(MU).name()); - qDBusRegisterMetaType(); - qDBusRegisterMetaType >(); - qRegisterMetaType(typeid(PQ).name()); - qDBusRegisterMetaType(); - qDBusRegisterMetaType >(); - } -}; + /*! + * \brief Plus operator + + * \param other + * \return + */ + PQ operator +(const PQ &other) const; -} // namespace + /*! + * \brief Minus operator - + * \param other + * \return + */ + PQ operator -(const PQ &other) const; + + /*! + * \brief Quantity value <= epsilon + * \return + */ + bool isZeroEpsilonConsidered() const + { + return this->m_unit.isEpsilon(this->m_value); + } + + /*! + * \brief Value >= 0 epsilon considered + * \return + */ + bool isPositiveWithEpsilonConsidered() const + { + return !this->isZeroEpsilonConsidered() && this->m_value > 0; + } + + /*! + * \brief Value <= 0 epsilon considered + * \return + */ + bool isNegativeWithEpsilonConsidered() const + { + return !this->isZeroEpsilonConsidered() && this->m_value < 0; + } + + /*! + * \brief Stream to DBus << + * \param argument + */ + virtual void marshallToDbus(QDBusArgument &argument) const; + + /*! + * \brief Stream from DBus >> + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); + + /*! + * \brief Register metadata of unit and quantity + */ + static void registerMetadata(); + + }; + + } // namespace } // namespace #endif // guard diff --git a/src/blackmisc/pqpressure.h b/src/blackmisc/pqpressure.h index febb338b1..09929932e 100644 --- a/src/blackmisc/pqpressure.h +++ b/src/blackmisc/pqpressure.h @@ -10,35 +10,35 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { -/*! - * \brief Physical unit distance - * \author KWB - */ -class CPressure : public CPhysicalQuantity -{ -public: - /*! - * \brief Default constructor - */ - CPressure() : CPhysicalQuantity(0, CPressureUnit::defaultUnit()) {} + /*! + * \brief Physical unit distance + * \author KWB + */ + class CPressure : public CPhysicalQuantity + { + public: + /*! + * \brief Default constructor + */ + CPressure() : CPhysicalQuantity(0, CPressureUnit::defaultUnit()) {} - /*! - *\brief Init by double value - * \param value - * \param unit - */ - CPressure(double value, const CPressureUnit &unit) : CPhysicalQuantity(value, unit) {} + /*! + *\brief Init by double value + * \param value + * \param unit + */ + CPressure(double value, const CPressureUnit &unit) : CPhysicalQuantity(value, unit) {} - /*! - * \brief Virtual destructor - */ - virtual ~CPressure() {} -}; + /*! + * \brief Virtual destructor + */ + virtual ~CPressure() {} + }; -} // namespace + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CPressure) diff --git a/src/blackmisc/pqspeed.h b/src/blackmisc/pqspeed.h index a783b4b9c..b010d4103 100644 --- a/src/blackmisc/pqspeed.h +++ b/src/blackmisc/pqspeed.h @@ -9,36 +9,35 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { -/*! - * \brief Speed class, e.g. "m/s", "NM/h", "km/h", "ft/s" - * \author KWB - */ -class CSpeed : public CPhysicalQuantity -{ + /*! + * \brief Speed class, e.g. "m/s", "NM/h", "km/h", "ft/s" + */ + class CSpeed : public CPhysicalQuantity + { -public: - /*! - * \brief Default constructor - */ - CSpeed() : CPhysicalQuantity(0, CSpeedUnit::defaultUnit()) {} + public: + /*! + * \brief Default constructor + */ + CSpeed() : CPhysicalQuantity(0, CSpeedUnit::defaultUnit()) {} - /*! - *\brief Init by double value - * \param value - * \param unit - */ - CSpeed(double value, const CSpeedUnit &unit) : CPhysicalQuantity(value, unit) {} + /*! + *\brief Init by double value + * \param value + * \param unit + */ + CSpeed(double value, const CSpeedUnit &unit) : CPhysicalQuantity(value, unit) {} - /*! - * \brief Destructor - */ - virtual ~CSpeed() {} -}; + /*! + * \brief Destructor + */ + virtual ~CSpeed() {} + }; -} // namespace + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CSpeed) diff --git a/src/blackmisc/pqtemperature.h b/src/blackmisc/pqtemperature.h index a94540967..42c11decf 100644 --- a/src/blackmisc/pqtemperature.h +++ b/src/blackmisc/pqtemperature.h @@ -9,35 +9,35 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { -/*! - * \brief Physical unit temperature - * \author KWB - */ -class CTemperature : public CPhysicalQuantity -{ -public: - /*! - * \brief Default constructor - */ - CTemperature() : CPhysicalQuantity(0, CTemperatureUnit::defaultUnit()) {} + /*! + * \brief Physical unit temperature + * \author KWB + */ + class CTemperature : public CPhysicalQuantity + { + public: + /*! + * \brief Default constructor + */ + CTemperature() : CPhysicalQuantity(0, CTemperatureUnit::defaultUnit()) {} - /*! - * \brief Init by double value - * \param value - * \param unit - */ - CTemperature(double value, const CTemperatureUnit &unit): CPhysicalQuantity(value, unit) {} + /*! + * \brief Init by double value + * \param value + * \param unit + */ + CTemperature(double value, const CTemperatureUnit &unit): CPhysicalQuantity(value, unit) {} - /*! - * \brief Destructor - */ - virtual ~CTemperature() {} -}; + /*! + * \brief Destructor + */ + virtual ~CTemperature() {} + }; -} // namespace + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CTemperature) diff --git a/src/blackmisc/pqtime.h b/src/blackmisc/pqtime.h index 8ecd81fc7..eb5540514 100644 --- a/src/blackmisc/pqtime.h +++ b/src/blackmisc/pqtime.h @@ -10,35 +10,35 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { -/*! - * \brief Time class, e.g. "ms", "hour", "s", "day" - * \author KWB - */ -class CTime : public CPhysicalQuantity -{ -public: - /*! - * \brief Default constructor - */ - CTime() : CPhysicalQuantity(0, CTimeUnit::defaultUnit()) {} + /*! + * \brief Time class, e.g. "ms", "hour", "s", "day" + * \author KWB + */ + class CTime : public CPhysicalQuantity + { + public: + /*! + * \brief Default constructor + */ + CTime() : CPhysicalQuantity(0, CTimeUnit::defaultUnit()) {} - /*! - *\brief Init by double value - * \param value - * \param unit - */ - CTime(double value, const CTimeUnit &unit) : CPhysicalQuantity(value, unit) {} + /*! + *\brief Init by double value + * \param value + * \param unit + */ + CTime(double value, const CTimeUnit &unit) : CPhysicalQuantity(value, unit) {} - /*! - * \brief Destructor - */ - virtual ~CTime() {} -}; + /*! + * \brief Destructor + */ + virtual ~CTime() {} + }; -} // namespace + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CTime) diff --git a/src/blackmisc/pqunits.cpp b/src/blackmisc/pqunits.cpp index e03e8f00b..ed7202835 100644 --- a/src/blackmisc/pqunits.cpp +++ b/src/blackmisc/pqunits.cpp @@ -7,90 +7,89 @@ namespace BlackMisc { -namespace PhysicalQuantities -{ + namespace PhysicalQuantities + { + using BlackMisc::Math::CMath; -using BlackMisc::Math::CMath; + /* + * Rounded to QString + */ + QString CAngleUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const + { + if (digits < 0) digits = this->getDisplayDigits(); + QString s; + if ((*this) == CAngleUnit::sexagesimalDeg()) + { + digits -= 4; + Q_ASSERT(digits >= 0); + double de = CMath::trunc(value); + double mi = CMath::trunc((value - de) * 100.0); + double se = CMath::trunc((value - de - mi / 100.0) * 1000000) / 100.0; + const char *fmt = value < 0 ? "-%L1 %L2 %L3" : "%L1 %L2 %L3"; + s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; + s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 2, 'f', digits, '0'); + } + else if ((*this) == CAngleUnit::sexagesimalDegMin()) + { + digits -= 2; + Q_ASSERT(digits >= 0); + double de = CMath::trunc(value); + double mi = CMath::trunc((value - de) * 100.0); + const char *fmt = value < 0 ? "-%L1 %L2" : "%L1 %L2"; + s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; + s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', digits, '0'); + } + else + { + s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, i18n); + } + return s; + } -/* - * Rounded to QString - */ -QString CAngleUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const -{ - if (digits < 0) digits = this->getDisplayDigits(); - QString s; - if ((*this) == CAngleUnit::sexagesimalDeg()) - { - digits -= 4; - Q_ASSERT(digits >= 0); - double de = CMath::trunc(value); - double mi = CMath::trunc((value - de) * 100.0); - double se = CMath::trunc((value - de - mi / 100.0) * 1000000) / 100.0; - const char *fmt = value < 0 ? "-%L1 %L2 %L3" : "%L1 %L2 %L3"; - s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; - s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 2, 'f', digits, '0'); - } - else if ((*this) == CAngleUnit::sexagesimalDegMin()) - { - digits -= 2; - Q_ASSERT(digits >= 0); - double de = CMath::trunc(value); - double mi = CMath::trunc((value - de) * 100.0); - const char *fmt = value < 0 ? "-%L1 %L2" : "%L1 %L2"; - s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; - s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', digits, '0'); - } - else - { - s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, i18n); - } - return s; -} - -/* - * Rounded to QString - */ -QString CTimeUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const -{ - if (digits < 0) digits = this->getDisplayDigits(); - QString s; - if ((*this) == CTimeUnit::hms()) - { - digits -= 4; - Q_ASSERT(digits >= 0); - double hr = CMath::trunc(value); - double mi = CMath::trunc((value - hr) * 100.0); - double se = CMath::trunc((value - hr - mi / 100.0) * 1000000) / 100.0; - const char *fmt = value < 0 ? "-%L1h%L2m%L3s" : "%L1h%L2m%L3s"; - s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; - s = s.arg(fabs(hr), 0, 'f', 0).arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 2, 'f', digits, '0'); - } - else if ((*this) == CTimeUnit::hrmin()) - { - digits -= 2; - Q_ASSERT(digits >= 0); - double hr = CMath::trunc(value); - double mi = CMath::trunc((value - hr) * 100.0); - const char *fmt = value < 0 ? "-%L1h%L2m" : "%L1h%L2m"; - s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; - s = s.arg(fabs(hr), 0, 'f', 0).arg(fabs(mi), 2, 'f', digits, '0'); - } - else if ((*this) == CTimeUnit::minsec()) - { - digits -= 2; - Q_ASSERT(digits >= 0); - double mi = CMath::trunc(value); - double se = CMath::trunc((value - mi) * 100.0); - const char *fmt = value < 0 ? "-%L2m%L3s" : "%L2m%L3s"; - s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; - s = s.arg(fabs(mi), 0, 'f', 0).arg(fabs(se), 2, 'f', digits, '0'); - } - else - { - s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, i18n); - } - return s; -} + /* + * Rounded to QString + */ + QString CTimeUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const + { + if (digits < 0) digits = this->getDisplayDigits(); + QString s; + if ((*this) == CTimeUnit::hms()) + { + digits -= 4; + Q_ASSERT(digits >= 0); + double hr = CMath::trunc(value); + double mi = CMath::trunc((value - hr) * 100.0); + double se = CMath::trunc((value - hr - mi / 100.0) * 1000000) / 100.0; + const char *fmt = value < 0 ? "-%L1h%L2m%L3s" : "%L1h%L2m%L3s"; + s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; + s = s.arg(fabs(hr), 0, 'f', 0).arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 2, 'f', digits, '0'); + } + else if ((*this) == CTimeUnit::hrmin()) + { + digits -= 2; + Q_ASSERT(digits >= 0); + double hr = CMath::trunc(value); + double mi = CMath::trunc((value - hr) * 100.0); + const char *fmt = value < 0 ? "-%L1h%L2m" : "%L1h%L2m"; + s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; + s = s.arg(fabs(hr), 0, 'f', 0).arg(fabs(mi), 2, 'f', digits, '0'); + } + else if ((*this) == CTimeUnit::minsec()) + { + digits -= 2; + Q_ASSERT(digits >= 0); + double mi = CMath::trunc(value); + double se = CMath::trunc((value - mi) * 100.0); + const char *fmt = value < 0 ? "-%L2m%L3s" : "%L2m%L3s"; + s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt; + s = s.arg(fabs(mi), 0, 'f', 0).arg(fabs(se), 2, 'f', digits, '0'); + } + else + { + s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, i18n); + } + return s; + } -} // namespace + } // namespace } // namespace diff --git a/src/blackmisc/pqunits.h b/src/blackmisc/pqunits.h index 1cbdf1e72..c34536489 100644 --- a/src/blackmisc/pqunits.h +++ b/src/blackmisc/pqunits.h @@ -21,1014 +21,1020 @@ // namespace BlackMisc { -namespace PhysicalQuantities -{ - -/*! - * \brief Specialized class for distance units (meter, foot, nautical miles). - */ -class CLengthUnit : public CMeasurementUnit -{ -private: - template - CLengthUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} - - struct NauticalMilesToMeters { static double factor() { return 1852.0; } }; - struct FeetToMeters { static double factor() { return 0.3048; } }; - struct MilesToMeters { static double factor() { return 1609.344; } }; - struct StatuteMilesToMeters { static double factor() { return 1609.3472; } }; - typedef One MetersToMeters; - -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CLengthUnit() : CMeasurementUnit(defaultUnit()) {} - - /*! - * \brief Default unit - * \return - */ - static const CLengthUnit &defaultUnit() { return m(); } - - /*! - * \brief Meter m - * \return - */ - static const CLengthUnit &m() + namespace PhysicalQuantities { - static CLengthUnit m(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter"), "m", IdentityConverter()); - return m; - } - /*! - * \brief Nautical miles NM - * \return - */ - static const CLengthUnit &NM() - { - static CLengthUnit NM(QT_TRANSLATE_NOOP("CMeasurementUnit", "nautical mile"), "NM", LinearConverter(), 3); - return NM; - } - - /*! - * \brief Foot ft - * \return - */ - static const CLengthUnit &ft() - { - static CLengthUnit ft(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot"), "ft", LinearConverter(), 0); - return ft; - } - - /*! - * \brief Kilometer km - * \return - */ - static const CLengthUnit &km() - { - static CLengthUnit km(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilometer"), "km", LinearConverter>(), 3); - return km; - } - - /*! - * \brief Centimeter cm - * \return - */ - static const CLengthUnit &cm() - { - static CLengthUnit cm(QT_TRANSLATE_NOOP("CMeasurementUnit", "centimeter"), "cm", LinearConverter>(), 1); - return cm; - } - - /*! - * \brief International mile - * \return - */ - static const CLengthUnit &mi() - { - static CLengthUnit mi(QT_TRANSLATE_NOOP("CMeasurementUnit", "mile"), "mi", LinearConverter(), 3); - return mi; - } - - /*! - * \brief Statute mile - * \return - */ - static const CLengthUnit &SM() - { - static CLengthUnit sm(QT_TRANSLATE_NOOP("CMeasurementUnit", "statute mile"), "SM", LinearConverter(), 3); - return sm; - } - - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + /*! + * \brief Specialized class for distance units (meter, foot, nautical miles). + */ + class CLengthUnit : public CMeasurementUnit { - u.append(CLengthUnit::cm()); - u.append(CLengthUnit::ft()); - u.append(CLengthUnit::km()); - u.append(CLengthUnit::m()); - u.append(CLengthUnit::mi()); - u.append(CLengthUnit::SM()); - u.append(CLengthUnit::NM()); - } - return u; - } + private: + template + CLengthUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + struct NauticalMilesToMeters { static double factor() { return 1852.0; } }; + struct FeetToMeters { static double factor() { return 0.3048; } }; + struct MilesToMeters { static double factor() { return 1609.344; } }; + struct StatuteMilesToMeters { static double factor() { return 1609.3472; } }; + typedef One MetersToMeters; -/*! - * \brief Specialized class for angles (degrees, radian). - */ -class CAngleUnit : public CMeasurementUnit -{ -private: - template - CAngleUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CLengthUnit() : CMeasurementUnit(defaultUnit()) {} - struct RadiansToDegrees { static double factor() { return 180.0 / M_PI; } }; - typedef One DegreesToDegrees; + /*! + * \brief Default unit + * \return + */ + static const CLengthUnit &defaultUnit() { return m(); } -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CAngleUnit() : CMeasurementUnit(defaultUnit()) {} + /*! + * \brief Meter m + * \return + */ + static const CLengthUnit &m() + { + static CLengthUnit m(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter"), "m", IdentityConverter()); + return m; + } - /*! - * \brief Default unit - * \return - */ - static const CAngleUnit &defaultUnit() { return deg(); } + /*! + * \brief Nautical miles NM + * \return + */ + static const CLengthUnit &NM() + { + static CLengthUnit NM(QT_TRANSLATE_NOOP("CMeasurementUnit", "nautical mile"), "NM", LinearConverter(), 3); + return NM; + } - /*! - * \brief Override for sexagesimal degrees. - * \param value - * \param digits - * \param i18n - * \return - */ - virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const; + /*! + * \brief Foot ft + * \return + */ + static const CLengthUnit &ft() + { + static CLengthUnit ft(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot"), "ft", LinearConverter(), 0); + return ft; + } - /*! - * \brief Radians - * \return - */ - static const CAngleUnit &rad() - { - static CAngleUnit rad(QT_TRANSLATE_NOOP("CMeasurementUnit", "radian"), "rad", LinearConverter()); - return rad; - } + /*! + * \brief Kilometer km + * \return + */ + static const CLengthUnit &km() + { + static CLengthUnit km(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilometer"), "km", LinearConverter >(), 3); + return km; + } - /*! - * \brief Degrees - * \return - */ - static const CAngleUnit °() - { - static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree"), QT_TRANSLATE_NOOP("CMeasurementUnit", "deg"), IdentityConverter()); - return deg; - } + /*! + * \brief Centimeter cm + * \return + */ + static const CLengthUnit &cm() + { + static CLengthUnit cm(QT_TRANSLATE_NOOP("CMeasurementUnit", "centimeter"), "cm", LinearConverter >(), 1); + return cm; + } - /*! - * \brief Sexagesimal degree (degrees, minutes, seconds, decimal seconds) - * \return - */ - static const CAngleUnit &sexagesimalDeg() - { - static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute, second"), "DMS", SubdivisionConverter2>(), 4); - return deg; - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2 %L3"); - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2 %L3"); - } + /*! + * \brief International mile + * \return + */ + static const CLengthUnit &mi() + { + static CLengthUnit mi(QT_TRANSLATE_NOOP("CMeasurementUnit", "mile"), "mi", LinearConverter(), 3); + return mi; + } - /*! - * \brief Sexagesimal degree (degrees, minutes, decimal minutes) - * \return - */ - static const CAngleUnit &sexagesimalDegMin() - { - static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute"), "MinDec", SubdivisionConverter>(), 4); - return deg; - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2"); - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2"); - } + /*! + * \brief Statute mile + * \return + */ + static const CLengthUnit &SM() + { + static CLengthUnit sm(QT_TRANSLATE_NOOP("CMeasurementUnit", "statute mile"), "SM", LinearConverter(), 3); + return sm; + } - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CLengthUnit::cm()); + u.append(CLengthUnit::ft()); + u.append(CLengthUnit::km()); + u.append(CLengthUnit::m()); + u.append(CLengthUnit::mi()); + u.append(CLengthUnit::SM()); + u.append(CLengthUnit::NM()); + } + return u; + } + + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; + + /*! + * \brief Specialized class for angles (degrees, radian). + */ + class CAngleUnit : public CMeasurementUnit { - u.append(CAngleUnit::deg()); - u.append(CAngleUnit::rad()); - u.append(CAngleUnit::sexagesimalDeg()); - u.append(CAngleUnit::sexagesimalDegMin()); - } - return u; - } + private: + template + CAngleUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + struct RadiansToDegrees { static double factor() { return 180.0 / M_PI; } }; + typedef One DegreesToDegrees; -/*! - * \brief Specialized class for frequency (hertz, mega hertz, kilo hertz). - */ -class CFrequencyUnit : public CMeasurementUnit -{ -private: - template - CFrequencyUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CAngleUnit() : CMeasurementUnit(defaultUnit()) {} - typedef One HertzToHertz; + /*! + * \brief Default unit + * \return + */ + static const CAngleUnit &defaultUnit() { return deg(); } -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CFrequencyUnit() : CMeasurementUnit(defaultUnit()) {} + /*! + * \brief Override for sexagesimal degrees. + * \param value + * \param digits + * \param i18n + * \return + */ + virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const; - /*! - * \brief Default unit - * \return - */ - static const CFrequencyUnit &defaultUnit() { return Hz(); } + /*! + * \brief Radians + * \return + */ + static const CAngleUnit &rad() + { + static CAngleUnit rad(QT_TRANSLATE_NOOP("CMeasurementUnit", "radian"), "rad", LinearConverter()); + return rad; + } - /*! - * \brief Hertz - * \return - */ - static const CFrequencyUnit &Hz() - { - static CFrequencyUnit Hz(QT_TRANSLATE_NOOP("CMeasurementUnit", "hertz"), "Hz", IdentityConverter()); - return Hz; - } + /*! + * \brief Degrees + * \return + */ + static const CAngleUnit °() + { + static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree"), QT_TRANSLATE_NOOP("CMeasurementUnit", "deg"), IdentityConverter()); + return deg; + } - /*! - * \brief Kilohertz - * \return - */ - static const CFrequencyUnit &kHz() - { - static CFrequencyUnit kHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilohertz"), "kHz", LinearConverter>(), 1); - return kHz; - } + /*! + * \brief Sexagesimal degree (degrees, minutes, seconds, decimal seconds) + * \return + */ + static const CAngleUnit &sexagesimalDeg() + { + static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute, second"), "DMS", SubdivisionConverter2 >(), 4); + return deg; + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2 %L3"); + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2 %L3"); + } - /*! - * \brief Megahertz - * \return - */ - static const CFrequencyUnit &MHz() - { - static CFrequencyUnit MHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "megahertz"), "MHz", LinearConverter>(), 2); - return MHz; - } + /*! + * \brief Sexagesimal degree (degrees, minutes, decimal minutes) + * \return + */ + static const CAngleUnit &sexagesimalDegMin() + { + static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute"), "MinDec", SubdivisionConverter >(), 4); + return deg; + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2"); + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2"); + } - /*! - * \brief Gigahertz - * \return - */ - static const CFrequencyUnit &GHz() - { - static CFrequencyUnit GHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "gigahertz"), "GHz", LinearConverter>(), 2); - return GHz; - } + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CAngleUnit::deg()); + u.append(CAngleUnit::rad()); + u.append(CAngleUnit::sexagesimalDeg()); + u.append(CAngleUnit::sexagesimalDegMin()); + } + return u; + } - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; + + /*! + * \brief Specialized class for frequency (hertz, mega hertz, kilo hertz). + */ + class CFrequencyUnit : public CMeasurementUnit { - u.append(CFrequencyUnit::GHz()); - u.append(CFrequencyUnit::Hz()); - u.append(CFrequencyUnit::kHz()); - u.append(CFrequencyUnit::MHz()); - } - return u; - } + private: + template + CFrequencyUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + typedef One HertzToHertz; -/*! - * \brief Specialized class for mass units (kg, lbs). - */ -class CMassUnit : public CMeasurementUnit -{ -private: - template - CMassUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CFrequencyUnit() : CMeasurementUnit(defaultUnit()) {} - typedef Milli GramsToKilograms; - struct PoundsToKilograms { static double factor() { return 0.45359237; } }; + /*! + * \brief Default unit + * \return + */ + static const CFrequencyUnit &defaultUnit() { return Hz(); } -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CMassUnit() : CMeasurementUnit(defaultUnit()) {} + /*! + * \brief Hertz + * \return + */ + static const CFrequencyUnit &Hz() + { + static CFrequencyUnit Hz(QT_TRANSLATE_NOOP("CMeasurementUnit", "hertz"), "Hz", IdentityConverter()); + return Hz; + } - /*! - * \brief Default unit - * \return - */ - static const CMassUnit &defaultUnit() { return kg(); } + /*! + * \brief Kilohertz + * \return + */ + static const CFrequencyUnit &kHz() + { + static CFrequencyUnit kHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilohertz"), "kHz", LinearConverter >(), 1); + return kHz; + } - /*! - * \brief Kilogram, SI base unit - * \return - */ - static const CMassUnit &kg() - { - static CMassUnit kg(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilogram"), "kg", IdentityConverter(), 1); - return kg; - } + /*! + * \brief Megahertz + * \return + */ + static const CFrequencyUnit &MHz() + { + static CFrequencyUnit MHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "megahertz"), "MHz", LinearConverter >(), 2); + return MHz; + } - /*! - * \brief Gram, SI unit - * \return - */ - static const CMassUnit &g() - { - static CMassUnit g(QT_TRANSLATE_NOOP("CMeasurementUnit", "gram"), "g", LinearConverter(), 0); - return g; - } + /*! + * \brief Gigahertz + * \return + */ + static const CFrequencyUnit &GHz() + { + static CFrequencyUnit GHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "gigahertz"), "GHz", LinearConverter >(), 2); + return GHz; + } - /*! - * \brief Tonne, aka metric ton (1000kg) - * \return - */ - static const CMassUnit &tonne() - { - static CMassUnit t(QT_TRANSLATE_NOOP("CMeasurementUnit", "tonne"), "t", LinearConverter>(), 3); - return t; - } + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CFrequencyUnit::GHz()); + u.append(CFrequencyUnit::Hz()); + u.append(CFrequencyUnit::kHz()); + u.append(CFrequencyUnit::MHz()); + } + return u; + } - /*! - * \brief Short ton (2000lb) used in the United States - * \return - */ - static const CMassUnit &shortTon() - { - static CMassUnit ton(QT_TRANSLATE_NOOP("CMeasurementUnit", "short ton"), "ton", LinearConverter>>(), 3); - return ton; - } + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; - /*! - * \brief Pound, aka mass pound - * \return - */ - static const CMassUnit &lb() - { - static CMassUnit lbs(QT_TRANSLATE_NOOP("CMeasurementUnit", "pound"), "lb", LinearConverter(), 1); - return lbs; - } - - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + /*! + * \brief Specialized class for mass units (kg, lbs). + */ + class CMassUnit : public CMeasurementUnit { - u.append(CMassUnit::g()); - u.append(CMassUnit::kg()); - u.append(CMassUnit::lb()); - u.append(CMassUnit::tonne()); - u.append(CMassUnit::shortTon()); - } - return u; - } + private: + template + CMassUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + typedef Milli GramsToKilograms; + struct PoundsToKilograms { static double factor() { return 0.45359237; } }; -/*! - * \brief Specialized class for pressure (psi, hPa, bar). - */ -class CPressureUnit : public CMeasurementUnit -{ -private: - template - CPressureUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CMassUnit() : CMeasurementUnit(defaultUnit()) {} - typedef Centi PascalsToHectopascals; - struct PsiToHectopascals { static double factor() { return 68.948; } }; - struct InchesToHectopascals { static double factor() { return 33.86389; } }; - struct MillimetersToHectopascals { static double factor() { return 860.142806; } }; + /*! + * \brief Default unit + * \return + */ + static const CMassUnit &defaultUnit() { return kg(); } -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CPressureUnit() : CMeasurementUnit(defaultUnit()) {} + /*! + * \brief Kilogram, SI base unit + * \return + */ + static const CMassUnit &kg() + { + static CMassUnit kg(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilogram"), "kg", IdentityConverter(), 1); + return kg; + } - /*! - * \brief Default unit - * \return - */ - static const CPressureUnit &defaultUnit() { return hPa(); } + /*! + * \brief Gram, SI unit + * \return + */ + static const CMassUnit &g() + { + static CMassUnit g(QT_TRANSLATE_NOOP("CMeasurementUnit", "gram"), "g", LinearConverter(), 0); + return g; + } - /*! - * \brief Pascal - * \return - */ - static const CPressureUnit &Pa() - { - static CPressureUnit Pa(QT_TRANSLATE_NOOP("CMeasurementUnit", "pascal"), "Pa", LinearConverter()); - return Pa; - } + /*! + * \brief Tonne, aka metric ton (1000kg) + * \return + */ + static const CMassUnit &tonne() + { + static CMassUnit t(QT_TRANSLATE_NOOP("CMeasurementUnit", "tonne"), "t", LinearConverter >(), 3); + return t; + } - /*! - * \brief Hectopascal - * \return - */ - static const CPressureUnit &hPa() - { - static CPressureUnit hPa(QT_TRANSLATE_NOOP("CMeasurementUnit", "hectopascal"), "hPa", IdentityConverter()); - return hPa; - } + /*! + * \brief Short ton (2000lb) used in the United States + * \return + */ + static const CMassUnit &shortTon() + { + static CMassUnit ton(QT_TRANSLATE_NOOP("CMeasurementUnit", "short ton"), "ton", LinearConverter > >(), 3); + return ton; + } - /*! - * \brief Pounds per square inch - * \return - */ - static const CPressureUnit &psi() - { - static CPressureUnit psi(QT_TRANSLATE_NOOP("CMeasurementUnit", "pound per square inch"), "psi", LinearConverter(), 2); - return psi; - } + /*! + * \brief Pound, aka mass pound + * \return + */ + static const CMassUnit &lb() + { + static CMassUnit lbs(QT_TRANSLATE_NOOP("CMeasurementUnit", "pound"), "lb", LinearConverter(), 1); + return lbs; + } - /*! - * \brief Bar - * \return - */ - static const CPressureUnit &bar() - { - static CPressureUnit bar(QT_TRANSLATE_NOOP("CMeasurementUnit", "bar"), "bar", LinearConverter>(), 1); - return bar; - } + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CMassUnit::g()); + u.append(CMassUnit::kg()); + u.append(CMassUnit::lb()); + u.append(CMassUnit::tonne()); + u.append(CMassUnit::shortTon()); + } + return u; + } - /*! - * \brief Millibar, actually the same as hPa - * \return - */ - static const CPressureUnit &mbar() - { - static CPressureUnit mbar(QT_TRANSLATE_NOOP("CMeasurementUnit", "millibar"), "mbar", IdentityConverter(), 1); - return mbar; - } + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; - /*! - * \brief Inch of mercury at 0°C - * \return - */ - static const CPressureUnit &inHg() - { - static CPressureUnit inhg(QT_TRANSLATE_NOOP("CMeasurementUnit", "inch of mercury"), "inHg", LinearConverter()); - return inhg; - } - - /*! - * \brief Millimeter of mercury - * \return - */ - static const CPressureUnit &mmHg() - { - static CPressureUnit mmhg(QT_TRANSLATE_NOOP("CMeasurementUnit", "millimeter of mercury"), "mmHg", LinearConverter()); - return mmhg; - } - - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + /*! + * \brief Specialized class for pressure (psi, hPa, bar). + */ + class CPressureUnit : public CMeasurementUnit { - u.append(CPressureUnit::bar()); - u.append(CPressureUnit::hPa()); - u.append(CPressureUnit::inHg()); - u.append(CPressureUnit::mmHg()); - u.append(CPressureUnit::mbar()); - u.append(CPressureUnit::psi()); - } - return u; - } + private: + template + CPressureUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + typedef Centi PascalsToHectopascals; + struct PsiToHectopascals { static double factor() { return 68.948; } }; + struct InchesToHectopascals { static double factor() { return 33.86389; } }; + struct MillimetersToHectopascals { static double factor() { return 860.142806; } }; -/*! - * \brief Specialized class for temperatur units (kelvin, centidegree). - */ -class CTemperatureUnit : public CMeasurementUnit -{ -private: - template - CTemperatureUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CPressureUnit() : CMeasurementUnit(defaultUnit()) {} - struct KelvinToCentigrade { static double factor() { return 1.0; } - static double offset() { return 273.15; } }; - struct FahrenheitToCentigrade { static double factor() { return 5.0 / 9.0; } - static double offset() { return 32.0; } }; + /*! + * \brief Default unit + * \return + */ + static const CPressureUnit &defaultUnit() { return hPa(); } -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CTemperatureUnit() : CMeasurementUnit(defaultUnit()) {} + /*! + * \brief Pascal + * \return + */ + static const CPressureUnit &Pa() + { + static CPressureUnit Pa(QT_TRANSLATE_NOOP("CMeasurementUnit", "pascal"), "Pa", LinearConverter()); + return Pa; + } - /*! - * \brief Default unit - * \return - */ - static const CTemperatureUnit &defaultUnit() { return C(); } + /*! + * \brief Hectopascal + * \return + */ + static const CPressureUnit &hPa() + { + static CPressureUnit hPa(QT_TRANSLATE_NOOP("CMeasurementUnit", "hectopascal"), "hPa", IdentityConverter()); + return hPa; + } - /*! - * \brief Kelvin - * \return - */ - static const CTemperatureUnit &K() - { - static CTemperatureUnit K(QT_TRANSLATE_NOOP("CMeasurementUnit", "Kelvin"), "K", AffineConverter()); - return K; - } + /*! + * \brief Pounds per square inch + * \return + */ + static const CPressureUnit &psi() + { + static CPressureUnit psi(QT_TRANSLATE_NOOP("CMeasurementUnit", "pound per square inch"), "psi", LinearConverter(), 2); + return psi; + } - /*! - * \brief Centigrade C - * \return - */ - static const CTemperatureUnit &C() - { - static CTemperatureUnit C(QT_TRANSLATE_NOOP("CMeasurementUnit", "centigrade"), QT_TRANSLATE_NOOP("CMeasurementUnit", "C"), IdentityConverter()); - return C; - } + /*! + * \brief Bar + * \return + */ + static const CPressureUnit &bar() + { + static CPressureUnit bar(QT_TRANSLATE_NOOP("CMeasurementUnit", "bar"), "bar", LinearConverter >(), 1); + return bar; + } - /*! - * \brief Fahrenheit F - * \return - */ - static const CTemperatureUnit &F() - { - static CTemperatureUnit F(QT_TRANSLATE_NOOP("CMeasurementUnit", "Fahrenheit"), QT_TRANSLATE_NOOP("CMeasurementUnit", "F"), AffineConverter()); - return F; - } + /*! + * \brief Millibar, actually the same as hPa + * \return + */ + static const CPressureUnit &mbar() + { + static CPressureUnit mbar(QT_TRANSLATE_NOOP("CMeasurementUnit", "millibar"), "mbar", IdentityConverter(), 1); + return mbar; + } - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + /*! + * \brief Inch of mercury at 0°C + * \return + */ + static const CPressureUnit &inHg() + { + static CPressureUnit inhg(QT_TRANSLATE_NOOP("CMeasurementUnit", "inch of mercury"), "inHg", LinearConverter()); + return inhg; + } + + /*! + * \brief Millimeter of mercury + * \return + */ + static const CPressureUnit &mmHg() + { + static CPressureUnit mmhg(QT_TRANSLATE_NOOP("CMeasurementUnit", "millimeter of mercury"), "mmHg", LinearConverter()); + return mmhg; + } + + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CPressureUnit::bar()); + u.append(CPressureUnit::hPa()); + u.append(CPressureUnit::inHg()); + u.append(CPressureUnit::mmHg()); + u.append(CPressureUnit::mbar()); + u.append(CPressureUnit::psi()); + } + return u; + } + + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; + + /*! + * \brief Specialized class for temperatur units (kelvin, centidegree). + */ + class CTemperatureUnit : public CMeasurementUnit { - u.append(CTemperatureUnit::C()); - u.append(CTemperatureUnit::F()); - u.append(CTemperatureUnit::K()); - } - return u; - } + private: + template + CTemperatureUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + struct KelvinToCentigrade + { + static double factor() { return 1.0; } + static double offset() { return 273.15; } + }; + struct FahrenheitToCentigrade + { + static double factor() { return 5.0 / 9.0; } + static double offset() { return 32.0; } + }; -/*! - * \brief Specialized class for speed units (m/s, ft/s, NM/h). - */ -class CSpeedUnit : public CMeasurementUnit -{ -private: - template - CSpeedUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CTemperatureUnit() : CMeasurementUnit(defaultUnit()) {} - struct KnotsToMps { static double factor() { return 1852.0 / 3600.0; } }; - struct KphToMps { static double factor() { return 1.0 / 3.6; } }; - struct FtPerSecToMps { static double factor() { return 0.3048 ; } }; - struct FtPerMinToMps { static double factor() { return 0.3048 / 60.0; } }; + /*! + * \brief Default unit + * \return + */ + static const CTemperatureUnit &defaultUnit() { return C(); } -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CSpeedUnit() : CMeasurementUnit(defaultUnit()) {} + /*! + * \brief Kelvin + * \return + */ + static const CTemperatureUnit &K() + { + static CTemperatureUnit K(QT_TRANSLATE_NOOP("CMeasurementUnit", "Kelvin"), "K", AffineConverter()); + return K; + } - /*! - * Default unit - * \return - */ - static const CSpeedUnit &defaultUnit() { return m_s(); } + /*! + * \brief Centigrade C + * \return + */ + static const CTemperatureUnit &C() + { + static CTemperatureUnit C(QT_TRANSLATE_NOOP("CMeasurementUnit", "centigrade"), QT_TRANSLATE_NOOP("CMeasurementUnit", "C"), IdentityConverter()); + return C; + } - /*! - * \brief Meter/second m/s - * \return - */ - static const CSpeedUnit &m_s() - { - static CSpeedUnit ms(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter per second"), "m/s", IdentityConverter()); - return ms; - } + /*! + * \brief Fahrenheit F + * \return + */ + static const CTemperatureUnit &F() + { + static CTemperatureUnit F(QT_TRANSLATE_NOOP("CMeasurementUnit", "Fahrenheit"), QT_TRANSLATE_NOOP("CMeasurementUnit", "F"), AffineConverter()); + return F; + } - /*! - * \brief Knots - * \return - */ - static const CSpeedUnit &kts() - { - static CSpeedUnit kts(QT_TRANSLATE_NOOP("CMeasurementUnit", "knot"), "kts", LinearConverter(), 1); - return kts; - } + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CTemperatureUnit::C()); + u.append(CTemperatureUnit::F()); + u.append(CTemperatureUnit::K()); + } + return u; + } - /*! - * \brief Nautical miles per hour NM/h (same as kts) - * \return - */ - static const CSpeedUnit &NM_h() - { - static CSpeedUnit NMh(QT_TRANSLATE_NOOP("CMeasurementUnit", "nautical mile per hour"), "NM/h", LinearConverter(), 1); - return NMh; - } + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; - /*! - * \brief Feet/second ft/s - * \return - */ - static const CSpeedUnit &ft_s() - { - static CSpeedUnit fts(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per second"), "ft/s", LinearConverter(), 0); - return fts; - } - - /*! - * \brief Feet/min ft/min - * \return - */ - static const CSpeedUnit &ft_min() - { - static CSpeedUnit ftmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per minute"), "ft/min", LinearConverter(), 0); - return ftmin; - } - - /*! - * \brief Kilometer/hour km/h - * \return - */ - static const CSpeedUnit &km_h() - { - static CSpeedUnit kmh(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilometer per hour"), "km/h", LinearConverter(), 1); - return kmh; - } - - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + /*! + * \brief Specialized class for speed units (m/s, ft/s, NM/h). + */ + class CSpeedUnit : public CMeasurementUnit { - u.append(CSpeedUnit::ft_min()); - u.append(CSpeedUnit::ft_s()); - u.append(CSpeedUnit::km_h()); - u.append(CSpeedUnit::kts()); - u.append(CSpeedUnit::m_s()); - u.append(CSpeedUnit::NM_h()); - } - return u; - } + private: + template + CSpeedUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + struct KnotsToMps { static double factor() { return 1852.0 / 3600.0; } }; + struct KphToMps { static double factor() { return 1.0 / 3.6; } }; + struct FtPerSecToMps { static double factor() { return 0.3048 ; } }; + struct FtPerMinToMps { static double factor() { return 0.3048 / 60.0; } }; -/*! - * \brief Specialized class for time units (ms, hour, min). - */ -class CTimeUnit : public CMeasurementUnit -{ -private: - template - CTimeUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CSpeedUnit() : CMeasurementUnit(defaultUnit()) {} - typedef One SecondsToSeconds; - struct DaysToSeconds { static double factor() { return 60.0 * 60.0 * 24.0; } }; - struct HoursToSeconds { static double factor() { return 60.0 * 60.0; } }; - struct MinutesToSeconds { static double factor() { return 60.0; } }; + /*! + * Default unit + * \return + */ + static const CSpeedUnit &defaultUnit() { return m_s(); } -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CTimeUnit() : CMeasurementUnit(defaultUnit()) {} + /*! + * \brief Meter/second m/s + * \return + */ + static const CSpeedUnit &m_s() + { + static CSpeedUnit ms(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter per second"), "m/s", IdentityConverter()); + return ms; + } - /*! - * Default unit - * \return - */ - static const CTimeUnit &defaultUnit() { return s(); } + /*! + * \brief Knots + * \return + */ + static const CSpeedUnit &kts() + { + static CSpeedUnit kts(QT_TRANSLATE_NOOP("CMeasurementUnit", "knot"), "kts", LinearConverter(), 1); + return kts; + } - /*! - * \brief Override for subdivisional units. - * \param value - * \param digits - * \param i18n - * \return - */ - virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const; + /*! + * \brief Nautical miles per hour NM/h (same as kts) + * \return + */ + static const CSpeedUnit &NM_h() + { + static CSpeedUnit NMh(QT_TRANSLATE_NOOP("CMeasurementUnit", "nautical mile per hour"), "NM/h", LinearConverter(), 1); + return NMh; + } - /*! - * \brief Second s - * \return - */ - static const CTimeUnit &s() - { - static CTimeUnit s(QT_TRANSLATE_NOOP("CMeasurementUnit", "second"), "s", IdentityConverter(), 1); - return s; - } + /*! + * \brief Feet/second ft/s + * \return + */ + static const CSpeedUnit &ft_s() + { + static CSpeedUnit fts(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per second"), "ft/s", LinearConverter(), 0); + return fts; + } - /*! - * \brief Millisecond ms - * \return - */ - static const CTimeUnit &ms() - { - static CTimeUnit ms(QT_TRANSLATE_NOOP("CMeasurementUnit", "millisecond"), "ms", LinearConverter>(), 0); - return ms; - } + /*! + * \brief Feet/min ft/min + * \return + */ + static const CSpeedUnit &ft_min() + { + static CSpeedUnit ftmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per minute"), "ft/min", LinearConverter(), 0); + return ftmin; + } - /*! - * \brief Hour - * \return - */ - static const CTimeUnit &h() - { - static CTimeUnit h(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour"), "h", LinearConverter(), 1); - return h; - } + /*! + * \brief Kilometer/hour km/h + * \return + */ + static const CSpeedUnit &km_h() + { + static CSpeedUnit kmh(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilometer per hour"), "km/h", LinearConverter(), 1); + return kmh; + } - /*! - * \brief Minute - * \return - */ - static const CTimeUnit &min() - { - static CTimeUnit min(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute"), "min", LinearConverter(), 2); - return min; - } + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CSpeedUnit::ft_min()); + u.append(CSpeedUnit::ft_s()); + u.append(CSpeedUnit::km_h()); + u.append(CSpeedUnit::kts()); + u.append(CSpeedUnit::m_s()); + u.append(CSpeedUnit::NM_h()); + } + return u; + } - /*! - * \brief Day - * \return - */ - static const CTimeUnit &d() - { - static CTimeUnit day(QT_TRANSLATE_NOOP("CMeasurementUnit", "day"), "d", LinearConverter(), 1); - return day; - } + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; - /*! - * \brief Hours, minutes, seconds - */ - static const CTimeUnit &hms() - { - static CTimeUnit hms(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute, second"), "hms", SubdivisionConverter2>(), 4); - return hms; - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m%L3s"); - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%-L1h%L2m%L3s"); - } - - /*! - * \brief Hours, minutes - */ - static const CTimeUnit &hrmin() - { - static CTimeUnit hrmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute"), "hm", SubdivisionConverter>(), 3); - return hrmin; - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m"); - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1h%L2m"); - } - - /*! - * \brief Minutes, seconds - */ - static const CTimeUnit &minsec() - { - static CTimeUnit minsec(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute, second"), "minsec", SubdivisionConverter>(), 2); - return minsec; - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1m%L2s"); - (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1m%L2s"); - } - - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + /*! + * \brief Specialized class for time units (ms, hour, min). + */ + class CTimeUnit : public CMeasurementUnit { - u.append(CTimeUnit::d()); - u.append(CTimeUnit::h()); - u.append(CTimeUnit::min()); - u.append(CTimeUnit::ms()); - u.append(CTimeUnit::s()); - } - return u; - } + private: + template + CTimeUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + typedef One SecondsToSeconds; + struct DaysToSeconds { static double factor() { return 60.0 * 60.0 * 24.0; } }; + struct HoursToSeconds { static double factor() { return 60.0 * 60.0; } }; + struct MinutesToSeconds { static double factor() { return 60.0; } }; -/*! - * \brief Specialized class for acceleration units (m/s2, ft/s2). - */ -class CAccelerationUnit : public CMeasurementUnit -{ -private: - template - CAccelerationUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : - CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) - {} + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CTimeUnit() : CMeasurementUnit(defaultUnit()) {} - struct FeetToMeters { static double factor() { return 0.3048; } }; + /*! + * Default unit + * \return + */ + static const CTimeUnit &defaultUnit() { return s(); } -public: - /*! - * Default constructor, required for Qt Metasystem - */ - CAccelerationUnit() : CMeasurementUnit(defaultUnit()) {} + /*! + * \brief Override for subdivisional units. + * \param value + * \param digits + * \param i18n + * \return + */ + virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const; - /*! - * Default unit - * \return - */ - static const CAccelerationUnit &defaultUnit() { return m_s2(); } + /*! + * \brief Second s + * \return + */ + static const CTimeUnit &s() + { + static CTimeUnit s(QT_TRANSLATE_NOOP("CMeasurementUnit", "second"), "s", IdentityConverter(), 1); + return s; + } - /*! - * \brief Meter/second^2 (m/s^2) - * \return - */ - static const CAccelerationUnit &m_s2() - { - static CAccelerationUnit ms2(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter per second per second"), QT_TRANSLATE_NOOP("CMeasurementUnit", "m/s^2"), IdentityConverter(), 1); - return ms2; - } + /*! + * \brief Millisecond ms + * \return + */ + static const CTimeUnit &ms() + { + static CTimeUnit ms(QT_TRANSLATE_NOOP("CMeasurementUnit", "millisecond"), "ms", LinearConverter >(), 0); + return ms; + } - /*! - * \brief Feet/second^2 - * \return - */ - static const CAccelerationUnit &ft_s2() - { - static CAccelerationUnit fts2(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per second per second"), QT_TRANSLATE_NOOP("CMeasurementUnit", "ft/s^2"), LinearConverter(), 0); - return fts2; - } + /*! + * \brief Hour + * \return + */ + static const CTimeUnit &h() + { + static CTimeUnit h(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour"), "h", LinearConverter(), 1); + return h; + } - /*! - * \brief All units - * \return - */ - static const QList &allUnits() - { - static QList u; - if (u.isEmpty()) + /*! + * \brief Minute + * \return + */ + static const CTimeUnit &min() + { + static CTimeUnit min(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute"), "min", LinearConverter(), 2); + return min; + } + + /*! + * \brief Day + * \return + */ + static const CTimeUnit &d() + { + static CTimeUnit day(QT_TRANSLATE_NOOP("CMeasurementUnit", "day"), "d", LinearConverter(), 1); + return day; + } + + /*! + * \brief Hours, minutes, seconds + */ + static const CTimeUnit &hms() + { + static CTimeUnit hms(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute, second"), "hms", SubdivisionConverter2 >(), 4); + return hms; + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m%L3s"); + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%-L1h%L2m%L3s"); + } + + /*! + * \brief Hours, minutes + */ + static const CTimeUnit &hrmin() + { + static CTimeUnit hrmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute"), "hm", SubdivisionConverter >(), 3); + return hrmin; + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m"); + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1h%L2m"); + } + + /*! + * \brief Minutes, seconds + */ + static const CTimeUnit &minsec() + { + static CTimeUnit minsec(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute, second"), "minsec", SubdivisionConverter >(), 2); + return minsec; + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1m%L2s"); + (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1m%L2s"); + } + + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CTimeUnit::d()); + u.append(CTimeUnit::h()); + u.append(CTimeUnit::min()); + u.append(CTimeUnit::ms()); + u.append(CTimeUnit::s()); + } + return u; + } + + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; + + /*! + * \brief Specialized class for acceleration units (m/s2, ft/s2). + */ + class CAccelerationUnit : public CMeasurementUnit { - u.append(CAccelerationUnit::ft_s2()); - u.append(CAccelerationUnit::m_s2()); - } - return u; - } + private: + template + CAccelerationUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) : + CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) + {} -protected: - /*! - * \brief Stream from DBus - * \param argument - */ - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - QString unitName; - argument >> unitName; - (*this) = CMeasurementUnit::unitFromSymbol(unitName); - } -}; + struct FeetToMeters { static double factor() { return 0.3048; } }; -} // namespace + public: + /*! + * Default constructor, required for Qt Metasystem + */ + CAccelerationUnit() : CMeasurementUnit(defaultUnit()) {} + + /*! + * Default unit + * \return + */ + static const CAccelerationUnit &defaultUnit() { return m_s2(); } + + /*! + * \brief Meter/second^2 (m/s^2) + * \return + */ + static const CAccelerationUnit &m_s2() + { + static CAccelerationUnit ms2(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter per second per second"), QT_TRANSLATE_NOOP("CMeasurementUnit", "m/s^2"), IdentityConverter(), 1); + return ms2; + } + + /*! + * \brief Feet/second^2 + * \return + */ + static const CAccelerationUnit &ft_s2() + { + static CAccelerationUnit fts2(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per second per second"), QT_TRANSLATE_NOOP("CMeasurementUnit", "ft/s^2"), LinearConverter(), 0); + return fts2; + } + + /*! + * \brief All units + * \return + */ + static const QList &allUnits() + { + static QList u; + if (u.isEmpty()) + { + u.append(CAccelerationUnit::ft_s2()); + u.append(CAccelerationUnit::m_s2()); + } + return u; + } + + protected: + /*! + * \brief Stream from DBus + * \param argument + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument) + { + QString unitName; + argument >> unitName; + (*this) = CMeasurementUnit::unitFromSymbol(unitName); + } + }; + + } // namespace } // namespace Q_DECLARE_METATYPE(BlackMisc::PhysicalQuantities::CLengthUnit) diff --git a/src/blackmisc_cpp2xml/blackmiscplugin.h b/src/blackmisc_cpp2xml/blackmiscplugin.h index b57584559..fb42bfbef 100644 --- a/src/blackmisc_cpp2xml/blackmiscplugin.h +++ b/src/blackmisc_cpp2xml/blackmiscplugin.h @@ -10,7 +10,9 @@ #include /*! - * \brief Plugin to register all relevant blackmisc classes for QDBusCpp2XmlPlugin + * Plugin to register all relevant blackmisc classes for QDBusCpp2XmlPlugin + * This needs to be recompiled whenever a new DBus compliant class has been created. + * Compile as "release", otherwise plugin will not work. */ class BlackmiscPlugin : public QObject, public QDBusCpp2XmlPlugin { @@ -25,4 +27,4 @@ public: virtual void registerMetaTypes(); }; -#endif // BLACKMISC_CPP2XML_H +#endif // guard diff --git a/tests/blackmisc/testaviation.cpp b/tests/blackmisc/testaviation.cpp index 191c5bac6..f7d4bd3f6 100644 --- a/tests/blackmisc/testaviation.cpp +++ b/tests/blackmisc/testaviation.cpp @@ -11,72 +11,72 @@ using namespace BlackMisc::PhysicalQuantities; namespace BlackMiscTest { -/* - * Constructor - */ -CTestAviation::CTestAviation(QObject *parent): QObject(parent) -{ - // void -} + /* + * Constructor + */ + CTestAviation::CTestAviation(QObject *parent): QObject(parent) + { + // void + } -/* - * Basic tests - */ -void CTestAviation::headingBasics() -{ - CHeading h1(180, CHeading::Magnetic, CAngleUnit::deg()); - CHeading h2(180, CHeading::True, CAngleUnit::deg()); - CHeading h3(181, CHeading::Magnetic, CAngleUnit::deg()); - CAngle a1(200, CAngleUnit::deg()); - CHeading h4; - h4 = h1; - QVERIFY2(h1 != h2, "Magnetic and true heading are not the same"); - QVERIFY2(h1 < h3, "180deg are less than 181deg"); - QVERIFY2(h3 > h1, "181deg are more than 181deg"); - QVERIFY2(a1 > h3, "200deg are more than 181deg"); - QVERIFY2(h4 == h1, "Values shall be equal"); + /* + * Basic tests + */ + void CTestAviation::headingBasics() + { + CHeading h1(180, CHeading::Magnetic, CAngleUnit::deg()); + CHeading h2(180, CHeading::True, CAngleUnit::deg()); + CHeading h3(181, CHeading::Magnetic, CAngleUnit::deg()); + CAngle a1(200, CAngleUnit::deg()); + CHeading h4; + h4 = h1; + QVERIFY2(h1 != h2, "Magnetic and true heading are not the same"); + QVERIFY2(h1 < h3, "180deg are less than 181deg"); + QVERIFY2(h3 > h1, "181deg are more than 181deg"); + QVERIFY2(a1 > h3, "200deg are more than 181deg"); + QVERIFY2(h4 == h1, "Values shall be equal"); - h1 -= h1; - QVERIFY2(h1.value() == 0, "Value shall be 0"); + h1 -= h1; + QVERIFY2(h1.value() == 0, "Value shall be 0"); - // h4 = h1 + h2; does not work, because misleading - h2 += h2; // add just angle - QVERIFY2(h2.value() == 360, "Value shall be 360"); -} + // h4 = h1 + h2; does not work, because misleading + h2 += h2; // add just angle + QVERIFY2(h2.value() == 360, "Value shall be 360"); + } -/* - * COM and NAV units - */ -void CTestAviation::comAndNav() -{ - CComSystem c1 = CComSystem::getCom1System(122.8); - CComSystem c2 = CComSystem::getCom2System(122.8); - QVERIFY2(c1 != c2, "COM system shall not be equal"); - c1 = c2; - QVERIFY2(c1 == c2, "COM system shall be equal"); - CFrequency f(100.0, CFrequencyUnit::MHz()); - QVERIFY2(!CComSystem::tryGetComSystem(c1, "no valid unit", f), "Expect not to get COM system"); - CNavSystem nav1; - QVERIFY2(CNavSystem::tryGetNav1System(nav1, 110.0), "Expect NAV system"); - QVERIFY2(!CNavSystem::tryGetNav1System(nav1, 200.0), "Expect no NAV system"); -} + /* + * COM and NAV units + */ + void CTestAviation::comAndNav() + { + CComSystem c1 = CComSystem::getCom1System(122.8); + CComSystem c2 = CComSystem::getCom2System(122.8); + QVERIFY2(c1 != c2, "COM system shall not be equal"); + c1 = c2; + QVERIFY2(c1 == c2, "COM system shall be equal"); + CFrequency f(100.0, CFrequencyUnit::MHz()); + QVERIFY2(!CComSystem::tryGetComSystem(c1, "no valid unit", f), "Expect not to get COM system"); + CNavSystem nav1; + QVERIFY2(CNavSystem::tryGetNav1System(nav1, 110.0), "Expect NAV system"); + QVERIFY2(!CNavSystem::tryGetNav1System(nav1, 200.0), "Expect no NAV system"); + } -/* - * COM and NAV units - */ -void CTestAviation::transponder() -{ - CTransponder t1 = CTransponder::getStandardTransponder(7000, CTransponder::StateStandby); - CTransponder t2 = t1; - QVERIFY2(t1 == t2, "Transponders shall be equal"); - t2.setTransponderMode(CTransponder::ModeC); - QVERIFY2(t1 != t2, "Transponders shall not be equal"); - QVERIFY2(!CTransponder::tryGetStandardTransponder(t1, 8888, CTransponder::StateStandby), "No valid transponder"); - QVERIFY2(!CTransponder::tryGetStandardTransponder(t1, 781, CTransponder::StateStandby), "No valid transponder"); - QVERIFY2(!CTransponder::tryGetStandardTransponder(t1, "0781", CTransponder::StateStandby), "No valid transponder"); - QVERIFY2(CTransponder::tryGetStandardTransponder(t1, "7211", CTransponder::StateStandby), "No valid transponder"); - QVERIFY2(!CTransponder::tryGetStandardTransponder(t1, "schnitzel", CTransponder::StateStandby), "No valid transponder"); + /* + * COM and NAV units + */ + void CTestAviation::transponder() + { + CTransponder t1 = CTransponder::getStandardTransponder(7000, CTransponder::StateStandby); + CTransponder t2 = t1; + QVERIFY2(t1 == t2, "Transponders shall be equal"); + t2.setTransponderMode(CTransponder::ModeC); + QVERIFY2(t1 != t2, "Transponders shall not be equal"); + QVERIFY2(!CTransponder::tryGetStandardTransponder(t1, 8888, CTransponder::StateStandby), "No valid transponder"); + QVERIFY2(!CTransponder::tryGetStandardTransponder(t1, 781, CTransponder::StateStandby), "No valid transponder"); + QVERIFY2(!CTransponder::tryGetStandardTransponder(t1, "0781", CTransponder::StateStandby), "No valid transponder"); + QVERIFY2(CTransponder::tryGetStandardTransponder(t1, "7211", CTransponder::StateStandby), "No valid transponder"); + QVERIFY2(!CTransponder::tryGetStandardTransponder(t1, "schnitzel", CTransponder::StateStandby), "No valid transponder"); -} + } } // namespace diff --git a/tests/blackmisc/testaviation.h b/tests/blackmisc/testaviation.h index b3c153ee1..2736b558f 100644 --- a/tests/blackmisc/testaviation.h +++ b/tests/blackmisc/testaviation.h @@ -16,35 +16,35 @@ namespace BlackMiscTest { -/*! - * \brief Aviation classes basic tests - */ -class CTestAviation : public QObject -{ - Q_OBJECT + /*! + * \brief Aviation classes basic tests + */ + class CTestAviation : public QObject + { + Q_OBJECT -public: - /*! - * \brief Standard test case constructor - * \param parent - */ - explicit CTestAviation(QObject *parent = 0); + public: + /*! + * \brief Standard test case constructor + * \param parent + */ + explicit CTestAviation(QObject *parent = 0); -private slots: - /*! - * \brief Basic unit tests for physical units - */ - void headingBasics(); - /*! - * \brief COM and NAV units - */ - void comAndNav(); + private slots: + /*! + * \brief Basic unit tests for physical units + */ + void headingBasics(); + /*! + * \brief COM and NAV units + */ + void comAndNav(); + + /*! + * \brief Transponder + */ + void transponder(); - /*! - * \brief Transponder - */ - void transponder(); -}; } // namespace diff --git a/tests/blackmisc/testblackmiscmain.cpp b/tests/blackmisc/testblackmiscmain.cpp index 005be5b48..42fd4ffc2 100644 --- a/tests/blackmisc/testblackmiscmain.cpp +++ b/tests/blackmisc/testblackmiscmain.cpp @@ -7,22 +7,23 @@ namespace BlackMiscTest { -/* - * Starting main, equivalent to QTEST_APPLESS_MAIN for multiple test classes. - */ -int CBlackMiscTestMain::unitMain(int argc, char *argv[]) -{ - int status = 0; + /* + * Starting main, equivalent to QTEST_APPLESS_MAIN for multiple test classes. + */ + int CBlackMiscTestMain::unitMain(int argc, char *argv[]) { - CTestPhysicalQuantities pqBaseTests ; - CTestAviation avBaseTests; - CTestVectorMatrix vmTests; - CTestGeo geoTests; - status |= QTest::qExec(&pqBaseTests, argc, argv); - status |= QTest::qExec(&avBaseTests, argc, argv); - status |= QTest::qExec(&vmTests, argc, argv); - status |= QTest::qExec(&geoTests, argc, argv); + int status = 0; + { + CTestPhysicalQuantities pqBaseTests ; + CTestAviation avBaseTests; + CTestVectorMatrix vmTests; + CTestGeo geoTests; + status |= QTest::qExec(&pqBaseTests, argc, argv); + status |= QTest::qExec(&avBaseTests, argc, argv); + status |= QTest::qExec(&vmTests, argc, argv); + status |= QTest::qExec(&geoTests, argc, argv); + + } + return status; } - return status; -} } // namespace diff --git a/tests/blackmisc/testphysicalquantities.h b/tests/blackmisc/testphysicalquantities.h index 8f159b1df..31aa48483 100644 --- a/tests/blackmisc/testphysicalquantities.h +++ b/tests/blackmisc/testphysicalquantities.h @@ -91,4 +91,4 @@ private slots: } // namespace -#endif // BLACKMISCTEST_TESTPHYSICALQUANTITIESBASE_H +#endif // guard