1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-18 19:52:09 +01:00

Improved graph slider info

This commit is contained in:
Martin Tůma 2017-09-29 11:43:09 +02:00
parent bb47a34823
commit da4a51e7fa
9 changed files with 94 additions and 28 deletions

View File

@ -2,7 +2,7 @@
#include "coordinates.h" #include "coordinates.h"
#include "format.h" #include "format.h"
QString Format::timeSpan(qreal time) QString Format::timeSpan(qreal time, bool full)
{ {
unsigned h, m, s; unsigned h, m, s;
@ -10,8 +10,12 @@ QString Format::timeSpan(qreal time)
m = (time - (h * 3600)) / 60; m = (time - (h * 3600)) / 60;
s = time - (h * 3600) - (m * 60); s = time - (h * 3600) - (m * 60);
return QString("%1:%2:%3").arg(h).arg(m, 2, 10, QChar('0')) if (full || h)
.arg(s, 2, 10, QChar('0')); return QString("%1:%2:%3").arg(h, 2, 10, QChar('0'))
.arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0'));
else
return QString("%1:%2").arg(m, 2, 10, QChar('0'))
.arg(s, 2, 10, QChar('0'));
} }
QString Format::distance(qreal value, Units units) QString Format::distance(qreal value, Units units)

View File

@ -8,7 +8,7 @@ class Coordinates;
namespace Format namespace Format
{ {
QString timeSpan(qreal time); QString timeSpan(qreal time, bool full = true);
QString distance(qreal value, Units units); QString distance(qreal value, Units units);
QString elevation(qreal value, Units units); QString elevation(qreal value, Units units);
QString coordinates(const Coordinates &value); QString coordinates(const Coordinates &value);

View File

@ -13,6 +13,7 @@
#include "graph.h" #include "graph.h"
#include "graphitem.h" #include "graphitem.h"
#include "pathitem.h" #include "pathitem.h"
#include "format.h"
#include "graphview.h" #include "graphview.h"
@ -171,6 +172,11 @@ void GraphView::showGrid(bool show)
_grid->setVisible(show); _grid->setVisible(show);
} }
void GraphView::showSliderInfo(bool show)
{
_sliderInfo->setVisible(show);
}
void GraphView::addGraph(GraphItem *graph, PathItem *path, int id) void GraphView::addGraph(GraphItem *graph, PathItem *path, int id)
{ {
graph->setUnits(_units); graph->setUnits(_units);
@ -374,22 +380,25 @@ void GraphView::updateSliderPosition()
_slider->setVisible(false); _slider->setVisible(false);
} }
updateSliderInfo(); if (_slider->isVisible())
updateSliderInfo();
} }
void GraphView::updateSliderInfo() void GraphView::updateSliderInfo()
{ {
_sliderInfo->setVisible(_visible.count() == 1); qreal r, y;
if (!_sliderInfo->isVisible())
return;
QRectF br(_visible.first()->bounds()); if (_visible.count() > 1)
if (br.height() < _minYRange) r = 0;
br.adjust(0, -(_minYRange/2 - br.height()/2), 0, else {
_minYRange/2 - br.height()/2); QRectF br(_visible.first()->bounds());
if (br.height() < _minYRange)
br.adjust(0, -(_minYRange/2 - br.height()/2), 0,
_minYRange/2 - br.height()/2);
qreal y = _visible.first()->yAtX(_sliderPos); y = _visible.first()->yAtX(_sliderPos);
qreal r = (y - br.bottom()) / br.height(); r = (y - br.bottom()) / br.height();
}
qreal pos = (_sliderPos / bounds().width()) * _slider->area().width(); qreal pos = (_sliderPos / bounds().width()) * _slider->area().width();
SliderInfoItem::Side s = (pos + _sliderInfo->boundingRect().width() SliderInfoItem::Side s = (pos + _sliderInfo->boundingRect().width()
@ -397,8 +406,11 @@ void GraphView::updateSliderInfo()
_sliderInfo->setSide(s); _sliderInfo->setSide(s);
_sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r)); _sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r));
_sliderInfo->setText(QString::number(-y * _yScale + _yOffset, 'f', _sliderInfo->setText(_graphType == Time ? Format::timeSpan(_sliderPos,
_precision)); bounds().width() > 3600) : QString::number(_sliderPos * _xScale, 'f', 1)
+ UNIT_SPACE + _xUnits, (_visible.count() > 1) ? QString()
: QString::number(-y * _yScale + _yOffset, 'f', _precision) + UNIT_SPACE
+ _yUnits);
} }
void GraphView::emitSliderPositionChanged(const QPointF &pos) void GraphView::emitSliderPositionChanged(const QPointF &pos)

View File

@ -34,6 +34,7 @@ public:
void setGraphType(GraphType type); void setGraphType(GraphType type);
void setUnits(Units units); void setUnits(Units units);
void showGrid(bool show); void showGrid(bool show);
void showSliderInfo(bool show);
void setPalette(const Palette &palette); void setPalette(const Palette &palette);
void setGraphWidth(int width); void setGraphWidth(int width);

View File

@ -425,6 +425,10 @@ void GUI::createActions()
_showGraphGridAction->setCheckable(true); _showGraphGridAction->setCheckable(true);
connect(_showGraphGridAction, SIGNAL(triggered(bool)), this, connect(_showGraphGridAction, SIGNAL(triggered(bool)), this,
SLOT(showGraphGrids(bool))); SLOT(showGraphGrids(bool)));
_showGraphSliderInfoAction = new QAction(tr("Show slider info"), this);
_showGraphSliderInfoAction->setCheckable(true);
connect(_showGraphSliderInfoAction, SIGNAL(triggered(bool)), this,
SLOT(showGraphSliderInfo(bool)));
// Settings actions // Settings actions
_showToolbarsAction = new QAction(tr("Show toolbars"), this); _showToolbarsAction = new QAction(tr("Show toolbars"), this);
@ -513,6 +517,7 @@ void GUI::createMenus()
graphMenu->addAction(_timeGraphAction); graphMenu->addAction(_timeGraphAction);
graphMenu->addSeparator(); graphMenu->addSeparator();
graphMenu->addAction(_showGraphGridAction); graphMenu->addAction(_showGraphGridAction);
graphMenu->addAction(_showGraphSliderInfoAction);
graphMenu->addSeparator(); graphMenu->addSeparator();
graphMenu->addAction(_showGraphsAction); graphMenu->addAction(_showGraphsAction);
@ -1177,6 +1182,12 @@ void GUI::showGraphGrids(bool show)
_tabs.at(i)->showGrid(show); _tabs.at(i)->showGrid(show);
} }
void GUI::showGraphSliderInfo(bool show)
{
for (int i = 0; i < _tabs.size(); i++)
_tabs.at(i)->showSliderInfo(show);
}
void GUI::loadMap() void GUI::loadMap()
{ {
QString fileName = QFileDialog::getOpenFileName(this, tr("Open map file"), QString fileName = QFileDialog::getOpenFileName(this, tr("Open map file"),
@ -1537,6 +1548,10 @@ void GUI::writeSettings()
if (_showGraphGridAction->isChecked() != SHOW_GRAPH_GRIDS_DEFAULT) if (_showGraphGridAction->isChecked() != SHOW_GRAPH_GRIDS_DEFAULT)
settings.setValue(SHOW_GRAPH_GRIDS_SETTING, settings.setValue(SHOW_GRAPH_GRIDS_SETTING,
_showGraphGridAction->isChecked()); _showGraphGridAction->isChecked());
if (_showGraphSliderInfoAction->isChecked()
!= SHOW_GRAPH_SLIDER_INFO_DEFAULT)
settings.setValue(SHOW_GRAPH_SLIDER_INFO_SETTING,
_showGraphSliderInfoAction->isChecked());
settings.endGroup(); settings.endGroup();
settings.beginGroup(POI_SETTINGS_GROUP); settings.beginGroup(POI_SETTINGS_GROUP);
@ -1724,6 +1739,11 @@ void GUI::readSettings()
showGraphGrids(false); showGraphGrids(false);
else else
_showGraphGridAction->setChecked(true); _showGraphGridAction->setChecked(true);
if (!settings.value(SHOW_GRAPH_SLIDER_INFO_SETTING,
SHOW_GRAPH_SLIDER_INFO_DEFAULT).toBool())
showGraphSliderInfo(false);
else
_showGraphSliderInfoAction->setChecked(true);
settings.endGroup(); settings.endGroup();
settings.beginGroup(POI_SETTINGS_GROUP); settings.beginGroup(POI_SETTINGS_GROUP);

View File

@ -51,6 +51,7 @@ private slots:
void closePOIFiles(); void closePOIFiles();
void showGraphs(bool show); void showGraphs(bool show);
void showGraphGrids(bool show); void showGraphGrids(bool show);
void showGraphSliderInfo(bool show);
void showToolbars(bool show); void showToolbars(bool show);
void showFullscreen(bool show); void showFullscreen(bool show);
void showTracks(bool show); void showTracks(bool show);
@ -156,6 +157,7 @@ private:
QAction *_clearMapCacheAction; QAction *_clearMapCacheAction;
QAction *_showGraphsAction; QAction *_showGraphsAction;
QAction *_showGraphGridAction; QAction *_showGraphGridAction;
QAction *_showGraphSliderInfoAction;
QAction *_distanceGraphAction; QAction *_distanceGraphAction;
QAction *_timeGraphAction; QAction *_timeGraphAction;
QAction *_showToolbarsAction; QAction *_showToolbarsAction;

View File

@ -25,6 +25,8 @@
#define GRAPH_TYPE_DEFAULT Distance #define GRAPH_TYPE_DEFAULT Distance
#define SHOW_GRAPH_GRIDS_SETTING "grid" #define SHOW_GRAPH_GRIDS_SETTING "grid"
#define SHOW_GRAPH_GRIDS_DEFAULT true #define SHOW_GRAPH_GRIDS_DEFAULT true
#define SHOW_GRAPH_SLIDER_INFO_SETTING "sliderInfo"
#define SHOW_GRAPH_SLIDER_INFO_DEFAULT true
#define MAP_SETTINGS_GROUP "Map" #define MAP_SETTINGS_GROUP "Map"
#define CURRENT_MAP_SETTING "map" #define CURRENT_MAP_SETTING "map"

View File

@ -17,10 +17,12 @@ void SliderInfoItem::updateBoundingRect()
font.setFamily(FONT_FAMILY); font.setFamily(FONT_FAMILY);
QFontMetrics fm(font); QFontMetrics fm(font);
qreal width = qMax(fm.width(_x), fm.width(_y));
qreal height = 2 * fm.height() - 2*fm.descent();
_boundingRect = (_side == Right) _boundingRect = (_side == Right)
? QRectF(-SIZE/2, 0, fm.width(_text) + SIZE, fm.height()) ? QRectF(-SIZE/2, -height/2, width + 1.5*SIZE, height)
: QRectF(-(fm.width(_text) + SIZE/2), 0, fm.width(_text) + SIZE, : QRectF(-(width + SIZE), -height/2, width + 1.5*SIZE, height);
fm.height());
} }
void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
@ -32,25 +34,48 @@ void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
font.setPixelSize(FONT_SIZE); font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY); font.setFamily(FONT_FAMILY);
QFontMetrics fm(font); QFontMetrics fm(font);
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);
painter->setFont(font); painter->setFont(font);
painter->setRenderHint(QPainter::Antialiasing, false); painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(Qt::red); painter->setPen(Qt::red);
if (_side == Right) if (_side == Right) {
painter->drawText(SIZE, fm.height() - fm.descent(), _text); painter->drawText(SIZE, -fm.descent()/2, _y);
else painter->drawText(SIZE, fm.height() - fm.descent()*1.5, _x);
painter->drawText(-(fm.width(_text) + SIZE/2), } else {
fm.height() - fm.descent(), _text); painter->drawText(-(width + SIZE), -fm.descent()/2, _y);
painter->drawText(-(width + SIZE), fm.height() - fm.descent()*1.5, _x);
}
painter->drawLine(QPointF(-SIZE/2, 0), QPointF(SIZE/2, 0)); painter->drawLine(QPointF(-SIZE/2, 0), QPointF(SIZE/2, 0));
//painter->drawRect(boundingRect()); //painter->drawRect(boundingRect());
} }
void SliderInfoItem::setText(const QString &text) void SliderInfoItem::setText(const QString &x, const QString &y)
{ {
prepareGeometryChange(); prepareGeometryChange();
_text = text; _x = x; _y = y;
updateBoundingRect(); updateBoundingRect();
} }

View File

@ -14,14 +14,14 @@ public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget); QWidget *widget);
void setText(const QString &text); void setText(const QString &x, const QString &y);
void setSide(Side side); void setSide(Side side);
private: private:
void updateBoundingRect(); void updateBoundingRect();
Side _side; Side _side;
QString _text; QString _x, _y;
QRectF _boundingRect; QRectF _boundingRect;
}; };