From 1160c6d3858e4d0796c6038bd24d913d5d8ba399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 17 Oct 2016 23:14:07 +0200 Subject: [PATCH] Added graph grid option Code cleanup --- gpxsee.pro | 6 +- lang/gpxsee_cs.ts | 225 +++++++++++++++++++++++---------------------- src/axisitem.cpp | 14 +++ src/axisitem.h | 1 + src/graphview.cpp | 19 +++- src/graphview.h | 3 + src/griditem.cpp | 48 ++++++++++ src/griditem.h | 23 +++++ src/gui.cpp | 36 ++++++-- src/gui.h | 10 +- src/infoitem.cpp | 3 + src/markeritem.cpp | 9 +- src/scaleitem.cpp | 8 +- src/settings.h | 1 + src/slideritem.cpp | 1 + 15 files changed, 265 insertions(+), 142 deletions(-) create mode 100644 src/griditem.cpp create mode 100644 src/griditem.h diff --git a/gpxsee.pro b/gpxsee.pro index 288f38a4..a17e56a9 100644 --- a/gpxsee.pro +++ b/gpxsee.pro @@ -52,7 +52,8 @@ HEADERS += src/config.h \ src/graphitem.h \ src/graph.h \ src/pathitem.h \ - src/pathview.h + src/pathview.h \ + src/griditem.h SOURCES += src/main.cpp \ src/gui.cpp \ src/gpx.cpp \ @@ -91,7 +92,8 @@ SOURCES += src/main.cpp \ src/routeitem.cpp \ src/graphitem.cpp \ src/pathitem.cpp \ - src/pathview.cpp + src/pathview.cpp \ + src/griditem.cpp RESOURCES += gpxsee.qrc TRANSLATIONS = lang/gpxsee_cs.ts macx { diff --git a/lang/gpxsee_cs.ts b/lang/gpxsee_cs.ts index 33788f15..d2a20530 100644 --- a/lang/gpxsee_cs.ts +++ b/lang/gpxsee_cs.ts @@ -1,6 +1,6 @@ - + ElevationGraph @@ -43,7 +43,7 @@ ExportDialog - + Export to PDF Exportovat do PDF @@ -98,34 +98,34 @@ Soubor: - + Output file Výstupní soubor - + Export Exportovat - - - + + + Error Chyba - + No output file selected. Nebyl zvolen žádný výstupní soubor. - + %1 is a directory. %1 je adresář. - + %1 is not writable. %1 nelze zapsat. @@ -133,12 +133,11 @@ FileSelectWidget - Browse... - Procházet... + Procházet... - + Select file Vybrat soubor @@ -146,392 +145,397 @@ GUI - + GPXSee is distributed under the terms of the GNU General Public License version 3. For more info about GPXSee visit the project homepage at Program GPXSee je distribuován pod podmínkami licence GNU General Public License verze 3. Pro více informací navštivte stránky programu na adrese - + Open file Otevřít soubor - + Open POI file Otevřít POI soubor - + Open Otevřít - + Quit Ukončit - - - + + + Keyboard controls Ovládací klávesy - + Close Zavřít - + Reload Znovu načíst - + Show Zobrazit - - + + File Soubor - - - + + + Data sources Zdroje dat - + Load POI file Nahrát POI soubor - + Close POI files Zavřit POI soubory - + Overlap POIs Překrývat POI - + Show POI labels Zobrazovat názvy POI - + Show POIs Zobrazit POI - + Show map Zobrazit mapu - + Clear tile cache Vymazat mezipaměť dlaždic - - - + + + Next map Následující mapa - + Show tracks Zobrazit cesty - + Show routes Zobrazit trasy - + Show waypoints Zobrazit navigační body - + Waypoint labels Názvy navigačních bodů - + Show graphs Zobrazovat grafy - + + Show grid + Zobrazit mřížku + + + Show toolbars Zobrazovat nástrojové lišty - + Metric Metrické - + Imperial Imperiální - + Fullscreen mode Celoobrazovkový režim - + Next Následující - + Previous Předchozí - + Last Poslední - + First První - + Map Mapa - + Graph Graf - + POI POI - + POI files POI soubory - + Data Data - + Display Zobrazit - + Settings Nastavení - + Units Jednotky - + Help Nápověda - + Previous map Předchozí mapa - + GPX files (*.gpx) Soubory GPX (*.gpx) - + CSV files (*.csv) Soubory CSV (*.csv) - + All files (*) Všechny soubory (*) - - + + Date Datum - + Routes Trasy - + No GPX files loaded Nejsou načteny žádné GPX soubory - + %1 files %1 souborů - + Next file Následující soubor - + Version Verze - + Print... Tisknout... - + Export to PDF... Exportovat do PDF... - + All POI files (*.gpx *.csv) Všechny POI soubory (*.gpx *.csv) - + Waypoints Navigační body - + Previous file Předchozí soubor - + Route waypoints Body tras - + First file První soubor - + Last file Poslední soubor - + Append modifier Modifikátor nahradit/přidat - + Map (tiles) source URLs are read on program startup from the following file: URL mapových zdrojů (dlaždic) jsou načteny při startu programu z následujícího souboru: - + The file format is one map entry per line, consisting of the map name and tiles URL delimited by a TAB character. The tile X and Y coordinates are replaced with $x and $y in the URL and the zoom level is replaced with $z. An example map file could look like: Formát souboru je jeden mapový záznam na řádku, kde mapový záznam sestává ze jména mapy a URL dlaždic navzájem oddělených tabulátorem. Souřadnice dlaždice jsou v URL nahrazeny řetězci $x a $y, úroven přiblížení (zoom) pak řetězcem $z. Příklad: - + To make GPXSee load a POI file automatically on startup, add the file to the following directory: POI soubory, které se mají automaticky nahrát při startu programu jsou načítány z následujícího adresáře: - + GPX files (*.gpx);;All files (*) Soubory GPX (*.gpx);;Všechny soubory (*) - - + + Line: %1 Řádka: %1 - + Tracks Cesty - - + + About GPXSee O aplikaci GPXSee - + Navigation Navigace - + Map sources Mapové zdroje - + POIs POI body - - + + Distance Vzdálenost - - + + Time Čas - - + + Error Chyba - + Error loading GPX file: %1 Soubor GPX nelze otevřít: %1 - + Error loading POI file: %1 Soubor POI nelze otevřít: @@ -541,47 +545,48 @@ GraphView - + m m - + km km - + ft ft - + mi mi - + s s - + min min - + h h - + + Distance Vzdálenost - + Time Čas diff --git a/src/axisitem.cpp b/src/axisitem.cpp index 939c99d0..4eb66a3c 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -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 AxisItem::ticks() const +{ + struct Label l; + QList 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; +} diff --git a/src/axisitem.h b/src/axisitem.h index 9b930d0c..f5a653a4 100644 --- a/src/axisitem.h +++ b/src/axisitem.h @@ -20,6 +20,7 @@ public: void setLabel(const QString& label); QSizeF margin() const; + QList ticks() const; private: void updateBoundingRect(); diff --git a/src/graphview.cpp b/src/graphview.cpp index 13dcb477..fea6253e 100644 --- a/src/graphview.cpp +++ b/src/graphview.cpp @@ -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(); diff --git a/src/graphview.h b/src/graphview.h index 86d88b69..7c4ede3c 100644 --- a/src/graphview.h +++ b/src/graphview.h @@ -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 _graphs; QList _visible; diff --git a/src/griditem.cpp b/src/griditem.cpp new file mode 100644 index 00000000..09fa868c --- /dev/null +++ b/src/griditem.cpp @@ -0,0 +1,48 @@ +#include +#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 &x, const QList &y) +{ + _xTicks = x; _yTicks = y; + update(); +} + +void GridItem::setSize(const QSizeF &size) +{ + prepareGeometryChange(); + + _boundingRect = QRectF(QPointF(0, -size.height()), size); +} diff --git a/src/griditem.h b/src/griditem.h new file mode 100644 index 00000000..058fca6e --- /dev/null +++ b/src/griditem.h @@ -0,0 +1,23 @@ +#ifndef GRIDITEM_H +#define GRIDITEM_H + +#include + +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 &x, const QList &y); + void setSize(const QSizeF &size); + +private: + QRectF _boundingRect; + QList _xTicks, _yTicks; +}; + +#endif // GRIDITEM_H diff --git a/src/gui.cpp b/src/gui.cpp index 83f7e955..bdb07a94 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -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); diff --git a/src/gui.h b/src/gui.h index 59ad2867..11434d04 100644 --- a/src/gui.h +++ b/src/gui.h @@ -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; diff --git a/src/infoitem.cpp b/src/infoitem.cpp index 1c11ae57..9b7b34ce 100644 --- a/src/infoitem.cpp +++ b/src/infoitem.cpp @@ -40,6 +40,9 @@ void InfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QList::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 + ": "); diff --git a/src/markeritem.cpp b/src/markeritem.cpp index 80c1b7ce..0317c649 100644 --- a/src/markeritem.cpp +++ b/src/markeritem.cpp @@ -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()); } diff --git a/src/scaleitem.cpp b/src/scaleitem.cpp index b1fe043c..15521488 100644 --- a/src/scaleitem.cpp +++ b/src/scaleitem.cpp @@ -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 diff --git a/src/settings.h b/src/settings.h index 2b89035f..c12092fd 100644 --- a/src/settings.h +++ b/src/settings.h @@ -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" diff --git a/src/slideritem.cpp b/src/slideritem.cpp index 6d4fc9f4..0da1344f 100644 --- a/src/slideritem.cpp +++ b/src/slideritem.cpp @@ -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());