1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-07 14:54:00 +01:00
GPXSee/src/GUI/infoitem.cpp

89 lines
1.9 KiB
C++
Raw Normal View History

2015-10-12 01:12:12 +02:00
#include <QFont>
#include <QPainter>
#include "font.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);
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
for (QList<KV<QString, QString> >::const_iterator i = _list.constBegin();
2018-05-18 01:38:33 +02:00
i != _list.constEnd(); i++) {
2021-01-01 12:51:39 +01:00
width += fm.horizontalAdvance(i->key() + ": " + i->value());
if (i != _list.constEnd() - 1)
width += 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);
for (QList<KV<QString, QString> >::const_iterator i = _list.constBegin();
2018-05-18 01:38:33 +02:00
i != _list.constEnd(); i++) {
2021-01-01 12:51:39 +01:00
QString text(i->key() + ": " + i->value());
painter->drawText(width, fm.height() - fm.descent(), text);
width += fm.horizontalAdvance(text);
2018-03-29 00:29:08 +02:00
if (i != _list.constEnd() - 1) {
2021-01-01 12:51:39 +01:00
width += PADDING;
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());
2015-10-12 01:12:12 +02:00
}
int InfoItem::indexOf(const QString &key) const
{
for (int i = 0; i < _list.size(); i++)
if (_list.at(i).key() == key)
return i;
return -1;
}
2015-10-12 01:12:12 +02:00
void InfoItem::insert(const QString &key, const QString &value)
{
2015-10-13 00:27:49 +02:00
int i;
2016-04-01 21:20:23 +02:00
prepareGeometryChange();
if ((i = indexOf(key)) < 0)
_list.append(KV<QString, QString>(key, value));
2015-10-13 00:27:49 +02:00
else
_list[i] = KV<QString, QString>(key, value);
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
}