diff --git a/src/format.cpp b/src/format.cpp index 714dc809..ef97ce09 100644 --- a/src/format.cpp +++ b/src/format.cpp @@ -2,7 +2,7 @@ #include "coordinates.h" #include "format.h" -QString Format::timeSpan(qreal time) +QString Format::timeSpan(qreal time, bool full) { unsigned h, m, s; @@ -10,8 +10,12 @@ QString Format::timeSpan(qreal time) m = (time - (h * 3600)) / 60; s = time - (h * 3600) - (m * 60); - return QString("%1:%2:%3").arg(h).arg(m, 2, 10, QChar('0')) - .arg(s, 2, 10, QChar('0')); + if (full || h) + 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) diff --git a/src/format.h b/src/format.h index d2ba767a..6bff2d1c 100644 --- a/src/format.h +++ b/src/format.h @@ -8,7 +8,7 @@ class Coordinates; namespace Format { - QString timeSpan(qreal time); + QString timeSpan(qreal time, bool full = true); QString distance(qreal value, Units units); QString elevation(qreal value, Units units); QString coordinates(const Coordinates &value); diff --git a/src/graphview.cpp b/src/graphview.cpp index a21955bc..3597f480 100644 --- a/src/graphview.cpp +++ b/src/graphview.cpp @@ -13,6 +13,7 @@ #include "graph.h" #include "graphitem.h" #include "pathitem.h" +#include "format.h" #include "graphview.h" @@ -171,6 +172,11 @@ void GraphView::showGrid(bool show) _grid->setVisible(show); } +void GraphView::showSliderInfo(bool show) +{ + _sliderInfo->setVisible(show); +} + void GraphView::addGraph(GraphItem *graph, PathItem *path, int id) { graph->setUnits(_units); @@ -374,22 +380,25 @@ void GraphView::updateSliderPosition() _slider->setVisible(false); } - updateSliderInfo(); + if (_slider->isVisible()) + updateSliderInfo(); } void GraphView::updateSliderInfo() { - _sliderInfo->setVisible(_visible.count() == 1); - if (!_sliderInfo->isVisible()) - return; + qreal r, y; - QRectF br(_visible.first()->bounds()); - if (br.height() < _minYRange) - br.adjust(0, -(_minYRange/2 - br.height()/2), 0, - _minYRange/2 - br.height()/2); + if (_visible.count() > 1) + r = 0; + else { + 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); - qreal r = (y - br.bottom()) / br.height(); + y = _visible.first()->yAtX(_sliderPos); + r = (y - br.bottom()) / br.height(); + } qreal pos = (_sliderPos / bounds().width()) * _slider->area().width(); SliderInfoItem::Side s = (pos + _sliderInfo->boundingRect().width() @@ -397,8 +406,11 @@ void GraphView::updateSliderInfo() _sliderInfo->setSide(s); _sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r)); - _sliderInfo->setText(QString::number(-y * _yScale + _yOffset, 'f', - _precision)); + _sliderInfo->setText(_graphType == Time ? Format::timeSpan(_sliderPos, + 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) diff --git a/src/graphview.h b/src/graphview.h index b68de5c9..f9d068ee 100644 --- a/src/graphview.h +++ b/src/graphview.h @@ -34,6 +34,7 @@ public: void setGraphType(GraphType type); void setUnits(Units units); void showGrid(bool show); + void showSliderInfo(bool show); void setPalette(const Palette &palette); void setGraphWidth(int width); diff --git a/src/gui.cpp b/src/gui.cpp index 9b7094bf..363df8d9 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -425,6 +425,10 @@ void GUI::createActions() _showGraphGridAction->setCheckable(true); connect(_showGraphGridAction, SIGNAL(triggered(bool)), this, 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 _showToolbarsAction = new QAction(tr("Show toolbars"), this); @@ -513,6 +517,7 @@ void GUI::createMenus() graphMenu->addAction(_timeGraphAction); graphMenu->addSeparator(); graphMenu->addAction(_showGraphGridAction); + graphMenu->addAction(_showGraphSliderInfoAction); graphMenu->addSeparator(); graphMenu->addAction(_showGraphsAction); @@ -1177,6 +1182,12 @@ void GUI::showGraphGrids(bool 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() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open map file"), @@ -1537,6 +1548,10 @@ void GUI::writeSettings() if (_showGraphGridAction->isChecked() != SHOW_GRAPH_GRIDS_DEFAULT) settings.setValue(SHOW_GRAPH_GRIDS_SETTING, _showGraphGridAction->isChecked()); + if (_showGraphSliderInfoAction->isChecked() + != SHOW_GRAPH_SLIDER_INFO_DEFAULT) + settings.setValue(SHOW_GRAPH_SLIDER_INFO_SETTING, + _showGraphSliderInfoAction->isChecked()); settings.endGroup(); settings.beginGroup(POI_SETTINGS_GROUP); @@ -1724,6 +1739,11 @@ void GUI::readSettings() showGraphGrids(false); else _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.beginGroup(POI_SETTINGS_GROUP); diff --git a/src/gui.h b/src/gui.h index d1e60e0d..10a12769 100644 --- a/src/gui.h +++ b/src/gui.h @@ -51,6 +51,7 @@ private slots: void closePOIFiles(); void showGraphs(bool show); void showGraphGrids(bool show); + void showGraphSliderInfo(bool show); void showToolbars(bool show); void showFullscreen(bool show); void showTracks(bool show); @@ -156,6 +157,7 @@ private: QAction *_clearMapCacheAction; QAction *_showGraphsAction; QAction *_showGraphGridAction; + QAction *_showGraphSliderInfoAction; QAction *_distanceGraphAction; QAction *_timeGraphAction; QAction *_showToolbarsAction; diff --git a/src/settings.h b/src/settings.h index a81aac6a..5fa4fcd2 100644 --- a/src/settings.h +++ b/src/settings.h @@ -25,6 +25,8 @@ #define GRAPH_TYPE_DEFAULT Distance #define SHOW_GRAPH_GRIDS_SETTING "grid" #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 CURRENT_MAP_SETTING "map" diff --git a/src/sliderinfoitem.cpp b/src/sliderinfoitem.cpp index 03f81d18..a7bf0fd0 100644 --- a/src/sliderinfoitem.cpp +++ b/src/sliderinfoitem.cpp @@ -17,10 +17,12 @@ void SliderInfoItem::updateBoundingRect() font.setFamily(FONT_FAMILY); QFontMetrics fm(font); + qreal width = qMax(fm.width(_x), fm.width(_y)); + qreal height = 2 * fm.height() - 2*fm.descent(); + _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()); + ? QRectF(-SIZE/2, -height/2, width + 1.5*SIZE, height) + : QRectF(-(width + SIZE), -height/2, width + 1.5*SIZE, height); } void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem @@ -32,25 +34,48 @@ void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem font.setPixelSize(FONT_SIZE); font.setFamily(FONT_FAMILY); 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->setRenderHint(QPainter::Antialiasing, false); 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); + 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); + } painter->drawLine(QPointF(-SIZE/2, 0), QPointF(SIZE/2, 0)); //painter->drawRect(boundingRect()); } -void SliderInfoItem::setText(const QString &text) +void SliderInfoItem::setText(const QString &x, const QString &y) { prepareGeometryChange(); - _text = text; + _x = x; _y = y; updateBoundingRect(); } diff --git a/src/sliderinfoitem.h b/src/sliderinfoitem.h index 2578ad82..da1fc85e 100644 --- a/src/sliderinfoitem.h +++ b/src/sliderinfoitem.h @@ -14,14 +14,14 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - void setText(const QString &text); + void setText(const QString &x, const QString &y); void setSide(Side side); private: void updateBoundingRect(); Side _side; - QString _text; + QString _x, _y; QRectF _boundingRect; };