Improve marker handling when at the edge of a graph

This commit is contained in:
Jan Käberich 2025-11-15 19:03:41 +01:00
parent 87ab2fccca
commit b6718c57b2
4 changed files with 47 additions and 27 deletions

View file

@ -562,6 +562,10 @@ Marker *TracePlot::markerAtPosition(QPoint p, bool onlyMovable)
// invalid, skip // invalid, skip
continue; continue;
} }
if(!positionWithinGraphArea(markerPoint) && !Preferences::getInstance().Marker.clipToYAxis) {
// this marker is currently not visible on the graph, do not accept it as a valid choice
continue;
}
auto diff = markerPoint - p; auto diff = markerPoint - p;
unsigned int distance = diff.x() * diff.x() + diff.y() * diff.y(); unsigned int distance = diff.x() * diff.x() + diff.y() * diff.y();
if(distance < closestDistance) { if(distance < closestDistance) {

View file

@ -130,7 +130,7 @@ bool TracePolar::positionWithinGraphArea(const QPoint &p)
QPoint TracePolar::dataToPixel(std::complex<double> d) QPoint TracePolar::dataToPixel(std::complex<double> d)
{ {
return transform.map(QPoint(d.real() * polarCoordMax * (1.0 / edgeReflection), -d.imag() * polarCoordMax * (1.0 / edgeReflection))); return transform.map(QPoint(round(d.real() * polarCoordMax * (1.0 / edgeReflection)), round(-d.imag() * polarCoordMax * (1.0 / edgeReflection))));
} }
QPoint TracePolar:: dataToPixel(Trace::Data d) QPoint TracePolar:: dataToPixel(Trace::Data d)
@ -165,13 +165,22 @@ std::complex<double> TracePolar::pixelToData(QPoint p)
QPoint TracePolar::markerToPixel(Marker *m) QPoint TracePolar::markerToPixel(Marker *m)
{ {
QPoint ret = QPoint(); QPoint ret = QPoint();
// if(!m->isTimeDomain()) { if(m->getPosition() >= sweep_fmin && m->getPosition() <= sweep_fmax) {
if(m->getPosition() >= sweep_fmin && m->getPosition() <= sweep_fmax) { auto d = m->getData();
auto d = m->getData(); d = dataAddOffset(d);
d = dataAddOffset(d); if(abs(d) > edgeReflection && limitToEdge) {
ret = dataToPixel(d); // marker position is outside of the graph
auto &pref = Preferences::getInstance();
if(pref.Marker.clipToYAxis) {
// we still want to show the marker but at the edge of the graph
d /= (abs(d) / edgeReflection);
} else {
// we do not show the marker
return ret;
}
} }
// } ret = dataToPixel(d);
}
return ret; return ret;
} }
@ -188,6 +197,10 @@ double TracePolar::nearestTracePoint(Trace *t, QPoint pixel, double *distance)
continue; continue;
} }
data = dataAddOffset(data); data = dataAddOffset(data);
if(limitToEdge && abs(data.y) > edgeReflection) {
// this trace point is outside of the visible range, clip to circle
data.y /= (abs(data.y) / edgeReflection);
}
auto plotPoint = dataToPixel(data); auto plotPoint = dataToPixel(data);
if (plotPoint.isNull()) { if (plotPoint.isNull()) {
// destination point outside of currently displayed range // destination point outside of currently displayed range

View file

@ -367,14 +367,16 @@ void TraceSmithChart::draw(QPainter &p) {
// marker not in trace range // marker not in trace range
continue; continue;
} }
auto coords = m->getData(); auto point = markerToPixel(m);
coords = dataAddOffset(coords); if(point.isNull()) {
// marker point is invalid
if (limitToEdge && abs(coords) > edgeReflection) {
// outside of visible area
continue; continue;
} }
auto point = dataToPixel(coords);
// if (limitToEdge && abs(pixelToData(point)) > edgeReflection) {
// // outside of visible area
// continue;
// }
auto symbol = m->getSymbol(); auto symbol = m->getSymbol();
p.drawPixmap(point.x() - symbol.width()/2, point.y() - symbol.height(), symbol); p.drawPixmap(point.x() - symbol.width()/2, point.y() - symbol.height(), symbol);
} }

View file

@ -452,8 +452,8 @@ void TraceXYPlot::draw(QPainter &p)
plotAreaWidth -= yAxisDisabledSpace; plotAreaWidth -= yAxisDisabledSpace;
} }
auto plotRect = QRect(plotAreaLeft, plotAreaTop, plotAreaWidth + 1, plotAreaBottom-plotAreaTop); auto plotRect = QRect(plotAreaLeft, plotAreaTop, plotAreaWidth + 1, plotAreaBottom-plotAreaTop + 1);
p.drawRect(plotRect); p.drawRect(QRect(plotAreaLeft, plotAreaTop, plotAreaWidth, plotAreaBottom-plotAreaTop));
// draw axis types // draw axis types
auto font = p.font(); auto font = p.font();
@ -490,7 +490,7 @@ void TraceXYPlot::draw(QPainter &p)
int lastTickLabelEnd = std::numeric_limits<int>::max(); int lastTickLabelEnd = std::numeric_limits<int>::max();
for(unsigned int j = 0; j < yAxis[i].getTicks().size(); j++) { for(unsigned int j = 0; j < yAxis[i].getTicks().size(); j++) {
auto yCoord = yAxis[i].transform(yAxis[i].getTicks()[j], w.height() - xAxisSpace, plotAreaTop); auto yCoord = yAxis[i].transform(yAxis[i].getTicks()[j], plotAreaBottom, plotAreaTop);
p.setPen(QPen(pref.Graphs.Color.axis, 1)); p.setPen(QPen(pref.Graphs.Color.axis, 1));
// draw tickmark on axis // draw tickmark on axis
auto tickStart = i == 0 ? plotAreaLeft : plotAreaLeft + plotAreaWidth; auto tickStart = i == 0 ? plotAreaLeft : plotAreaLeft + plotAreaWidth;
@ -630,15 +630,6 @@ void TraceXYPlot::draw(QPainter &p)
p.drawLine(p1, p2); p.drawLine(p1, p2);
} }
if(pref.Marker.clipToYAxis) {
// clip Y coordinate of markers to visible area (always show markers, even when out of range)
if(point.y() < plotRect.top()) {
point.ry() = plotRect.top();
} else if(point.y() > plotRect.bottom()) {
point.ry() = plotRect.bottom();
}
}
if(!plotRect.contains(point)) { if(!plotRect.contains(point)) {
// out of screen // out of screen
continue; continue;
@ -1116,8 +1107,8 @@ QPointF TraceXYPlot::traceToCoordinate(Trace *t, unsigned int sample, YAxis &yax
QPoint TraceXYPlot::plotValueToPixel(QPointF plotValue, int Yaxis) QPoint TraceXYPlot::plotValueToPixel(QPointF plotValue, int Yaxis)
{ {
QPoint p; QPoint p;
p.setX(xAxis.transform(plotValue.x(), plotAreaLeft, plotAreaLeft + plotAreaWidth)); p.setX(round(xAxis.transform(plotValue.x(), plotAreaLeft, plotAreaLeft + plotAreaWidth)));
p.setY(yAxis[Yaxis].transform(plotValue.y(), plotAreaBottom, plotAreaTop)); p.setY(round(yAxis[Yaxis].transform(plotValue.y(), plotAreaBottom, plotAreaTop)));
return p; return p;
} }
@ -1156,6 +1147,14 @@ QPoint TraceXYPlot::markerToPixel(Marker *m)
} else { } else {
markerPoint = traceToCoordinate(t, t->index(xPosition), yAxis[0]); markerPoint = traceToCoordinate(t, t->index(xPosition), yAxis[0]);
} }
auto &pref = Preferences::getInstance();
if(pref.Marker.clipToYAxis) {
if(markerPoint.y() > yAxis[0].getRangeMax()) {
markerPoint.setY(yAxis[0].getRangeMax());
} else if(markerPoint.y() < yAxis[0].getRangeMin()) {
markerPoint.setY(yAxis[0].getRangeMin());
}
}
return plotValueToPixel(markerPoint, 0); return plotValueToPixel(markerPoint, 0);
} }
@ -1175,6 +1174,8 @@ double TraceXYPlot::nearestTracePoint(Trace *t, QPoint pixel, double *distance)
continue; continue;
} }
auto plotPoint = plotValueToPixel(point, 0); auto plotPoint = plotValueToPixel(point, 0);
// constrain to the visible Y axis range
Util::constrain(plotPoint.ry(), plotAreaTop, plotAreaBottom);
QPointF diff = plotPoint - pixel; QPointF diff = plotPoint - pixel;
auto distance = diff.x() * diff.x() + diff.y() * diff.y(); auto distance = diff.x() * diff.x() + diff.y() * diff.y();
if(distance < closestDistance) { if(distance < closestDistance) {