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

88 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
{
2018-05-18 01:38:33 +02:00
_font.setPixelSize(FONT_SIZE);
_font.setFamily(FONT_FAMILY);
#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
{
2018-05-18 01:38:33 +02:00
QFontMetrics fm(_font);
qreal width = 0;
2015-10-12 01:12:12 +02:00
2018-05-18 01:38:33 +02:00
for (QList<KV>::const_iterator i = _list.constBegin();
i != _list.constEnd(); i++) {
2015-10-13 00:27:49 +02:00
width += fm.width(i->key + ": ");
2018-05-18 01:38:33 +02:00
width += fm.width(i->value) + ((i == _list.constEnd() - 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);
2018-05-18 01:38:33 +02:00
QFontMetrics fm(_font);
2015-10-12 01:12:12 +02:00
int width = 0;
2018-05-18 01:38:33 +02:00
painter->setFont(_font);
2016-10-17 23:14:07 +02:00
painter->setRenderHint(QPainter::Antialiasing, false);
2018-05-18 01:38:33 +02:00
for (QList<KV>::const_iterator i = _list.constBegin();
i != _list.constEnd(); i++) {
2015-10-13 00:27:49 +02:00
painter->drawText(width, fm.height() - fm.descent(), i->key + ": ");
width += fm.width(i->key + ": ");
painter->drawText(width, fm.height() - fm.descent(), i->value);
2018-05-18 01:38:33 +02:00
width += fm.width(i->value) + ((i == _list.constEnd() - 1)
? 0 : PADDING);
2018-03-29 00:29:08 +02:00
if (i != _list.constEnd() - 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
}