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

172 lines
3.2 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"
2019-11-10 17:59:58 +01:00
QString 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());
2019-11-10 17:59:58 +01:00
return tt.toString();
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
{
_map = map;
_digitalZoom = 0;
_width = 2;
_opacity = 0.5;
QBrush brush(Qt::SolidPattern);
_pen = QPen(brush, _width);
updatePainterPath();
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
}
QPainterPath AreaItem::painterPath(const Polygon &polygon)
{
QPainterPath path;
const QVector<Coordinates> &lr = polygon.first();
path.moveTo(_map->ll2xy(lr.first()));
for (int i = 1; i < lr.size(); i++)
path.lineTo(_map->ll2xy(lr.at(i)));
path.closeSubpath();
for (int i = 1; i < polygon.size(); i++) {
const QVector<Coordinates> &lr = polygon.at(i);
QPainterPath hole;
hole.moveTo(_map->ll2xy(lr.first()));
for (int j = 1; j < lr.size(); j++)
hole.lineTo(_map->ll2xy(lr.at(j)));
hole.closeSubpath();
path = path.subtracted(hole);
}
return path;
}
void AreaItem::updatePainterPath()
{
_painterPath = QPainterPath();
for (int i = 0; i < _area.polygons().size(); i++)
_painterPath.addPath(painterPath(_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();
_map = map;
updatePainterPath();
}
void AreaItem::setColor(const QColor &color)
{
if (_pen.color() == color)
return;
QColor bc(color);
bc.setAlphaF(_opacity * color.alphaF());
_pen.setColor(color);
_brush = QBrush(bc);
update();
}
void AreaItem::setOpacity(qreal opacity)
{
if (_opacity == opacity)
return;
_opacity = opacity;
QColor bc(_pen.color());
bc.setAlphaF(_opacity * _pen.color().alphaF());
_brush = QBrush(bc);
update();
}
void AreaItem::setWidth(qreal width)
{
if (_width == width)
return;
prepareGeometryChange();
_width = width;
_pen.setWidthF(_width * pow(2, -_digitalZoom));
}
void AreaItem::setStyle(Qt::PenStyle style)
{
if (_pen.style() == style)
return;
_pen.setStyle(style);
update();
}
void AreaItem::setDigitalZoom(int zoom)
{
if (_digitalZoom == zoom)
return;
prepareGeometryChange();
_digitalZoom = zoom;
_pen.setWidthF(_width * pow(2, -_digitalZoom));
}
void AreaItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF((_width + 1) * pow(2, -_digitalZoom));
update();
}
void AreaItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(_width * pow(2, -_digitalZoom));
update();
}