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

Added slider info

This commit is contained in:
Martin Tůma 2015-10-17 01:33:02 +02:00
parent da81839529
commit c21ef0b587
20 changed files with 127 additions and 25 deletions

View File

@ -21,7 +21,8 @@ HEADERS += src/config.h \
src/markeritem.h \ src/markeritem.h \
src/infoitem.h \ src/infoitem.h \
src/elevationgraph.h \ src/elevationgraph.h \
src/speedgraph.h src/speedgraph.h \
src/sliderinfoitem.h
SOURCES += src/main.cpp \ SOURCES += src/main.cpp \
src/gui.cpp \ src/gui.cpp \
src/gpx.cpp \ src/gpx.cpp \
@ -37,7 +38,8 @@ SOURCES += src/main.cpp \
src/markeritem.cpp \ src/markeritem.cpp \
src/infoitem.cpp \ src/infoitem.cpp \
src/elevationgraph.cpp \ src/elevationgraph.cpp \
src/speedgraph.cpp src/speedgraph.cpp \
src/sliderinfoitem.cpp
RESOURCES += gpxsee.qrc RESOURCES += gpxsee.qrc
TRANSLATIONS = lang/gpxsee_cs.ts TRANSLATIONS = lang/gpxsee_cs.ts
macx:ICON = icons/gpxsee.icns macx:ICON = icons/gpxsee.icns

View File

@ -1,5 +1,6 @@
#include <cmath> #include <cmath>
#include <QPainter> #include <QPainter>
#include "config.h"
#include "axisitem.h" #include "axisitem.h"
#include <QDebug> #include <QDebug>
@ -8,8 +9,6 @@
#define PADDING 6 #define PADDING 6
#define XTICKS 15 #define XTICKS 15
#define YTICKS 10 #define YTICKS 10
#define FONT_FAMILY "Arial"
#define FONT_SIZE 12
struct Label { struct Label {
@ -64,7 +63,7 @@ static struct Label label(double min, double max, int ticks)
} }
AxisItem::AxisItem(Type type) AxisItem::AxisItem(Type type, QGraphicsItem *parent) : QGraphicsItem(parent)
{ {
_type = type; _type = type;
_size = 0; _size = 0;

View File

@ -8,7 +8,7 @@ class AxisItem : public QGraphicsItem
public: public:
enum Type {X, Y}; enum Type {X, Y};
AxisItem(Type type); AxisItem(Type type, QGraphicsItem *parent = 0);
QRectF boundingRect() const {return _boundingRect;} QRectF boundingRect() const {return _boundingRect;}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,

View File

@ -5,4 +5,7 @@
#define APP_HOMEPAGE "http://tumic.wz.cz/gpxsee" #define APP_HOMEPAGE "http://tumic.wz.cz/gpxsee"
#define APP_VERSION "0.1" #define APP_VERSION "0.1"
#define FONT_FAMILY "Arial"
#define FONT_SIZE 12
#endif /* CONFIG_H */ #endif /* CONFIG_H */

View File

@ -1,7 +1,7 @@
#include <float.h> #include <float.h>
#include "elevationgraph.h" #include "elevationgraph.h"
ElevationGraph::ElevationGraph() ElevationGraph::ElevationGraph(QWidget *parent) : Graph(parent)
{ {
_ascent = 0; _ascent = 0;
_descent = 0; _descent = 0;

View File

@ -8,7 +8,7 @@ class ElevationGraph : public Graph
Q_OBJECT Q_OBJECT
public: public:
ElevationGraph(); ElevationGraph(QWidget *parent = 0);
void loadData(const QVector<QPointF> &data); void loadData(const QVector<QPointF> &data);
void clear(); void clear();

View File

@ -2,12 +2,16 @@
#include <QGraphicsView> #include <QGraphicsView>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QEvent> #include <QEvent>
#include <QGraphicsSimpleTextItem>
#include "slideritem.h" #include "slideritem.h"
#include "sliderinfoitem.h"
#include "infoitem.h" #include "infoitem.h"
#include "config.h"
#include "graph.h" #include "graph.h"
#include <QDebug> #include <QDebug>
#define MARGIN 10.0 #define MARGIN 10.0
@ -39,6 +43,9 @@ Graph::Graph(QWidget *parent)
_info = new InfoItem(); _info = new InfoItem();
_sliderInfo = new SliderInfoItem(_slider);
_sliderInfo->setZValue(2.0);
_xMax = -FLT_MAX; _xMax = -FLT_MAX;
_xMin = FLT_MAX; _xMin = FLT_MAX;
_yMax = -FLT_MAX; _yMax = -FLT_MAX;
@ -46,6 +53,8 @@ Graph::Graph(QWidget *parent)
_xScale = 1; _xScale = 1;
_yScale = 1; _yScale = 1;
_precision = 0;
} }
Graph::~Graph() Graph::~Graph()
@ -135,6 +144,9 @@ void Graph::loadData(const QVector<QPointF> &data)
_scene->addItem(pi); _scene->addItem(pi);
_graphs.append(pi); _graphs.append(pi);
if (_graphs.size() > 1)
_sliderInfo->hide();
resize(viewport()->size() - QSizeF(MARGIN, MARGIN)); resize(viewport()->size() - QSizeF(MARGIN, MARGIN));
} }
@ -223,6 +235,8 @@ void Graph::clear()
if (_info->scene() == _scene) if (_info->scene() == _scene)
_scene->removeItem(_info); _scene->removeItem(_info);
_sliderInfo->show();
_info->clear(); _info->clear();
_scene->clear(); _scene->clear();
_graphs.clear(); _graphs.clear();
@ -240,6 +254,13 @@ void Graph::emitSliderPositionChanged(const QPointF &pos)
{ {
qreal val = pos.x() / _slider->area().width(); qreal val = pos.x() / _slider->area().width();
emit sliderPositionChanged(val); emit sliderPositionChanged(val);
const QPainterPath &path = _graphs.at(0)->path();
QPointF p = path.pointAtPercent(val);
qreal r = (p.y() - path.boundingRect().bottom())
/ path.boundingRect().height();
_sliderInfo->setPos(QPointF(0, _slider->boundingRect().height() * r));
_sliderInfo->setText(QString::number(-p.y() * _yScale, 'f', _precision));
} }
qreal Graph::sliderPosition() const qreal Graph::sliderPosition() const

View File

@ -11,6 +11,7 @@
class SliderItem; class SliderItem;
class SliderInfoItem;
class InfoItem; class InfoItem;
class Scene : public QGraphicsScene class Scene : public QGraphicsScene
@ -40,6 +41,7 @@ public:
void setYUnits(const QString &units); void setYUnits(const QString &units);
void setXScale(qreal scale) {_xScale = scale;} void setXScale(qreal scale) {_xScale = scale;}
void setYScale(qreal scale) {_yScale = scale;} void setYScale(qreal scale) {_yScale = scale;}
void setPrecision(int p) {_precision = p;}
void plot(QPainter *painter, const QRectF &target); void plot(QPainter *painter, const QRectF &target);
void clear(); void clear();
@ -58,6 +60,7 @@ protected:
qreal _xScale, _yScale; qreal _xScale, _yScale;
QString _xUnits, _yUnits; QString _xUnits, _yUnits;
QString _xLabel, _yLabel; QString _xLabel, _yLabel;
int _precision;
private slots: private slots:
void emitSliderPositionChanged(const QPointF &pos); void emitSliderPositionChanged(const QPointF &pos);
@ -70,13 +73,14 @@ private:
void resize(const QSizeF &size); void resize(const QSizeF &size);
Scene *_scene; Scene *_scene;
AxisItem *_xAxis, *_yAxis; AxisItem *_xAxis, *_yAxis;
SliderItem *_slider; SliderItem *_slider;
SliderInfoItem *_sliderInfo;
InfoItem *_info; InfoItem *_info;
QList<QGraphicsPathItem*> _graphs; QList<QGraphicsPathItem*> _graphs;
qreal _xMin, _xMax, _yMin, _yMax; qreal _xMin, _xMax, _yMin, _yMax;
ColorShop _colorShop; ColorShop _colorShop;
}; };

View File

@ -1,12 +1,12 @@
#include <QFont> #include <QFont>
#include <QPainter> #include <QPainter>
#include "config.h"
#include "infoitem.h" #include "infoitem.h"
#define FONT_FAMILY "Arial"
#define FONT_SIZE 12
#define PADDING 10 #define PADDING 10
InfoItem::InfoItem() InfoItem::InfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{ {
} }
@ -73,4 +73,6 @@ void InfoItem::insert(const QString &key, const QString &value)
_list.append(kv); _list.append(kv);
else else
_list[i] = kv; _list[i] = kv;
prepareGeometryChange();
} }

View File

@ -7,7 +7,7 @@
class InfoItem : public QGraphicsItem class InfoItem : public QGraphicsItem
{ {
public: public:
InfoItem(); InfoItem(QGraphicsItem *parent = 0);
QRectF boundingRect() const; QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,

View File

@ -3,7 +3,7 @@
#define SIZE 8 #define SIZE 8
MarkerItem::MarkerItem() MarkerItem::MarkerItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{ {
} }

View File

@ -6,7 +6,7 @@
class MarkerItem : public QGraphicsItem class MarkerItem : public QGraphicsItem
{ {
public: public:
MarkerItem(); MarkerItem(QGraphicsItem *parent = 0);
QRectF boundingRect() const; QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,

View File

@ -1,14 +1,14 @@
#include <QPainter> #include <QPainter>
#include "config.h"
#include "poiitem.h" #include "poiitem.h"
#include <QDebug> #include <QDebug>
#define FONT_FAMILY "Arial"
#define FONT_SIZE 12
#define POINT_SIZE 8 #define POINT_SIZE 8
POIItem::POIItem(const QString &text) POIItem::POIItem(const QString &text, QGraphicsItem *parent)
: QGraphicsItem(parent)
{ {
_text = text; _text = text;
updateBoundingRect(); updateBoundingRect();

View File

@ -6,7 +6,7 @@
class POIItem : public QGraphicsItem class POIItem : public QGraphicsItem
{ {
public: public:
POIItem(const QString &text); POIItem(const QString &text, QGraphicsItem *parent = 0);
QRectF boundingRect() const {return _boundingRect;} QRectF boundingRect() const {return _boundingRect;}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,

46
src/sliderinfoitem.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <QPainter>
#include "config.h"
#include "sliderinfoitem.h"
#define SIZE 5
SliderInfoItem::SliderInfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{
}
void SliderInfoItem::updateBoundingRect()
{
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
_boundingRect = QRectF(-SIZE/2, -SIZE/2, fm.width(_text) + SIZE, fm.height());
}
void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
*option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
painter->setFont(font);
painter->setPen(Qt::red);
painter->drawText(SIZE, fm.height() - fm.descent(), _text);
painter->fillRect(QRect(-SIZE/2, -SIZE/2, SIZE, SIZE), Qt::red);
// painter->drawRect(boundingRect());
}
void SliderInfoItem::setText(const QString &text)
{
_text = text;
updateBoundingRect();
prepareGeometryChange();
}

24
src/sliderinfoitem.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef SLIDERINFOITEM_H
#define SLIDERINFOITEM_H
#include <QGraphicsItem>
class SliderInfoItem : public QGraphicsItem
{
public:
SliderInfoItem(QGraphicsItem *parent = 0);
QRectF boundingRect() const {return _boundingRect;}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
void setText(const QString &text);
private:
void updateBoundingRect();
QString _text;
QRectF _boundingRect;
};
#endif // SLIDERINFOITEM_H

View File

@ -3,9 +3,9 @@
#include <QDebug> #include <QDebug>
#define SIZE 10 #define SIZE 10
SliderItem::SliderItem() SliderItem::SliderItem(QGraphicsObject *parent) : QGraphicsObject(parent)
{ {
setFlag(ItemIsMovable); setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges); setFlag(ItemSendsGeometryChanges);

View File

@ -8,7 +8,7 @@ class SliderItem : public QGraphicsObject
Q_OBJECT Q_OBJECT
public: public:
SliderItem(); SliderItem(QGraphicsObject *parent = 0);
QRectF boundingRect() const; QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,

View File

@ -2,7 +2,7 @@
#include <QDebug> #include <QDebug>
SpeedGraph::SpeedGraph() SpeedGraph::SpeedGraph(QWidget *parent) : Graph(parent)
{ {
_max = 0; _max = 0;
@ -12,6 +12,7 @@ SpeedGraph::SpeedGraph()
Graph::setYUnits(tr("km/h")); Graph::setYUnits(tr("km/h"));
Graph::setXScale(0.001); Graph::setXScale(0.001);
Graph::setYScale(3.6); Graph::setYScale(3.6);
Graph::setPrecision(1);
} }
void SpeedGraph::loadData(const QVector<QPointF> &data, qreal time) void SpeedGraph::loadData(const QVector<QPointF> &data, qreal time)

View File

@ -9,7 +9,7 @@ class SpeedGraph : public Graph
Q_OBJECT Q_OBJECT
public: public:
SpeedGraph(); SpeedGraph(QWidget *parent = 0);
void loadData(const QVector<QPointF> &data, qreal time); void loadData(const QVector<QPointF> &data, qreal time);
void clear(); void clear();