00001
00002 #include <vector>
00003 using namespace std;
00004
00005
00006 #include <mythcontext.h>
00007
00008
00009 #include "weather.h"
00010 #include "weatherScreen.h"
00011
00012 WeatherScreen *WeatherScreen::loadScreen(MythScreenStack *parent,
00013 ScreenListInfo *screenDefn, int id)
00014 {
00015 return new WeatherScreen(parent, screenDefn, id);
00016 }
00017
00018 WeatherScreen::WeatherScreen(MythScreenStack *parent,
00019 ScreenListInfo *screenDefn, int id) :
00020 MythScreenType (parent, screenDefn->title),
00021 m_units(SI_UNITS),
00022 m_screenDefn(screenDefn),
00023 m_name(m_screenDefn->name),
00024 m_inuse(false),
00025 m_prepared(false),
00026 m_id(id)
00027 {
00028
00029 QStringList types = m_screenDefn->dataTypes;
00030
00031 for (int i = 0; i < types.size(); ++i)
00032 {
00033 m_dataValueMap[types.at(i)] = "";
00034 }
00035 }
00036
00037 WeatherScreen::~WeatherScreen()
00038 {
00039 }
00040
00041 bool WeatherScreen::Create()
00042 {
00043
00044 bool foundtheme = false;
00045
00046
00047 foundtheme = LoadWindowFromXML("weather-ui.xml", m_name, this);
00048
00049 if (!foundtheme)
00050 return false;
00051
00052 if (!prepareScreen(true))
00053 return false;
00054
00055 return true;
00056 }
00057
00058 bool WeatherScreen::canShowScreen()
00059 {
00060 if (!inUse())
00061 return false;
00062
00063 bool ok = true;
00064 QMapIterator<QString, QString> i(m_dataValueMap);
00065 while (i.hasNext()) {
00066 i.next();
00067 if (i.key().isEmpty())
00068 {
00069 LOG(VB_GENERAL, LOG_DEBUG, i.key());
00070 ok = false;
00071 }
00072 }
00073
00074 return ok;
00075 }
00076
00077
00078 void WeatherScreen::setValue(const QString &key, const QString &value)
00079 {
00080 if (m_dataValueMap.contains(key))
00081 m_dataValueMap[key] = prepareDataItem(key, value);
00082 }
00083
00084 void WeatherScreen::newData(QString loc, units_t units, DataMap data)
00085 {
00086 (void) loc;
00087 (void) units;
00088
00089 DataMap::iterator itr = data.begin();
00090 while (itr != data.end())
00091 {
00092 setValue(itr.key(), *itr);
00093 ++itr;
00094 }
00095
00096
00097
00098 if (!prepareScreen())
00099 LOG(VB_GENERAL, LOG_ERR, "Theme is missing a required widget!");
00100
00101 emit screenReady(this);
00102 }
00103
00104 QString WeatherScreen::getTemperatureUnit()
00105 {
00106 if (m_units == ENG_UNITS)
00107 return QString::fromUtf8("°") + "F";
00108 else
00109 return QString::fromUtf8("°") + "C";
00110 }
00111
00112 bool WeatherScreen::prepareScreen(bool checkOnly)
00113 {
00114 QMap<QString, QString>::iterator itr = m_dataValueMap.begin();
00115 while (itr != m_dataValueMap.end())
00116 {
00117 QString name = itr.key();
00118 MythUIType *widget = GetChild(name);
00119
00120 if (!widget)
00121 {
00122 LOG(VB_GENERAL, LOG_ERR, "Widget not found " + itr.key());
00123
00124 if (name == "copyright")
00125 {
00126 LOG(VB_GENERAL, LOG_WARNING,
00127 QString("No copyright widget found, skipping screen %1.")
00128 .arg(m_name));
00129 return false;
00130 }
00131 }
00132
00133 if( !widget || checkOnly )
00134 {
00135 ++itr;
00136 continue;
00137 }
00138
00139 if (dynamic_cast<MythUIText *>(widget))
00140 {
00141 ((MythUIText *) widget)->SetText(itr.value());
00142 }
00143 else if (dynamic_cast<MythUIImage *>(widget))
00144 {
00145 ((MythUIImage *) widget)->SetFilename(itr.value());
00146 ((MythUIImage *) widget)->Load();
00147 }
00148
00149 prepareWidget(widget);
00150 ++itr;
00151 }
00152
00153 m_prepared = true;
00154 return true;
00155 }
00156
00157 void WeatherScreen::prepareWidget(MythUIType *widget)
00158 {
00159 MythUIImage *img;
00160
00161
00162
00163
00164 if ((img = dynamic_cast<MythUIImage *>(widget)))
00165 {
00166 img->Load();
00167 }
00168 }
00169
00170 QString WeatherScreen::formatDataItem(const QString &key, const QString &value)
00171 {
00172 if (key == "relative_humidity")
00173 return value + " %";
00174
00175 if (key == "pressure")
00176 return value + (m_units == ENG_UNITS ? " in" : " mb");
00177
00178 if (key == "visibility")
00179 return value + (m_units == ENG_UNITS ? " mi" : " km");
00180
00181 if (key == "temp" || key == "appt" || key.contains("low",Qt::CaseInsensitive) ||
00182 key.contains("high",Qt::CaseInsensitive) ||
00183 key.contains("temp",Qt::CaseInsensitive))
00184 {
00185 if ( (value == "NA") || (value == "N/A") )
00186 return value;
00187 else
00188 return value + getTemperatureUnit();
00189 }
00190
00191 if (key == "wind_gust" || key == "wind_spdgst" || key == "wind_speed")
00192 return value + (m_units == ENG_UNITS ? " mph" : " km/h");
00193
00194
00195
00196 if (key.startsWith("date-"))
00197 {
00198 bool isNumber;
00199 value.toInt( &isNumber);
00200
00201 if (isNumber)
00202 {
00203 int dayOfWeek = value.toInt();
00204
00205 switch (dayOfWeek)
00206 {
00207 case DAY_SUNDAY :
00208 return tr("Sunday");
00209 break;
00210 case DAY_MONDAY :
00211 return tr("Monday");
00212 break;
00213 case DAY_TUESDAY :
00214 return tr("Tuesday");
00215 break;
00216 case DAY_WENDESDAY :
00217 return tr("Wednesday");
00218 break;
00219 case DAY_THURSDAY :
00220 return tr("Thursday");
00221 break;
00222 case DAY_FRIDAY :
00223 return tr("Friday");
00224 break;
00225 case DAY_SATURDAY :
00226 return tr("Saturday");
00227 break;
00228 }
00229 }
00230 }
00231
00232 return value;
00233 }
00234
00235 QString WeatherScreen::prepareDataItem(const QString &key, const QString &value)
00236 {
00237 return formatDataItem(key, value);
00238 }
00239
00240 bool WeatherScreen::keyPressEvent(QKeyEvent *event)
00241 {
00242 if (GetFocusWidget() && GetFocusWidget()->keyPressEvent(event))
00243 return true;
00244
00245 return false;
00246 }
00247
00248
00249
00250