From 6668b34bc5deb36e73aa45e0457ed6740f828efd Mon Sep 17 00:00:00 2001 From: Mavridis Philippe Date: Sun, 12 Dec 2021 15:50:37 +0200 Subject: Implemented icon theme option. Changes in this commit include: * The option itself; * A method of updating the option right after Apply or Ok is pressed in the settings dialog; * A new WeatherIconPrivate class to store the methods previously in WeatherIcons, as well as the settings; * Improved icon name helper function in METAR parser; * A few cleanups to the WeatherIcon class. Signed-off-by: Mavridis Philippe --- kweather/weather_icon.cpp | 132 +++++++++++++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 43 deletions(-) (limited to 'kweather/weather_icon.cpp') diff --git a/kweather/weather_icon.cpp b/kweather/weather_icon.cpp index e358833..8cd2520 100644 --- a/kweather/weather_icon.cpp +++ b/kweather/weather_icon.cpp @@ -1,12 +1,92 @@ +#include + #include #include #include #include "weather_icon.h" -WeatherIcon::WeatherIcon( int condition, bool night ) +WeatherIconPrivate* WeatherIconPrivate::s_instance = 0; + +WeatherIconPrivate::WeatherIconPrivate() { iconLoader = new TDEIconLoader("kweather"); +} + +WeatherIconPrivate::~WeatherIconPrivate() +{ + delete iconLoader; +} + +WeatherIconPrivate* WeatherIconPrivate::instance() +{ + if ( s_instance == 0 ) + s_instance = new WeatherIconPrivate(); + + return s_instance; +} + +void WeatherIconPrivate::useIconTheme( bool use ) +{ + m_useIconTheme = use; +} + +bool WeatherIconPrivate::usingIconTheme() +{ + return m_useIconTheme; +} + +TQPair WeatherIconPrivate::findIcon( TQStringList fallback ) +{ + kdDebug() << "[WeatherIcon::findIcon] use icon theme? " << m_useIconTheme << endl; + if( m_useIconTheme ) + { + // Check in theme + for ( TQStringList::Iterator icon = fallback.begin(); icon != fallback.end(); ++icon ) + { + kdDebug() << "[WeatherIcon::findIcon] Searching for `" << *icon << "` in theme" << endl; + TQString iPath = iconPath(*icon, true); + if( !( iPath.isNull() ) ) + { + kdDebug() << "[WeatherIcon::findIcon] FOUND `" << *icon << "` in theme: " << iPath << endl; + return qMakePair(*icon, iPath); + } + } + } + + // Check in kweather fallback + for ( TQStringList::Iterator icon = fallback.begin(); icon != fallback.end(); ++icon ) + { + kdDebug() << "[WeatherIcon::findIcon] Searching for `" << *icon << "` in kweather icons" << endl; + TQString iPath = iconPath(*icon, false); + if( !( iPath.isNull() ) ) + { + kdDebug() << "[WeatherIcon::findIcon] FOUND `" << *icon << "` in kweather icons: " << iPath << endl; + return qMakePair(*icon, iPath); + } + } + return qMakePair(WeatherIcon::unknown(), iconPath(WeatherIcon::unknown())); +} + +TQString WeatherIconPrivate::iconPath( TQString icon, bool inTheme ) +{ + if( inTheme ) + { + return iconLoader->iconPath(icon, TDEIcon::Desktop, true); + } + else + { + return locate( "data", "kweather/" + icon + ".png" ); + } +} + +TQString WeatherIconPrivate::iconPath( TQString icon ) +{ + return iconPath(icon, m_useIconTheme); +} + +WeatherIcon::WeatherIcon( int condition, bool night ) +{ TQStringList fallback; switch( condition ) @@ -82,13 +162,14 @@ WeatherIcon::WeatherIcon( int condition, bool night ) } } - iconName = findIcon(fallback); + TQPair foundIcon = WeatherIconPrivate::instance()->findIcon(fallback); + iconName = foundIcon.first; + iconPath = foundIcon.second; return; } WeatherIcon::WeatherIcon( int condition, bool night, unsigned int strength ) { - iconLoader = new TDEIconLoader("kweather"); TQStringList fallback; switch ( condition ) @@ -367,48 +448,13 @@ WeatherIcon::WeatherIcon( int condition, bool night, unsigned int strength ) break; } - iconName = findIcon(fallback); + TQPair foundIcon = WeatherIconPrivate::instance()->findIcon(fallback); + iconName = foundIcon.first; + iconPath = foundIcon.second; return; } WeatherIcon::~WeatherIcon() -{} - - -TQString WeatherIcon::findIcon( TQStringList fallback ) { - // Check in theme - for ( TQStringList::Iterator icon = fallback.begin(); icon != fallback.end(); ++icon ) - { - kdDebug() << "[WeatherIcon::findIcon] Searching for `" << *icon << "` in theme" << endl; - if( iconExists(*icon) ) - { - kdDebug() << "[WeatherIcon::findIcon] FOUND `" << *icon << "` in theme" << endl; - return *icon; - } - } - - // Check in kweather fallback - for ( TQStringList::Iterator icon = fallback.begin(); icon != fallback.end(); ++icon ) - { - kdDebug() << "[WeatherIcon::findIcon] Searching for `" << *icon << "` in kweather icons" << endl; - if( iconExists(*icon, false) ) - { - kdDebug() << "[WeatherIcon::findIcon] FOUND `" << *icon << "` in kweather icons" << endl; - return *icon; - } - } - return unknown(); -} - -bool WeatherIcon::iconExists( TQString& icon, bool inTheme ) -{ - if( inTheme ) - { - return !( iconLoader->iconPath(icon, TDEIcon::Desktop, true).isNull() ); - } - else - { - return !( locate( "data", "kweather/" + icon + ".png" ).isNull() ); - } -} + iconName = TQString::null; +} \ No newline at end of file -- cgit v1.2.1