Added missing item update on content change
[GPXSee.git] / src / infoitem.cpp
blobcd10df3a4d511fd3e258e57f9b64e81946dfcd6e
1 #include <QFont>
2 #include <QPainter>
3 #include "config.h"
4 #include "infoitem.h"
6 #define PADDING 10
8 InfoItem::InfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
10 #ifndef Q_OS_MAC
11 setCacheMode(QGraphicsItem::DeviceCoordinateCache);
12 #endif // Q_OS_MAC
15 void InfoItem::updateBoundingRect()
17 QFont font;
18 font.setPixelSize(FONT_SIZE);
19 font.setFamily(FONT_FAMILY);
20 QFontMetrics fm(font);
21 QList<KV>::const_iterator i;
22 qreal width = 0;
24 for (i = _list.constBegin(); i != _list.constEnd(); i++) {
25 width += fm.width(i->key + ": ");
26 width += fm.width(i->value) + ((i == _list.end() - 1) ? 0 : PADDING);
29 _boundingRect = QRectF(0, 0, width, _list.isEmpty() ? 0 : fm.height());
32 void InfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
33 QWidget *widget)
35 Q_UNUSED(option);
36 Q_UNUSED(widget);
37 QFont font;
38 font.setPixelSize(FONT_SIZE);
39 font.setFamily(FONT_FAMILY);
40 painter->setFont(font);
41 QFontMetrics fm(font);
42 QList<KV>::const_iterator i;
43 int width = 0;
46 painter->setRenderHint(QPainter::Antialiasing, false);
48 for (i = _list.constBegin(); i != _list.constEnd(); i++) {
49 painter->drawText(width, fm.height() - fm.descent(), i->key + ": ");
50 width += fm.width(i->key + ": ");
51 painter->drawText(width, fm.height() - fm.descent(), i->value);
52 width += fm.width(i->value) + ((i == _list.end() - 1) ? 0 : PADDING);
53 if (i != _list.end() - 1) {
54 painter->save();
55 painter->setPen(Qt::gray);
56 painter->drawLine(width - PADDING/2, fm.descent(),
57 width - PADDING/2, fm.height() - fm.descent());
58 painter->restore();
63 painter->setPen(Qt::red);
64 painter->drawRect(boundingRect());
68 void InfoItem::insert(const QString &key, const QString &value)
70 KV kv(key, value);
71 int i;
73 prepareGeometryChange();
75 if ((i = _list.indexOf(kv)) < 0)
76 _list.append(kv);
77 else
78 _list[i] = kv;
80 updateBoundingRect();
81 update();
84 void InfoItem::clear()
86 prepareGeometryChange();
87 _list.clear();
88 updateBoundingRect();