4 #include <QGraphicsSceneMouseEvent>
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())
22 static void growRight(Map
*map
, const Coordinates
&c
, QRectF
&rect
)
25 QPointF
p(map
->ll2xy(c
));
27 if (p
.x() > rect
.right())
31 static void growTop(Map
*map
, const Coordinates
&c
, QRectF
&rect
)
33 QPointF
p(map
->ll2xy(c
));
35 if (p
.y() > rect
.top())
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)
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()));
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
);
74 ToolTip
MapItem::info(bool extended
) const
80 tt
.insert(tr("Name"), _name
);
81 if (!_fileName
.isEmpty())
82 tt
.insert(tr("File"), Util::displayName(_fileName
));
87 MapItem::MapItem(MapAction
*action
, Map
*map
, GraphicsItem
*parent
)
90 Map
*src
= action
->data().value
<Map
*>();
91 Q_ASSERT(map
->isReady());
94 _fileName
= src
->path();
95 _bounds
= src
->llBounds();
97 connect(this, &MapItem::triggered
, action
, &MapAction::trigger
);
103 QBrush
brush(Qt::SolidPattern
);
104 _pen
= QPen(brush
, _width
);
106 updatePainterPath(map
);
108 setCursor(Qt::ArrowCursor
);
109 setAcceptHoverEvents(true);
112 void MapItem::updatePainterPath(Map
*map
)
114 _painterPath
= QPainterPath();
116 QRectF
r(bbox(_bounds
, map
));
118 if (r
.left() > r
.right()) {
119 QRectF
r1(bbox(RectC(_bounds
.topLeft(),
120 Coordinates(180, _bounds
.bottomRight().lat())), map
));
121 QRectF
r2(bbox(RectC(Coordinates(-180, _bounds
.topLeft().lat()),
122 _bounds
.bottomRight()), map
));
124 _painterPath
.addRect(r1
);
125 _painterPath
.addRect(r2
);
127 _painterPath
.addRect(r
);
130 void MapItem::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
,
136 painter
->setPen(_width
? _pen
: QPen(Qt::NoPen
));
137 painter
->drawPath(_painterPath
);
138 painter
->fillPath(_painterPath
, _brush
);
140 //QPen p = QPen(QBrush(Qt::red), 0);
141 //painter->setPen(p);
142 //painter->drawRect(boundingRect());
145 void MapItem::setMap(Map
*map
)
147 prepareGeometryChange();
148 updatePainterPath(map
);
151 void MapItem::setColor(const QColor
&color
)
153 if (_pen
.color() == color
)
157 bc
.setAlphaF(_opacity
* color
.alphaF());
159 _pen
.setColor(color
);
164 void MapItem::setOpacity(qreal opacity
)
166 if (_opacity
== opacity
)
170 QColor
bc(_pen
.color());
171 bc
.setAlphaF(_opacity
* _pen
.color().alphaF());
177 void MapItem::setWidth(qreal width
)
182 prepareGeometryChange();
185 _pen
.setWidthF(_width
* pow(2, -_digitalZoom
));
188 void MapItem::setPenStyle(Qt::PenStyle style
)
190 if (_pen
.style() == style
)
193 _pen
.setStyle(style
);
197 void MapItem::setDigitalZoom(int zoom
)
199 if (_digitalZoom
== zoom
)
202 prepareGeometryChange();
205 _pen
.setWidthF(_width
* pow(2, -_digitalZoom
));
208 void MapItem::hoverEnterEvent(QGraphicsSceneHoverEvent
*event
)
212 _pen
.setWidthF((_width
+ 1) * pow(2, -_digitalZoom
));
216 GraphicsScene
*gs
= dynamic_cast<GraphicsScene
*>(scene());
218 Popup::show(event
->screenPos(), info(gs
->showExtendedInfo()),
220 #endif // Q_OS_ANDROID
223 void MapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent
*event
)
227 _pen
.setWidthF(_width
* pow(2, -_digitalZoom
));
231 void MapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
*event
)