1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 03:35:53 +01:00

Fixed slider position handling

This commit is contained in:
Martin Tůma 2016-03-30 20:50:51 +02:00
parent f469b4f600
commit 284f7d0a36
6 changed files with 48 additions and 39 deletions

View File

@ -51,6 +51,8 @@ GraphView::GraphView(QWidget *parent)
_precision = 0;
_minYRange = 0.01;
_sliderPos = 0;
}
GraphView::~GraphView()
@ -215,15 +217,12 @@ void GraphView::redraw(const QSizeF &size)
_scene->addItem(_xAxis);
_scene->addItem(_yAxis);
qreal sp = (_slider->pos().x() == _slider->area().left())
? 0 : (_slider->pos().x() - _slider->area().left())
/ _slider->area().width();
_slider->setArea(r);
_slider->setPos(QPointF(sp * r.width(), r.bottom()));
_slider->setPos((_sliderPos / _bounds.width()) * _slider->area().width(),
r.bottom());
_scene->addItem(_slider);
_sliderInfo->setVisible(_graphs.size() == 1);
updateSliderInfo();
r = _scene->itemsBoundingRect();
_info->setPos(r.topLeft() + QPointF(r.width()/2
@ -272,6 +271,7 @@ void GraphView::clear()
_palette.reset();
_bounds = QRectF();
_sliderPos = 0;
_scene->setSceneRect(0, 0, 0, 0);
}
@ -307,13 +307,9 @@ static qreal yAtX(const QPainterPath &path, qreal x)
return l.pointAt((x - l.p1().x()) / (l.p2().x() - l.p1().x())).y();
}
void GraphView::emitSliderPositionChanged(const QPointF &pos)
void GraphView::updateSliderInfo()
{
if (_graphs.isEmpty())
return;
qreal val = pos.x() / _slider->area().width();
emit sliderPositionChanged(val * _bounds.width());
_sliderInfo->setVisible(_graphs.size() == 1);
if (_graphs.size() > 1)
return;
@ -324,10 +320,11 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos)
br.adjust(0, -(_minYRange/2 - br.height()/2), 0,
_minYRange/2 - br.height()/2);
qreal y = yAtX(path, val * _bounds.width());
qreal y = yAtX(path, _sliderPos);
qreal r = (y - br.bottom()) / br.height();
SliderInfoItem::Side s = (pos.x() + _sliderInfo->boundingRect().width()
qreal pos = (_sliderPos / _bounds.width()) * _slider->area().width();
SliderInfoItem::Side s = (pos + _sliderInfo->boundingRect().width()
> _slider->area().right()) ? SliderInfoItem::Left : SliderInfoItem::Right;
_sliderInfo->setSide(s);
@ -335,16 +332,21 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos)
_sliderInfo->setText(QString::number(-y * _yScale, 'f', _precision));
}
qreal GraphView::sliderPosition() const
void GraphView::emitSliderPositionChanged(const QPointF &pos)
{
if (!_slider->isVisible())
return -1;
else
return (_slider->pos().x() / _slider->area().width()) * _bounds.width();
if (_graphs.isEmpty())
return;
_sliderPos = (pos.x() / _slider->area().width()) * _bounds.width();
emit sliderPositionChanged(_sliderPos);
updateSliderInfo();
}
void GraphView::setSliderPosition(qreal pos)
{
_sliderPos = pos;
if (_graphs.isEmpty())
return;

View File

@ -58,7 +58,7 @@ public:
void setSliderPrecision(int precision) {_precision = precision;}
void setMinYRange(qreal range) {_minYRange = range;}
qreal sliderPosition() const;
qreal sliderPosition() const {return _sliderPos;}
void setSliderPosition(qreal pos);
void plot(QPainter *painter, const QRectF &target);
@ -82,12 +82,14 @@ private:
void createXLabel();
void createYLabel();
void updateBounds(const QPointF &point);
void updateSliderInfo();
qreal _xScale, _yScale;
QString _xUnits, _yUnits;
QString _xLabel, _yLabel;
int _precision;
qreal _minYRange;
qreal _sliderPos;
Scene *_scene;

View File

@ -61,12 +61,12 @@ GUI::GUI(QWidget *parent) : QMainWindow(parent)
createTrackGraphs();
createStatusBar();
connect(_elevationGraph, SIGNAL(sliderPositionChanged(qreal)), _track,
SLOT(movePositionMarker(qreal)));
connect(_speedGraph, SIGNAL(sliderPositionChanged(qreal)), _track,
SLOT(movePositionMarker(qreal)));
connect(_heartRateGraph, SIGNAL(sliderPositionChanged(qreal)), _track,
SLOT(movePositionMarker(qreal)));
connect(_elevationGraph, SIGNAL(sliderPositionChanged(qreal)), this,
SLOT(sliderPositionChanged(qreal)));
connect(_speedGraph, SIGNAL(sliderPositionChanged(qreal)), this,
SLOT(sliderPositionChanged(qreal)));
connect(_heartRateGraph, SIGNAL(sliderPositionChanged(qreal)), this,
SLOT(sliderPositionChanged(qreal)));
_browser = new FileBrowser(this);
_browser->setFilter(QStringList("*.gpx"));
@ -86,8 +86,8 @@ GUI::GUI(QWidget *parent) : QMainWindow(parent)
_time = 0;
_trackCount = 0;
_lastGraph = 0;
_lastSliderPos = -1.0;
_sliderPos = 0;
updateGraphTabs();
updateTrackView();
@ -515,6 +515,7 @@ bool GUI::loadFile(const QString &fileName)
_track->loadGPX(gpx);
if (_showPOIAction->isChecked())
_track->loadPOI(_poi);
_track->movePositionMarker(_sliderPos);
for (int i = 0; i < gpx.trackCount(); i++) {
_distance += gpx.track(i).distance();
@ -680,6 +681,8 @@ void GUI::closeFiles()
_time = 0;
_trackCount = 0;
_sliderPos = 0;
_elevationGraph->clear();
_speedGraph->clear();
_heartRateGraph->clear();
@ -783,18 +786,19 @@ void GUI::poiFileChecked(int index)
_track->loadPOI(_poi);
}
void GUI::sliderPositionChanged(qreal pos)
{
_track->movePositionMarker(_sliderPos);
_sliderPos = pos;
}
void GUI::graphChanged(int index)
{
if (index < 0)
return;
GraphView *tv = static_cast<GraphView*>(_trackGraphs->widget(index));
if (_lastGraph) {
if (_lastGraph->sliderPosition() >= 0)
_lastSliderPos = _lastGraph->sliderPosition();
tv->setSliderPosition(_lastSliderPos);
}
_lastGraph = tv;
GraphView *gv = static_cast<GraphView*>(_trackGraphs->widget(index));
gv->setSliderPosition(_sliderPos);
}
void GUI::updateNavigationActions()

View File

@ -58,6 +58,8 @@ private slots:
void setMetricUnits();
void setImperialUnits();
void sliderPositionChanged(qreal pos);
private:
void loadMaps();
void loadPOIs();
@ -146,8 +148,7 @@ private:
qreal _time;
int _trackCount;
GraphView *_lastGraph;
qreal _lastSliderPos;
qreal _sliderPos;
};
#endif // GUI_H

View File

@ -41,7 +41,7 @@ QVariant SliderItem::itemChange(GraphicsItemChange change, const QVariant &value
}
}
if (change == ItemPositionHasChanged)
if (change == ItemPositionHasChanged && scene())
emit positionChanged(value.toPointF());
return QGraphicsItem::itemChange(change, value);

View File

@ -420,7 +420,7 @@ void TrackView::movePositionMarker(qreal val)
for (int i = 0; i < _paths.size(); i++) {
qreal f = _maxPath / _paths.at(i)->path().length();
if (mp * f > 1.0)
if (mp * f < 0 || mp * f > 1.0)
_markers.at(i)->setVisible(false);
else {
QPointF pos = _paths.at(i)->path().pointAtPercent(mp * f);