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

91 lines
2.2 KiB
C++
Raw Normal View History

2015-10-17 01:33:02 +02:00
#include <QPainter>
#include "config.h"
#include "sliderinfoitem.h"
#define SIZE 5
SliderInfoItem::SliderInfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{
_side = Right;
2015-10-17 01:33:02 +02:00
}
void SliderInfoItem::updateBoundingRect()
{
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
2017-09-29 11:43:09 +02:00
qreal width = qMax(fm.width(_x), fm.width(_y));
qreal height = 2 * fm.height() - 2*fm.descent();
_boundingRect = (_side == Right)
2017-09-29 11:43:09 +02:00
? QRectF(-SIZE/2, -height/2, width + 1.5*SIZE, height)
: QRectF(-(width + SIZE), -height/2, width + 1.5*SIZE, height);
2015-10-17 01:33:02 +02:00
}
void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
*option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
2017-09-29 11:43:09 +02:00
QRectF rx, ry;
qreal width = qMax(fm.width(_x), fm.width(_y));
if (_side == Right) {
ry = QRectF(SIZE, -fm.height() + fm.descent(), fm.width(_y),
fm.height() - fm.descent());
rx = QRectF(SIZE, 0, fm.width(_x), fm.height()
- fm.descent());
} else {
ry = QRectF(-(width + SIZE), -fm.height() + fm.descent(), fm.width(_y),
fm.height() - fm.descent());
rx = QRectF(-(width + SIZE), 0, fm.width(_x), fm.height()
- fm.descent());
}
painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(QColor(255, 255, 255, 196)));
painter->drawRect(ry);
painter->drawRect(rx);
painter->setBrush(Qt::NoBrush);
2015-10-17 01:33:02 +02:00
2016-12-06 01:48:26 +01:00
painter->setFont(font);
painter->setRenderHint(QPainter::Antialiasing, false);
2015-10-17 01:33:02 +02:00
painter->setPen(Qt::red);
2016-12-06 01:48:26 +01:00
2017-09-29 11:43:09 +02:00
if (_side == Right) {
painter->drawText(SIZE, -fm.descent()/2, _y);
painter->drawText(SIZE, fm.height() - fm.descent()*1.5, _x);
} else {
painter->drawText(-(width + SIZE), -fm.descent()/2, _y);
painter->drawText(-(width + SIZE), fm.height() - fm.descent()*1.5, _x);
}
2015-10-19 23:57:45 +02:00
painter->drawLine(QPointF(-SIZE/2, 0), QPointF(SIZE/2, 0));
2015-10-17 01:33:02 +02:00
2015-10-19 23:57:45 +02:00
//painter->drawRect(boundingRect());
2015-10-17 01:33:02 +02:00
}
2017-09-29 11:43:09 +02:00
void SliderInfoItem::setText(const QString &x, const QString &y)
2015-10-17 01:33:02 +02:00
{
2016-04-01 21:20:23 +02:00
prepareGeometryChange();
2017-09-29 11:43:09 +02:00
_x = x; _y = y;
2015-10-17 01:33:02 +02:00
updateBoundingRect();
}
void SliderInfoItem::setSide(Side side)
{
if (side == _side)
return;
2016-04-01 21:20:23 +02:00
prepareGeometryChange();
_side = side;
updateBoundingRect();
}