1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45: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; _precision = 0;
_minYRange = 0.01; _minYRange = 0.01;
_sliderPos = 0;
} }
GraphView::~GraphView() GraphView::~GraphView()
@ -215,15 +217,12 @@ void GraphView::redraw(const QSizeF &size)
_scene->addItem(_xAxis); _scene->addItem(_xAxis);
_scene->addItem(_yAxis); _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->setArea(r);
_slider->setPos(QPointF(sp * r.width(), r.bottom())); _slider->setPos((_sliderPos / _bounds.width()) * _slider->area().width(),
r.bottom());
_scene->addItem(_slider); _scene->addItem(_slider);
_sliderInfo->setVisible(_graphs.size() == 1); updateSliderInfo();
r = _scene->itemsBoundingRect(); r = _scene->itemsBoundingRect();
_info->setPos(r.topLeft() + QPointF(r.width()/2 _info->setPos(r.topLeft() + QPointF(r.width()/2
@ -272,6 +271,7 @@ void GraphView::clear()
_palette.reset(); _palette.reset();
_bounds = QRectF(); _bounds = QRectF();
_sliderPos = 0;
_scene->setSceneRect(0, 0, 0, 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(); 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()) _sliderInfo->setVisible(_graphs.size() == 1);
return;
qreal val = pos.x() / _slider->area().width();
emit sliderPositionChanged(val * _bounds.width());
if (_graphs.size() > 1) if (_graphs.size() > 1)
return; return;
@ -324,10 +320,11 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos)
br.adjust(0, -(_minYRange/2 - br.height()/2), 0, br.adjust(0, -(_minYRange/2 - br.height()/2), 0,
_minYRange/2 - br.height()/2); _minYRange/2 - br.height()/2);
qreal y = yAtX(path, val * _bounds.width()); qreal y = yAtX(path, _sliderPos);
qreal r = (y - br.bottom()) / br.height(); 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; > _slider->area().right()) ? SliderInfoItem::Left : SliderInfoItem::Right;
_sliderInfo->setSide(s); _sliderInfo->setSide(s);
@ -335,16 +332,21 @@ void GraphView::emitSliderPositionChanged(const QPointF &pos)
_sliderInfo->setText(QString::number(-y * _yScale, 'f', _precision)); _sliderInfo->setText(QString::number(-y * _yScale, 'f', _precision));
} }
qreal GraphView::sliderPosition() const void GraphView::emitSliderPositionChanged(const QPointF &pos)
{ {
if (!_slider->isVisible()) if (_graphs.isEmpty())
return -1; return;
else
return (_slider->pos().x() / _slider->area().width()) * _bounds.width(); _sliderPos = (pos.x() / _slider->area().width()) * _bounds.width();
emit sliderPositionChanged(_sliderPos);
updateSliderInfo();
} }
void GraphView::setSliderPosition(qreal pos) void GraphView::setSliderPosition(qreal pos)
{ {
_sliderPos = pos;
if (_graphs.isEmpty()) if (_graphs.isEmpty())
return; return;

View File

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

View File

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

View File

@ -58,6 +58,8 @@ private slots:
void setMetricUnits(); void setMetricUnits();
void setImperialUnits(); void setImperialUnits();
void sliderPositionChanged(qreal pos);
private: private:
void loadMaps(); void loadMaps();
void loadPOIs(); void loadPOIs();
@ -146,8 +148,7 @@ private:
qreal _time; qreal _time;
int _trackCount; int _trackCount;
GraphView *_lastGraph; qreal _sliderPos;
qreal _lastSliderPos;
}; };
#endif // GUI_H #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()); emit positionChanged(value.toPointF());
return QGraphicsItem::itemChange(change, value); return QGraphicsItem::itemChange(change, value);

View File

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