diff options
author | Luiz Agostini <luiz.agostini@openbossa.org> | 2009-11-05 18:54:10 -0300 |
---|---|---|
committer | Luiz Agostini <luiz.agostini@openbossa.org> | 2009-11-05 18:57:33 -0300 |
commit | 2313108f1cc8cb39c138d5a9bec5bdcb323d7bcd (patch) | |
tree | 0817055f67e1c7677e26acda133f15db293a9499 | |
parent | 2684f9d1e24f296df3ae35a6bbb437ba42df3f63 (diff) |
Weather: carroussel/manager city list synchronization.
Signed-off-by: Luiz Agostini <luiz.agostini@openbossa.org>
-rw-r--r-- | weather/citycarroussel.cpp | 43 | ||||
-rw-r--r-- | weather/citycarroussel.h | 7 | ||||
-rw-r--r-- | weather/citylist.cpp | 5 | ||||
-rw-r--r-- | weather/citylist.h | 6 | ||||
-rw-r--r-- | weather/citymanager.cpp | 2 | ||||
-rw-r--r-- | weather/citymanager.h | 6 | ||||
-rw-r--r-- | weather/forecastdata.h | 10 | ||||
-rw-r--r-- | weather/mainview.cpp | 4 |
8 files changed, 71 insertions, 12 deletions
diff --git a/weather/citycarroussel.cpp b/weather/citycarroussel.cpp index bcdeb2a..8b1e4e3 100644 --- a/weather/citycarroussel.cpp +++ b/weather/citycarroussel.cpp @@ -249,6 +249,7 @@ CityCarroussel::CityCarroussel(QGraphicsItem *parent) , m_transparencySize(transparencyRef * m_boundingRect.width()) , m_distance(m_backgroundWidth - m_transparencySize) , m_active(true) + , m_deleteAfterMove(false) { m_gestureBox->setRect(boundingRect()); m_carroussel.add(&m_data); @@ -348,6 +349,40 @@ void CityCarroussel::add(ForecastData item) updateMainItem(); } +void CityCarroussel::update(QList<ForecastData> items) +{ + if (items.isEmpty()) + return; + + bool wasEmpty = m_data.count() == 0; + bool transitionNeeded = false; + + if (!wasEmpty) { + for (int i = 0; i < items.count(); ++i) { + if (items[i].key() == m_data[0].key()) { + for (int j = 0; j < i; ++j) + items.append(items.takeFirst()); + break; + } + } + transitionNeeded = m_data[0] != items[0]; + if (transitionNeeded) + items.push_front(m_data[0]); + } + + m_data.reset(items, 0); + updateBackground(-1); + updateBackground(1); + + if (wasEmpty) { + updateBackground(0); + updateMainItem(); + } + if (transitionNeeded) { + move(-1, true); + } +} + void CityCarroussel::updateMainItem() { delete m_view; @@ -391,6 +426,11 @@ void CityCarroussel::moveEnd(int direction) m_background[i]->setReferencePos(m_positions[i + 1]); m_background[i]->setZValue(i); } + if (m_deleteAfterMove) { + m_data.remove(direction); + updateBackground(direction); + } + updateBackground(-direction); updateMainItem(); } @@ -414,11 +454,12 @@ void CityCarroussel::setGestureDisplacement(qreal displacement) setDisplacement(displacement); } -void CityCarroussel::move(int direction) +void CityCarroussel::move(int direction, bool deleteLastItem) { m_gestureBox->setActive(false); m_gestureBox->abort(); disconnect(m_cityInfo[0]); + m_deleteAfterMove = deleteLastItem; QPropertyAnimation *animation = new QPropertyAnimation(this, "displacement"); diff --git a/weather/citycarroussel.h b/weather/citycarroussel.h index 5e953e7..64d0570 100644 --- a/weather/citycarroussel.h +++ b/weather/citycarroussel.h @@ -108,12 +108,14 @@ public: ~CityCarroussel(); void add(ForecastData item); + void update(QList<ForecastData> items); + static int loadImages(); bool active() const { return m_active; } void setActive(bool value); - int selected() const { return m_data.pos(); } + QString selected() const { return m_data.count() > 0 ? m_data[0].key() : QString(); } QRectF boundingRect () const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); @@ -145,10 +147,11 @@ private: qreal m_positions[3]; bool m_active; + bool m_deleteAfterMove; void setGestureDisplacement(qreal displacement); void moveBack() { move(0); } - void move(int direction); + void move(int direction, bool deleteLastItem = false); void moveEnd(int direction); void updateBackground(int idx); diff --git a/weather/citylist.cpp b/weather/citylist.cpp index 9868b4f..3531d96 100644 --- a/weather/citylist.cpp +++ b/weather/citylist.cpp @@ -199,6 +199,7 @@ CityListItem::CityListItem(const ForecastData &data, CityContentList *list) { m_delete->setPos(ITEM_BUTTON_LEFT, getCenterVerticalPos(m_delete)); m_check->setPos(ITEM_CHECK_LEFT, getCenterVerticalPos(m_check)); + m_check->hide(); m_text->setPos(TEXT_LEFT, getCenterVerticalPos(m_text)); m_text->setCacheMode(ItemCoordinateCache); @@ -266,12 +267,12 @@ void CityContentList::addForecast(const ForecastData &data) addItem(new CityListItem(data, this)); } -void CityContentList::select(int selected) +void CityContentList::select(const QString &selected) { for (int i = 0; i < itemCount(); ++i) { CityListItem *item = static_cast<CityListItem*>(getItem(i)); if (item) - item->select(i == selected); + item->select(item->data().key() == selected); } } diff --git a/weather/citylist.h b/weather/citylist.h index e6a343f..5fd1e00 100644 --- a/weather/citylist.h +++ b/weather/citylist.h @@ -69,6 +69,8 @@ public: CityListItem(const ForecastData &data, CityContentList *list); void select(bool selected); + const ForecastData &data() const { return m_data; } + qreal contentHeight() const; QAbstractAnimation *getShowAnimation(); QAbstractAnimation *getHideAnimation(); @@ -102,7 +104,7 @@ class CityContentList : public ContentList public: CityContentList(QList<ForecastData> &contentList, QObject *holder); void addForecast(const ForecastData &data); - void select(int selected); + void select(const QString &selected); protected: QAbstractAnimation *getInsertAnimation(int idx, qreal height); @@ -129,7 +131,7 @@ class CityList : public QObject, public QGraphicsItem Q_INTERFACES(QGraphicsItem); public: CityList(QList<ForecastData> &contentList, QGraphicsItem *parent = 0); - void select(int selected) { m_list->select(selected); } + void select(const QString &selected) { m_list->select(selected); } QRectF boundingRect () const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); diff --git a/weather/citymanager.cpp b/weather/citymanager.cpp index 603c26f..665c3b7 100644 --- a/weather/citymanager.cpp +++ b/weather/citymanager.cpp @@ -117,7 +117,7 @@ void CityManager::mousePressEvent(QGraphicsSceneMouseEvent *event) startAnimation(false); } -void CityManager::showManager(int selected) +void CityManager::showManager(const QString &selected) { m_content->select(selected); startAnimation(true); diff --git a/weather/citymanager.h b/weather/citymanager.h index 57ca5ca..3b80393 100644 --- a/weather/citymanager.h +++ b/weather/citymanager.h @@ -45,7 +45,7 @@ class CityManagerContent : public QObject, public QGraphicsItem Q_PROPERTY(qreal top READ getTop WRITE setTop); public: CityManagerContent(QList<ForecastData> &contentList, QGraphicsItem *parent = 0); - void select(int selected) { m_list->select(selected); } + void select(const QString &selected) { m_list->select(selected); } QRectF boundingRect () const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); @@ -71,11 +71,13 @@ class CityManager : public QObject, public QGraphicsItem Q_INTERFACES(QGraphicsItem) public: CityManager(QList<ForecastData> contentList, QGraphicsItem *parent = 0); - void showManager(int selected); + void showManager(const QString &selected); QRectF boundingRect () const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + QList<ForecastData> forecastList() const { return m_contentList; } + protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); diff --git a/weather/forecastdata.h b/weather/forecastdata.h index 324f133..c224e48 100644 --- a/weather/forecastdata.h +++ b/weather/forecastdata.h @@ -34,6 +34,9 @@ public: int current() const { return m_current; } int upper() const { return m_upper; } + inline bool operator==(const ForecastData &other); + bool operator!=(const ForecastData &other) { return !operator==(other); } + private: Forecast::ForecastType m_type; bool m_night; @@ -45,4 +48,11 @@ private: int m_upper; }; +bool ForecastData::operator==(const ForecastData &other) +{ + return m_type == other.m_type && m_night == other.m_night && m_error == other.m_error + && m_key == other.m_key && m_cityName == other.m_cityName && m_lower == other.m_lower + && m_current == other.m_current && m_upper == other.m_upper; +} + #endif // FORECASTDATA_H diff --git a/weather/mainview.cpp b/weather/mainview.cpp index 7340b43..9da5923 100644 --- a/weather/mainview.cpp +++ b/weather/mainview.cpp @@ -94,8 +94,7 @@ void MainView::showCarroussel() connect(this, SIGNAL(moveLeft()), m_carroussel, SLOT(moveLeft())); connect(this, SIGNAL(moveRight()), m_carroussel, SLOT(moveRight())); - foreach (const ForecastData &forecast, data) - m_carroussel->add(forecast); + m_carroussel->update(data); m_carroussel->setPos(0.0, 0.0); m_carroussel->setZValue(0.0); m_scene.addItem(m_carroussel); @@ -128,6 +127,7 @@ void MainView::closeCityManager() m_manager->hide(); m_scene.removeItem(m_manager); disconnect(m_manager, SIGNAL(terminated()), this, SLOT(closeCityManager())); + m_carroussel->update(m_manager->forecastList()); m_carroussel->setActive(true); } |