4 #include <QGraphicsSceneMouseEvent>
12 static void growLeft(Map
*map
, const Coordinates
&c
, QRectF
&rect
)
14 QPointF
p(map
->ll2xy(c
));
16 if (p
.x() < rect
.left())
20 static void growRight(Map
*map
, const Coordinates
&c
, QRectF
&rect
)
23 QPointF
p(map
->ll2xy(c
));
25 if (p
.x() > rect
.right())
29 static void growTop(Map
*map
, const Coordinates
&c
, QRectF
&rect
)
31 QPointF
p(map
->ll2xy(c
));
33 if (p
.y() > rect
.top())
37 static void growBottom(Map
*map
, const Coordinates
&c
, QRectF
&rect
)
39 QPointF
p(map
->ll2xy(c
));
41 if (p
.y() < rect
.bottom())
42 rect
.setBottom(p
.y());
45 static QRectF
bbox(const RectC
&rect
, Map
*map
, int samples
= 100)
50 double dx
= rect
.width() / samples
;
51 double dy
= rect
.height() / samples
;
53 QPointF
tl(map
->ll2xy(rect
.topLeft()));
54 QPointF
br(map
->ll2xy(rect
.bottomRight()));
57 for (int i
= 0; i
<= samples
; i
++) {
58 double x
= remainder(rect
.left() + i
* dx
, 360.0);
59 growTop(map
, Coordinates(x
, rect
.bottom()), prect
);
60 growBottom(map
, Coordinates(x
, rect
.top()), prect
);
63 for (int i
= 0; i
<= samples
; i
++) {
64 double y
= rect
.bottom() + i
* dy
;
65 growLeft(map
, Coordinates(rect
.left(), y
), prect
);
66 growRight(map
, Coordinates(rect
.right(), y
), prect
);
72 ToolTip
MapItem::info() const
77 tt
.insert(tr("Name"), _name
);
78 if (!_fileName
.isEmpty())
79 tt
.insert(tr("File"), Util::displayName(_fileName
));
84 MapItem::MapItem(MapAction
*action
, Map
*map
, const Projection
&proj
,
85 GraphicsItem
*parent
) : PlaneItem(parent
)
87 Map
*src
= action
->data().value
<Map
*>();
88 Q_ASSERT(map
->isReady());
91 _fileName
= src
->path();
92 _bounds
= src
->llBounds(proj
);
94 connect(this, &MapItem::triggered
, action
, &MapAction::trigger
);
100 QBrush
brush(Qt::SolidPattern
);
101 _pen
= QPen(brush
, _width
);
103 updatePainterPath(map
);
105 setCursor(Qt::ArrowCursor
);
106 setAcceptHoverEvents(true);
109 void MapItem::updatePainterPath(Map
*map
)
111 _painterPath
= QPainterPath();
113 QRectF
r(bbox(_bounds
, map
));
115 if (r
.left() > r
.right()) {
116 QRectF
r1(bbox(RectC(_bounds
.topLeft(),
117 Coordinates(180, _bounds
.bottomRight().lat())), map
));
118 QRectF
r2(bbox(RectC(Coordinates(-180, _bounds
.topLeft().lat()),
119 _bounds
.bottomRight()), map
));
121 _painterPath
.addRect(r1
);
122 _painterPath
.addRect(r2
);
124 _painterPath
.addRect(r
);
127 void MapItem::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
,
133 painter
->setPen(_width
? _pen
: QPen(Qt::NoPen
));
134 painter
->drawPath(_painterPath
);
135 painter
->fillPath(_painterPath
, _brush
);
137 //QPen p = QPen(QBrush(Qt::red), 0);
138 //painter->setPen(p);
139 //painter->drawRect(boundingRect());
142 void MapItem::setMap(Map
*map
)
144 prepareGeometryChange();
145 updatePainterPath(map
);
148 void MapItem::setColor(const QColor
&color
)
150 if (_pen
.color() == color
)
154 bc
.setAlphaF(_opacity
* color
.alphaF());
156 _pen
.setColor(color
);
161 void MapItem::setOpacity(qreal opacity
)
163 if (_opacity
== opacity
)
167 QColor
bc(_pen
.color());
168 bc
.setAlphaF(_opacity
* _pen
.color().alphaF());
174 void MapItem::setWidth(qreal width
)
179 prepareGeometryChange();
182 _pen
.setWidthF(_width
* pow(2, -_digitalZoom
));
185 void MapItem::setPenStyle(Qt::PenStyle style
)
187 if (_pen
.style() == style
)
190 _pen
.setStyle(style
);
194 void MapItem::setDigitalZoom(int zoom
)
196 if (_digitalZoom
== zoom
)
199 prepareGeometryChange();
202 _pen
.setWidthF(_width
* pow(2, -_digitalZoom
));
205 void MapItem::hoverEnterEvent(QGraphicsSceneHoverEvent
*event
)
209 _pen
.setWidthF((_width
+ 1) * pow(2, -_digitalZoom
));
213 Popup::show(event
->screenPos(), info(), event
->widget());
214 #endif // Q_OS_ANDROID
217 void MapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent
*event
)
221 _pen
.setWidthF(_width
* pow(2, -_digitalZoom
));
225 void MapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
*event
)