diff options
author | Luiz Agostini <luiz.agostini@openbossa.org> | 2009-11-05 23:53:29 -0300 |
---|---|---|
committer | Luiz Agostini <luiz.agostini@openbossa.org> | 2009-11-05 23:53:29 -0300 |
commit | 8b6cb940381126438480db1cea91b902e3cf3d40 (patch) | |
tree | 3a3057cb362426e5be70c4ca33e8ad67baf3e73e | |
parent | a851eb64f0dc8420276f5fcc689d6941fdacd072 (diff) |
Weather: save/read city list from ini file.
Signed-off-by: Luiz Agostini <luiz.agostini@openbossa.org>
-rw-r--r-- | weather/bootmanager.cpp | 11 | ||||
-rw-r--r-- | weather/bootmanager.h | 4 | ||||
-rw-r--r-- | weather/forecastdata.h | 4 | ||||
-rw-r--r-- | weather/mainview.cpp | 9 | ||||
-rw-r--r-- | weather/mainview.h | 1 | ||||
-rw-r--r-- | weather/resources.qrc | 1 | ||||
-rw-r--r-- | weather/settings.cpp | 35 | ||||
-rw-r--r-- | weather/settings.h | 1 | ||||
-rw-r--r-- | weather/weather.ini | 43 |
9 files changed, 80 insertions, 29 deletions
diff --git a/weather/bootmanager.cpp b/weather/bootmanager.cpp index 2f3b040..de2b6ff 100644 --- a/weather/bootmanager.cpp +++ b/weather/bootmanager.cpp @@ -26,8 +26,10 @@ void BootManager::run() { m_keys = Settings::getCurrentCities(); ForecastProvider::connectToResponseSignal(this, SLOT(forecastResponse(ForecastData))); - foreach (const QString & city, m_keys) + foreach (const QString & city, m_keys) { ForecastProvider::getForecast(city); + m_data.append(ForecastData()); + } m_pendingResponses = m_keys.count(); PixmapLoader::connectToOnIdleSignal(this, SLOT(pixmapLoaderIsIdle())); int count = ForecastHungItem::loadImages(); @@ -48,10 +50,11 @@ void BootManager::run() void BootManager::forecastResponse(const ForecastData &forecast) { - if (m_keys.indexOf(forecast.key()) >= 0) { - if (!m_data.contains(forecast.key())) + int idx = m_keys.indexOf(forecast.key()); + if (idx >= 0) { + if (m_data[idx].isNull()) --m_pendingResponses; - m_data[forecast.key()] = forecast; + m_data[idx] = forecast; } if (!m_pendingResponses) { ForecastProvider::disconnectReceiver(this); diff --git a/weather/bootmanager.h b/weather/bootmanager.h index 57fbbdd..a94e0f1 100644 --- a/weather/bootmanager.h +++ b/weather/bootmanager.h @@ -12,7 +12,7 @@ class BootManager : public QObject public: BootManager(QObject *parent = 0); void run(); - QList<ForecastData> data() const { return m_data.values(); } + QList<ForecastData> data() const { return m_data; } signals: void ready(); @@ -23,7 +23,7 @@ private slots: private: QStringList m_keys; - QMap<QString, ForecastData> m_data; + QList<ForecastData> m_data; int m_pendingResponses; bool m_imagesLoaded; }; diff --git a/weather/forecastdata.h b/weather/forecastdata.h index c224e48..eaf711f 100644 --- a/weather/forecastdata.h +++ b/weather/forecastdata.h @@ -12,6 +12,7 @@ public: , m_lower(0) , m_current(0) , m_upper(0) + , m_isNull(true) {} ForecastData(bool error, Forecast::ForecastType type, bool night, const QString &key, const QString &cityName, int lower, int current, int upper) @@ -23,6 +24,7 @@ public: , m_lower(lower) , m_current(current) , m_upper(upper) + , m_isNull(false) {} Forecast::ForecastType type() const { return m_type; } @@ -33,6 +35,7 @@ public: int lower() const { return m_lower; } int current() const { return m_current; } int upper() const { return m_upper; } + bool isNull() const { return m_isNull; } inline bool operator==(const ForecastData &other); bool operator!=(const ForecastData &other) { return !operator==(other); } @@ -46,6 +49,7 @@ private: int m_lower; int m_current; int m_upper; + bool m_isNull; }; bool ForecastData::operator==(const ForecastData &other) diff --git a/weather/mainview.cpp b/weather/mainview.cpp index 9da5923..b3bb3b4 100644 --- a/weather/mainview.cpp +++ b/weather/mainview.cpp @@ -127,10 +127,19 @@ void MainView::closeCityManager() m_manager->hide(); m_scene.removeItem(m_manager); disconnect(m_manager, SIGNAL(terminated()), this, SLOT(closeCityManager())); + saveManagerList(); m_carroussel->update(m_manager->forecastList()); m_carroussel->setActive(true); } +void MainView::saveManagerList() +{ + QStringList list; + foreach(const ForecastData &data, m_manager->forecastList()) + list.append(data.key()); + Settings::setCurrentCities(list); +} + void MainView::keyPressEvent(QKeyEvent* event) { if (event->key() == Qt::Key_Left) diff --git a/weather/mainview.h b/weather/mainview.h index 7501675..2663b0e 100644 --- a/weather/mainview.h +++ b/weather/mainview.h @@ -74,6 +74,7 @@ private: void showCarroussel(); void createCityManager(); + void saveManagerList(); }; #endif // MAINVIEW_H diff --git a/weather/resources.qrc b/weather/resources.qrc index 7b3652b..b4a8863 100644 --- a/weather/resources.qrc +++ b/weather/resources.qrc @@ -1,6 +1,5 @@ <RCC> <qresource prefix="/"> - <file alias="weather.ini">weather.ini</file> <file>images/weather_elements/bg_day_clear.png</file> <file>images/weather_elements/bg_day_heavyrain.png</file> <file>images/weather_elements/bg_day_rain.png</file> diff --git a/weather/settings.cpp b/weather/settings.cpp index 975b474..896c90d 100644 --- a/weather/settings.cpp +++ b/weather/settings.cpp @@ -42,7 +42,7 @@ #define SETTINGS_FILE "e://resources/apps/weather.ini" #else -#define SETTINGS_FILE ":/weather.ini" +#define SETTINGS_FILE "weather.ini" #endif Settings::Settings() @@ -72,15 +72,48 @@ bool Settings::scaledImages() return size.width() != 480 || size.height() != 864; } +class CitySortHelper +{ +public: + CitySortHelper(const QMap<QString, int> &map) : m_map(map) {} + bool operator()(const QString &city1, const QString &city2) + { + qDebug() << city1 << m_map[city1] << city2 << m_map[city2] << (m_map[city1] < m_map[city2]); + return m_map[city1] < m_map[city2]; + } + +private: + const QMap<QString, int> &m_map; +}; + QStringList Settings::getCurrentCities() { Settings *obj = instance(); + obj->m_settings.beginGroup("Cities"); + QStringList result = obj->m_settings.allKeys(); + QMap<QString, int> map; + foreach(const QString &key, result) + map[key] = obj->m_settings.value(key).toInt(); + obj->m_settings.endGroup(); + + qSort(result.begin(), result.end(), CitySortHelper(map)); return result; } +void Settings::setCurrentCities(QStringList cities) +{ + Settings *obj = instance(); + obj->m_settings.beginGroup("Cities"); + obj->m_settings.remove(""); + for (int i = 0; i < cities.count(); ++i) + obj->m_settings.setValue(cities[i], i); + obj->m_settings.endGroup(); + obj->m_settings.sync(); +} + void Settings::configPortrait() { #ifdef Q_OS_SYMBIAN diff --git a/weather/settings.h b/weather/settings.h index 9bc2d9b..5e4bdde 100644 --- a/weather/settings.h +++ b/weather/settings.h @@ -61,6 +61,7 @@ public: static qreal scaleHeight(qreal height) { return height * heightFactor(); } static QStringList getCurrentCities(); + static void setCurrentCities(QStringList cities); private slots: void configPortrait(); diff --git a/weather/weather.ini b/weather/weather.ini index 2e22feb..59f46a6 100644 --- a/weather/weather.ini +++ b/weather/weather.ini @@ -3,24 +3,25 @@ windowSize = @Size(480 864) [Cities] Araxa = 1 -Belo Horizonte = 1 -Recife = 1 -Ipojuca = 1 -Fortaleza = 1 -Sao Paulo = 1 -Uberlandia = 1 -Governador Valadares = 1 -Brasilia = 1 -Florianopolis = 1 -Gravata = 1 -Maceio = 1 -Porto Alegre = 1 -Batatais = 1 -Ribeirao Preto = 1 -Campinas = 1 -Campo belo = 1 -Sertaozinho = 1 -Pirassununga = 1 -Goiania = 1 -Manaus = 1 -Belem = 1 +Belo Horizonte = 2 +Recife = 3 +Ipojuca = 4 +Fortaleza = 5 +Sao Paulo = 6 +Uberlandia = 7 +Governador Valadares = 8 +Brasilia = 9 +Florianopolis = 10 +Gravata = 11 +Maceio = 12 +Porto Alegre = 13 +Batatais = 14 +Ribeirao Preto = 15 +Campinas = 16 +Campo belo = 17 +Sertaozinho = 18 +Pirassununga = 19 +Goiania = 20 +Manaus = 21 +Belem = 22 +[Cities] |