Commit 41a85714 authored by Volker Krause's avatar Volker Krause
Browse files

Fix hit detection for labels with a fixed (maximum) text width

QStaticText will always report the maximum size, which can result in
too large bounding boxes and thus wrong hits on nearby labels. To fix
this we now compute the text width manually in that case, and adjust the
hit box accordingly.
parent 7da03c22
Pipeline #273325 passed with stage
in 2 minutes and 10 seconds
......@@ -12,6 +12,7 @@
#include "view.h"
#include <QBrush>
#include <QFontMetrics>
using namespace KOSMIndoorMap;
......@@ -108,6 +109,18 @@ bool HitDetector::itemContainsPoint(const PolylineItem *item, QPointF scenePos,
bool HitDetector::itemContainsPoint(const LabelItem *item, QPointF screenPos, const View *view) const
auto hitBox = item->boundingRect();
// QStaticText::size doesn't return the actual bounding box with QStaticText::textWidth is set,
// so we need to compute that manually here to not end up with overly large hitboxes
if (item->text.textWidth() > 0) {
double width = QFontMetrics(item->font).horizontalAdvance(item->text.text());
if (!item->icon.isNull()) {
width = std::max(width, item->iconSize.width());
width += std::max(item->frameWidth, item->haloRadius) + item->casingWidth;
const auto widthDelta = (hitBox.width() - width) / 2.0;
hitBox.adjust(widthDelta, 0, -widthDelta, 0);
return hitBox.contains(screenPos);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment