1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-06-27 11:39:16 +02:00

Added graph grid option

Code cleanup
This commit is contained in:
2016-10-17 23:14:07 +02:00
parent a6b2a477a1
commit 1160c6d385
15 changed files with 265 additions and 142 deletions

View File

@ -118,6 +118,7 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QPen pen = QPen(Qt::black, AXIS_WIDTH);
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setFont(font);
painter->setPen(pen);
@ -202,3 +203,16 @@ QSizeF AxisItem::margin() const
+ TICK/2, es.height()/2 + fm.descent());
}
}
QList<qreal> AxisItem::ticks() const
{
struct Label l;
QList<qreal> list;
l = label(_range.min(), _range.max(), (_type == X) ? XTICKS : YTICKS);
for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++)
list.append(((_size/_range.size()) * ((l.min + i * l.d)
- _range.min())));
return list;
}

View File

@ -20,6 +20,7 @@ public:
void setLabel(const QString& label);
QSizeF margin() const;
QList<qreal> ticks() const;
private:
void updateBoundingRect();

View File

@ -7,6 +7,7 @@
#include "slideritem.h"
#include "sliderinfoitem.h"
#include "infoitem.h"
#include "griditem.h"
#include "graph.h"
#include "graphitem.h"
#include "pathitem.h"
@ -43,6 +44,7 @@ GraphView::GraphView(QWidget *parent)
_sliderInfo = new SliderInfoItem(_slider);
_sliderInfo->setZValue(2.0);
_info = new InfoItem();
_grid = new GridItem();
connect(_slider, SIGNAL(positionChanged(const QPointF&)), this,
SLOT(emitSliderPositionChanged(const QPointF&)));
@ -60,9 +62,7 @@ GraphView::GraphView(QWidget *parent)
_units = Metric;
_graphType = Distance;
setGraphType(_graphType);
setUnits(_units);
_xLabel = tr("Distance");
}
GraphView::~GraphView()
@ -75,6 +75,8 @@ GraphView::~GraphView()
delete _slider;
if (_info->scene() != _scene)
delete _info;
if (_grid->scene() != _scene)
delete _grid;
for (int i = 0; i < _graphs.count(); i++)
if (_graphs.at(i)->scene() != _scene)
@ -165,6 +167,11 @@ void GraphView::setGraphType(GraphType type)
redraw();
}
void GraphView::showGrid(bool show)
{
_grid->setVisible(show);
}
void GraphView::loadGraph(const Graph &graph, PathItem *path, int id)
{
if (graph.size() < 2)
@ -249,6 +256,7 @@ void GraphView::redraw(const QSizeF &size)
removeItem(_yAxis);
removeItem(_slider);
removeItem(_info);
removeItem(_grid);
_scene->setSceneRect(QRectF());
return;
}
@ -257,6 +265,7 @@ void GraphView::redraw(const QSizeF &size)
addItem(_yAxis);
addItem(_slider);
addItem(_info);
addItem(_grid);
rx = RangeF(bounds().left() * _xScale, bounds().right() * _xScale);
ry = RangeF(bounds().top() * _yScale + _yOffset, bounds().bottom() * _yScale
@ -293,6 +302,10 @@ void GraphView::redraw(const QSizeF &size)
_xAxis->setPos(r.bottomLeft());
_yAxis->setPos(r.bottomLeft());
_grid->setSize(r.size());
_grid->setTicks(_xAxis->ticks(), _yAxis->ticks());
_grid->setPos(r.bottomLeft());
_slider->setArea(r);
updateSliderPosition();

View File

@ -18,6 +18,7 @@ class SliderInfoItem;
class InfoItem;
class GraphItem;
class PathItem;
class GridItem;
class Scene : public QGraphicsScene
{
@ -47,6 +48,7 @@ public:
void showGraph(bool show, int id = 0);
void setGraphType(GraphType type);
void setUnits(Units units);
void showGrid(bool show);
const QString &yLabel() const {return _yLabel;}
const QString &yUnits() const {return _yUnits;}
@ -103,6 +105,7 @@ private:
SliderItem *_slider;
SliderInfoItem *_sliderInfo;
InfoItem *_info;
GridItem *_grid;
QList<GraphItem*> _graphs;
QList<GraphItem*> _visible;

48
src/griditem.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <QPainter>
#include "griditem.h"
#define GRID_WIDTH 1
GridItem::GridItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{
}
void GridItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QBrush brush(Qt::gray);
QPen pen = QPen(brush, GRID_WIDTH, Qt::DotLine);
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(pen);
for (int i = 0; i < _xTicks.size(); i++)
painter->drawLine(_xTicks.at(i), 0, _xTicks.at(i),
-_boundingRect.height());
for (int i = 0; i < _yTicks.size(); i++)
painter->drawLine(0, -_yTicks.at(i), boundingRect().width(),
-_yTicks.at(i));
/*
painter->setPen(Qt::red);
painter->drawRect(boundingRect());
*/
}
void GridItem::setTicks(const QList<qreal> &x, const QList<qreal> &y)
{
_xTicks = x; _yTicks = y;
update();
}
void GridItem::setSize(const QSizeF &size)
{
prepareGeometryChange();
_boundingRect = QRectF(QPointF(0, -size.height()), size);
}

23
src/griditem.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef GRIDITEM_H
#define GRIDITEM_H
#include <QGraphicsItem>
class GridItem : public QGraphicsItem
{
public:
GridItem(QGraphicsItem *parent = 0);
QRectF boundingRect() const {return _boundingRect;}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
void setTicks(const QList<qreal> &x, const QList<qreal> &y);
void setSize(const QSizeF &size);
private:
QRectF _boundingRect;
QList<qreal> _xTicks, _yTicks;
};
#endif // GRIDITEM_H

View File

@ -322,12 +322,18 @@ void GUI::createActions()
_distanceGraphAction->setShortcut(DISTANCE_GRAPH_SHORTCUT);
connect(_distanceGraphAction, SIGNAL(triggered()), this,
SLOT(setDistanceGraph()));
addAction(_distanceGraphAction);
_timeGraphAction = new QAction(tr("Time"), this);
_timeGraphAction->setCheckable(true);
_timeGraphAction->setActionGroup(ag);
_timeGraphAction->setShortcut(TIME_GRAPH_SHORTCUT);
connect(_timeGraphAction, SIGNAL(triggered()), this,
SLOT(setTimeGraph()));
addAction(_timeGraphAction);
_showGraphGridAction = new QAction(tr("Show grid"), this);
_showGraphGridAction->setCheckable(true);
connect(_showGraphGridAction, SIGNAL(triggered(bool)), this,
SLOT(showGraphGrids(bool)));
// Settings actions
_showToolbarsAction = new QAction(tr("Show toolbars"), this);
@ -398,6 +404,8 @@ void GUI::createMenus()
graphMenu->addAction(_distanceGraphAction);
graphMenu->addAction(_timeGraphAction);
graphMenu->addSeparator();
graphMenu->addAction(_showGraphGridAction);
graphMenu->addSeparator();
graphMenu->addAction(_showGraphsAction);
QMenu *poiMenu = menuBar()->addMenu(tr("POI"));
@ -858,22 +866,22 @@ void GUI::closeAll()
updateTrackView();
}
void GUI::showMap(bool checked)
void GUI::showMap(bool show)
{
if (checked)
if (show)
_pathView->setMap(_currentMap);
else
_pathView->setMap(0);
}
void GUI::showGraphs(bool checked)
void GUI::showGraphs(bool show)
{
_graphTabWidget->setHidden(!checked);
_graphTabWidget->setHidden(!show);
}
void GUI::showToolbars(bool checked)
void GUI::showToolbars(bool show)
{
if (checked) {
if (show) {
addToolBar(_fileToolBar);
addToolBar(_showToolBar);
addToolBar(_navigationToolBar);
@ -887,9 +895,9 @@ void GUI::showToolbars(bool checked)
}
}
void GUI::showFullscreen(bool checked)
void GUI::showFullscreen(bool show)
{
if (checked) {
if (show) {
_frameStyle = _pathView->frameStyle();
_showGraphs = _showGraphsAction->isChecked();
@ -935,6 +943,12 @@ void GUI::showRoutes(bool show)
updateStatusBarInfo();
}
void GUI::showGraphGrids(bool show)
{
for (int i = 0; i < _tabs.size(); i++)
_tabs.at(i)->showGrid(show);
}
void GUI::clearMapCache()
{
_currentMap->clearCache();
@ -1215,6 +1229,8 @@ void GUI::writeSettings()
settings.setValue(SHOW_GRAPHS_SETTING, _showGraphsAction->isChecked());
settings.setValue(GRAPH_TYPE_SETTING, _timeGraphAction->isChecked()
? Time : Distance);
settings.setValue(SHOW_GRAPH_GRIDS_SETTING,
_showGraphGridAction->isChecked());
settings.endGroup();
settings.beginGroup(POI_SETTINGS_GROUP);
@ -1291,6 +1307,10 @@ void GUI::readSettings()
_timeGraphAction->setChecked(true);
} else
_distanceGraphAction->setChecked(true);
if (settings.value(SHOW_GRAPH_GRIDS_SETTING, true).toBool() == false)
showGraphGrids(false);
else
_showGraphGridAction->setChecked(true);
settings.endGroup();
settings.beginGroup(POI_SETTINGS_GROUP);

View File

@ -45,10 +45,11 @@ private slots:
void reloadFile();
void openPOIFile();
void closePOIFiles();
void showMap(bool checked);
void showGraphs(bool checked);
void showToolbars(bool checked);
void showFullscreen(bool checked);
void showMap(bool show);
void showGraphs(bool show);
void showGraphGrids(bool show);
void showToolbars(bool show);
void showFullscreen(bool show);
void showTracks(bool show);
void showRoutes(bool show);
void clearMapCache();
@ -138,6 +139,7 @@ private:
QAction *_fullscreenAction;
QAction *_clearMapCacheAction;
QAction *_showGraphsAction;
QAction *_showGraphGridAction;
QAction *_distanceGraphAction;
QAction *_timeGraphAction;
QAction *_showToolbarsAction;

View File

@ -40,6 +40,9 @@ void InfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QList<KV>::const_iterator i;
int width = 0;
painter->setRenderHint(QPainter::Antialiasing, false);
for (i = _list.constBegin(); i != _list.constEnd(); i++) {
painter->drawText(width, fm.height() - fm.descent(), i->key + ": ");
width += fm.width(i->key + ": ");

View File

@ -19,18 +19,11 @@ void MarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
{
Q_UNUSED(option);
Q_UNUSED(widget);
bool aa;
if ((aa = painter->testRenderHint(QPainter::Antialiasing)))
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(Qt::red);
painter->drawLine(-SIZE/2, 0, SIZE/2, 0);
painter->drawLine(0, -SIZE/2, 0, SIZE/2);
if (aa)
painter->setRenderHint(QPainter::Antialiasing, true);
// painter->drawRect(boundingRect());
}

View File

@ -52,12 +52,9 @@ void ScaleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QFontMetrics fm(font);
QRect br;
QPen pen = QPen(Qt::black, BORDER_WIDTH);
bool aa;
if ((aa = painter->testRenderHint(QPainter::Antialiasing)))
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setFont(font);
painter->setPen(pen);
@ -79,9 +76,6 @@ void ScaleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
painter->setPen(Qt::red);
painter->drawRect(boundingRect());
*/
if (aa)
painter->setRenderHint(QPainter::Antialiasing, true);
}
QString ScaleItem::units() const

View File

@ -12,6 +12,7 @@
#define GRAPH_SETTINGS_GROUP "Graph"
#define SHOW_GRAPHS_SETTING "show"
#define GRAPH_TYPE_SETTING "type"
#define SHOW_GRAPH_GRIDS_SETTING "grid"
#define MAP_SETTINGS_GROUP "Map"
#define CURRENT_MAP_SETTING "map"

View File

@ -21,6 +21,7 @@ void SliderItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(Qt::red);
painter->drawLine(0, 0, 0, -_area.height());