mirror of
https://github.com/tumic0/GPXSee.git
synced 2025-02-07 12:05:14 +01:00
Fixed/improved path label layout algorithm
This commit is contained in:
parent
6d0ce2da7e
commit
197c3b91be
@ -29,24 +29,6 @@ static void swap(const QLineF &line, QPointF *p1, QPointF *p2)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p)
|
|
||||||
{
|
|
||||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.topRight()), p)
|
|
||||||
== QLineF::BoundedIntersection)
|
|
||||||
return true;
|
|
||||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
|
||||||
== QLineF::BoundedIntersection)
|
|
||||||
return true;
|
|
||||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
|
||||||
== QLineF::BoundedIntersection)
|
|
||||||
return true;
|
|
||||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
|
||||||
== QLineF::BoundedIntersection)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
||||||
QPointF *p2)
|
QPointF *p2)
|
||||||
{
|
{
|
||||||
@ -84,22 +66,42 @@ static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
|
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p)
|
||||||
|
{
|
||||||
|
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.topRight()), p)
|
||||||
|
== QLineF::BoundedIntersection)
|
||||||
|
return true;
|
||||||
|
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||||
|
== QLineF::BoundedIntersection)
|
||||||
|
return true;
|
||||||
|
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||||
|
== QLineF::BoundedIntersection)
|
||||||
|
return true;
|
||||||
|
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||||
|
== QLineF::BoundedIntersection)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static QPainterPath subpath(const QPolygonF &path, int start, int end,
|
||||||
qreal cut)
|
qreal cut)
|
||||||
{
|
{
|
||||||
qreal ss = 0, es = 0;
|
qreal ss = 0, es = 0;
|
||||||
int si = start, ei = end;
|
int si = start, ei = end;
|
||||||
|
|
||||||
for (int i = start; i <= end; i++) {
|
for (int i = start; i < end; i++) {
|
||||||
qreal len = lines.at(i).length();
|
QLineF l(path.at(i), path.at(i+1));
|
||||||
|
qreal len = l.length();
|
||||||
if (ss + len < cut / 2) {
|
if (ss + len < cut / 2) {
|
||||||
ss += len;
|
ss += len;
|
||||||
si++;
|
si++;
|
||||||
} else
|
} else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
for (int i = end; i >= start; i--) {
|
for (int i = end; i > start; i--) {
|
||||||
qreal len = lines.at(i).length();
|
QLineF l(path.at(i), path.at(i-1));
|
||||||
|
qreal len = l.length();
|
||||||
if (es + len < cut / 2) {
|
if (es + len < cut / 2) {
|
||||||
es += len;
|
es += len;
|
||||||
ei--;
|
ei--;
|
||||||
@ -107,116 +109,118 @@ static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
QLineF sl(lines.at(si).p2(), lines.at(si).p1());
|
QLineF sl(path.at(si+1), path.at(si));
|
||||||
sl.setLength(sl.length() - (cut / 2 - ss));
|
sl.setLength(sl.length() - (cut / 2 - ss));
|
||||||
QLineF el(lines.at(ei));
|
QLineF el(path.at(ei-1), path.at(ei));
|
||||||
el.setLength(el.length() - (cut / 2 - es));
|
el.setLength(el.length() - (cut / 2 - es));
|
||||||
|
|
||||||
QPainterPath p(sl.p2());
|
QPainterPath p(sl.p2());
|
||||||
for (int i = si; i <= ei; i++)
|
for (int i = si + 1; i < ei; i++)
|
||||||
p.lineTo(lines.at(i).p2());
|
p.lineTo(path.at(i));
|
||||||
p.setElementPositionAt(p.elementCount() - 1, el.p2().x(), el.p2().y());
|
p.lineTo(el.p2());
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QList<QLineF> lineString(const QPolygonF &path,
|
static QList<QPolygonF> polyLines(const QPolygonF &path, const QRectF &rect)
|
||||||
const QRectF &boundingRect)
|
|
||||||
{
|
{
|
||||||
QList<QLineF> lines;
|
QList<QPolygonF> lines;
|
||||||
int start = -1, end = -1;
|
QPolygonF line;
|
||||||
|
bool lastIn = rect.contains(path.first());
|
||||||
|
|
||||||
|
for (int i = 1; i < path.size(); i++) {
|
||||||
|
if (rect.contains(path.at(i))) {
|
||||||
|
if (lastIn) {
|
||||||
|
if (line.isEmpty())
|
||||||
|
line.append(path.at(i-1));
|
||||||
|
line.append(path.at(i));
|
||||||
|
} else {
|
||||||
|
QPointF p;
|
||||||
|
QLineF l(path.at(i-1), path.at(i));
|
||||||
|
|
||||||
for (int i = 0; i < path.count(); i++) {
|
if (intersection(l, rect, &p))
|
||||||
if (boundingRect.contains(path.at(i))) {
|
line.append(p);
|
||||||
start = i;
|
line.append(path.at(i));
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = path.count() - 1; i >= 0; i--) {
|
|
||||||
if (boundingRect.contains(path.at(i))) {
|
|
||||||
end = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start < 0) {
|
|
||||||
QPointF p1, p2;
|
|
||||||
|
|
||||||
for (int i = 1; i < path.count(); i++) {
|
|
||||||
QLineF l(path.at(i-1), path.at(i));
|
|
||||||
if (intersection(l, boundingRect, &p1, &p2)) {
|
|
||||||
lines.append(QLineF(p1, p2));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
QPointF p;
|
|
||||||
|
|
||||||
if (start > 0) {
|
lastIn = true;
|
||||||
QLineF l(path.at(start-1), path.at(start));
|
} else {
|
||||||
if (intersection(l, boundingRect, &p))
|
QLineF l(path.at(i-1), path.at(i));
|
||||||
lines.append(QLineF(p, path.at(start)));
|
|
||||||
}
|
if (lastIn) {
|
||||||
for (int i = start + 1; i <= end; i++)
|
QPointF p;
|
||||||
lines.append(QLineF(path.at(i-1), path.at(i)));
|
if (intersection(l, rect, &p))
|
||||||
if (end < path.count() - 1) {
|
line.append(p);
|
||||||
QLineF l(path.at(end), path.at(end+1));
|
lines.append(line);
|
||||||
if (intersection(l, boundingRect, &p))
|
line.clear();
|
||||||
lines.append(QLineF(path.at(end), p));
|
} else {
|
||||||
|
QPointF p1, p2;
|
||||||
|
if (intersection(l, rect, &p1, &p2)) {
|
||||||
|
line.append(p1);
|
||||||
|
line.append(p2);
|
||||||
|
lines.append(line);
|
||||||
|
line.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastIn = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!line.isEmpty())
|
||||||
|
lines.append(line);
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QList<QLineF> lineString(const QPainterPath &path,
|
static QList<QPolygonF> polyLines(const QPainterPath &path, const QRectF &rect)
|
||||||
const QRectF &boundingRect)
|
|
||||||
{
|
{
|
||||||
QList<QLineF> lines;
|
QList<QPolygonF> lines;
|
||||||
int start = -1, end = -1;
|
QPolygonF line;
|
||||||
|
bool lastIn = rect.contains(path.elementAt(0));
|
||||||
|
|
||||||
|
for (int i = 1; i < path.elementCount(); i++) {
|
||||||
|
if (rect.contains(path.elementAt(i))) {
|
||||||
|
if (lastIn) {
|
||||||
|
if (line.isEmpty())
|
||||||
|
line.append(path.elementAt(i-1));
|
||||||
|
line.append(path.elementAt(i));
|
||||||
|
} else {
|
||||||
|
QPointF p;
|
||||||
|
QLineF l(path.elementAt(i-1), path.elementAt(i));
|
||||||
|
|
||||||
for (int i = 0; i < path.elementCount(); i++) {
|
if (intersection(l, rect, &p))
|
||||||
if (boundingRect.contains(path.elementAt(i))) {
|
line.append(p);
|
||||||
start = i;
|
line.append(path.elementAt(i));
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = path.elementCount() - 1; i >= 0; i--) {
|
|
||||||
if (boundingRect.contains(path.elementAt(i))) {
|
|
||||||
end = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start < 0) {
|
|
||||||
QPointF p1, p2;
|
|
||||||
|
|
||||||
for (int i = 1; i < path.elementCount(); i++) {
|
|
||||||
QLineF l(path.elementAt(i-1), path.elementAt(i));
|
|
||||||
if (intersection(l, boundingRect, &p1, &p2)) {
|
|
||||||
lines.append(QLineF(p1, p2));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
QPointF p;
|
|
||||||
|
|
||||||
if (start > 0) {
|
lastIn = true;
|
||||||
QLineF l(path.elementAt(start-1), path.elementAt(start));
|
} else {
|
||||||
if (intersection(l, boundingRect, &p))
|
QLineF l(path.elementAt(i-1), path.elementAt(i));
|
||||||
lines.append(QLineF(p, path.elementAt(start)));
|
|
||||||
}
|
if (lastIn) {
|
||||||
for (int i = start + 1; i <= end; i++)
|
QPointF p;
|
||||||
lines.append(QLineF(path.elementAt(i-1), path.elementAt(i)));
|
if (intersection(l, rect, &p))
|
||||||
if (end < path.elementCount() - 1) {
|
line.append(p);
|
||||||
QLineF l(path.elementAt(end), path.elementAt(end+1));
|
lines.append(line);
|
||||||
if (intersection(l, boundingRect, &p))
|
line.clear();
|
||||||
lines.append(QLineF(path.elementAt(end), p));
|
} else {
|
||||||
|
QPointF p1, p2;
|
||||||
|
if (intersection(l, rect, &p1, &p2)) {
|
||||||
|
line.append(p1);
|
||||||
|
line.append(p2);
|
||||||
|
lines.append(line);
|
||||||
|
line.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastIn = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!line.isEmpty())
|
||||||
|
lines.append(line);
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,35 +232,40 @@ static bool reverse(const QPainterPath &path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
static QPainterPath textPath(const T &path, qreal textWidth,
|
static QPainterPath textPath(const T &path, qreal textWidth, qreal charWidth,
|
||||||
qreal charWidth, const QRectF &tileRect)
|
const QRectF &tileRect)
|
||||||
{
|
{
|
||||||
QList<QLineF> lines(lineString(path, tileRect));
|
if (path.isEmpty())
|
||||||
if (lines.isEmpty())
|
|
||||||
return QPainterPath();
|
return QPainterPath();
|
||||||
qreal length = 0;
|
|
||||||
qreal angle = lines.first().angle();
|
QList<QPolygonF> lines(polyLines(path, tileRect));
|
||||||
int last = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < lines.size(); i++) {
|
for (int i = 0; i < lines.size(); i++) {
|
||||||
qreal sl = lines.at(i).length();
|
const QPolygonF &pl = lines.at(i);
|
||||||
qreal a = lines.at(i).angle();
|
qreal angle, length = 0;
|
||||||
|
int last = 0;
|
||||||
|
|
||||||
if (!tileRect.contains(lines.at(i).p2()) || sl < charWidth
|
for (int j = 1; j < pl.size(); j ++) {
|
||||||
|| qAbs(angle - a) > MAX_TEXT_ANGLE) {
|
QLineF l(pl.at(j-1), pl.at(j));
|
||||||
if (length > textWidth)
|
qreal sl = l.length();
|
||||||
return subpath(lines, last, i - 1, length - textWidth);
|
qreal a = l.angle();
|
||||||
last = i;
|
|
||||||
length = 0;
|
|
||||||
} else
|
|
||||||
length += sl;
|
|
||||||
|
|
||||||
angle = a;
|
if ((sl < charWidth) || (j > 1 && qAbs(angle - a) > MAX_TEXT_ANGLE)) {
|
||||||
|
if (length > textWidth)
|
||||||
|
return subpath(pl, last, j - 1, length - textWidth);
|
||||||
|
last = j;
|
||||||
|
length = 0;
|
||||||
|
} else
|
||||||
|
length += sl;
|
||||||
|
|
||||||
|
angle = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length > textWidth)
|
||||||
|
return subpath(pl, last, pl.size() - 1, length - textWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (length > textWidth)
|
return QPainterPath();
|
||||||
? subpath(lines, last, lines.size() - 1, length - textWidth)
|
|
||||||
: QPainterPath();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user