Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88d0a52
commit 9a96611
Showing
10 changed files
with
267 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#include "kmlgeopolygon.h" | ||
|
||
#include <QPainter> | ||
#include <QGeoRectangle> | ||
#include <math.h> | ||
|
||
KmlGeoPolygon::KmlGeoPolygon(QQuickItem *parent): | ||
QQuickPaintedItem(parent), | ||
m_map(nullptr) | ||
{ | ||
setRenderTarget(QQuickPaintedItem::FramebufferObject); | ||
setAntialiasing(false); | ||
setMipmap(true); | ||
} | ||
|
||
void KmlGeoPolygon::registerQmlType() | ||
{ | ||
qmlRegisterType<KmlGeoPolygon>("KmlGeoPolygon", 1, 0, "KmlGeoPolygon"); | ||
} | ||
|
||
void KmlGeoPolygon::clearPolygon() | ||
{ | ||
m_polygon.clear(); | ||
m_holes.clear(); | ||
} | ||
|
||
void KmlGeoPolygon::prepareForDrawing() | ||
{ | ||
QRegion clip(m_polygon); | ||
|
||
for(int i = 0; i < m_holes.size(); i++) | ||
clip -= QRegion(m_holes[i]); | ||
|
||
QRegion window(QRect(x(), y(), width(), height())); | ||
m_clip = clip;//.intersected(window); | ||
} | ||
|
||
void KmlGeoPolygon::setGeoPolygon(const QGeoPolygon &geoPolygon) | ||
{ | ||
m_geoPolygon = geoPolygon; | ||
recalcCoordinates(); | ||
prepareForDrawing(); | ||
} | ||
|
||
QGeoPolygon KmlGeoPolygon::getGeoPolygon() const | ||
{ | ||
return m_geoPolygon; | ||
} | ||
|
||
void KmlGeoPolygon::setMap(QObject *map) | ||
{ | ||
m_map = map; | ||
recalcCoordinates(); | ||
prepareForDrawing(); | ||
} | ||
|
||
QObject *KmlGeoPolygon::getMap() const | ||
{ | ||
return m_map; | ||
} | ||
|
||
QGeoShape KmlGeoPolygon::getArea() const | ||
{ | ||
return m_area; | ||
} | ||
|
||
void KmlGeoPolygon::setArea(const QGeoShape &area) | ||
{ | ||
m_area = area; | ||
if(m_map) | ||
{ | ||
recalcCoordinates(); | ||
prepareForDrawing(); | ||
update(); | ||
} | ||
} | ||
|
||
void KmlGeoPolygon::setColor(const QColor &color) | ||
{ | ||
if(m_color != color) | ||
{ | ||
m_color = color; | ||
emit colorChanged(); | ||
update(); | ||
} | ||
} | ||
|
||
QColor KmlGeoPolygon::getColor() const | ||
{ | ||
return m_color; | ||
} | ||
|
||
void KmlGeoPolygon::paint(QPainter *painter) | ||
{ | ||
painter->setPen(m_color); | ||
|
||
painter->setClipRegion(m_clip); | ||
QPainterPath p; | ||
p.addPolygon(m_polygon); | ||
painter->fillPath(p, QBrush(m_color, Qt::SolidPattern)); | ||
} | ||
|
||
QPoint KmlGeoPolygon::fromCoordinate(const QGeoCoordinate &c) | ||
{ | ||
QPointF point; | ||
QMetaObject::invokeMethod(m_map, "fromCoordinate", | ||
Q_RETURN_ARG(QPointF, point), | ||
Q_ARG(QGeoCoordinate, c), | ||
Q_ARG(bool, false)); | ||
return point.toPoint(); | ||
} | ||
|
||
void KmlGeoPolygon::recalcCoordinates() | ||
{ | ||
clearPolygon(); | ||
auto visibleRegion = m_map->property("visibleRegion").value<QGeoShape>().boundingGeoRectangle(); | ||
QRect vr(fromCoordinate(visibleRegion.topLeft()), | ||
fromCoordinate(visibleRegion.bottomRight())); | ||
for(int i = 0; i < m_geoPolygon.size(); i++) | ||
{ | ||
QPoint point = fromCoordinate(m_geoPolygon.coordinateAt(i)); | ||
if(std::isnan(point.x()) || std::isnan(point.y())) | ||
continue; | ||
|
||
m_polygon.append(point); | ||
} | ||
for(int j = 0; j < m_geoPolygon.holesCount(); j++) | ||
{ | ||
QPolygon polyHole; | ||
for(int k = 0; k < m_geoPolygon.hole(j).size(); k++) | ||
{ | ||
QPoint point = fromCoordinate(m_geoPolygon.hole(j)[k].value<QGeoCoordinate>()); | ||
if(std::isnan(point.x()) || std::isnan(point.y())) | ||
continue; | ||
polyHole.append(point); | ||
} | ||
m_holes.append(polyHole); | ||
} | ||
|
||
setX(vr.x()); | ||
setY(vr.y()); | ||
setWidth(vr.width()); | ||
setHeight(vr.height()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef KMLGEOPOLYGON_H | ||
#define KMLGEOPOLYGON_H | ||
|
||
#include <QGeoPolygon> | ||
#include <QPolygon> | ||
#include <QQuickPaintedItem> | ||
|
||
class KmlGeoPolygon: public QQuickPaintedItem | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(QGeoPolygon geoPolygon READ getGeoPolygon WRITE setGeoPolygon) | ||
Q_PROPERTY(QObject *map READ getMap WRITE setMap); | ||
Q_PROPERTY(QGeoShape area READ getArea WRITE setArea); | ||
Q_PROPERTY(QColor color READ getColor WRITE setColor NOTIFY colorChanged); | ||
public: | ||
|
||
explicit KmlGeoPolygon(QQuickItem *parent = 0); | ||
|
||
static void registerQmlType(); | ||
|
||
void setGeoPolygon(const QGeoPolygon &geoPolygon); | ||
QGeoPolygon getGeoPolygon() const; | ||
|
||
void setMap(QObject *map); | ||
QObject* getMap() const; | ||
|
||
QGeoShape getArea() const; | ||
void setArea(const QGeoShape &area); | ||
|
||
void setColor(const QColor &color); | ||
QColor getColor() const; | ||
|
||
void paint(QPainter *painter); | ||
|
||
private: | ||
QObject *m_map; | ||
QGeoShape m_area; | ||
QGeoPolygon m_geoPolygon; | ||
QPolygon m_polygon; | ||
QVector<QPolygon> m_holes; | ||
QColor m_color; | ||
QRegion m_clip; | ||
|
||
QPoint fromCoordinate(const QGeoCoordinate &c); | ||
|
||
void clearPolygon(); | ||
void prepareForDrawing(); | ||
void recalcCoordinates(); | ||
|
||
signals: | ||
void colorChanged(); | ||
}; | ||
|
||
#endif // KMLGEOPOLYGON_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.