1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 13:41:16 +01:00
GPXSee/src/GUI/areaitem.cpp

195 lines
3.5 KiB
C++
Raw Normal View History

2019-01-31 01:46:53 +01:00
#include <cmath>
#include <QApplication>
#include <QCursor>
#include <QPainter>
2019-10-14 20:07:05 +02:00
#include <QGraphicsSceneMouseEvent>
2019-01-31 01:46:53 +01:00
#include "map/map.h"
2019-10-14 20:07:05 +02:00
#include "popup.h"
2020-12-02 23:58:11 +01:00
#include "tooltip.h"
2019-01-31 01:46:53 +01:00
#include "areaitem.h"
ToolTip AreaItem::info() const
2019-01-31 01:46:53 +01:00
{
ToolTip tt;
if (!_area.name().isEmpty())
tt.insert(qApp->translate("PolygonItem", "Name"), _area.name());
if (!_area.description().isEmpty())
tt.insert(qApp->translate("PolygonItem", "Description"),
_area.description());
return tt;
2019-01-31 01:46:53 +01:00
}
2019-11-10 17:59:58 +01:00
AreaItem::AreaItem(const Area &area, Map *map, GraphicsItem *parent)
2020-12-02 23:58:11 +01:00
: PlaneItem(parent), _area(area)
2019-01-31 01:46:53 +01:00
{
_digitalZoom = 0;
2022-09-25 02:15:24 +02:00
_width = 2;
_opacity = 0.5;
_color = Qt::black;
_penStyle = Qt::SolidLine;
2019-01-31 01:46:53 +01:00
2022-09-25 02:15:24 +02:00
_pen = QPen(strokeColor(), width());
_brush = QBrush(fillColor());
2019-01-31 01:46:53 +01:00
updatePainterPath(map);
2019-01-31 01:46:53 +01:00
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
}
QPainterPath AreaItem::painterPath(Map *map, const Polygon &polygon)
2019-01-31 01:46:53 +01:00
{
QPainterPath path;
2021-04-10 15:27:40 +02:00
for (int i = 0; i < polygon.size(); i++) {
const QVector<Coordinates> &subpath = polygon.at(i);
path.moveTo(map->ll2xy(subpath.first()));
2021-04-10 15:27:40 +02:00
for (int j = 1; j < subpath.size(); j++)
path.lineTo(map->ll2xy(subpath.at(j)));
2021-03-09 23:21:10 +01:00
path.closeSubpath();
2019-01-31 01:46:53 +01:00
}
return path;
}
void AreaItem::updatePainterPath(Map *map)
2019-01-31 01:46:53 +01:00
{
_painterPath = QPainterPath();
for (int i = 0; i < _area.polygons().size(); i++)
_painterPath.addPath(painterPath(map, _area.polygons().at(i)));
2019-01-31 01:46:53 +01:00
}
void AreaItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
2019-02-16 09:57:47 +01:00
painter->setPen(_width ? _pen : QPen(Qt::NoPen));
2019-01-31 01:46:53 +01:00
painter->drawPath(_painterPath);
painter->fillPath(_painterPath, _brush);
/*
QPen p = QPen(QBrush(Qt::red), 0);
painter->setPen(p);
painter->drawRect(boundingRect());
*/
}
void AreaItem::setMap(Map *map)
{
prepareGeometryChange();
updatePainterPath(map);
2019-01-31 01:46:53 +01:00
}
2022-09-25 02:15:24 +02:00
const QColor &AreaItem::strokeColor() const
2019-01-31 01:46:53 +01:00
{
2022-09-25 02:15:24 +02:00
return (_useStyle && _area.style().isValid())
? _area.style().stroke() : _color;
}
QColor AreaItem::fillColor() const
{
if (_useStyle && _area.style().isValid())
return _area.style().fill();
else {
QColor fc(_color);
fc.setAlphaF(_opacity * _color.alphaF());
return fc;
}
}
2019-01-31 01:46:53 +01:00
2022-09-25 02:15:24 +02:00
void AreaItem::setColor(const QColor &color)
{
_color = color;
updateColor();
}
2019-01-31 01:46:53 +01:00
2022-09-25 02:15:24 +02:00
void AreaItem::updateColor()
{
_pen.setColor(strokeColor());
_brush = QBrush(fillColor());
2019-01-31 01:46:53 +01:00
update();
}
void AreaItem::setOpacity(qreal opacity)
{
_opacity = opacity;
2022-09-25 02:15:24 +02:00
updateColor();
}
2019-01-31 01:46:53 +01:00
2022-09-25 02:15:24 +02:00
qreal AreaItem::width() const
{
return (_useStyle && _area.style().width() > 0)
? _area.style().width() : _width;
2019-01-31 01:46:53 +01:00
}
void AreaItem::setWidth(qreal width)
{
2022-09-25 02:15:24 +02:00
_width = width;
updateWidth();
}
2019-01-31 01:46:53 +01:00
2022-09-25 02:15:24 +02:00
void AreaItem::updateWidth()
{
2019-01-31 01:46:53 +01:00
prepareGeometryChange();
2022-09-25 02:15:24 +02:00
_pen.setWidthF(width() * pow(2, -_digitalZoom));
2019-01-31 01:46:53 +01:00
}
2022-09-25 02:15:24 +02:00
Qt::PenStyle AreaItem::penStyle() const
2019-01-31 01:46:53 +01:00
{
2022-09-25 02:15:24 +02:00
return _useStyle ? Qt::SolidLine : _penStyle;
}
void AreaItem::setPenStyle(Qt::PenStyle style)
{
_penStyle = style;
updatePenStyle();
}
2019-01-31 01:46:53 +01:00
2022-09-25 02:15:24 +02:00
void AreaItem::updatePenStyle()
{
_pen.setStyle(penStyle());
2019-01-31 01:46:53 +01:00
update();
}
2022-09-25 02:15:24 +02:00
void AreaItem::updateStyle()
{
updateColor();
updateWidth();
updatePenStyle();
}
2019-01-31 01:46:53 +01:00
void AreaItem::setDigitalZoom(int zoom)
{
if (_digitalZoom == zoom)
return;
prepareGeometryChange();
_digitalZoom = zoom;
2023-02-14 00:58:44 +01:00
_pen.setWidthF(width() * pow(2, -_digitalZoom));
2019-01-31 01:46:53 +01:00
}
void AreaItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
2023-02-14 00:55:19 +01:00
_pen.setWidthF((width() + 1) * pow(2, -_digitalZoom));
update();
}
void AreaItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
2023-02-14 00:55:19 +01:00
_pen.setWidthF(width() * pow(2, -_digitalZoom));
update();
}