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

Optimization & code cleanup

This commit is contained in:
Martin Tůma 2017-10-04 23:15:39 +02:00
parent 69f9d05ccb
commit 3155e8436b
33 changed files with 141 additions and 105 deletions

View File

@ -34,7 +34,7 @@ void CadenceGraph::loadData(const Data &data, const QList<PathItem *> &paths)
continue;
}
CadenceGraphItem *gi = new CadenceGraphItem(graph);
CadenceGraphItem *gi = new CadenceGraphItem(graph, _graphType);
GraphView::addGraph(gi, paths.at(i));
_avg.append(QPointF(data.tracks().at(i)->distance(), gi->avg()));

View File

@ -1,8 +1,8 @@
#include "tooltip.h"
#include "cadencegraphitem.h"
CadenceGraphItem::CadenceGraphItem(const Graph &graph, QGraphicsItem *parent)
: GraphItem(graph, parent)
CadenceGraphItem::CadenceGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
qreal sum = 0;

View File

@ -8,7 +8,8 @@ class CadenceGraphItem : public GraphItem
Q_OBJECT
public:
CadenceGraphItem(const Graph &graph, QGraphicsItem *parent = 0);
CadenceGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal max() const {return -bounds().top();}
qreal avg() const {return _avg;}

View File

@ -44,7 +44,7 @@ ElevationGraph::ElevationGraph(QWidget *parent) : GraphTab(parent)
_showRoutes = true;
_showTracks = true;
setYUnits();
setYUnits(Metric);
setYLabel(tr("Elevation"));
setMinYRange(50.0);
}
@ -72,7 +72,7 @@ void ElevationGraph::loadGraph(const Graph &graph, Type type, PathItem *path)
return;
}
ElevationGraphItem *gi = new ElevationGraphItem(graph);
ElevationGraphItem *gi = new ElevationGraphItem(graph, _graphType);
GraphView::addGraph(gi, path, type);
if (type == Track) {
@ -116,9 +116,9 @@ void ElevationGraph::clear()
GraphView::clear();
}
void ElevationGraph::setYUnits()
void ElevationGraph::setYUnits(Units units)
{
if (_units == Metric) {
if (units == Metric) {
GraphView::setYUnits(tr("m"));
setYScale(1);
} else {
@ -127,14 +127,12 @@ void ElevationGraph::setYUnits()
}
}
void ElevationGraph::setUnits(enum Units units)
void ElevationGraph::setUnits(Units units)
{
GraphView::setUnits(units);
setYUnits();
setYUnits(units);
setInfo();
redraw();
GraphView::setUnits(units);
}
void ElevationGraph::showTracks(bool show)

View File

@ -25,7 +25,7 @@ private:
qreal ascent() const;
qreal descent() const;
void setYUnits();
void setYUnits(Units units);
void setInfo();
void loadGraph(const Graph &graph, Type type, PathItem *path);

View File

@ -1,8 +1,8 @@
#include "tooltip.h"
#include "elevationgraphitem.h"
ElevationGraphItem::ElevationGraphItem(const Graph &graph, QGraphicsItem *parent)
: GraphItem(graph, parent)
ElevationGraphItem::ElevationGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
_ascent = _descent = 0;

View File

@ -8,7 +8,8 @@ class ElevationGraphItem : public GraphItem
Q_OBJECT
public:
ElevationGraphItem(const Graph &graph, QGraphicsItem *parent = 0);
ElevationGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal ascent() const {return _ascent;}
qreal descent() const {return _descent;}

View File

@ -2,7 +2,7 @@
#include "graphitem.h"
GraphItem::GraphItem(const Graph &graph, QGraphicsItem *parent)
GraphItem::GraphItem(const Graph &graph, GraphType type, QGraphicsItem *parent)
: QGraphicsObject(parent)
{
_id = 0;
@ -10,7 +10,7 @@ GraphItem::GraphItem(const Graph &graph, QGraphicsItem *parent)
_pen = QPen(Qt::black, _width);
_type = Distance;
_type = type;
_graph = graph;
_sx = 1.0; _sy = 1.0;
@ -56,6 +56,9 @@ void GraphItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
void GraphItem::setGraphType(GraphType type)
{
if (type == _type)
return;
prepareGeometryChange();
_type = type;
@ -66,12 +69,18 @@ void GraphItem::setGraphType(GraphType type)
void GraphItem::setColor(const QColor &color)
{
if (_pen.color() == color)
return;
_pen.setColor(color);
update();
}
void GraphItem::setWidth(int width)
{
if (width == _width)
return;
prepareGeometryChange();
_width = width;

View File

@ -11,7 +11,7 @@ class GraphItem : public QGraphicsObject
Q_OBJECT
public:
GraphItem(const Graph &graph, QGraphicsItem *parent = 0);
GraphItem(const Graph &graph, GraphType type, QGraphicsItem *parent = 0);
QPainterPath shape() const {return _shape;}
QRectF boundingRect() const {return _shape.boundingRect();}
@ -19,18 +19,19 @@ public:
QWidget *widget);
const QRectF &bounds() const {return _bounds;}
void setScale(qreal sx, qreal sy);
void setScale(qreal sx, qreal sy);
void setGraphType(GraphType type);
int id() const {return _id;}
void setId(int id) {_id = id;}
void setColor(const QColor &color);
void setWidth(int width);
virtual void setUnits(Units units) {Q_UNUSED(units);}
qreal yAtX(qreal x);
qreal distanceAtTime(qreal time);
virtual void setUnits(Units units) {Q_UNUSED(units);}
void redraw();
signals:
void sliderPositionChanged(qreal);

View File

@ -19,8 +19,9 @@ public:
virtual QString label() const = 0;
virtual void loadData(const Data &data, const QList<PathItem *> &paths) = 0;
virtual void clear() {}
virtual void clear() {GraphView::clear();}
virtual void setUnits(enum Units units) {GraphView::setUnits(units);}
virtual void setGraphType(GraphType type) {GraphView::setGraphType(type);}
virtual void setTimeType(enum TimeType type) {Q_UNUSED(type)}
virtual void showTracks(bool show) {Q_UNUSED(show)}
virtual void showRoutes(bool show) {Q_UNUSED(show)}

View File

@ -145,6 +145,8 @@ void GraphView::setUnits(Units units)
_graphs.at(i)->setUnits(units);
setXUnits();
redraw();
}
void GraphView::setGraphType(GraphType type)
@ -180,7 +182,6 @@ void GraphView::showSliderInfo(bool show)
void GraphView::addGraph(GraphItem *graph, PathItem *path, int id)
{
graph->setUnits(_units);
graph->setGraphType(_graphType);
graph->setId(id);
graph->setColor(_palette.nextColor());
graph->setWidth(_width);
@ -235,11 +236,6 @@ void GraphView::showGraph(bool show, int id)
}
}
void GraphView::redraw()
{
redraw(viewport()->size() - QSizeF(MARGIN, MARGIN));
}
QRectF GraphView::bounds() const
{
QRectF br(_bounds);
@ -247,6 +243,11 @@ QRectF GraphView::bounds() const
return br;
}
void GraphView::redraw()
{
redraw(viewport()->size() - QSizeF(MARGIN, MARGIN));
}
void GraphView::redraw(const QSizeF &size)
{
QRectF r;
@ -467,6 +468,8 @@ void GraphView::setGraphWidth(int width)
for (int i = 0; i < _graphs.count(); i++)
_graphs.at(i)->setWidth(width);
redraw();
}
void GraphView::useOpenGL(bool use)

View File

@ -25,19 +25,29 @@ public:
GraphView(QWidget *parent = 0);
~GraphView();
void addGraph(GraphItem *graph, PathItem *path, int id = 0);
int count() const {return _graphs.count();}
void redraw();
bool isEmpty() const {return _graphs.isEmpty();}
void clear();
void plot(QPainter *painter, const QRectF &target, qreal scale);
void setPalette(const Palette &palette);
void setGraphWidth(int width);
void showGrid(bool show);
void showSliderInfo(bool show);
void useOpenGL(bool use);
void useAntiAliasing(bool use);
void setSliderPosition(qreal pos);
signals:
void sliderPositionChanged(qreal);
protected:
void addGraph(GraphItem *graph, PathItem *path, int id = 0);
void showGraph(bool show, int id = 0);
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);
const QString &yLabel() const {return _yLabel;}
const QString &yUnits() const {return _yUnits;}
@ -51,26 +61,14 @@ public:
void setSliderPrecision(int precision) {_precision = precision;}
void setMinYRange(qreal range) {_minYRange = range;}
qreal sliderPosition() const {return _sliderPos;}
void setSliderPosition(qreal pos);
void plot(QPainter *painter, const QRectF &target, qreal scale);
void useOpenGL(bool use);
void useAntiAliasing(bool use);
signals:
void sliderPositionChanged(qreal);
protected:
QRectF bounds() const;
void redraw();
void redraw(const QSizeF &size);
void addInfo(const QString &key, const QString &value);
void clearInfo();
void skipColor() {_palette.nextColor();}
QList<GraphItem*> _graphs;
Units _units;
GraphType _graphType;
private slots:
@ -89,6 +87,7 @@ private:
void resizeEvent(QResizeEvent *);
void mousePressEvent(QMouseEvent *);
Units _units;
qreal _xScale, _yScale;
qreal _yOffset;
QString _xUnits, _yUnits;

View File

@ -361,7 +361,7 @@ void GUI::createActions()
tr("Load map..."), this);
connect(_loadMapAction, SIGNAL(triggered()), this, SLOT(loadMap()));
_clearMapCacheAction = new QAction(tr("Clear tile cache"), this);
connect(_clearMapCacheAction, SIGNAL(triggered()), this,
connect(_clearMapCacheAction, SIGNAL(triggered()), _pathView,
SLOT(clearMapCache()));
createMapActions();
_nextMapAction = new QAction(tr("Next map"), this);
@ -1022,7 +1022,7 @@ void GUI::plot(QPrinter *printer)
int cnt = 0;
for (int i = 0; i < _tabs.size(); i++)
if (_tabs.at(i)->count())
if (!_tabs.at(i)->isEmpty())
cnt++;
qreal sp = ratio * 20;
@ -1031,7 +1031,7 @@ void GUI::plot(QPrinter *printer)
qreal y = 0;
for (int i = 0; i < _tabs.size(); i++) {
if (_tabs.at(i)->count()) {
if (!_tabs.at(i)->isEmpty()) {
_tabs.at(i)->plot(&p, QRectF(0, y, printer->width(), gh),
ratio);
y += gh + sp;
@ -1217,12 +1217,6 @@ void GUI::loadMap()
}
}
void GUI::clearMapCache()
{
_map->clearCache();
_pathView->redraw();
}
void GUI::updateStatusBarInfo()
{
if (_files.count() == 0)
@ -1334,13 +1328,13 @@ void GUI::updateGraphTabs()
for (int i = 0; i < _tabs.size(); i++) {
tab = _tabs.at(i);
if (!tab->count() && (index = _graphTabWidget->indexOf(tab)) >= 0)
if (tab->isEmpty() && (index = _graphTabWidget->indexOf(tab)) >= 0)
_graphTabWidget->removeTab(index);
}
for (int i = 0; i < _tabs.size(); i++) {
tab = _tabs.at(i);
if (tab->count() && _graphTabWidget->indexOf(tab) < 0)
if (!tab->isEmpty() && _graphTabWidget->indexOf(tab) < 0)
_graphTabWidget->insertTab(i, tab, _tabs.at(i)->label());
}

View File

@ -57,7 +57,6 @@ private slots:
void showTracks(bool show);
void showRoutes(bool show);
void loadMap();
void clearMapCache();
void nextMap();
void prevMap();
void openOptions();

View File

@ -34,7 +34,7 @@ void HeartRateGraph::loadData(const Data &data, const QList<PathItem *> &paths)
continue;
}
HeartRateGraphItem *gi = new HeartRateGraphItem(graph);
HeartRateGraphItem *gi = new HeartRateGraphItem(graph, _graphType);
GraphView::addGraph(gi, paths.at(i));
_avg.append(QPointF(data.tracks().at(i)->distance(), gi->avg()));

View File

@ -1,8 +1,8 @@
#include "tooltip.h"
#include "heartrategraphitem.h"
HeartRateGraphItem::HeartRateGraphItem(const Graph &graph, QGraphicsItem *parent)
: GraphItem(graph, parent)
HeartRateGraphItem::HeartRateGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
qreal sum = 0;

View File

@ -8,7 +8,8 @@ class HeartRateGraphItem : public GraphItem
Q_OBJECT
public:
HeartRateGraphItem(const Graph &graph, QGraphicsItem *parent = 0);
HeartRateGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal max() const {return -bounds().top();}
qreal avg() const {return _avg;}

View File

@ -56,13 +56,21 @@ OnlineMap::OnlineMap(const QString &name, const QString &url,
_zooms = zooms;
_zoom = zooms.max();
connect(downloader, SIGNAL(finished()), this, SLOT(emitLoaded()));
QString path = TILES_DIR + QString("/") + name;
if (!QDir().mkpath(path))
qWarning("Error creating tiles dir: %s\n", qPrintable(path));
}
void OnlineMap::load()
{
connect(downloader, SIGNAL(finished()), this, SLOT(emitLoaded()));
}
void OnlineMap::unload()
{
disconnect(downloader, SIGNAL(finished()), this, SLOT(emitLoaded()));
}
void OnlineMap::fillTile(Tile &tile)
{
tile.pixmap() = QPixmap(TILE_SIZE, TILE_SIZE);

View File

@ -37,6 +37,9 @@ public:
static void setDownloader(Downloader *downloader)
{OnlineMap::downloader = downloader;}
void load();
void unload();
private slots:
void emitLoaded();

View File

@ -66,9 +66,10 @@ void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
void PathItem::setMap(Map *map)
{
_map = map;
prepareGeometryChange();
_map = map;
updatePainterPath(map);
updateShape();
@ -77,12 +78,18 @@ void PathItem::setMap(Map *map)
void PathItem::setColor(const QColor &color)
{
if (_pen.color() == color)
return;
_pen.setColor(color);
update();
}
void PathItem::setWidth(qreal width)
{
if (_width == width)
return;
prepareGeometryChange();
_width = width;
@ -93,12 +100,18 @@ void PathItem::setWidth(qreal width)
void PathItem::setStyle(Qt::PenStyle style)
{
if (_pen.style() == style)
return;
_pen.setStyle(style);
update();
}
void PathItem::setDigitalZoom(int zoom)
{
if (_digitalZoom == zoom)
return;
prepareGeometryChange();
_digitalZoom = zoom;

View File

@ -44,7 +44,7 @@ PathView::PathView(Map *map, POI *poi, QWidget *parent)
_map = map;
_poi = poi;
connect(_map, SIGNAL(loaded()), this, SLOT(redraw()));
connect(_map, SIGNAL(loaded()), this, SLOT(reloadMap()));
connect(_poi, SIGNAL(pointsChanged()), this, SLOT(updatePOI()));
_units = Metric;
@ -269,11 +269,11 @@ void PathView::setMap(Map *map)
qreal resolution = _map->resolution(pos);
_map->unload();
disconnect(_map, SIGNAL(loaded()), this, SLOT(redraw()));
disconnect(_map, SIGNAL(loaded()), this, SLOT(reloadMap()));
_map = map;
_map->load();
connect(_map, SIGNAL(loaded()), this, SLOT(redraw()));
connect(_map, SIGNAL(loaded()), this, SLOT(reloadMap()));
resetDigitalZoom();
@ -366,8 +366,9 @@ void PathView::setUnits(enum Units units)
it.value()->setUnits(units);
}
void PathView::redraw()
void PathView::clearMapCache()
{
_map->clearCache();
resetCachedContent();
}
@ -795,3 +796,8 @@ void PathView::useAntiAliasing(bool use)
{
setRenderHint(QPainter::Antialiasing, use);
}
void PathView::reloadMap()
{
resetCachedContent();
}

View File

@ -58,8 +58,6 @@ public:
void useAntiAliasing(bool use);
public slots:
void redraw();
void showMap(bool show);
void showPOI(bool show);
void setPOIOverlap(bool overlap);
@ -69,9 +67,11 @@ public slots:
void showRoutes(bool show);
void showWaypoints(bool show);
void showRouteWaypoints(bool show);
void clearMapCache();
private slots:
void updatePOI();
void reloadMap();
private:
PathItem *addTrack(const Track &track);

View File

@ -34,7 +34,7 @@ void PowerGraph::loadData(const Data &data, const QList<PathItem *> &paths)
continue;
}
PowerGraphItem *gi = new PowerGraphItem(graph);
PowerGraphItem *gi = new PowerGraphItem(graph, _graphType);
GraphView::addGraph(gi, paths.at(i));
_avg.append(QPointF(data.tracks().at(i)->distance(), gi->avg()));

View File

@ -1,8 +1,8 @@
#include "tooltip.h"
#include "powergraphitem.h"
PowerGraphItem::PowerGraphItem(const Graph &graph, QGraphicsItem *parent)
: GraphItem(graph, parent)
PowerGraphItem::PowerGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
qreal sum = 0;

View File

@ -8,7 +8,8 @@ class PowerGraphItem : public GraphItem
Q_OBJECT
public:
PowerGraphItem(const Graph &graph, QGraphicsItem *parent = 0);
PowerGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal max() const {return -bounds().top();}
qreal avg() const {return _avg;}

View File

@ -10,7 +10,7 @@ SpeedGraph::SpeedGraph(QWidget *parent) : GraphTab(parent)
_timeType = Total;
_showTracks = true;
setYUnits();
setYUnits(Metric);
setYLabel(tr("Speed"));
setSliderPrecision(1);
@ -38,7 +38,8 @@ void SpeedGraph::loadData(const Data &data, const QList<PathItem *> &paths)
continue;
}
SpeedGraphItem *gi = new SpeedGraphItem(graph, track->movingTime());
SpeedGraphItem *gi = new SpeedGraphItem(graph, _graphType,
track->movingTime());
gi->setTimeType(_timeType);
GraphView::addGraph(gi, paths.at(i));
@ -76,9 +77,9 @@ void SpeedGraph::clear()
GraphView::clear();
}
void SpeedGraph::setYUnits()
void SpeedGraph::setYUnits(Units units)
{
if (_units == Metric) {
if (units == Metric) {
GraphView::setYUnits(tr("km/h"));
setYScale(MS2KMH);
} else {
@ -87,14 +88,12 @@ void SpeedGraph::setYUnits()
}
}
void SpeedGraph::setUnits(enum Units units)
void SpeedGraph::setUnits(Units units)
{
GraphView::setUnits(units);
setYUnits();
setYUnits(units);
setInfo();
redraw();
GraphView::setUnits(units);
}
void SpeedGraph::setTimeType(enum TimeType type)

View File

@ -21,7 +21,7 @@ public:
private:
qreal avg() const;
qreal max() const {return bounds().bottom();}
void setYUnits();
void setYUnits(Units units);
void setInfo();
QList<QPointF> _avg;

View File

@ -1,8 +1,8 @@
#include "tooltip.h"
#include "speedgraphitem.h"
SpeedGraphItem::SpeedGraphItem(const Graph &graph, qreal movingTime,
QGraphicsItem *parent) : GraphItem(graph, parent)
SpeedGraphItem::SpeedGraphItem(const Graph &graph, GraphType type,
qreal movingTime, QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
_units = Metric;
_timeType = Total;

View File

@ -9,7 +9,7 @@ class SpeedGraphItem : public GraphItem
Q_OBJECT
public:
SpeedGraphItem(const Graph &graph, qreal movingTime,
SpeedGraphItem(const Graph &graph, GraphType type, qreal movingTime,
QGraphicsItem *parent = 0);
qreal max() const {return -bounds().top();}

View File

@ -7,7 +7,7 @@ TemperatureGraph::TemperatureGraph(QWidget *parent) : GraphTab(parent)
{
_showTracks = true;
setYUnits();
setYUnits(Metric);
setYLabel(tr("Temperature"));
setSliderPrecision(1);
@ -36,7 +36,7 @@ void TemperatureGraph::loadData(const Data &data, const QList<PathItem *> &paths
continue;
}
TemperatureGraphItem *gi = new TemperatureGraphItem(graph);
TemperatureGraphItem *gi = new TemperatureGraphItem(graph, _graphType);
GraphView::addGraph(gi, paths.at(i));
_avg.append(QPointF(data.tracks().at(i)->distance(), gi->avg()));
@ -70,9 +70,9 @@ void TemperatureGraph::clear()
GraphView::clear();
}
void TemperatureGraph::setYUnits()
void TemperatureGraph::setYUnits(Units units)
{
if (_units == Metric) {
if (units == Metric) {
GraphView::setYUnits(QChar(0x00B0) + tr("C"));
setYScale(1);
setYOffset(0);
@ -83,14 +83,12 @@ void TemperatureGraph::setYUnits()
}
}
void TemperatureGraph::setUnits(enum Units units)
void TemperatureGraph::setUnits(Units units)
{
GraphView::setUnits(units);
setYUnits();
setYUnits(units);
setInfo();
redraw();
GraphView::setUnits(units);
}
void TemperatureGraph::showTracks(bool show)

View File

@ -20,7 +20,7 @@ private:
qreal avg() const;
qreal min() const {return bounds().top();}
qreal max() const {return bounds().bottom();}
void setYUnits();
void setYUnits(Units units);
void setInfo();
QList<QPointF> _avg;

View File

@ -1,8 +1,8 @@
#include "tooltip.h"
#include "temperaturegraphitem.h"
TemperatureGraphItem::TemperatureGraphItem(const Graph &graph,
QGraphicsItem *parent) : GraphItem(graph, parent)
TemperatureGraphItem::TemperatureGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent) : GraphItem(graph, type, parent)
{
qreal sum = 0;

View File

@ -8,7 +8,8 @@ class TemperatureGraphItem : public GraphItem
Q_OBJECT
public:
TemperatureGraphItem(const Graph &graph, QGraphicsItem *parent = 0);
TemperatureGraphItem(const Graph &graph, GraphType type,
QGraphicsItem *parent = 0);
qreal max() const {return -bounds().top();}
qreal min() const {return -bounds().bottom();}