1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/infoitem.cpp

90 lines
1.9 KiB
C++
Raw Normal View History

2015-10-12 01:12:12 +02:00
#include <QFont>
#include <QPainter>
2015-10-17 01:33:02 +02:00
#include "config.h"
2015-10-12 01:12:12 +02:00
#include "infoitem.h"
#define PADDING 10
2015-10-17 01:33:02 +02:00
InfoItem::InfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
2015-10-12 01:12:12 +02:00
{
#ifndef Q_OS_MAC
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
#endif // Q_OS_MAC
2015-10-12 01:12:12 +02:00
}
void InfoItem::updateBoundingRect()
2015-10-12 01:12:12 +02:00
{
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
2015-10-13 00:27:49 +02:00
QList<KV>::const_iterator i;
qreal width = 0;
2015-10-12 01:12:12 +02:00
2015-10-13 00:27:49 +02:00
for (i = _list.constBegin(); i != _list.constEnd(); i++) {
width += fm.width(i->key + ": ");
width += fm.width(i->value) + ((i == _list.end() - 1) ? 0 : PADDING);
2015-10-12 01:12:12 +02:00
}
_boundingRect = QRectF(0, 0, width, _list.isEmpty() ? 0 : fm.height());
2015-10-12 01:12:12 +02:00
}
void InfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
painter->setFont(font);
QFontMetrics fm(font);
2015-10-13 00:27:49 +02:00
QList<KV>::const_iterator i;
2015-10-12 01:12:12 +02:00
int width = 0;
2016-10-17 23:14:07 +02:00
painter->setRenderHint(QPainter::Antialiasing, false);
2015-10-13 00:27:49 +02:00
for (i = _list.constBegin(); i != _list.constEnd(); i++) {
painter->drawText(width, fm.height() - fm.descent(), i->key + ": ");
width += fm.width(i->key + ": ");
painter->drawText(width, fm.height() - fm.descent(), i->value);
width += fm.width(i->value) + ((i == _list.end() - 1) ? 0 : PADDING);
if (i != _list.end() - 1) {
2015-10-12 01:12:12 +02:00
painter->save();
painter->setPen(Qt::gray);
painter->drawLine(width - PADDING/2, fm.descent(),
width - PADDING/2, fm.height() - fm.descent());
painter->restore();
}
}
/*
painter->setPen(Qt::red);
painter->drawRect(boundingRect());
*/
}
void InfoItem::insert(const QString &key, const QString &value)
{
2015-10-13 00:27:49 +02:00
KV kv(key, value);
int i;
2016-04-01 21:20:23 +02:00
prepareGeometryChange();
2015-10-13 00:27:49 +02:00
if ((i = _list.indexOf(kv)) < 0)
_list.append(kv);
else
_list[i] = kv;
2015-10-17 01:33:02 +02:00
updateBoundingRect();
update();
}
void InfoItem::clear()
{
2016-04-01 21:20:23 +02:00
prepareGeometryChange();
_list.clear();
updateBoundingRect();
2015-10-12 01:12:12 +02:00
}