Commit ab4bf790 authored by Nicolas Fella's avatar Nicolas Fella
Browse files

Make KWeatherSettings a singleton

This avoids creating lots of temporary settings objects
parent fd6d133f
Pipeline #77190 passed with stage
in 18 seconds
......@@ -25,7 +25,7 @@ QString Formatter::formatTemperatureRounded(qreal temperature) const
QString Formatter::formatWindSpeed(qreal speed) const
{
if (KWeatherSettings().speedUnits() == QLatin1String("kph")) {
if (KWeatherSettings::self()->speedUnits() == QLatin1String("kph")) {
return i18n("%1 km/h", QString::number(speed, 'g', 1));
} else {
return i18n("%1 mph", QString::number(speed * 0.62, 'g', 1));
......
......@@ -16,7 +16,7 @@ static const QString WEATHER_LOCATIONS_CFG_GROUP = QStringLiteral("WeatherLocati
static double convertTemp(double temp)
{
if (KWeatherSettings().temperatureUnits() == "Fahrenheit") {
if (KWeatherSettings::self()->temperatureUnits() == "Fahrenheit") {
return temp * 1.8 + 32;
} else {
return temp;
......
......@@ -4,3 +4,4 @@ Mutators=true
DefaultValueGetters=true
GenerateProperties=true
ParentInConstructor=true
Singleton=true
......@@ -65,10 +65,8 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
engine.rootContext()->setContextProperty(QStringLiteral("kweatherAboutData"), QVariant::fromValue(aboutData));
KWeatherSettings settings;
engine.rootContext()->setContextProperty("weatherLocationListModel", WeatherForecastManager::inst()->model());
engine.rootContext()->setContextProperty("settingsModel", &settings);
engine.rootContext()->setContextProperty("settingsModel", KWeatherSettings::self());
Formatter formatter;
qmlRegisterSingletonInstance<Formatter>("kweather", 1, 0, "Formatter", &formatter);
......
......@@ -17,7 +17,7 @@ KWeather_1x4::KWeather_1x4(QObject *parent, const QVariantList &args)
auto group = config->group("general");
QString locationID = group.readEntry("locationID");
if (!locationID.isEmpty()) {
auto m_config = KWeatherSettings(this).sharedConfig()->group("WeatherLocations");
auto m_config = KWeatherSettings::self()->config()->group("WeatherLocations");
auto m_group = m_config.group(locationID);
m_location = m_group.readEntry("locationName");
m_latitude = m_group.readEntry("latitude").toDouble();
......@@ -47,7 +47,7 @@ void KWeather_1x4::update()
QStringList KWeather_1x4::locationsInSystem()
{
auto m_config = KWeatherSettings(this).sharedConfig()->group("WeatherLocations");
auto m_config = KWeatherSettings::self()->config()->group("WeatherLocations");
QStringList list;
for (const auto &loc : m_config.groupList()) {
......@@ -57,7 +57,7 @@ QStringList KWeather_1x4::locationsInSystem()
}
void KWeather_1x4::setLocation(const QString &location)
{
auto m_config = KWeatherSettings(this).sharedConfig()->group("WeatherLocations");
auto m_config = KWeatherSettings::self()->config()->group("WeatherLocations");
for (const auto &loc : m_config.groupList()) {
auto m_group = m_config.group(loc);
if (location == m_group.readEntry("locationName")) {
......
......@@ -56,7 +56,7 @@ WeatherLocation::WeatherLocation(QString locationId,
}
WeatherLocation *WeatherLocation::load(const QString &groupName)
{
auto config = KWeatherSettings().sharedConfig()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP).group(groupName);
auto config = KWeatherSettings::self()->config()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP).group(groupName);
if (config.isValid()) {
return new WeatherLocation(groupName,
config.readEntry("locationName"),
......@@ -69,7 +69,7 @@ WeatherLocation *WeatherLocation::load(const QString &groupName)
}
void WeatherLocation::save()
{
auto config = KWeatherSettings(this).sharedConfig()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP).group(locationId());
auto config = KWeatherSettings::self()->config()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP).group(locationId());
config.writeEntry("locationName", locationName());
config.writeEntry("latitude", latitude());
config.writeEntry("longitude", longitude());
......@@ -79,13 +79,13 @@ void WeatherLocation::save()
void WeatherLocation::saveOrder(int index)
{
auto config = KWeatherSettings(this).sharedConfig()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP).group(locationId());
auto config = KWeatherSettings::self()->config()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP).group(locationId());
config.writeEntry("index", index);
config.sync();
}
int WeatherLocation::index()
{
auto config = KWeatherSettings(this).sharedConfig()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP).group(locationId());
auto config = KWeatherSettings::self()->config()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP).group(locationId());
auto res = config.readEntry("index");
if (res.isEmpty()) {
return -1;
......@@ -95,7 +95,7 @@ int WeatherLocation::index()
}
void WeatherLocation::deleteConfig()
{
auto config = KWeatherSettings(this).sharedConfig()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP);
auto config = KWeatherSettings::self()->config()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP);
config.deleteGroup(locationId());
config.sync();
}
......
......@@ -25,7 +25,7 @@ WeatherLocationListModel::WeatherLocationListModel(QObject *parent)
void WeatherLocationListModel::load()
{
// load locations from kconfig
auto config = KWeatherSettings(this).sharedConfig()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP);
auto config = KWeatherSettings::self()->config()->group(Kweather::WEATHER_LOCATIONS_CFG_GROUP);
auto locations = config.groupList();
for (const auto &location : locations) {
auto location_ptr = WeatherLocation::load(location);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment