1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 06:43:22 +02:00

Fixed slider info displaying on right graph border

This commit is contained in:
Martin Tůma 2016-03-30 01:48:48 +02:00
parent eec797125b
commit f469b4f600
3 changed files with 34 additions and 12 deletions

View File

@ -27,6 +27,9 @@ GraphView::GraphView(QWidget *parent)
_scene = new Scene(this);
setScene(_scene);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_xAxis = new AxisItem(AxisItem::X);
_yAxis = new AxisItem(AxisItem::Y);
@ -148,9 +151,6 @@ void GraphView::loadData(const QVector<QPointF> &data)
pi->setPen(pen);
_scene->addItem(pi);
_graphs.append(pi);
if (_graphs.size() > 1)
_sliderInfo->hide();
}
void GraphView::redraw()
@ -223,9 +223,7 @@ void GraphView::redraw(const QSizeF &size)
_slider->setPos(QPointF(sp * r.width(), r.bottom()));
_scene->addItem(_slider);
const QPainterPath &path = _graphs.at(0)->path();
QPointF p = path.pointAtPercent(sp);
_sliderInfo->setText(QString::number(-p.y() * _yScale, 'f', _precision));
_sliderInfo->setVisible(_graphs.size() == 1);
r = _scene->itemsBoundingRect();
_info->setPos(r.topLeft() + QPointF(r.width()/2
@ -262,13 +260,10 @@ void GraphView::clear()
_scene->removeItem(_xAxis);
if (_yAxis->scene() == _scene)
_scene->removeItem(_yAxis);
if (_slider->scene() == _scene)
_scene->removeItem(_slider);
if (_info->scene() == _scene)
_scene->removeItem(_info);
_sliderInfo->show();
_slider->clear();
_info->clear();
@ -320,7 +315,7 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos)
qreal val = pos.x() / _slider->area().width();
emit sliderPositionChanged(val * _bounds.width());
if (!_sliderInfo->isVisible())
if (_graphs.size() > 1)
return;
const QPainterPath &path = _graphs.at(0)->path();
@ -331,6 +326,11 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos)
qreal y = yAtX(path, val * _bounds.width());
qreal r = (y - br.bottom()) / br.height();
SliderInfoItem::Side s = (pos.x() + _sliderInfo->boundingRect().width()
> _slider->area().right()) ? SliderInfoItem::Left : SliderInfoItem::Right;
_sliderInfo->setSide(s);
_sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r));
_sliderInfo->setText(QString::number(-y * _yScale, 'f', _precision));
}

View File

@ -7,6 +7,7 @@
SliderInfoItem::SliderInfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{
_side = Right;
}
void SliderInfoItem::updateBoundingRect()
@ -16,7 +17,10 @@ void SliderInfoItem::updateBoundingRect()
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
_boundingRect = QRectF(-SIZE/2, 0, fm.width(_text) + SIZE, fm.height());
_boundingRect = (_side == Right)
? QRectF(-SIZE/2, 0, fm.width(_text) + SIZE, fm.height())
: QRectF(-(fm.width(_text) + SIZE/2), 0, fm.width(_text) + SIZE,
fm.height());
}
void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
@ -31,7 +35,11 @@ void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
painter->setFont(font);
painter->setPen(Qt::red);
if (_side == Right)
painter->drawText(SIZE, fm.height() - fm.descent(), _text);
else
painter->drawText(-(fm.width(_text) + SIZE/2),
fm.height() - fm.descent(), _text);
painter->drawLine(QPointF(-SIZE/2, 0), QPointF(SIZE/2, 0));
//painter->drawRect(boundingRect());
@ -43,3 +51,13 @@ void SliderInfoItem::setText(const QString &text)
updateBoundingRect();
prepareGeometryChange();
}
void SliderInfoItem::setSide(Side side)
{
if (side == _side)
return;
_side = side;
updateBoundingRect();
prepareGeometryChange();
}

View File

@ -6,6 +6,8 @@
class SliderInfoItem : public QGraphicsItem
{
public:
enum Side {Left, Right};
SliderInfoItem(QGraphicsItem *parent = 0);
QRectF boundingRect() const {return _boundingRect;}
@ -13,10 +15,12 @@ public:
QWidget *widget);
void setText(const QString &text);
void setSide(Side side);
private:
void updateBoundingRect();
Side _side;
QString _text;
QRectF _boundingRect;
};