diff options
author | Luiz Agostini <luiz.agostini@openbossa.org> | 2009-11-04 22:49:54 -0300 |
---|---|---|
committer | Luiz Agostini <luiz.agostini@openbossa.org> | 2009-11-05 00:42:09 -0300 |
commit | 74f78d0bf40ae9951d69c4939185bca231a5d464 (patch) | |
tree | 19e15c07a89bdf2f17c5d7f09d0ae60dc5be2d73 /weather | |
parent | 3073be2326fd9be80e22ef1e246652545c4a6eeb (diff) |
Weather: Text painting object.
Signed-off-by: Luiz Agostini <luiz.agostini@openbossa.org>
Diffstat (limited to 'weather')
-rw-r--r-- | weather/painttextitem.cpp | 67 | ||||
-rw-r--r-- | weather/painttextitem.h | 49 | ||||
-rw-r--r-- | weather/weather.pro | 6 |
3 files changed, 120 insertions, 2 deletions
diff --git a/weather/painttextitem.cpp b/weather/painttextitem.cpp new file mode 100644 index 0000000..7356815 --- /dev/null +++ b/weather/painttextitem.cpp @@ -0,0 +1,67 @@ +#include "painttextitem.h" +#include <QPainter> +#include <QDebug> + +TextPainter::TextPainter(int fontSize, QColor color, const QString &text) + : m_pen(color) + , m_brush(color) + , m_text(text) + , m_quoted(false) + , m_maxWidth(-1) +{ + m_font.setFamily("Nokia Sans"); + m_font.setPixelSize(fontSize); + m_font.setStyleStrategy(QFont::PreferAntialias); + m_pen.setJoinStyle(Qt::RoundJoin); +} + +int TextPainter::width() const +{ + QFontMetrics m(m_font); + int result = 0; + if (m_quoted) + result += 2 * m.width('"'); + QString text = m_maxWidth > 0 ? m.elidedText(m_text, Qt::ElideRight, m_maxWidth - result) + : m_text; + return result + m.width(text); +} + +void TextPainter::paint(QPainter *painter) +{ + QFontMetrics m(m_font); + QString text; + if (m_quoted) { + int quote = 2 * m.width('"'); + text = m_maxWidth > 0 ? m.elidedText(m_text, Qt::ElideRight, m_maxWidth - quote) : m_text; + text = '"' + text + '"'; + } else + text = m_maxWidth > 0 ? m.elidedText(m_text, Qt::ElideRight, m_maxWidth) : m_text; + painter->setFont(m_font); + painter->setPen(m_pen); + painter->setBrush(m_brush); + + painter->drawText(QPointF(m_pos.x(), m_pos.y() + m.ascent()), text); +} + +void TextPainter::locateAtCenter(TextPainter *item, qreal left, qreal top, qreal width) +{ + item->setPos(left + (width - item->width()) / 2, top); +} + +void TextPainter::locateAtCenter(QList<TextPainter*> items, qreal left, qreal top, qreal width) +{ + static const int margin = 5; + + if (items.isEmpty()) + return; + + int itemsWidth = (items.count() - 1) * margin; + for (int i = 0; i < items.count(); ++i) + itemsWidth += items[i]->width(); + + left += (width - itemsWidth) / 2; + for (int i = 0; i < items.count(); ++i) { + items[i]->setPos(left, top); + left += items[i]->width() + margin; + } +} diff --git a/weather/painttextitem.h b/weather/painttextitem.h new file mode 100644 index 0000000..9811226 --- /dev/null +++ b/weather/painttextitem.h @@ -0,0 +1,49 @@ +#ifndef PAINTTEXTITEM_H +#define PAINTTEXTITEM_H + +#include <QFont> +#include <QPen> +#include <QBrush> +#include <QFontMetrics> +#include <QGraphicsItem> + +class TextPainter +{ +public: + TextPainter(int fontSize, QColor color, const QString &text); + QFont &font() { return m_font; } + QPen &pen() { return m_pen; } + QBrush &brush() { return m_brush; } + + const QString text() const { return m_text; } + void setText(const QString &text) { m_text = text; } + + QPointF pos() const { return m_pos; } + void setPos(QPointF pos) { m_pos = pos; } + void setPos(qreal x, qreal y) { setPos(QPointF(x, y)); } + + bool quoted() const { return m_quoted; } + void setQuoted(bool quoted) { m_quoted = quoted; } + + int maxWidth() const { return m_maxWidth; } + void setMaxWidth(int width) { m_maxWidth = width; } + + int width() const; + int height() const { return QFontMetrics(m_font).height(); } + + void paint(QPainter *painter); + + static void locateAtCenter(QList<TextPainter*> items, qreal left, qreal top, qreal width); + static void locateAtCenter(TextPainter *item, qreal left, qreal top, qreal width); + +private: + QFont m_font; + QPen m_pen; + QBrush m_brush; + QString m_text; + QPointF m_pos; + bool m_quoted; + int m_maxWidth; +}; + +#endif // PAINTTEXTITEM_H diff --git a/weather/weather.pro b/weather/weather.pro index 1af84b2..bbbdddf 100644 --- a/weather/weather.pro +++ b/weather/weather.pro @@ -49,7 +49,8 @@ HEADERS += mainview.h \ contentlist.h \ citylist.h \ citymanager.h \ - addcitytool.h + addcitytool.h \ + painttextitem.h SOURCES += mainview.cpp \ main.cpp \ settings.cpp \ @@ -71,4 +72,5 @@ SOURCES += mainview.cpp \ contentlist.cpp \ citylist.cpp \ citymanager.cpp \ - addcitytool.cpp + addcitytool.cpp \ + painttextitem.cpp |