diff --git a/applets/weather/plugin/abstractunitlistmodel.h b/applets/weather/plugin/abstractunitlistmodel.h index 124bb7781..659c697a7 100644 --- a/applets/weather/plugin/abstractunitlistmodel.h +++ b/applets/weather/plugin/abstractunitlistmodel.h @@ -1,59 +1,61 @@ /* * Copyright 2016 Friedrich W. H. Kossebau * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef ABSTRACTUNITLISTMODEL_H #define ABSTRACTUNITLISTMODEL_H +#include "util.h" + #include #include #include class UnitItem { public: UnitItem() {} - UnitItem(const QString &_name, KUnitConversion::UnitId _unitId) : name(_name), unitId(_unitId) {} + UnitItem(KUnitConversion::UnitId _unitId) : name(Util::nameFromUnitId(_unitId)), unitId(_unitId) {} QString name; KUnitConversion::UnitId unitId; }; Q_DECLARE_METATYPE(UnitItem) Q_DECLARE_TYPEINFO(UnitItem, Q_MOVABLE_TYPE); class AbstractUnitListModel : public QAbstractListModel { Q_OBJECT public: explicit AbstractUnitListModel(const QVector &items, QObject *parent = nullptr); public: // QAbstractListModel API QVariant data(const QModelIndex &index, int role) const override; int rowCount(const QModelIndex &index) const override; public: Q_INVOKABLE int listIndexForUnitId(int unitId) const; Q_INVOKABLE int unitIdForListIndex(int listIndex) const; private: const QVector m_items; }; #endif // ABSTRACTUNITLISTMODEL_H diff --git a/applets/weather/plugin/plugin.cpp b/applets/weather/plugin/plugin.cpp index 07d5facc1..319316273 100644 --- a/applets/weather/plugin/plugin.cpp +++ b/applets/weather/plugin/plugin.cpp @@ -1,115 +1,115 @@ /* * Copyright 2016 Friedrich W. H. Kossebau * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "plugin.h" #include "abstractunitlistmodel.h" #include "locationlistmodel.h" #include "servicelistmodel.h" #include "util.h" // KF #include // Qt #include static QObject* temperatureUnitListModelSingletonTypeProvider(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) QVector items { - UnitItem(i18nc("@item", "Celsius \302\260C"), KUnitConversion::Celsius), - UnitItem(i18nc("@item", "Fahrenheit \302\260F"), KUnitConversion::Fahrenheit), - UnitItem(i18nc("@item", "Kelvin K"), KUnitConversion::Kelvin) + UnitItem(KUnitConversion::Celsius), + UnitItem(KUnitConversion::Fahrenheit), + UnitItem(KUnitConversion::Kelvin) }; return new AbstractUnitListModel(items); } static QObject* pressureUnitListModelSingletonTypeProvider(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) QVector items { - UnitItem(i18nc("@item", "Hectopascals hPa"), KUnitConversion::Hectopascal), - UnitItem(i18nc("@item", "Kilopascals kPa"), KUnitConversion::Kilopascal), - UnitItem(i18nc("@item", "Millibars mbar"), KUnitConversion::Millibar), - UnitItem(i18nc("@item", "Inches of Mercury inHg"), KUnitConversion::InchesOfMercury), - UnitItem(i18nc("@item", "Millimeters of Mercury mmHg"), KUnitConversion::MillimetersOfMercury) + UnitItem(KUnitConversion::Hectopascal), + UnitItem(KUnitConversion::Kilopascal), + UnitItem(KUnitConversion::Millibar), + UnitItem(KUnitConversion::InchesOfMercury), + UnitItem(KUnitConversion::MillimetersOfMercury) }; return new AbstractUnitListModel(items); } static QObject* windSpeedUnitListModelSingletonTypeProvider(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) QVector items { - UnitItem(i18nc("@item", "Meters per Second m/s"), KUnitConversion::MeterPerSecond), - UnitItem(i18nc("@item", "Kilometers per Hour km/h"), KUnitConversion::KilometerPerHour), - UnitItem(i18nc("@item", "Miles per Hour mph"), KUnitConversion::MilePerHour), - UnitItem(i18nc("@item", "Knots kt"), KUnitConversion::Knot), - UnitItem(i18nc("@item", "Beaufort scale bft"), KUnitConversion::Beaufort) + UnitItem(KUnitConversion::MeterPerSecond), + UnitItem(KUnitConversion::KilometerPerHour), + UnitItem(KUnitConversion::MilePerHour), + UnitItem(KUnitConversion::Knot), + UnitItem(KUnitConversion::Beaufort) }; return new AbstractUnitListModel(items); } static QObject* visibilityUnitListModelSingletonTypeProvider(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) QVector items { - UnitItem(i18nc("@item", "Kilometers"), KUnitConversion::Kilometer), - UnitItem(i18nc("@item", "Miles"), KUnitConversion::Mile) + UnitItem(KUnitConversion::Kilometer), + UnitItem(KUnitConversion::Mile) }; return new AbstractUnitListModel(items); } static QObject* utilSingletonTypeProvider(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) return new Util(); } void WeatherPlugin::registerTypes(const char *uri) { Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.plasma.private.weather")); qmlRegisterSingletonType(uri, 1, 0, "TemperatureUnitListModel", temperatureUnitListModelSingletonTypeProvider); qmlRegisterSingletonType(uri, 1, 0, "PressureUnitListModel", pressureUnitListModelSingletonTypeProvider); qmlRegisterSingletonType(uri, 1, 0, "WindSpeedUnitListModel", windSpeedUnitListModelSingletonTypeProvider); qmlRegisterSingletonType(uri, 1, 0, "VisibilityUnitListModel", visibilityUnitListModelSingletonTypeProvider); qmlRegisterSingletonType(uri, 1, 0, "Util", utilSingletonTypeProvider); qmlRegisterType(uri, 1, 0, "LocationListModel"); qmlRegisterType(uri, 1, 0, "ServiceListModel"); } diff --git a/applets/weather/plugin/util.cpp b/applets/weather/plugin/util.cpp index 7e23f8495..2b2de9baa 100644 --- a/applets/weather/plugin/util.cpp +++ b/applets/weather/plugin/util.cpp @@ -1,83 +1,92 @@ /* * Copyright 2018 Friedrich W. H. Kossebau * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "util.h" // KF +#include #include #include // Qt #include #include #include // Std #include template T clampValue(T value, int decimals) { const T mul = std::pow(static_cast(10), decimals); return int(value * mul) / mul; } Util::Util(QObject *parent) : QObject(parent) { } +KUnitConversion::Converter Util::m_converter; QString Util::existingWeatherIconName(const QString &iconName) const { const bool isValid = !iconName.isEmpty() && QIcon::hasThemeIcon(iconName); return isValid ? iconName : QStringLiteral("weather-not-available"); } QString Util::temperatureToDisplayString(int displayUnitType, double value, int valueUnitType, bool rounded, bool degreesOnly) const { KUnitConversion::Value v(value, static_cast(valueUnitType)); v = v.convertTo(static_cast(displayUnitType)); const QString unit = degreesOnly ? i18nc("Degree, unit symbol", "°") : v.unit().symbol(); if (rounded) { int tempNumber = qRound(v.number()); return i18nc("temperature unitsymbol", "%1 %2", tempNumber, unit); } const QString formattedTemp = QLocale().toString(clampValue(v.number(), 1), 'f', 1); return i18nc("temperature unitsymbol", "%1 %2", formattedTemp, unit); } QString Util::valueToDisplayString(int displayUnitType, double value, int valueUnitType, int precision) const { KUnitConversion::Value v(value, static_cast(valueUnitType)); v = v.convertTo(static_cast(displayUnitType)); // TODO: fix KUnitConversion to do locale encoded values and use that const QString formattedValue = QLocale().toString(clampValue(v.number(), precision), 'f', precision); return i18nc("value unitsymbol", "%1 %2", formattedValue, v.unit().symbol()); } QString Util::percentToDisplayString(double value) const { const QString formattedPercentValue = QLocale().toString(clampValue(value, 0), 'f', 0); return i18nc("value percentsymbol", "%1 %", formattedPercentValue); } +QString Util::nameFromUnitId(KUnitConversion::UnitId unitId) +{ + const KUnitConversion::Unit unit = m_converter.unit(unitId); + QString unitDescription = i18nc("@item %1 is a unit description and %2 its unit symbol", + "%1 (%2)", unit.description(), unit.symbol()); + return unitDescription; +} diff --git a/applets/weather/plugin/util.h b/applets/weather/plugin/util.h index 57cfdad5d..a3cb293c8 100644 --- a/applets/weather/plugin/util.h +++ b/applets/weather/plugin/util.h @@ -1,51 +1,53 @@ /* * Copyright 2018 Friedrich W. H. Kossebau * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef UTIL_H #define UTIL_H // KF #include // Qt #include class Util : public QObject { Q_OBJECT public: explicit Util(QObject *parent = nullptr); public: /** * Returns the @p iconName if the current icon theme contains an icon with that name, * otherwise returns "weather-not-available" (expecting the icon theme to have that in any case). */ Q_INVOKABLE QString existingWeatherIconName(const QString &iconName) const; Q_INVOKABLE QString temperatureToDisplayString(int displayUnitType, double value, int valueUnitType, bool rounded = false, bool degreesOnly = false) const; Q_INVOKABLE QString valueToDisplayString(int displayUnitType, double value, int valueUnitType, int precision = 0) const; Q_INVOKABLE QString percentToDisplayString(double value) const; + static QString nameFromUnitId(KUnitConversion::UnitId unitId); + private: - KUnitConversion::Converter m_converter; + static KUnitConversion::Converter m_converter; }; #endif