Merge remote-tracking branch 'weblate/master'
[GPXSee.git] / src / GUI / areaitem.cpp
blobd35763d257e8f79e5357cbd13cb7dd269e5803fb
1 #include <cmath>
2 #include <QApplication>
3 #include <QCursor>
4 #include <QPainter>
5 #include <QGraphicsSceneMouseEvent>
6 #include "map/map.h"
7 #include "popup.h"
8 #include "tooltip.h"
9 #include "areaitem.h"
12 ToolTip AreaItem::info() const
14 ToolTip tt;
16 if (!_area.name().isEmpty())
17 tt.insert(qApp->translate("PolygonItem", "Name"), _area.name());
18 if (!_area.description().isEmpty())
19 tt.insert(qApp->translate("PolygonItem", "Description"),
20 _area.description());
22 return tt;
25 AreaItem::AreaItem(const Area &area, Map *map, GraphicsItem *parent)
26 : PlaneItem(parent), _area(area)
28 _digitalZoom = 0;
29 _width = 2;
30 _opacity = 0.5;
31 _color = Qt::black;
32 _penStyle = Qt::SolidLine;
34 _pen = QPen(strokeColor(), width());
35 _brush = QBrush(fillColor());
37 updatePainterPath(map);
39 setCursor(Qt::ArrowCursor);
40 setAcceptHoverEvents(true);
43 QPainterPath AreaItem::painterPath(Map *map, const Polygon &polygon)
45 QPainterPath path;
47 for (int i = 0; i < polygon.size(); i++) {
48 const QVector<Coordinates> &subpath = polygon.at(i);
50 path.moveTo(map->ll2xy(subpath.first()));
51 for (int j = 1; j < subpath.size(); j++)
52 path.lineTo(map->ll2xy(subpath.at(j)));
53 path.closeSubpath();
56 return path;
59 void AreaItem::updatePainterPath(Map *map)
61 _painterPath = QPainterPath();
63 for (int i = 0; i < _area.polygons().size(); i++)
64 _painterPath.addPath(painterPath(map, _area.polygons().at(i)));
67 void AreaItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
68 QWidget *widget)
70 Q_UNUSED(option);
71 Q_UNUSED(widget);
73 painter->setPen(_width ? _pen : QPen(Qt::NoPen));
74 painter->drawPath(_painterPath);
75 painter->fillPath(_painterPath, _brush);
78 QPen p = QPen(QBrush(Qt::red), 0);
79 painter->setPen(p);
80 painter->drawRect(boundingRect());
84 void AreaItem::setMap(Map *map)
86 prepareGeometryChange();
87 updatePainterPath(map);
90 const QColor &AreaItem::strokeColor() const
92 return (_useStyle && _area.style().isValid())
93 ? _area.style().stroke() : _color;
96 QColor AreaItem::fillColor() const
98 if (_useStyle && _area.style().isValid())
99 return _area.style().fill();
100 else {
101 QColor fc(_color);
102 fc.setAlphaF(_opacity * _color.alphaF());
103 return fc;
107 void AreaItem::setColor(const QColor &color)
109 _color = color;
110 updateColor();
113 void AreaItem::updateColor()
115 _pen.setColor(strokeColor());
116 _brush = QBrush(fillColor());
117 update();
120 void AreaItem::setOpacity(qreal opacity)
122 _opacity = opacity;
123 updateColor();
126 qreal AreaItem::width() const
128 return (_useStyle && _area.style().width() > 0)
129 ? _area.style().width() : _width;
132 void AreaItem::setWidth(qreal width)
134 _width = width;
135 updateWidth();
138 void AreaItem::updateWidth()
140 prepareGeometryChange();
142 _pen.setWidthF(width() * pow(2, -_digitalZoom));
145 Qt::PenStyle AreaItem::penStyle() const
147 return _useStyle ? Qt::SolidLine : _penStyle;
150 void AreaItem::setPenStyle(Qt::PenStyle style)
152 _penStyle = style;
153 updatePenStyle();
156 void AreaItem::updatePenStyle()
158 _pen.setStyle(penStyle());
159 update();
162 void AreaItem::updateStyle()
164 updateColor();
165 updateWidth();
166 updatePenStyle();
169 void AreaItem::setDigitalZoom(int zoom)
171 if (_digitalZoom == zoom)
172 return;
174 prepareGeometryChange();
176 _digitalZoom = zoom;
177 _pen.setWidthF(width() * pow(2, -_digitalZoom));
180 void AreaItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
182 Q_UNUSED(event);
184 _pen.setWidthF((width() + 1) * pow(2, -_digitalZoom));
185 update();
188 void AreaItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
190 Q_UNUSED(event);
192 _pen.setWidthF(width() * pow(2, -_digitalZoom));
193 update();