Use the canonical file name also for the "already open" check
[GPXSee.git] / src / GUI / mapitem.cpp
blobfadbfba0cfd3e5aea7d986717f104a190d253f11
1 #include <cmath>
2 #include <QCursor>
3 #include <QPainter>
4 #include <QGraphicsSceneMouseEvent>
5 #include "map/map.h"
6 #include "mapaction.h"
7 #include "tooltip.h"
8 #include "mapitem.h"
9 #ifdef Q_OS_ANDROID
10 #include "popup.h"
11 #endif // Q_OS_ANDROID
14 static void growLeft(Map *map, const Coordinates &c, QRectF &rect)
16 QPointF p(map->ll2xy(c));
18 if (p.x() < rect.left())
19 rect.setLeft(p.x());
22 static void growRight(Map *map, const Coordinates &c, QRectF &rect)
25 QPointF p(map->ll2xy(c));
27 if (p.x() > rect.right())
28 rect.setRight(p.x());
31 static void growTop(Map *map, const Coordinates &c, QRectF &rect)
33 QPointF p(map->ll2xy(c));
35 if (p.y() > rect.top())
36 rect.setTop(p.y());
39 static void growBottom(Map *map, const Coordinates &c, QRectF &rect)
41 QPointF p(map->ll2xy(c));
43 if (p.y() < rect.bottom())
44 rect.setBottom(p.y());
47 static QRectF bbox(const RectC &rect, Map *map, int samples = 100)
49 if (!rect.isValid())
50 return QRectF();
52 double dx = rect.width() / samples;
53 double dy = rect.height() / samples;
55 QPointF tl(map->ll2xy(rect.topLeft()));
56 QPointF br(map->ll2xy(rect.bottomRight()));
57 QRectF prect(tl, br);
59 for (int i = 0; i <= samples; i++) {
60 double x = remainder(rect.left() + i * dx, 360.0);
61 growTop(map, Coordinates(x, rect.bottom()), prect);
62 growBottom(map, Coordinates(x, rect.top()), prect);
65 for (int i = 0; i <= samples; i++) {
66 double y = rect.bottom() + i * dy;
67 growLeft(map, Coordinates(rect.left(), y), prect);
68 growRight(map, Coordinates(rect.right(), y), prect);
71 return prect;
74 ToolTip MapItem::info() const
76 ToolTip tt;
78 if (!_name.isEmpty())
79 tt.insert(tr("Name"), _name);
80 if (!_fileName.isEmpty())
81 tt.insert(tr("File"), Util::displayName(_fileName));
83 return tt;
86 MapItem::MapItem(MapAction *action, Map *map, GraphicsItem *parent)
87 : PlaneItem(parent)
89 Map *src = action->data().value<Map*>();
90 Q_ASSERT(map->isReady());
92 _name = src->name();
93 _fileName = src->path();
94 _bounds = src->llBounds();
96 connect(this, &MapItem::triggered, action, &MapAction::trigger);
98 _digitalZoom = 0;
100 _width = 2;
101 _opacity = 0.5;
102 QBrush brush(Qt::SolidPattern);
103 _pen = QPen(brush, _width);
105 updatePainterPath(map);
107 setCursor(Qt::ArrowCursor);
108 setAcceptHoverEvents(true);
111 void MapItem::updatePainterPath(Map *map)
113 _painterPath = QPainterPath();
115 QRectF r(bbox(_bounds, map));
117 if (r.left() > r.right()) {
118 QRectF r1(bbox(RectC(_bounds.topLeft(),
119 Coordinates(180, _bounds.bottomRight().lat())), map));
120 QRectF r2(bbox(RectC(Coordinates(-180, _bounds.topLeft().lat()),
121 _bounds.bottomRight()), map));
123 _painterPath.addRect(r1);
124 _painterPath.addRect(r2);
125 } else
126 _painterPath.addRect(r);
129 void MapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
130 QWidget *widget)
132 Q_UNUSED(option);
133 Q_UNUSED(widget);
135 painter->setPen(_width ? _pen : QPen(Qt::NoPen));
136 painter->drawPath(_painterPath);
137 painter->fillPath(_painterPath, _brush);
139 //QPen p = QPen(QBrush(Qt::red), 0);
140 //painter->setPen(p);
141 //painter->drawRect(boundingRect());
144 void MapItem::setMap(Map *map)
146 prepareGeometryChange();
147 updatePainterPath(map);
150 void MapItem::setColor(const QColor &color)
152 if (_pen.color() == color)
153 return;
155 QColor bc(color);
156 bc.setAlphaF(_opacity * color.alphaF());
158 _pen.setColor(color);
159 _brush = QBrush(bc);
160 update();
163 void MapItem::setOpacity(qreal opacity)
165 if (_opacity == opacity)
166 return;
168 _opacity = opacity;
169 QColor bc(_pen.color());
170 bc.setAlphaF(_opacity * _pen.color().alphaF());
171 _brush = QBrush(bc);
173 update();
176 void MapItem::setWidth(qreal width)
178 if (_width == width)
179 return;
181 prepareGeometryChange();
183 _width = width;
184 _pen.setWidthF(_width * pow(2, -_digitalZoom));
187 void MapItem::setPenStyle(Qt::PenStyle style)
189 if (_pen.style() == style)
190 return;
192 _pen.setStyle(style);
193 update();
196 void MapItem::setDigitalZoom(int zoom)
198 if (_digitalZoom == zoom)
199 return;
201 prepareGeometryChange();
203 _digitalZoom = zoom;
204 _pen.setWidthF(_width * pow(2, -_digitalZoom));
207 void MapItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
209 Q_UNUSED(event);
211 _pen.setWidthF((_width + 1) * pow(2, -_digitalZoom));
212 update();
214 #ifdef Q_OS_ANDROID
215 Popup::show(event->screenPos(), info(), event->widget());
216 #endif // Q_OS_ANDROID
219 void MapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
221 Q_UNUSED(event);
223 _pen.setWidthF(_width * pow(2, -_digitalZoom));
224 update();
227 void MapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
229 Q_UNUSED(event);
231 emit triggered();