summaryrefslogtreecommitdiffstats
path: root/weather
diff options
context:
space:
mode:
authorLuiz Agostini <luiz.agostini@openbossa.org>2009-11-04 23:07:16 -0300
committerLuiz Agostini <luiz.agostini@openbossa.org>2009-11-05 00:42:10 -0300
commiteb47fcf330750f2ca8492b3f612b6a40f3b6eb8a (patch)
treee1dcac969aa14c6e2da84e777add65580e199e9f /weather
parentc3213274dad3c3e9d4e65bd4989fdb6aab8e12fb (diff)
Weather: City manager enhancements.
* city manager kept alive. * using paint text items. * forecast data list maintenance. Signed-off-by: Luiz Agostini <luiz.agostini@openbossa.org>
Diffstat (limited to 'weather')
-rw-r--r--weather/addcitytool.cpp294
-rw-r--r--weather/addcitytool.h52
-rw-r--r--weather/citylist.cpp70
-rw-r--r--weather/citylist.h31
-rw-r--r--weather/citymanager.cpp96
-rw-r--r--weather/citymanager.h25
-rw-r--r--weather/mainview.cpp31
-rw-r--r--weather/mainview.h1
8 files changed, 297 insertions, 303 deletions
diff --git a/weather/addcitytool.cpp b/weather/addcitytool.cpp
index dd2539f..6ecbd89 100644
--- a/weather/addcitytool.cpp
+++ b/weather/addcitytool.cpp
@@ -1,6 +1,7 @@
#include "addcitytool.h"
#include "settings.h"
#include "forecastprovider.h"
+#include "painttextitem.h"
#include <QPainter>
#include <QInputDialog>
@@ -86,8 +87,6 @@ AddCityFirstScreen::AddCityFirstScreen(const QRectF &boundingRect, QGraphicsItem
rect.adjust(5, 5, -5, -5);
m_lineEdit->setGeometry(rect);
- qDebug() << m_lineEdit->geometry();
-
m_proxy->setWidget(m_lineEdit);
m_proxy->setParentItem(m_textBackground);
m_proxy->setPos(5.0, 5.0);
@@ -97,33 +96,9 @@ AddCityFirstScreen::AddCityFirstScreen(const QRectF &boundingRect, QGraphicsItem
connect(m_button, SIGNAL(clicked()), this, SLOT(buttonClick()));
}
-QVariant AddCityFirstScreen::itemChange(GraphicsItemChange change, const QVariant &value)
-{
-// if (change == ItemSceneHasChanged)
-// createInputElement();
- return AddCityScreen::itemChange(change, value);
-}
-
-void AddCityFirstScreen::createInputElement()
+void AddCityFirstScreen::clean()
{
- if (m_proxy) {
- delete m_proxy;
- m_proxy = 0;
- m_lineEdit = 0;
- }
-
- if (!scene())
- return;
-
- /*
- m_lineEdit = new QLineEdit();
- m_proxy = scene()->addWidget(m_lineEdit);
- m_proxy->setParentItem(this);
- m_proxy->setPos(5.0, 5.0);
- m_lineEdit->setText("sao paulo");
- m_lineEdit->setFocus();
- */
-
+ m_lineEdit->setText("");
}
void AddCityFirstScreen::buttonClick()
@@ -134,172 +109,157 @@ void AddCityFirstScreen::buttonClick()
// AddCitySearchScreen
-AddCitySearchScreen::AddCitySearchScreen(const QRectF &boundingRect,
- const QString &city, QGraphicsItem *parent)
+AddCitySearchScreen::AddCitySearchScreen(const QRectF &boundingRect, QGraphicsItem *parent)
: AddCityScreen(boundingRect, parent)
- , m_city(city)
, m_loading(new Loading(this))
{
+ m_loading->hide();
m_loading->setPos(BUTTON_LEFT, getCenterVerticalPos(m_loading));
- m_loading->start();
- ForecastProvider::connectToResponseSignal(this, SLOT(forecastResponse(ForecastData)));
- ForecastProvider::getForecast(city, this);
}
void AddCitySearchScreen::forecastResponse(const ForecastData &forecast)
{
if (forecast.key() == m_city) {
- if (forecast.error())
- emit forecastIsNotOk(forecast);
- else
- emit forecastIsOk(forecast);
+ reset();
+ emit forecastReceived(forecast);
}
}
+void AddCitySearchScreen::setCityName(const QString &name)
+{
+ m_city = name;
+ ForecastProvider::connectToResponseSignal(this, SLOT(forecastResponse(ForecastData)));
+ ForecastProvider::getForecast(m_city, false);
+ m_loading->start();
+ m_loading->show();
+ update();
+}
+
+void AddCitySearchScreen::reset()
+{
+ ForecastProvider::disconnectReceiver(this);
+ m_loading->stop();
+ m_loading->hide();
+}
+
void AddCitySearchScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt, QWidget *widget)
{
Q_UNUSED(opt);
Q_UNUSED(widget);
- QBrush brush(Qt::white);
- QFont font;
- font.setFamily("Nokia Sans");
- font.setPixelSize(ADD_SCREEN_FONT_SIZE);
- font.setStyleStrategy(QFont::PreferAntialias);
-
- QColor color(255, 255, 255, 102);
-
- QPen pen;
- pen.setJoinStyle(Qt::RoundJoin);
-
- pen.setColor(color);
- pen.setBrush(color);
- brush.setColor(color);
-
- QFontMetrics m(font);
-
- int height = m.height();
- qreal pos = 10.0 + height;
-
- painter->setBrush(brush);
- painter->setPen(pen);
- painter->setFont(font);
-
- QFont fontB(font);
- fontB.setBold(true);
- QFontMetrics mB(fontB);
-
- static const QString header("Searching for");
- QString text = mB.elidedText(m_city, Qt::ElideRight, BUTTON_LEFT - 20);
- text = "\"" + text + "\"";
+ TextPainter header(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 102), "Searching for");
- int headerWidth = m.width(header);
- int bWidth = mB.width(text);
+ TextPainter city(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 255), m_city);
+ city.setQuoted(true);
+ city.font().setBold(true);
+ city.setMaxWidth(BUTTON_LEFT);
+ qreal top = (boundingRect().height() - header.height() - city.height()) / 2;
+ top += boundingRect().top();
- qreal topLeft = (BUTTON_LEFT - qreal(headerWidth)) / 2;
- qreal bottomLeft = (BUTTON_LEFT - qreal(bWidth)) / 2;
+ TextPainter::locateAtCenter(&header, 0.0, top, BUTTON_LEFT);
+ header.paint(painter);
- painter->drawText(QPointF(topLeft, pos), header);
-
- pen.setColor(Qt::white);
- pen.setBrush(Qt::white);
- brush.setColor(Qt::white);
-
- painter->setBrush(brush);
- painter->setPen(pen);
- painter->setFont(fontB);
- painter->drawText(QPointF(bottomLeft, pos + height), text);
+ TextPainter::locateAtCenter(&city, 0.0, top + header.height(), BUTTON_LEFT);
+ city.paint(painter);
}
// AddCityErrorScreen
-AddCityErrorScreen::AddCityErrorScreen(const QString &city, QGraphicsItem *parent)
+AddCityErrorScreen::AddCityErrorScreen(QGraphicsItem *parent)
: QGraphicsPixmapItem(ADD_ERROR_BACKGROUND, parent)
, m_button(new PixmapButton(80.0, CLOSE_BUTTON_PIXMAP, this))
- , m_city(city)
{
m_button->setPos(BUTTON_LEFT, getCenterVerticalPos(m_button));
connect(m_button, SIGNAL(clicked()), this, SIGNAL(closed()));
}
+void AddCityErrorScreen::setCityName(const QString &name, ErrorType type)
+{
+ m_city = name;
+ m_type = type;
+ update();
+}
+
void AddCityErrorScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt, QWidget *widget)
{
Q_UNUSED(opt);
Q_UNUSED(widget);
-
QGraphicsPixmapItem::paint(painter, opt, widget);
+ if (m_type == NotFound)
+ paintNotFound(painter);
+ else
+ paintAlreadyInList(painter);
+}
- QBrush brush(Qt::white);
- QFont font;
- font.setFamily("Nokia Sans");
- font.setPixelSize(ADD_SCREEN_FONT_SIZE);
- font.setStyleStrategy(QFont::PreferAntialias);
-
- QColor color(255, 255, 255, 102);
-
- QPen pen;
-
- pen.setColor(color);
- pen.setBrush(color);
- pen.setJoinStyle(Qt::RoundJoin);
-
- brush.setColor(color);
-
- QFontMetrics m(font);
-
- int height = m.height(); // + m.descent();
- qreal pos = 10.0 + height;
-
- painter->setBrush(brush);
- painter->setPen(pen);
- painter->setFont(font);
-
- QFont fontB(font);
- fontB.setBold(true);
- QFontMetrics mB(fontB);
-
- static const QString header("The city");
- static const QString footer("wasn't found");
- QString text = mB.elidedText(m_city, Qt::ElideRight, Settings::scaleWidth(200.0));
- text = "\"" + text + "\"";
-
- int margin = 5;
- int headerWidth = m.width(header);
- int bWidth = mB.width(text) + margin;
- int footerWidth = m.width(footer);
+void AddCityErrorScreen::paintNotFound(QPainter *painter)
+{
+ TextPainter header(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 102), "The city");
+ TextPainter footer(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 102), "wasn't found");
+ TextPainter city(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 255), m_city);
+ city.setQuoted(true);
+ city.font().setBold(true);
+ city.setMaxWidth(BUTTON_LEFT - header.width() - 5.0);
- qreal topLeft = (BUTTON_LEFT - qreal(headerWidth + bWidth)) / 2;
- int bottomLeft = ((headerWidth + bWidth - footerWidth) >> 1) + topLeft;
+ qreal top = (boundingRect().height() - city.height() - footer.height()) / 2;
+ top += boundingRect().top();
- qDebug() << bWidth;
+ QList<TextPainter*> topLine;
+ topLine.append(&header);
+ topLine.append(&city);
- painter->drawText(QPointF(topLeft, pos), header);
- painter->drawText(QPointF(bottomLeft, pos + height), footer);
+ TextPainter::locateAtCenter(topLine, 0.0, top, BUTTON_LEFT);
+ header.paint(painter);
+ city.paint(painter);
- pen.setColor(Qt::white);
- pen.setBrush(Qt::white);
- brush.setColor(Qt::white);
+ TextPainter::locateAtCenter(&footer, 0.0, top + city.height(), BUTTON_LEFT);
+ footer.paint(painter);
+}
- painter->setBrush(brush);
- painter->setPen(pen);
- painter->setFont(fontB);
- painter->drawText(QPointF(topLeft + headerWidth + margin, pos), text);
+void AddCityErrorScreen::paintAlreadyInList(QPainter *painter)
+{
+ TextPainter header(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 102), "The city");
+ TextPainter header1(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 102), "is");
+ TextPainter footer(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 102), "already on your list");
+
+ TextPainter city(ADD_SCREEN_FONT_SIZE, QColor(255, 255, 255, 255), m_city);
+ city.setQuoted(true);
+ city.font().setBold(true);
+ city.setMaxWidth(BUTTON_LEFT - header.width() - header1.width() - 10.0);
+
+ qreal top = (boundingRect().height() - city.height() - footer.height()) / 2;
+ top += boundingRect().top();
+
+ QList<TextPainter*> topLine;
+ topLine.append(&header);
+ topLine.append(&city);
+ topLine.append(&header1);
+
+ TextPainter::locateAtCenter(topLine, 0.0, top, BUTTON_LEFT);
+ header.paint(painter);
+ city.paint(painter);
+ header1.paint(painter);
+
+ TextPainter::locateAtCenter(&footer, 0.0, top + city.height(), BUTTON_LEFT);
+ footer.paint(painter);
}
// AddCityTool
-AddCityTool::AddCityTool(QGraphicsItem *parent)
+AddCityTool::AddCityTool(const QList<ForecastData> &content, QGraphicsItem *parent)
: QGraphicsPixmapItem(ADD_BACKGROUND, parent)
- , m_screen(createFirstScreen())
+ , m_content(content)
+ , m_firstScreen(createFirstScreen())
+ , m_SearchScreen(createSearchScreen())
+ , m_ErrorScreen(createErrorScreen())
{
+ setCurrentScreen(m_firstScreen);
}
AddCityTool::~AddCityTool()
{
- m_screen->deleteLater();
}
void AddCityTool::mousePressEvent(QGraphicsSceneMouseEvent *event)
@@ -307,6 +267,12 @@ void AddCityTool::mousePressEvent(QGraphicsSceneMouseEvent *event)
Q_UNUSED(event);
}
+void AddCityTool::cancel()
+{
+ m_SearchScreen->cancel();
+ setCurrentScreen(m_firstScreen);
+}
+
AddCityFirstScreen *AddCityTool::createFirstScreen()
{
AddCityFirstScreen *result = new AddCityFirstScreen(boundingRect(), this);
@@ -315,47 +281,57 @@ AddCityFirstScreen *AddCityTool::createFirstScreen()
return result;
}
-AddCitySearchScreen *AddCityTool::createSearchScreen(const QString &city)
+AddCitySearchScreen *AddCityTool::createSearchScreen()
{
- AddCitySearchScreen *result = new AddCitySearchScreen(boundingRect(), city, this);
- connect(result, SIGNAL(forecastIsNotOk(ForecastData)),
- this, SLOT(forecastIsNotOk(ForecastData)));
- connect(result, SIGNAL(forecastIsOk(ForecastData)), this, SLOT(forecastIsOk(ForecastData)));
+ AddCitySearchScreen *result = new AddCitySearchScreen(boundingRect(), this);
+ connect(result, SIGNAL(forecastReceived(ForecastData)),
+ this, SLOT(forecastReceived(ForecastData)));
result->setPos(0.0, 0.0);
return result;
}
-AddCityErrorScreen *AddCityTool::createErrorScreen(const QString &city)
+AddCityErrorScreen *AddCityTool::createErrorScreen()
{
- AddCityErrorScreen *result = new AddCityErrorScreen(city, this);
+ AddCityErrorScreen *result = new AddCityErrorScreen(this);
connect(result, SIGNAL(closed()), this, SLOT(errorScreenClosed()));
result->setPos(0.0, 0.0);
return result;
}
-void AddCityTool::replaceScreen(QObject *screen)
+void AddCityTool::setCurrentScreen(QGraphicsItem *screen)
{
- m_screen->deleteLater();
- m_screen = screen;
+ m_firstScreen->clean();
+ m_firstScreen->setVisible(screen == m_firstScreen);
+ m_SearchScreen->setVisible(screen == m_SearchScreen);
+ m_ErrorScreen->setVisible(screen == m_ErrorScreen);
}
void AddCityTool::errorScreenClosed()
{
- replaceScreen(createFirstScreen());
+ setCurrentScreen(m_firstScreen);
}
-void AddCityTool::forecastIsOk(const ForecastData &forecast)
+void AddCityTool::forecastReceived(const ForecastData &forecast)
{
- emit newForecast(forecast);
- replaceScreen(createFirstScreen());
-}
-
-void AddCityTool::forecastIsNotOk(const ForecastData &forecast)
-{
- replaceScreen(createErrorScreen(forecast.key()));
+ if (forecast.error()) {
+ m_ErrorScreen->setCityName(forecast.key(), AddCityErrorScreen::NotFound);
+ setCurrentScreen(m_ErrorScreen);
+ }
+ else {
+ emit newForecast(forecast);
+ setCurrentScreen(m_firstScreen);
+ }
}
void AddCityTool::citySelected(const QString &city)
{
- replaceScreen(createSearchScreen(city));
+ QString text = city.toUpper();
+ for (int i = 0; i < m_content.count(); ++i)
+ if (m_content[i].key().toUpper() == text) {
+ m_ErrorScreen->setCityName(city, AddCityErrorScreen::AlreadyInList);
+ setCurrentScreen(m_ErrorScreen);
+ return;
+ }
+ m_SearchScreen->setCityName(city);
+ setCurrentScreen(m_SearchScreen);
}
diff --git a/weather/addcitytool.h b/weather/addcitytool.h
index 788bec7..b483d1a 100644
--- a/weather/addcitytool.h
+++ b/weather/addcitytool.h
@@ -10,6 +10,7 @@
#include <QFont>
#include <QGraphicsProxyWidget>
#include <QLineEdit>
+#include <QList>
#include "pixmapbutton.h"
#include "forecastprovider.h"
@@ -33,24 +34,18 @@ class AddCityFirstScreen : public QObject, public AddCityScreen
Q_OBJECT
public:
AddCityFirstScreen(const QRectF &boundingRect, QGraphicsItem *parent = 0);
+ void clean();
signals:
void citySelected(const QString &city);
-protected:
- QVariant itemChange(GraphicsItemChange change, const QVariant &value);
-
private:
QGraphicsPixmapItem * const m_textBackground;
PixmapButton * const m_button;
-// QGraphicsSimpleTextItem * const m_textItem;
QString m_text;
QGraphicsProxyWidget *m_proxy;
QLineEdit *m_lineEdit;
- void createInputElement();
-
-
private slots:
void buttonClick();
};
@@ -59,17 +54,20 @@ class AddCitySearchScreen : public QObject , public AddCityScreen
{
Q_OBJECT
public:
- AddCitySearchScreen(const QRectF &boundingRect, const QString &city, QGraphicsItem *parent = 0);
+ AddCitySearchScreen(const QRectF &boundingRect, QGraphicsItem *parent = 0);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
+ void setCityName(const QString &name);
+ void cancel() { reset(); }
signals:
- void forecastIsOk(const ForecastData &forecast);
- void forecastIsNotOk(const ForecastData &forecast);
+ void forecastReceived(const ForecastData &forecast);
private:
- const QString m_city;
+ QString m_city;
Loading *m_loading;
+ void reset();
+
private slots:
void forecastResponse(const ForecastData &forecast);
};
@@ -78,31 +76,46 @@ class AddCityErrorScreen : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
- AddCityErrorScreen(const QString &city, QGraphicsItem *parent = 0);
+ enum ErrorType
+ {
+ NotFound,
+ AlreadyInList
+ };
+ AddCityErrorScreen(QGraphicsItem *parent = 0);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
+ void setCityName(const QString &name, ErrorType type);
signals:
void closed();
private:
PixmapButton * const m_button;
- const QString m_city;
+ QString m_city;
+ ErrorType m_type;
+
+ void paintNotFound(QPainter *painter);
+ void paintAlreadyInList(QPainter *painter);
};
class AddCityTool : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
- AddCityTool(QGraphicsItem *parent = 0);
+ AddCityTool(const QList<ForecastData> &content, QGraphicsItem *parent = 0);
~AddCityTool();
+ void cancel();
private:
- QObject *m_screen;
+ const QList<ForecastData> &m_content;
+ AddCityFirstScreen * const m_firstScreen;
+ AddCitySearchScreen * const m_SearchScreen;
+ AddCityErrorScreen * const m_ErrorScreen;
AddCityFirstScreen *createFirstScreen();
- AddCitySearchScreen *createSearchScreen(const QString &city);
- AddCityErrorScreen *createErrorScreen(const QString &city);
- void replaceScreen(QObject *screen);
+ AddCitySearchScreen *createSearchScreen();
+ AddCityErrorScreen *createErrorScreen();
+
+ void setCurrentScreen(QGraphicsItem *screen);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
@@ -112,8 +125,7 @@ signals:
private slots:
void errorScreenClosed();
- void forecastIsOk(const ForecastData &forecast);
- void forecastIsNotOk(const ForecastData &forecast);
+ void forecastReceived(const ForecastData &forecast);
void citySelected(const QString &city);
};
diff --git a/weather/citylist.cpp b/weather/citylist.cpp
index f0377d4..e6bc361 100644
--- a/weather/citylist.cpp
+++ b/weather/citylist.cpp
@@ -5,7 +5,6 @@
#include <cmath>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
-#include <QDebug>
#define LIST_TOP_PIXMAP (PixmapLoader::getPic("list_top"))
@@ -163,6 +162,29 @@ void CityListScrollBox::timerEvent(QTimerEvent *event)
QObject::timerEvent(event);
}
+// CityListText
+
+CityListText::CityListText(const QString &text, QGraphicsItem *parent)
+ : QGraphicsItem(parent)
+ , m_painter(CITY_NAME_FONT_SIZE, Qt::white, text)
+{
+ setCacheMode(ItemCoordinateCache);
+ m_painter.setPos(0.0, 0.0);
+ m_painter.setMaxWidth(ITEM_BUTTON_LEFT - TEXT_LEFT);
+}
+
+QRectF CityListText::boundingRect() const
+{
+ return QRectF(0.0, 0.0, m_painter.width(), m_painter.height());
+}
+
+void CityListText::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt, QWidget *widget)
+{
+ Q_UNUSED(opt);
+ Q_UNUSED(widget);
+ m_painter.paint(painter);
+}
+
// CityListItem
CityListItem::CityListItem(const ForecastData &data, CityContentList *list)
@@ -173,16 +195,14 @@ CityListItem::CityListItem(const ForecastData &data, CityContentList *list)
, m_background(new QGraphicsPixmapItem(ITEM_BACKGROUND, this))
, m_check(new QGraphicsPixmapItem(ITEM_CHECK_PIXMAP, m_background))
, m_delete(new PixmapButton(80.0, ITEM_BUTTON_PIXMAP, m_background))
- , m_text(createTextItem())
+ , m_text(new CityListText(m_data.cityName(), this))
{
m_delete->setPos(ITEM_BUTTON_LEFT, getCenterVerticalPos(m_delete));
m_check->setPos(ITEM_CHECK_LEFT, getCenterVerticalPos(m_check));
- m_text->setPos(TEXT_LEFT, Settings::scaleHeight(5.0));
+ m_text->setPos(TEXT_LEFT, getCenterVerticalPos(m_text));
m_text->setCacheMode(ItemCoordinateCache);
connect(m_delete, SIGNAL(clicked()), this, SLOT(removeFromList()));
-
- hide();
}
void CityListItem::removeFromList()
@@ -255,11 +275,21 @@ QAbstractAnimation *CityListItem::getFadeAnimation(bool hide, int msecs)
// CityContentList
-CityContentList::CityContentList(QObject *holder)
+CityContentList::CityContentList(QList<ForecastData> &contentList, QObject *holder)
: ContentList(0)
, m_holder(holder)
+ , m_contentList(contentList)
{
+ QList<ContentListItem*> items;
+ foreach (const ForecastData &data, m_contentList)
+ items.append(new CityListItem(data, this));
+ appendItems(items);
+}
+void CityContentList::addForecast(const ForecastData &data)
+{
+ m_contentList.append(data);
+ addItem(new CityListItem(data, this));
}
QAbstractAnimation *CityContentList::getInsertAnimation(int idx, qreal height)
@@ -277,6 +307,12 @@ QAbstractAnimation *CityContentList::getInsertAnimation(int idx, qreal height)
list.append(getItemMoveAnimation(i, height));
}
+ if (parentItem() && itemCount() >= maxVisibleItems) {
+ qreal top = parentItem()->boundingRect().height() - boundingRect().height() - height;
+ list.append(getMoveAnimation(this, top - pos().y()));
+
+ }
+
return createCompoundAnimation(list);
}
@@ -294,6 +330,23 @@ QAbstractAnimation *CityContentList::getRemoveAnimation(int idx)
for (int i = idx + 1; i < itemCount(); ++i)
list.append(getItemMoveAnimation(i, offset));
+ qreal newHeight = boundingRect().height() - getItem(idx)->contentHeight();
+
+ if (parentItem()) {
+ if (newHeight < parentItem()->boundingRect().height()) {
+ if (pos().y() != 0)
+ list.append(getMoveAnimation(this, -pos().y()));
+ } else {
+ if (pos().y() + newHeight < parentItem()->boundingRect().bottom()) {
+ qreal offset = parentItem()->boundingRect().bottom() - pos().y() - newHeight;
+ list.append(getMoveAnimation(this, offset));
+ }
+ }
+ }
+
+ m_contentList.removeAt(idx);
+
+
return createCompoundAnimation(list);
}
@@ -331,18 +384,19 @@ QAbstractAnimation *CityContentList::getItemMoveAnimation(int idx, qreal offset)
// CityList
-CityList::CityList(QGraphicsItem *parent)
+CityList::CityList(QList<ForecastData> &contentList, QGraphicsItem *parent)
: QGraphicsItem(parent)
, m_itemHeight(ITEM_BACKGROUND.height())
, m_topHeight(LIST_TOP_PIXMAP.height())
, m_boundingRect(getBoundingRect())
, m_paintRect(getPaintRect())
, m_top(new QGraphicsPixmapItem(LIST_TOP_PIXMAP, this))
- , m_list(new CityContentList(this))
+ , m_list(new CityContentList(contentList, this))
, m_scrollBox(new CityListScrollBox(m_list, this))
{
m_top->setPos(-m_top->boundingRect().left(), -m_top->boundingRect().top());
m_scrollBox->setRect(m_paintRect);
+ m_list->setWidth(m_paintRect.width());
}
qreal CityList::initialTop() const
diff --git a/weather/citylist.h b/weather/citylist.h
index 6e9a54f..1bcbcbf 100644
--- a/weather/citylist.h
+++ b/weather/citylist.h
@@ -10,6 +10,7 @@
#include "forecastdata.h"
#include "pixmapbutton.h"
#include "scrollbar.h"
+#include "painttextitem.h"
// CityListScrollBox
@@ -42,6 +43,20 @@ private:
void contentPositionUpdated();
};
+// CityListText
+
+class CityListText : public QGraphicsItem
+{
+public:
+ CityListText(const QString &text, QGraphicsItem *parent = 0);
+ QRectF boundingRect() const;
+ void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
+
+private:
+ TextPainter m_painter;
+
+};
+
// CityListItem
class CityContentList;
@@ -68,7 +83,7 @@ private:
QGraphicsPixmapItem * const m_background;
QGraphicsPixmapItem * const m_check;
PixmapButton * const m_delete;
- QGraphicsSimpleTextItem * const m_text;
+ CityListText * const m_text;
QAbstractAnimation *getFadeAnimation(bool hide, int msecs);
@@ -84,10 +99,11 @@ private slots:
class CityContentList : public ContentList
{
+ Q_OBJECT
+ Q_PROPERTY(qreal top READ getTop WRITE setTop);
public:
- CityContentList(QObject *holder);
-
- void addForecast(const ForecastData &data) { addItem(new CityListItem(data, this)); }
+ CityContentList(QList<ForecastData> &contentList, QObject *holder);
+ void addForecast(const ForecastData &data);
protected:
QAbstractAnimation *getInsertAnimation(int idx, qreal height);
@@ -95,9 +111,14 @@ protected:
private:
QObject * const m_holder;
+ QList<ForecastData> &m_contentList;
+
QAbstractAnimation *createCompoundAnimation(QList<QAbstractAnimation*> list);
QAbstractAnimation *getItemMoveAnimation(int idx, qreal offset);
QAbstractAnimation *getMoveAnimation(QObject *object, qreal offset);
+
+ qreal getTop() { return pos().y(); }
+ void setTop(qreal top) { setPos(pos().x(), top); }
};
// CityList
@@ -108,7 +129,7 @@ class CityList : public QObject, public QGraphicsItem
Q_PROPERTY(qreal top READ getTop WRITE setTop);
Q_INTERFACES(QGraphicsItem);
public:
- CityList(QGraphicsItem *parent = 0);
+ CityList(QList<ForecastData> &contentList, QGraphicsItem *parent = 0);
QRectF boundingRect () const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
diff --git a/weather/citymanager.cpp b/weather/citymanager.cpp
index 6e5e67a..c711cd5 100644
--- a/weather/citymanager.cpp
+++ b/weather/citymanager.cpp
@@ -33,27 +33,6 @@
#include "settings.h"
#include <QGraphicsSceneMouseEvent>
-#include <QDebug>
-
-#define ITEM_BACKGROUND (Settings::getScaledPic("list_item_bg"))
-#define SELECTED_ITEM_BACKGROUND (Settings::getScaledPic("list_item_selected_bg"))
-
-#define ITEM_BUTTON_PIXMAP (Settings::getScaledPic("button_list_delete"))
-#define ITEM_BUTTON_LEFT (Settings::scaleWidth(406.0))
-
-#define ITEM_CHECK_PIXMAP (Settings::getScaledPic("list_check"))
-#define ITEM_CHECK_LEFT (Settings::scaleWidth(20.0))
-
-static inline qreal getCenterVerticalPos(QGraphicsItem *parent, QGraphicsItem *item)
-{
- const qreal top = (parent->boundingRect().height() - item->boundingRect().height()) / 2;
- return top - parent->boundingRect().top() - item->boundingRect().top();
-}
-
-static inline qreal getCenterVerticalPos(QGraphicsItem *item)
-{
- return getCenterVerticalPos(item->parentItem(), item);
-}
static inline qreal getBottomVerticalPos(qreal &bottom, QGraphicsItem *item)
{
@@ -62,77 +41,31 @@ static inline qreal getBottomVerticalPos(qreal &bottom, QGraphicsItem *item)
return result;
}
-// CityListElement
-
-CityListElement::CityListElement(bool checked, QGraphicsItem *parent)
- : QGraphicsPixmapItem(ITEM_BACKGROUND, parent)
- , m_check(checked ? new QGraphicsPixmapItem(ITEM_CHECK_PIXMAP, this) : 0)
- , m_button(new QGraphicsPixmapItem(ITEM_BUTTON_PIXMAP, this))
-{
- m_button->setPos(ITEM_BUTTON_LEFT, getCenterVerticalPos(m_button));
- if (m_check)
- m_check->setPos(ITEM_CHECK_LEFT, getCenterVerticalPos(m_check));
-}
-
// CityManagerContent
-struct CityInfo
-{
- const char * name;
- const int lower;
- const int temp;
- const int upper;
- const bool night;
-};
-
-static const CityInfo cityNames[Forecast::UnknownForecast] = {
- {"Araxa", -7, -4, -1, true}, // MostlyCloudy
- {"Belo Horizonte", 18, 22, 27, false}, // Cloudy
- {"Recife", 25, 31, 34, true}, // MostlySunny
- {"Ipojuca", 22, 29, 36, false}, // PartlyCloudy
- {"Fortaleza", 31, 34, 36, true}, // Sunny
- {"Sao Paulo", -1, 10, 12, false}, // Flurries
- {"Uberlandia", -26, -23, -20, false}, // Fog
- {"Governador Valadares", 3, 17, 18, true}, // Haze
- {"Brasilia", 22, 25, 30, false}, // Sand
- {"Florianopolis", 0, 8, 10, true}, // Dust
- {"Gravata", -10, -2, 3, false}, // Icy
- {"Maceio", 22, 35, 38, true}, // Sleet
- {"Porto Alegre", -8, -6, -4, true}, // ChanceOfSleet
- {"Batatais", -20, -12, -10, false}, // Snow
- {"Ribeirao Preto", -20, -17, 0, true}, // ChanceOfSnow
- {"Campinas", 1, 4, 10, false}, // Mist
- {"Campo belo", 12, 19, 21, true}, // Rain
- {"Sertaozinho", 10, 15, 17, true}, // ChanceOfRain
- {"Pirassununga", -15, -12, 2, false}, // Storm
- {"Goiania", 7, 13, 18, true}, // ChanceOfStorm
- {"Manaus", 35, 41, 44, false}, // Thunderstorm
- {"Belem", -14, -13, -5, true} // ChanceOfThunderstorm
-};
-
-CityManagerContent::CityManagerContent(QGraphicsItem *parent)
+CityManagerContent::CityManagerContent(QList<ForecastData> &contentList, QGraphicsItem *parent)
: QGraphicsItem(parent)
, m_boundingRect(QPointF(0.0, 0.0), Settings::windowSize())
{
setFlag(ItemHasNoContents);
qreal bottom = m_boundingRect.bottom();
- m_addTool = new AddCityTool(this);
+ m_addTool = new AddCityTool(contentList, this);
m_addTool->setPos(0.0, getBottomVerticalPos(bottom, m_addTool));
m_addTool->setZValue(10.0);
- m_list = new CityList(this);
+ m_list = new CityList(contentList, this);
m_list->setPos(0.0, getBottomVerticalPos(bottom, m_list) + m_list->initialTop());
m_list->setZValue(0.0);
- connect(m_addTool, SIGNAL(newForecast(ForecastData)), m_list, SLOT(addForecast(ForecastData)));
-
+ connect(m_addTool, SIGNAL(newForecast(ForecastData)),
+ this, SLOT(forecastSelected(ForecastData)));
- for (int i = 0; i < Forecast::UnknownForecast; ++i)
- m_list->addForecast(ForecastData(false, Forecast::ForecastType(i),
- i % 2, cityNames[i].name,cityNames[i].name,
- cityNames[i].lower, cityNames[i].temp, cityNames[i].upper));
+}
+void CityManagerContent::forecastSelected(ForecastData data)
+{
+ m_list->addForecast(data);
}
QRectF CityManagerContent::boundingRect () const
@@ -150,19 +83,15 @@ void CityManagerContent::paint(QPainter *painter,
// CityManager
-CityManager::CityManager(QGraphicsItem *parent)
+CityManager::CityManager(QList<ForecastData> contentList, QGraphicsItem *parent)
: QGraphicsItem(parent)
, m_boundingRect(QPointF(0.0, 0.0), Settings::windowSize())
+ , m_contentList(contentList)
{
- m_content = new CityManagerContent(this);
+ m_content = new CityManagerContent(m_contentList, this);
m_content->setPos(0.0, 0.0);
}
-void CityManager::newCitySelected(const QString &city)
-{
- qDebug() << "new city:" << city;
-}
-
QRectF CityManager::boundingRect () const
{
return m_boundingRect;
@@ -177,7 +106,6 @@ void CityManager::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt,
void CityManager::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
- qDebug() << "manager clicked";
event->accept();
emit terminated();
}
diff --git a/weather/citymanager.h b/weather/citymanager.h
index 1e926b4..108012f 100644
--- a/weather/citymanager.h
+++ b/weather/citymanager.h
@@ -38,22 +38,13 @@
#include "addcitytool.h"
#include "citylist.h"
-class CityListElement : public QGraphicsPixmapItem
-{
-public:
- CityListElement(bool checked, QGraphicsItem *parent = 0);
-
-private:
- QGraphicsPixmapItem *m_check;
- QGraphicsPixmapItem *m_button;
-};
-
class CityManagerContent : public QObject, public QGraphicsItem
{
Q_OBJECT
Q_INTERFACES(QGraphicsItem)
+ Q_PROPERTY(qreal top READ getTop WRITE setTop);
public:
- CityManagerContent(QGraphicsItem *parent = 0);
+ CityManagerContent(QList<ForecastData> &contentList, QGraphicsItem *parent = 0);
QRectF boundingRect () const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
@@ -62,6 +53,12 @@ private:
const QRectF m_boundingRect;
AddCityTool *m_addTool;
CityList *m_list;
+
+ qreal getTop() { return pos().y(); }
+ void setTop(qreal top) { setPos(pos().x(), top); }
+
+private slots:
+ void forecastSelected(ForecastData data);
};
class CityManager : public QObject, public QGraphicsItem
@@ -69,7 +66,7 @@ class CityManager : public QObject, public QGraphicsItem
Q_OBJECT
Q_INTERFACES(QGraphicsItem)
public:
- CityManager(QGraphicsItem *parent = 0);
+ CityManager(QList<ForecastData> contentList, QGraphicsItem *parent = 0);
QRectF boundingRect () const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
@@ -83,9 +80,7 @@ signals:
private:
const QRectF m_boundingRect;
CityManagerContent *m_content;
-
-private slots:
- void newCitySelected(const QString &city);
+ QList<ForecastData> m_contentList;
};
#endif // CITYMANAGER_H
diff --git a/weather/mainview.cpp b/weather/mainview.cpp
index 8191663..906460f 100644
--- a/weather/mainview.cpp
+++ b/weather/mainview.cpp
@@ -94,6 +94,7 @@ void MainView::bootEnd()
m_loading->deleteLater();
m_loading = 0;
showCarroussel();
+ createCityManager();
m_bootManager->deleteLater();
m_bootManager = 0;
}
@@ -110,25 +111,31 @@ void MainView::showCarroussel()
m_scene.addItem(m_carroussel);
}
+void MainView::createCityManager()
+{
+ m_manager = new CityManager(m_bootManager->data());
+ m_manager->setPos(0.0, 0.0);
+ m_manager->setZValue(1.0);
+ m_manager->hide();
+
+}
+
void MainView::cityNameClicked()
{
- if (m_manager)
+ if (!m_manager)
return;
- qDebug() << "city name clicked";
m_carroussel->setActive(false);
- m_manager = new CityManager();
- m_manager->setPos(0.0, 0.0);
- m_manager->setZValue(1.0);
- m_scene.addItem(m_manager);
connect(m_manager, SIGNAL(terminated()), this, SLOT(closeCityManager()));
+ m_scene.addItem(m_manager);
+ m_manager->show();
}
void MainView::closeCityManager()
{
- if (m_manager) {
- qDebug() << "close manager";
- m_manager->deleteLater();
- m_manager = 0;
- m_carroussel->setActive(true);
- }
+ if (!m_manager)
+ return;
+ m_manager->hide();
+ m_scene.removeItem(m_manager);
+ disconnect(m_manager, SIGNAL(terminated()), this, SLOT(closeCityManager()));
+ m_carroussel->setActive(true);
}
diff --git a/weather/mainview.h b/weather/mainview.h
index 67459a3..9c80b67 100644
--- a/weather/mainview.h
+++ b/weather/mainview.h
@@ -70,6 +70,7 @@ private:
CityManager *m_manager;
void showCarroussel();
+ void createCityManager();
};
#endif // MAINVIEW_H