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/kcmweather.cpp | 8 +++ kweather/kweather.cpp | 19 +++++++ kweather/kweather.h | 6 +- kweather/metar_parser.cpp | 63 ++++++++++----------- kweather/metar_parser.h | 4 +- kweather/prefdialogdata.ui | 63 ++++++++++++++++++--- kweather/weather_icon.cpp | 132 +++++++++++++++++++++++++++++--------------- kweather/weather_icon.h | 31 +++++++++-- kweather/weatherlib.cpp | 24 +++++++- kweather/weatherlib.h | 1 + kweather/weatherservice.cpp | 20 ++++--- kweather/weatherservice.h | 3 + 12 files changed, 274 insertions(+), 100 deletions(-) diff --git a/kweather/kcmweather.cpp b/kweather/kcmweather.cpp index 9251737..b480980 100644 --- a/kweather/kcmweather.cpp +++ b/kweather/kcmweather.cpp @@ -71,6 +71,8 @@ KCMWeather::KCMWeather( TQWidget *parent, const char *name ) TQT_SLOT( reportLocationChanged() ) ); connect( mWidget->m_textColor, TQT_SIGNAL( changed(const TQColor &) ), TQT_SLOT( textColorChanged(const TQColor &) ) ); + connect( mWidget->m_iconTheme, TQT_SIGNAL( released( int ) ), + TQT_SLOT( changed() ) ); TDEAboutData *about = new TDEAboutData( I18N_NOOP( "kcmweather" ), @@ -182,6 +184,9 @@ void KCMWeather::load() mWidget->m_viewMode->setButton( config.readNumEntry( "smallview_mode", dockwidget::ShowAll ) ); changeViewMode( config.readNumEntry( "smallview_mode", dockwidget::ShowAll ) ); + + mWidget->m_iconTheme->setButton( config.readBoolEntry("use_icon_theme", true) ? 0 : 1 ); + emit changed( false ); } @@ -202,6 +207,9 @@ void KCMWeather::save() config.writeEntry( "report_location", loc); config.writeEntry( "smallview_mode", mViewMode ); + + config.writeEntry( "use_icon_theme", mWidget->m_iconTheme->selectedId() == 0 ); + config.sync(); emit changed( false ); diff --git a/kweather/kweather.cpp b/kweather/kweather.cpp index eb87f98..2682a72 100644 --- a/kweather/kweather.cpp +++ b/kweather/kweather.cpp @@ -216,6 +216,7 @@ void kweather::loadPrefs(){ fileName = kcConfig->readPathEntry("log_file_name"); reportLocation = kcConfig->readEntry("report_location"); mViewMode = kcConfig->readNumEntry("smallview_mode", dockwidget::ShowAll); + setUseIconTheme(kcConfig->readBoolEntry("use_icon_theme", true)); static TQColor black(TQt::black); mTextColor = kcConfig->readColorEntry("textColor", &black); @@ -231,9 +232,23 @@ void kweather::savePrefs(){ kcConfig->writeEntry("report_location", reportLocation); kcConfig->writeEntry("smallview_mode", mViewMode); kcConfig->writePathEntry("log_file_name", fileName ); + kcConfig->writeEntry("use_icon_theme", useIconTheme ); kcConfig->sync(); } +void kweather::setUseIconTheme( bool use ) +{ + useIconTheme = use; + + if ( !mWeatherService ) + initDCOP(); + + DCOPRef ws( "KWeatherService", "WeatherService" ); + DCOPReply reply = ws.call( "useIconTheme", useIconTheme ); + if( ! reply.isValid() ) + kdDebug() << "[kweather::setUseIconTheme] DCOP call failed" << endl; +} + void kweather::showWeather() { kdDebug(12004) << "Show weather" << endl; @@ -332,6 +347,10 @@ void kweather::slotPrefsAccepted() dockWidget->setLocationCode(reportLocation); dockWidget->setViewMode(mViewMode); + + setUseIconTheme(useIconTheme); + slotUpdateNow(); + setLabelColor(); emit updateLayout(); diff --git a/kweather/kweather.h b/kweather/kweather.h index 5a3b06e..c8b7113 100644 --- a/kweather/kweather.h +++ b/kweather/kweather.h @@ -68,13 +68,15 @@ private: // Private methods void showWeather(); void writeLogEntry(); void mousePressEvent(TQMouseEvent *e); - + void setUseIconTheme(bool use); + bool attach(); - + TQString reportLocation; TQString fileName; TQString metarData; bool logOn; + bool useIconTheme; bool mFirstRun; int mViewMode; TQTimer *timeOut; diff --git a/kweather/metar_parser.cpp b/kweather/metar_parser.cpp index 7ec252d..9456095 100644 --- a/kweather/metar_parser.cpp +++ b/kweather/metar_parser.cpp @@ -58,6 +58,7 @@ void MetarParser::reset() { // Initialize the WeatherInfo structure weatherInfo.theWeather = TQString(); + weatherInfo.iconPath = TQString(); weatherInfo.clouds = 0; weatherInfo.windMPH = 0; weatherInfo.tempC = 0; @@ -258,7 +259,7 @@ bool MetarParser::parseCurrent(const TQString &s) if (sCode.contains("DZ")) { phenomena = i18n("Drizzle"); - weatherInfo.theWeather = iconName( WeatherIcon::LightRain, false ); + getIcon( WeatherIcon::LightRain, false ); } else if (sCode.contains("RA")) { @@ -273,32 +274,32 @@ bool MetarParser::parseCurrent(const TQString &s) else if (sCode.contains("SG")) { phenomena = i18n("Snow Grains"); - weatherInfo.theWeather = iconName( WeatherIcon::Snow, false, 4 ); + getIcon( WeatherIcon::Snow, false, 4 ); } else if (sCode.contains("IC")) { phenomena = i18n("Ice Crystals"); - weatherInfo.theWeather = iconName( WeatherIcon::Hail, false ); + getIcon( WeatherIcon::Hail, false ); } else if (sCode.contains("PE")) { phenomena = i18n("Ice Pellets"); - weatherInfo.theWeather = iconName( WeatherIcon::Hail, false ); + getIcon( WeatherIcon::Hail, false ); } else if (s.contains("GR")) { phenomena = i18n("Hail"); - weatherInfo.theWeather = iconName( WeatherIcon::Hail, false ); + getIcon( WeatherIcon::Hail, false ); } else if (sCode.contains("GS")) { phenomena = i18n("Small Hail Pellets"); - weatherInfo.theWeather = iconName( WeatherIcon::Hail, false ); + getIcon( WeatherIcon::Hail, false ); } else if (s.contains("UP")) { phenomena = i18n("Unknown Precipitation"); - weatherInfo.theWeather = iconName( WeatherIcon::Showers, isNight(weatherInfo.reportLocation), 1); + getIcon( WeatherIcon::Showers, isNight(weatherInfo.reportLocation), 1); } else if (sCode.contains("BR")) { @@ -727,17 +728,17 @@ void MetarParser::calcCurrentIcon() if (weatherInfo.theWeather.isEmpty()) { if (weatherInfo.clouds == 0) - weatherInfo.theWeather = iconName( WeatherIcon::Sunny, night ); + getIcon( WeatherIcon::Sunny, night ); else if (weatherInfo.clouds > 0 && weatherInfo.clouds <= 2) - weatherInfo.theWeather = iconName( WeatherIcon::Cloudy, night, 1 ); + getIcon( WeatherIcon::Cloudy, night, 1 ); else if ( weatherInfo.clouds > 2 && weatherInfo.clouds <= 4) - weatherInfo.theWeather = iconName( WeatherIcon::Cloudy, night, 2 ); + getIcon( WeatherIcon::Cloudy, night, 2 ); else if ( weatherInfo.clouds > 4 && weatherInfo.clouds <= 8) - weatherInfo.theWeather = iconName( WeatherIcon::Cloudy, night, 3 ); + getIcon( WeatherIcon::Cloudy, night, 3 ); else if ( weatherInfo.clouds > 8 && weatherInfo.clouds < 63) - weatherInfo.theWeather = iconName( WeatherIcon::Cloudy, night, 4 ); + getIcon( WeatherIcon::Cloudy, night, 4 ); else - weatherInfo.theWeather = iconName( WeatherIcon::Cloudy, night, 5 ); + getIcon( WeatherIcon::Cloudy, night, 5 ); } else if (weatherInfo.theWeather == "tstorm") { @@ -745,11 +746,11 @@ void MetarParser::calcCurrentIcon() weatherInfo.clouds = 30; if (weatherInfo.clouds >= 0 && weatherInfo.clouds <= 10) - weatherInfo.theWeather = iconName( WeatherIcon::Thunderstorm, night, 1 ); + getIcon( WeatherIcon::Thunderstorm, night, 1 ); else if ( weatherInfo.clouds > 10 && weatherInfo.clouds <= 20) - weatherInfo.theWeather = iconName( WeatherIcon::Thunderstorm, night, 2 ); + getIcon( WeatherIcon::Thunderstorm, night, 2 ); else - weatherInfo.theWeather = iconName( WeatherIcon::Thunderstorm, night, 3 ); + getIcon( WeatherIcon::Thunderstorm, night, 3 ); } else if (weatherInfo.theWeather == "shower") { @@ -757,11 +758,11 @@ void MetarParser::calcCurrentIcon() weatherInfo.clouds = 30; if (weatherInfo.clouds >= 0 && weatherInfo.clouds <= 10) - weatherInfo.theWeather = iconName( WeatherIcon::Showers, night, 1 ); + getIcon( WeatherIcon::Showers, night, 1 ); else if ( weatherInfo.clouds > 10 && weatherInfo.clouds <= 20) - weatherInfo.theWeather = iconName( WeatherIcon::Showers, night, 2 ); + getIcon( WeatherIcon::Showers, night, 2 ); else - weatherInfo.theWeather = iconName( WeatherIcon::Showers, night, 3 ); + getIcon( WeatherIcon::Showers, night, 3 ); } else if (weatherInfo.theWeather == "snow") { @@ -769,22 +770,22 @@ void MetarParser::calcCurrentIcon() weatherInfo.clouds = 30; if (weatherInfo.clouds >= 0 && weatherInfo.clouds <= 8) - weatherInfo.theWeather = iconName( WeatherIcon::Snow, night, 1 ); + getIcon( WeatherIcon::Snow, night, 1 ); else if ( weatherInfo.clouds > 8 && weatherInfo.clouds <= 16) - weatherInfo.theWeather = iconName( WeatherIcon::Snow, night, 2 ); + getIcon( WeatherIcon::Snow, night, 2 ); else if (weatherInfo.clouds > 16 && weatherInfo.clouds <= 24) - weatherInfo.theWeather = iconName( WeatherIcon::Snow, night, 3 ); + getIcon( WeatherIcon::Snow, night, 3 ); else - weatherInfo.theWeather = iconName( WeatherIcon::Snow, night, 5 ); + getIcon( WeatherIcon::Snow, night, 5 ); } else if ( weatherInfo.theWeather == "mist" || weatherInfo.theWeather == "fog" ) { if ( weatherInfo.clouds >= 63 ) - weatherInfo.theWeather = iconName( WeatherIcon::Cloudy, night, 5 ); + getIcon( WeatherIcon::Cloudy, night, 5 ); else if ( weatherInfo.theWeather == "mist" ) - weatherInfo.theWeather = iconName( WeatherIcon::Mist, night ); + getIcon( WeatherIcon::Mist, night ); else if ( weatherInfo.theWeather == "fog" ) - weatherInfo.theWeather = iconName( WeatherIcon::Fog, night ); + getIcon( WeatherIcon::Fog, night ); } kdDebug(12006) << "Clouds: " << weatherInfo.clouds << ", Icon: " @@ -860,23 +861,23 @@ bool MetarParser::isNight(const TQString &stationID) const } } -TQString MetarParser::iconName( int condition, bool night, int strength ) const +void MetarParser::getIcon( int condition, bool night, int strength ) { - TQString _iconName; if( strength != 0 ) { // Ranged WeatherIcon* wi = new WeatherIcon( condition, night, strength ); - _iconName = wi->name(); + weatherInfo.iconName = wi->name(); + weatherInfo.iconPath = wi->path(); delete wi; } else { // Simple WeatherIcon* wi = new WeatherIcon( condition, night ); - _iconName = wi->name(); + weatherInfo.iconName = wi->name(); + weatherInfo.iconPath = wi->path(); delete wi; } - return _iconName; } diff --git a/kweather/metar_parser.h b/kweather/metar_parser.h index a5ab028..6a645d2 100644 --- a/kweather/metar_parser.h +++ b/kweather/metar_parser.h @@ -32,6 +32,8 @@ struct WeatherInfo { /** The current weather state outside */ TQString theWeather; + TQString iconName; + TQString iconPath; int clouds; float windMPH; float tempC; @@ -92,7 +94,7 @@ class MetarParser void calcCurrentIcon(); void calcWindChill(); bool isNight(const TQString &stationID) const; - TQString iconName( int condition, bool night, int strength = 0 ) const; + void getIcon( int condition, bool night, int strength = 0 ); /* * Reset the internal WeatherInfo struct of the class so that diff --git a/kweather/prefdialogdata.ui b/kweather/prefdialogdata.ui index db4844e..8fa70b1 100644 --- a/kweather/prefdialogdata.ui +++ b/kweather/prefdialogdata.ui @@ -9,8 +9,8 @@ 0 0 - 429 - 341 + 761 + 464 @@ -27,6 +27,9 @@ 0 + + prefsDialogData + TabFocus @@ -50,7 +53,7 @@ - GroupBoxPanel + NoFrame Sunken @@ -67,7 +70,7 @@ TextLabel1_4_2_2 - &Location: + Lo&cation: m_reportLocation @@ -152,7 +155,10 @@ - &Show icon only + Show icon onl&y + + + Alt+Y 1 @@ -171,6 +177,9 @@ Show &icon and temperature + + Alt+I + 2 @@ -180,7 +189,7 @@ CheckBox10 - Show icon, temperature, &wind and pressure information + Show icon, temperature, wind &and pressure information true @@ -271,6 +280,44 @@ + + + m_iconTheme + + + Weather Icon + + + + unnamed + + + + m_systemIcons + + + &Use system icon theme + + + Alt+U + + + true + + + + + m_kweatherIcons + + + Use &KWeather theme + + + Alt+K + + + + groupBox3 @@ -320,7 +367,7 @@ - 170 + 637 20 @@ -329,8 +376,6 @@ - - m_enableLog 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 diff --git a/kweather/weather_icon.h b/kweather/weather_icon.h index 3ae8ddc..f18c472 100644 --- a/kweather/weather_icon.h +++ b/kweather/weather_icon.h @@ -1,5 +1,28 @@ class TDEIconLoader; +class WeatherIconPrivate { + friend class WeatherIcon; + + public: + WeatherIconPrivate(); + ~WeatherIconPrivate(); + + static WeatherIconPrivate* instance(); + + void useIconTheme( bool use ); + bool usingIconTheme(); + + TQString iconPath( TQString icon, bool inTheme ); + TQString iconPath( TQString icon ); + + private: + static WeatherIconPrivate* s_instance; + TDEIconLoader* iconLoader; + bool m_useIconTheme; + + TQPair findIcon( TQStringList fallback ); +}; + class WeatherIcon { public: enum SimpleCondition { Sunny, Fog, Mist, Overcast, Hail, LightRain, Sleet }; @@ -10,12 +33,10 @@ class WeatherIcon { ~WeatherIcon(); static TQString unknown() { return "weather-none-available"; }; - TQString& name() { return iconName; } + TQString name() { return iconName; } + TQString path() { return iconPath; } private: - TQString findIcon( TQStringList fallback ); - bool iconExists( TQString& icon, bool inTheme = true ); - - TDEIconLoader* iconLoader; TQString iconName; + TQString iconPath; }; diff --git a/kweather/weatherlib.cpp b/kweather/weatherlib.cpp index 32d653c..a7bb13c 100644 --- a/kweather/weatherlib.cpp +++ b/kweather/weatherlib.cpp @@ -272,14 +272,34 @@ TQString WeatherLib::windChill(const TQString &stationID){ TQString WeatherLib::iconName(const TQString &stationID){ - TQString result = WeatherIcon::unknown(); + TQString result; + + // isEmpty is true for null or 0 length strings + if ( !stationID.isEmpty() ) + { + Data *d = findData(stationID); + result = d->wi.iconName; + } + + if( result == TQString::null ) + result = WeatherIcon::unknown(); + + return result; +} + +TQString WeatherLib::iconPath(const TQString &stationID){ + + TQString result; // isEmpty is true for null or 0 length strings if ( !stationID.isEmpty() ) { Data *d = findData(stationID); - result = d->wi.theWeather; + result = d->wi.iconPath; } + + if( result == TQString::null ) + result = WeatherIconPrivate::instance()->iconPath(WeatherIcon::unknown()); return result; } diff --git a/kweather/weatherlib.h b/kweather/weatherlib.h index 2e5f8b3..5dde37c 100644 --- a/kweather/weatherlib.h +++ b/kweather/weatherlib.h @@ -48,6 +48,7 @@ class WeatherLib : public TQObject TQString wind(const TQString &stationID); TQString pressure(const TQString &stationID); TQString iconName(const TQString &stationID); + TQString iconPath(const TQString &stationID); TQString date(const TQString &stationID); TQStringList weather(const TQString &stationID); TQString visibility(const TQString &stationID); diff --git a/kweather/weatherservice.cpp b/kweather/weatherservice.cpp index f54d803..556a015 100644 --- a/kweather/weatherservice.cpp +++ b/kweather/weatherservice.cpp @@ -30,6 +30,7 @@ #include "weatherlib.h" #include "weatherservice.h" #include "stationdatabase.h" +#include "weather_icon.h" #include "sun.h" WeatherService::WeatherService(TQObject *parent, const char *name) : TQObject (parent, name), DCOPObject("WeatherService") @@ -150,13 +151,18 @@ TQString WeatherService::currentIconString(const TQString &stationID) TQString WeatherService::iconFileName(const TQString &stationID) { - TQString _name = m_weatherLib->iconName(stationID); - TQString icon = kapp->iconLoader()->iconPath(_name, TDEIcon::Desktop, true); - if( icon.isNull() ) - { - icon = locate( "data", "kweather/" + _name + ".png" ); - } - return icon; + return m_weatherLib->iconPath(stationID); +} + +void WeatherService::useIconTheme(bool use) +{ + kdDebug() << "[!!!] received signal to set useIconTheme to " << use << endl; + WeatherIconPrivate::instance()->useIconTheme(use); +} + +bool WeatherService::usingIconTheme() +{ + return WeatherIconPrivate::instance()->usingIconTheme(); } TQString WeatherService::date(const TQString &stationID) diff --git a/kweather/weatherservice.h b/kweather/weatherservice.h index dba1ac9..ae0f39b 100644 --- a/kweather/weatherservice.h +++ b/kweather/weatherservice.h @@ -90,6 +90,9 @@ class WeatherService : public TQObject, public DCOPObject void addStation(const TQString &stationID); TQStringList listStations(); TQString stationCode( const TQString &stationName ); + + void useIconTheme( bool use ); + bool usingIconTheme(); void exit(); -- cgit v1.2.1