2015-10-12 01:12:12 +02:00
|
|
|
#include <QFont>
|
|
|
|
#include <QPainter>
|
2018-11-02 20:01:19 +01:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2016-02-02 20:12:56 +01:00
|
|
|
void InfoItem::updateBoundingRect()
|
2015-10-12 01:12:12 +02:00
|
|
|
{
|
2018-05-18 01:38:33 +02:00
|
|
|
QFontMetrics fm(_font);
|
2016-02-02 20:12:56 +01:00
|
|
|
qreal width = 0;
|
2015-10-12 01:12:12 +02:00
|
|
|
|
2019-08-01 08:36:58 +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
|
|
|
}
|
|
|
|
|
2016-02-02 20:12:56 +01: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);
|
|
|
|
|
2019-08-01 08:36:58 +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
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 14:38:52 +01:00
|
|
|
//painter->setPen(Qt::red);
|
|
|
|
//painter->drawRect(boundingRect());
|
2015-10-12 01:12:12 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 08:57:42 +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();
|
|
|
|
|
2021-08-04 08:57:42 +02:00
|
|
|
if ((i = indexOf(key)) < 0)
|
|
|
|
_list.append(KV<QString, QString>(key, value));
|
2015-10-13 00:27:49 +02:00
|
|
|
else
|
2021-08-04 08:57:42 +02:00
|
|
|
_list[i] = KV<QString, QString>(key, value);
|
2015-10-17 01:33:02 +02:00
|
|
|
|
2016-02-02 20:12:56 +01:00
|
|
|
updateBoundingRect();
|
2017-01-24 18:11:08 +01:00
|
|
|
update();
|
2016-02-02 20:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void InfoItem::clear()
|
|
|
|
{
|
2016-04-01 21:20:23 +02:00
|
|
|
prepareGeometryChange();
|
2016-02-02 20:12:56 +01:00
|
|
|
_list.clear();
|
|
|
|
updateBoundingRect();
|
2015-10-12 01:12:12 +02:00
|
|
|
}
|