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

Hyperlink + copy&paste enabled tool tips

This commit is contained in:
Martin Tůma 2019-10-13 20:20:32 +02:00
parent de9bae9d66
commit a9ce6f54c7
33 changed files with 286 additions and 96 deletions

View File

@ -19,6 +19,7 @@ equals(QT_MAJOR_VERSION, 5) : lessThan(QT_MINOR_VERSION, 4) {QT += opengl}
INCLUDEPATH += ./src
HEADERS += src/common/config.h \
src/GUI/popup.h \
src/common/staticassert.h \
src/common/coordinates.h \
src/common/range.h \
@ -185,6 +186,7 @@ HEADERS += src/common/config.h \
src/data/csv.h \
src/data/cupparser.h
SOURCES += src/main.cpp \
src/GUI/popup.cpp \
src/common/coordinates.cpp \
src/common/rectc.cpp \
src/common/range.cpp \

View File

@ -7,11 +7,11 @@ CadenceGraphItem::CadenceGraphItem(const Graph &graph, GraphType type,
int width, const QColor &color, QGraphicsItem *parent)
: GraphItem(graph, type, width, color, parent)
{
setToolTip(toolTip());
}
QString CadenceGraphItem::toolTip() const
QString CadenceGraphItem::toolTip(Units units) const
{
Q_UNUSED(units);
ToolTip tt;
QLocale l(QLocale::system());

View File

@ -11,8 +11,7 @@ public:
CadenceGraphItem(const Graph &graph, GraphType type, int width,
const QColor &color, QGraphicsItem *parent = 0);
private:
QString toolTip() const;
QString toolTip(Units units) const;
};
#endif // CADENCEGRAPHITEM_H

View File

@ -24,8 +24,6 @@ ElevationGraphItem::ElevationGraphItem(const Graph &graph, GraphType type,
_descent += prev - cur;
}
}
setToolTip(toolTip(Metric));
}
QString ElevationGraphItem::toolTip(Units units) const
@ -46,8 +44,3 @@ QString ElevationGraphItem::toolTip(Units units) const
return tt.toString();
}
void ElevationGraphItem::setUnits(Units units)
{
setToolTip(toolTip(units));
}

View File

@ -16,11 +16,9 @@ public:
qreal max() const {return _max;}
qreal min() const {return _min;}
void setUnits(Units units);
private:
QString toolTip(Units units) const;
private:
qreal _ascent, _descent, _min, _max;
};

View File

@ -25,12 +25,11 @@ GearRatioGraphItem::GearRatioGraphItem(const Graph &graph, GraphType type,
}
}
_top = key;
setToolTip(toolTip());
}
QString GearRatioGraphItem::toolTip() const
QString GearRatioGraphItem::toolTip(Units units) const
{
Q_UNUSED(units);
ToolTip tt;
QLocale l(QLocale::system());

View File

@ -13,12 +13,11 @@ public:
const QColor &color, QGraphicsItem *parent = 0);
qreal top() const {return _top;}
const QMap<qreal, qreal> &map() const {return _map;}
private:
QString toolTip() const;
QString toolTip(Units units) const;
private:
QMap<qreal, qreal> _map;
qreal _top;
};

View File

@ -1,4 +1,6 @@
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include "popup.h"
#include "graphitem.h"
@ -8,6 +10,7 @@ GraphItem::GraphItem(const Graph &graph, GraphType type, int width,
{
Q_ASSERT(_graph.isValid());
_units = Metric;
_pen = QPen(color, width);
_sx = 0; _sy = 0;
_time = _graph.hasTime();
@ -303,3 +306,9 @@ void GraphItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
emit selected(false);
}
void GraphItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Popup::show(event->screenPos(), toolTip(_units), event->widget());
QGraphicsObject::mousePressEvent(event);
}

View File

@ -15,6 +15,8 @@ public:
QGraphicsItem *parent = 0);
virtual ~GraphItem() {}
virtual QString toolTip(Units units) const = 0;
QPainterPath shape() const {return _shape;}
QRectF boundingRect() const {return _shape.boundingRect();}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
@ -30,7 +32,7 @@ public:
void setGraphType(GraphType type);
void setColor(const QColor &color);
void setWidth(int width);
virtual void setUnits(Units units) {Q_UNUSED(units);}
void setUnits(Units units) {_units = units;}
qreal yAtX(qreal x);
qreal distanceAtTime(qreal time);
@ -45,10 +47,12 @@ public slots:
void emitSliderPositionChanged(qreal);
void hover(bool hover);
private:
protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
const GraphSegment *segment(qreal x, GraphType type) const;
void updatePath();
void updateShape();
@ -56,6 +60,7 @@ private:
Graph _graph;
GraphType _type;
Units _units;
QPainterPath _path;
QPainterPath _shape;
QRectF _bounds;

View File

@ -357,13 +357,13 @@ void GraphView::wheelEvent(QWheelEvent *e)
QGraphicsView::wheelEvent(e);
}
void GraphView::paintEvent(QPaintEvent *event)
void GraphView::paintEvent(QPaintEvent *e)
{
QRectF viewRect(mapToScene(rect()).boundingRect());
_info->setPos(QPointF(viewRect.left() + (viewRect.width()
- _info->boundingRect().width())/2.0, _info->pos().y()));
QGraphicsView::paintEvent(event);
QGraphicsView::paintEvent(e);
}
void GraphView::plot(QPainter *painter, const QRectF &target, qreal scale)

View File

@ -48,10 +48,15 @@ signals:
protected:
void addGraph(GraphItem *graph);
void removeGraph(GraphItem *graph);
void setGraphType(GraphType type);
void setUnits(Units units);
void resizeEvent(QResizeEvent *e);
void mousePressEvent(QMouseEvent *e);
void wheelEvent(QWheelEvent *e);
void changeEvent(QEvent *e);
void paintEvent(QPaintEvent *e);
const QString &yLabel() const {return _yLabel;}
const QString &yUnits() const {return _yUnits;}
qreal yScale() const {return _yScale;}
@ -88,12 +93,6 @@ private:
void removeItem(QGraphicsItem *item);
void addItem(QGraphicsItem *item);
void resizeEvent(QResizeEvent *e);
void mousePressEvent(QMouseEvent *e);
void wheelEvent(QWheelEvent *e);
void changeEvent(QEvent *e);
void paintEvent(QPaintEvent *event);
QGraphicsScene *_scene;
AxisItem *_xAxis, *_yAxis;

View File

@ -1580,7 +1580,6 @@ void GUI::keyPressEvent(QKeyEvent *event)
else
_movingTimeAction->trigger();
break;
case Qt::Key_Escape:
if (_fullscreenAction->isChecked()) {
_fullscreenAction->setChecked(false);

View File

@ -7,11 +7,11 @@ HeartRateGraphItem::HeartRateGraphItem(const Graph &graph, GraphType type,
int width, const QColor &color, QGraphicsItem *parent)
: GraphItem(graph, type, width, color, parent)
{
setToolTip(toolTip());
}
QString HeartRateGraphItem::toolTip() const
QString HeartRateGraphItem::toolTip(Units units) const
{
Q_UNUSED(units);
ToolTip tt;
QLocale l(QLocale::system());

View File

@ -11,8 +11,7 @@ public:
HeartRateGraphItem(const Graph &graph, GraphType type, int width,
const QColor &color, QGraphicsItem *parent = 0);
private:
QString toolTip() const;
QString toolTip(Units units) const;
};
#endif // HEARTRATEGRAPHITEM_H

View File

@ -1,9 +1,11 @@
#include <cmath>
#include <QCursor>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include "common/greatcircle.h"
#include "map/map.h"
#include "pathtickitem.h"
#include "popup.h"
#include "pathitem.h"
@ -336,7 +338,6 @@ void PathItem::updateTicks()
_ticks[i] = new PathTickItem(tr, (i + 1) * ts, this);
_ticks[i]->setPos(position((i + 1) * ts * xInM()));
_ticks[i]->setColor(_pen.color());
_ticks[i]->setToolTip(toolTip());
}
}
@ -381,3 +382,9 @@ void PathItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
emit selected(false);
}
void PathItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Popup::show(event->screenPos(), toolTip(_units), event->widget());
QGraphicsObject::mousePressEvent(event);
}

View File

@ -16,6 +16,9 @@ class PathItem : public QGraphicsObject
public:
PathItem(const Path &path, Map *map, QGraphicsItem *parent = 0);
virtual ~PathItem() {}
virtual QString toolTip(Units units) const = 0;
QPainterPath shape() const {return _shape;}
QRectF boundingRect() const {return _shape.boundingRect();}
@ -44,6 +47,11 @@ public slots:
signals:
void selected(bool);
protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
const PathSegment *segment(qreal x) const;
QPointF position(qreal distance) const;
@ -55,9 +63,6 @@ private:
unsigned tickSize() const;
void updateTicks();
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
Path _path;
Map *_map;
qreal _markerDistance;

View File

@ -1,6 +1,9 @@
#include <QPainter>
#include <QCursor>
#include <QGraphicsSceneMouseEvent>
#include "font.h"
#include "popup.h"
#include "pathitem.h"
#include "pathtickitem.h"
@ -69,3 +72,10 @@ QRect PathTickItem::tickRect(int value)
return fm.boundingRect(QRect(), Qt::AlignCenter,
QString::number(qMax(value, 10))).adjusted(-2, 0, 2, 0);
}
void PathTickItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
const PathItem *pi = static_cast<PathItem*>(parentItem());
Popup::show(event->screenPos(), pi->toolTip(pi->units()), event->widget());
QGraphicsItem::mousePressEvent(event);
}

View File

@ -18,6 +18,9 @@ public:
static QRect tickRect(int value);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
QRectF _tickRect;
QString _text;

162
src/GUI/popup.cpp Normal file
View File

@ -0,0 +1,162 @@
#include <QToolTip>
#include <QStyle>
#include <QStylePainter>
#include <QStyleOptionFrame>
#include <QLabel>
#include <QMouseEvent>
#include <QApplication>
#include <QDesktopWidget>
#include <QBasicTimer>
#include "popup.h"
#include <QDebug>
class Label : public QLabel
{
public:
Label(const QString &text, QWidget *parent = 0);
~Label();
bool eventFilter(QObject *o, QEvent *ev);
void place(const QPoint &pos, QWidget *w);
void deleteAfterTimer();
void stopTimer() {_timer.stop();}
static Label *_instance;
protected:
void paintEvent(QPaintEvent *event);
void timerEvent(QTimerEvent *event);
private:
QBasicTimer _timer;
};
Label *Label::_instance = 0;
Label::Label(const QString &text, QWidget *parent)
: QLabel(text, parent, Qt::ToolTip | Qt::BypassGraphicsProxyWidget)
{
delete _instance;
_instance = this;
setForegroundRole(QPalette::ToolTipText);
setBackgroundRole(QPalette::ToolTipBase);
setPalette(QToolTip::palette());
ensurePolished();
setMargin(1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0,
this));
setFrameStyle(QFrame::NoFrame);
setAlignment(Qt::AlignLeft);
setIndent(1);
setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, 0,
this) / 255.0);
setTextInteractionFlags(Qt::TextBrowserInteraction);
setOpenExternalLinks(true);
setMouseTracking(true);
qApp->installEventFilter(this);
}
Label::~Label()
{
_instance = 0;
}
void Label::paintEvent(QPaintEvent *event)
{
QStylePainter p(this);
QStyleOptionFrame opt;
opt.init(this);
p.drawPrimitive(QStyle::PE_PanelTipLabel, opt);
p.end();
QLabel::paintEvent(event);
}
void Label::timerEvent(QTimerEvent *event)
{
if (event->timerId() == _timer.timerId()) {
_timer.stop();
deleteLater();
}
}
bool Label::eventFilter(QObject *o, QEvent *ev)
{
Q_UNUSED(o);
switch (ev->type()) {
case QEvent::KeyPress:
case QEvent::KeyRelease: {
const int key = static_cast<QKeyEvent *>(ev)->key();
if (key == Qt::Key_Escape) {
deleteLater();
return true;
}
break;
}
case QEvent::FocusIn:
case QEvent::FocusOut:
case QEvent::WindowActivate:
case QEvent::WindowDeactivate:
case QEvent::Close:
deleteLater();
break;
case QEvent::MouseMove: {
QRectF r(geometry().adjusted(-5, -20, 5, 20));
QPointF p(static_cast<QMouseEvent*>(ev)->screenPos());
if (!r.contains(p))
deleteAfterTimer();
break;
}
default:
break;
}
return false;
}
void Label::place(const QPoint &pos, QWidget *w)
{
QRect screen = QApplication::desktop()->screenGeometry(w);
QPoint p(pos.x() + 2, pos.y() + 16);
if (p.x() + width() > screen.x() + screen.width())
p.rx() -= 4 + width();
if (p.y() + height() > screen.y() + screen.height())
p.ry() -= 24 + height();
if (p.y() < screen.y())
p.setY(screen.y());
if (p.x() + width() > screen.x() + screen.width())
p.setX(screen.x() + screen.width() - width());
if (p.x() < screen.x())
p.setX(screen.x());
if (p.y() + height() > screen.y() + screen.height())
p.setY(screen.y() + screen.height() - height());
this->move(p);
}
void Label::deleteAfterTimer()
{
if (!_timer.isActive())
_timer.start(300, this);
}
void Popup::show(const QPoint &pos, const QString &text, QWidget *w)
{
if (Label::_instance) {
Label::_instance->stopTimer();
Label::_instance->setText(text);
Label::_instance->resize(Label::_instance->sizeHint());
} else {
Label::_instance = new Label(text);
Label::_instance->showNormal();
}
Label::_instance->place(pos, w);
}

14
src/GUI/popup.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef POPUP_H
#define POPUP_H
class QPoint;
class QString;
class QWidget;
class Popup
{
public:
static void show(const QPoint &pos, const QString &text, QWidget *w);
};
#endif // POPUP_H

View File

@ -7,11 +7,11 @@ PowerGraphItem::PowerGraphItem(const Graph &graph, GraphType type, int width,
const QColor &color, QGraphicsItem *parent)
: GraphItem(graph, type, width, color, parent)
{
setToolTip(toolTip());
}
QString PowerGraphItem::toolTip() const
QString PowerGraphItem::toolTip(Units units) const
{
Q_UNUSED(units);
ToolTip tt;
QLocale l(QLocale::system());

View File

@ -11,8 +11,7 @@ public:
PowerGraphItem(const Graph &graph, GraphType type, int width,
const QColor &color, QGraphicsItem *parent = 0);
private:
QString toolTip() const;
QString toolTip(Units units) const;
};
#endif // POWERGRAPHITEM_H

View File

@ -33,8 +33,6 @@ RouteItem::RouteItem(const Route &route, Map *map, QGraphicsItem *parent)
_name = route.name();
_desc = route.description();
_coordinatesFormat = DecimalDegrees;
setToolTip(toolTip(Metric));
}
void RouteItem::setMap(Map *map)
@ -50,9 +48,8 @@ void RouteItem::setUnits(Units u)
if (units() == u)
return;
setToolTip(toolTip(units()));
for (int i = 0; i < _waypoints.count(); i++)
_waypoints[i]->setToolTipFormat(units(), _coordinatesFormat);
_waypoints[i]->setToolTipFormat(u, _coordinatesFormat);
PathItem::setUnits(u);
}

View File

@ -23,9 +23,9 @@ public:
void showWaypoints(bool show);
void showWaypointLabels(bool show);
private:
QString toolTip(Units units) const;
private:
QString _name;
QString _desc;
CoordinatesFormat _coordinatesFormat;

View File

@ -8,26 +8,23 @@ SpeedGraphItem::SpeedGraphItem(const Graph &graph, GraphType type, int width,
const QColor &color, qreal movingTime, QGraphicsItem *parent)
: GraphItem(graph, type, width, color, parent)
{
_units = Metric;
_timeType = Total;
_max = GraphItem::max();
_avg = graph.last().last().s() / graph.last().last().t();
_mavg = graph.last().last().s() / movingTime;
setToolTip(toolTip());
}
QString SpeedGraphItem::toolTip() const
QString SpeedGraphItem::toolTip(Units units) const
{
ToolTip tt;
qreal scale = (_units == Imperial) ? MS2MIH : (_units == Nautical)
qreal scale = (units == Imperial) ? MS2MIH : (units == Nautical)
? MS2KN : MS2KMH;
QString su = (_units == Imperial) ? tr("mi/h") : (_units == Nautical)
QString su = (units == Imperial) ? tr("mi/h") : (units == Nautical)
? tr("kn") : tr("km/h");
QString pace = Format::timeSpan((3600.0 / ((_timeType == Total)
? avg() * scale : mavg() * scale)), false);
QString pu = (_units == Metric) ? tr("min/km") : (_units == Imperial) ?
QString pu = (units == Metric) ? tr("min/km") : (units == Imperial) ?
tr("min/mi") : tr("min/nmi");
QLocale l(QLocale::system());
@ -40,14 +37,7 @@ QString SpeedGraphItem::toolTip() const
return tt.toString();
}
void SpeedGraphItem::setUnits(Units units)
{
_units = units;
setToolTip(toolTip());
}
void SpeedGraphItem::setTimeType(TimeType type)
{
_timeType = type;
setToolTip(toolTip());
}

View File

@ -16,15 +16,13 @@ public:
qreal mavg() const {return _mavg;}
qreal max() const {return _max;}
void setUnits(Units units);
QString toolTip(Units units) const;
void setTimeType(TimeType type);
private:
QString toolTip() const;
qreal _avg, _mavg, _max;
TimeType _timeType;
Units _units;
};
#endif // SPEEDGRAPHITEM_H

View File

@ -10,8 +10,6 @@ TemperatureGraphItem::TemperatureGraphItem(const Graph &graph, GraphType type,
_min = GraphItem::min();
_max = GraphItem::max();
_avg = GraphItem::avg();
setToolTip(toolTip(Metric));
}
QString TemperatureGraphItem::toolTip(Units units) const
@ -32,8 +30,3 @@ QString TemperatureGraphItem::toolTip(Units units) const
return tt.toString();
}
void TemperatureGraphItem::setUnits(Units units)
{
setToolTip(toolTip(units));
}

View File

@ -15,11 +15,9 @@ public:
qreal min() const {return _min;}
qreal avg() const {return _avg;}
void setUnits(Units units);
private:
QString toolTip(Units units) const;
private:
qreal _min, _max, _avg;
};

View File

@ -1,4 +1,6 @@
#include <QImageReader>
#include <QLabel>
#include "popup.h"
#include "tooltip.h"
@ -26,7 +28,8 @@ QString ToolTip::toString() const
}
html += "<div align=\"center\">";
html += QString("<img src=\"file:%0\" width=\"%1\" height=\"%2\"/>")
html += QString("<a href=\"file:%0\">"
"<img src=\"file:%0\" width=\"%1\" height=\"%2\"/></a>")
.arg(_img.path(), QString::number(width), QString::number(height));
html += "</div>";
}

View File

@ -33,12 +33,4 @@ TrackItem::TrackItem(const Track &track, Map *map, QGraphicsItem *parent)
_date = track.date();
_time = track.time();
_movingTime = track.movingTime();
setToolTip(toolTip(Metric));
}
void TrackItem::setUnits(Units units)
{
setToolTip(toolTip(units));
PathItem::setUnits(units);
}

View File

@ -16,11 +16,9 @@ class TrackItem : public PathItem
public:
TrackItem(const Track &track, Map *map, QGraphicsItem *parent = 0);
void setUnits(Units units);
private:
QString toolTip(Units units) const;
private:
QString _name;
QString _desc;
QDateTime _date;

View File

@ -1,7 +1,10 @@
#include <QApplication>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QLabel>
#include "font.h"
#include "tooltip.h"
#include "popup.h"
#include "waypointitem.h"
@ -10,7 +13,7 @@
#define FS(size) \
((int)((qreal)size * 1.41))
QString WaypointItem::toolTip(Units units, CoordinatesFormat format)
ToolTip WaypointItem::toolTip(Units units, CoordinatesFormat format)
{
ToolTip tt;
@ -29,7 +32,7 @@ QString WaypointItem::toolTip(Units units, CoordinatesFormat format)
_waypoint.description());
tt.setImage(_waypoint.image());
return tt.toString();
return tt;
}
WaypointItem::WaypointItem(const Waypoint &waypoint, Map *map,
@ -43,10 +46,12 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, Map *map,
_font.setPixelSize(FS(_size));
_font.setFamily(FONT_FAMILY);
_units = Metric;
_format = DecimalDegrees;
updateCache();
setPos(map->ll2xy(waypoint.coordinates()));
setToolTip(toolTip(Metric, DecimalDegrees));
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
}
@ -116,7 +121,8 @@ void WaypointItem::setColor(const QColor &color)
void WaypointItem::setToolTipFormat(Units units, CoordinatesFormat format)
{
setToolTip(toolTip(units, format));
_units = units;
_format = format;
}
void WaypointItem::showLabel(bool show)
@ -148,3 +154,10 @@ void WaypointItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
updateCache();
setZValue(zValue() - 1.0);
}
void WaypointItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
ToolTip tt(toolTip(_units, _format));
Popup::show(event->screenPos(), tt.toString(), event->widget());
QGraphicsItem::mousePressEvent(event);
}

View File

@ -7,6 +7,8 @@
#include "map/map.h"
#include "units.h"
#include "format.h"
#include "tooltip.h"
class WaypointItem : public QGraphicsItem
{
@ -16,23 +18,25 @@ public:
const Waypoint &waypoint() const {return _waypoint;}
void setMap(Map *map) {setPos(map->ll2xy(_waypoint.coordinates()));}
void setToolTipFormat(Units units, CoordinatesFormat format);
void setSize(int size);
void setColor(const QColor &color);
void showLabel(bool show);
void setDigitalZoom(int zoom) {setScale(pow(2, -zoom));}
void setToolTipFormat(Units units, CoordinatesFormat format);
QPainterPath shape() const {return _shape;}
QRectF boundingRect() const {return _shape.boundingRect();}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
private:
protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
void updateCache();
QString toolTip(Units units, CoordinatesFormat format);
ToolTip toolTip(Units units, CoordinatesFormat format);
Waypoint _waypoint;
QPainterPath _shape;
@ -41,6 +45,9 @@ private:
bool _showLabel;
QFont _font;
QRect _labelBB;
Units _units;
CoordinatesFormat _format;
};
#endif // WAYPOINTITEM_H