LCOV - code coverage report
Current view: top level - muse/framework/uicomponents/qml/Muse/UiComponents - polylineplot.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 0.0 % 810 0
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2026 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "polylineplot.h"
      24              : 
      25              : #include <QCursor>
      26              : #include <QPainter>
      27              : #include <QPainterPath>
      28              : #include <QPen>
      29              : #include <QBrush>
      30              : #include <QHoverEvent>
      31              : #include <QMouseEvent>
      32              : 
      33              : #include <algorithm>
      34              : #include <cmath>
      35              : 
      36              : #include "realfn.h"
      37              : 
      38              : using namespace muse::uicomponents;
      39              : 
      40              : namespace {
      41              : constexpr double MOVE_THRESHOLD = 3.0;
      42              : constexpr double EPSILON = 1e-12;
      43              : constexpr double BOUNDARY_MARGIN = 0.1;
      44              : 
      45              : static constexpr int INVALID_POINT_IDX = -1;
      46              : static constexpr int PENDING_POINT_IDX = -2;
      47              : 
      48            0 : static inline qreal toPxX(const QQuickItem* item, qreal xN)
      49              : {
      50            0 :     return xN * item->width();
      51              : }
      52              : 
      53            0 : static inline qreal toPxY(const QQuickItem* item, qreal yN)
      54              : {
      55            0 :     const qreal h = item->height();
      56            0 :     if (h <= 0.0) {
      57            0 :         return 0.0;
      58              :     }
      59              : 
      60              :     // NOTE: preserve full height of the line at top/bottom edges
      61            0 :     const auto* polyline = static_cast<const PolylinePlot*>(item);
      62            0 :     const qreal inset = std::clamp(polyline->lineWidth() * 0.5, 0.0, h * 0.5);
      63            0 :     const qreal drawableHeight = h - (2.0 * inset);
      64              : 
      65            0 :     return inset + (1.0 - yN) * drawableHeight;
      66              : }
      67              : 
      68            0 : static qreal pointToSegmentDistance(const QPointF& point, const QPointF& segmentStart, const QPointF& segmentEnd)
      69              : {
      70            0 :     const QPointF segment = segmentEnd - segmentStart;
      71            0 :     const qreal segmentLengthSquared = QPointF::dotProduct(segment, segment);
      72            0 :     if (segmentLengthSquared <= EPSILON) {
      73            0 :         return std::hypot(point.x() - segmentStart.x(), point.y() - segmentStart.y());
      74              :     }
      75              : 
      76            0 :     const QPointF segmentStartToPoint = point - segmentStart;
      77            0 :     qreal t = QPointF::dotProduct(segmentStartToPoint, segment) / segmentLengthSquared;
      78            0 :     t = std::max<qreal>(0.0, std::min<qreal>(1.0, t));
      79              : 
      80            0 :     const QPointF closestPoint = segmentStart + segment * t;
      81            0 :     return std::hypot(point.x() - closestPoint.x(), point.y() - closestPoint.y());
      82              : }
      83              : 
      84            0 : static GhostPoint ghostPointToSegmentDistance(const QPointF& point, const QPointF& segmentStart, const QPointF& segmentEnd)
      85              : {
      86            0 :     const QPointF segment = segmentEnd - segmentStart;
      87            0 :     const qreal segmentLengthSquared = QPointF::dotProduct(segment, segment);
      88            0 :     if (segmentLengthSquared <= EPSILON) {
      89            0 :         const qreal distance = std::hypot(point.x() - segmentStart.x(), point.y() - segmentStart.y());
      90            0 :         return { segmentStart, distance };
      91              :     }
      92              : 
      93            0 :     const QPointF segmentStartToPoint = point - segmentStart;
      94            0 :     qreal t = QPointF::dotProduct(segmentStartToPoint, segment) / segmentLengthSquared;
      95            0 :     t = std::max<qreal>(0.0, std::min<qreal>(1.0, t));
      96              : 
      97            0 :     const QPointF closestPoint = segmentStart + segment * t;
      98            0 :     const qreal distance = std::hypot(point.x() - closestPoint.x(), point.y() - closestPoint.y());
      99            0 :     return { closestPoint, distance };
     100              : }
     101              : 
     102              : // NOTE: can be replaced with std::lerp in C++20
     103            0 : static double lerp(double a, double b, double t)
     104              : {
     105            0 :     return a + (b - a) * t;
     106              : }
     107              : 
     108              : // compute Y at X using linear interpolation
     109            0 : static double valueAtX(const QVector<QPointF>& sortedPoints, double x)
     110              : {
     111            0 :     if (sortedPoints.isEmpty()) {
     112            0 :         return 0.0;
     113              :     }
     114              : 
     115            0 :     if (x <= sortedPoints.front().x()) {
     116            0 :         return sortedPoints.front().y();
     117              :     }
     118              : 
     119            0 :     if (x >= sortedPoints.back().x()) {
     120            0 :         return sortedPoints.back().y();
     121              :     }
     122              : 
     123            0 :     for (int i = 0; i < sortedPoints.size() - 1; ++i) {
     124            0 :         const auto& a = sortedPoints[i];
     125            0 :         const auto& b = sortedPoints[i + 1];
     126            0 :         if (x >= a.x() && x <= b.x()) {
     127            0 :             const double dx = b.x() - a.x();
     128            0 :             if (std::abs(dx) <= EPSILON) {
     129            0 :                 return a.y();
     130              :             }
     131            0 :             const double t = (x - a.x()) / dx;
     132            0 :             return lerp(a.y(), b.y(), t);
     133              :         }
     134              :     }
     135              : 
     136            0 :     return sortedPoints.back().y();
     137              : }
     138              : }
     139              : 
     140            0 : PolylinePlot::PolylinePlot(QQuickItem* parent)
     141            0 :     : QQuickPaintedItem(parent)
     142              : {
     143            0 :     setAcceptHoverEvents(true);
     144            0 :     setAcceptedMouseButtons(Qt::LeftButton);
     145              : 
     146            0 :     setAntialiasing(true);
     147            0 :     setOpaquePainting(false);
     148              : 
     149            0 :     m_standardPointStyle = new PolylinePointStyle(this);
     150            0 :     QObject::connect(m_standardPointStyle, &PolylinePointStyle::styleChanged, this, [this]() {
     151            0 :         update();
     152            0 :     });
     153              : 
     154            0 :     m_ghostPointStyle = new PolylinePointStyle(this);
     155            0 :     QObject::connect(m_ghostPointStyle, &PolylinePointStyle::styleChanged, this, [this]() {
     156            0 :         update();
     157            0 :     });
     158              : 
     159            0 :     m_selectedPointStyle = new PolylinePointStyle(this);
     160            0 :     QObject::connect(m_selectedPointStyle, &PolylinePointStyle::styleChanged, this, [this]() {
     161            0 :         update();
     162            0 :     });
     163            0 : }
     164              : 
     165            0 : void PolylinePlot::cancelEdit()
     166              : {
     167            0 :     m_hoveredOnLine = false;
     168            0 :     resetGestureState();
     169            0 : }
     170              : 
     171            0 : PolylinePointStyle* PolylinePlot::standardPointStyle()
     172              : {
     173            0 :     return m_standardPointStyle;
     174              : }
     175              : 
     176            0 : PolylinePointStyle* PolylinePlot::ghostPointStyle()
     177              : {
     178            0 :     return m_ghostPointStyle;
     179              : }
     180              : 
     181            0 : PolylinePointStyle* PolylinePlot::selectedPointStyle()
     182              : {
     183            0 :     return m_selectedPointStyle;
     184              : }
     185              : 
     186            0 : bool PolylinePlot::ghostPointsEnabled() const
     187              : {
     188            0 :     return m_ghostPointsEnabled;
     189              : }
     190              : 
     191            0 : void PolylinePlot::setGhostPointsEnabled(bool v)
     192              : {
     193            0 :     if (m_ghostPointsEnabled == v) {
     194            0 :         return;
     195              :     }
     196              : 
     197            0 :     m_ghostPointsEnabled = v;
     198            0 :     emit ghostPointsEnabledChanged();
     199              : 
     200            0 :     update();
     201              : }
     202              : 
     203            0 : bool PolylinePlot::selectedPointsEnabled() const
     204              : {
     205            0 :     return m_selectedPointsEnabled;
     206              : }
     207              : 
     208            0 : void PolylinePlot::setSelectedPointsEnabled(bool v)
     209              : {
     210            0 :     if (m_selectedPointsEnabled == v) {
     211            0 :         return;
     212              :     }
     213              : 
     214            0 :     m_selectedPointsEnabled = v;
     215            0 :     emit selectedPointsEnabledChanged();
     216              : 
     217            0 :     update();
     218              : }
     219              : 
     220            0 : QColor PolylinePlot::lineColor() const
     221              : {
     222            0 :     return m_lineColor;
     223              : }
     224              : 
     225            0 : void PolylinePlot::setLineColor(const QColor& c)
     226              : {
     227            0 :     if (m_lineColor == c) {
     228            0 :         return;
     229              :     }
     230              : 
     231            0 :     m_lineColor = c;
     232            0 :     emit lineColorChanged();
     233            0 :     update();
     234              : }
     235              : 
     236            0 : qreal PolylinePlot::lineWidth() const
     237              : {
     238            0 :     return m_lineWidth;
     239              : }
     240              : 
     241            0 : void PolylinePlot::setLineWidth(qreal w)
     242              : {
     243            0 :     if (m_lineWidth == w) {
     244            0 :         return;
     245              :     }
     246              : 
     247            0 :     m_lineWidth = w;
     248            0 :     emit lineWidthChanged();
     249              : 
     250            0 :     update();
     251              : }
     252              : 
     253            0 : bool PolylinePlot::drawBackground() const
     254              : {
     255            0 :     return m_drawBackground;
     256              : }
     257              : 
     258            0 : void PolylinePlot::setDrawBackground(bool v)
     259              : {
     260            0 :     if (m_drawBackground == v) {
     261            0 :         return;
     262              :     }
     263              : 
     264            0 :     m_drawBackground = v;
     265            0 :     emit drawBackgroundChanged();
     266              : 
     267            0 :     update();
     268              : }
     269              : 
     270            0 : qreal PolylinePlot::baselineN() const
     271              : {
     272            0 :     return m_baselineN;
     273              : }
     274              : 
     275            0 : void PolylinePlot::setBaselineN(qreal v)
     276              : {
     277            0 :     v = clamp01(v);
     278              : 
     279            0 :     if (m_baselineN == v) {
     280            0 :         return;
     281              :     }
     282              : 
     283            0 :     m_baselineN = v;
     284            0 :     emit baselineNChanged();
     285              : 
     286            0 :     update();
     287              : }
     288              : 
     289            0 : qreal PolylinePlot::hitRadius() const
     290              : {
     291            0 :     return m_hitRadius;
     292              : }
     293              : 
     294            0 : void PolylinePlot::setHitRadius(qreal r)
     295              : {
     296            0 :     if (m_hitRadius == r) {
     297            0 :         return;
     298              :     }
     299              : 
     300            0 :     m_hitRadius = r;
     301            0 :     emit hitRadiusChanged();
     302              : }
     303              : 
     304            0 : bool PolylinePlot::isSnapEnabled() const
     305              : {
     306            0 :     return m_isSnapEnabled;
     307              : }
     308              : 
     309            0 : void PolylinePlot::setIsSnapEnabled(bool v)
     310              : {
     311            0 :     if (m_isSnapEnabled == v) {
     312            0 :         return;
     313              :     }
     314              : 
     315            0 :     m_isSnapEnabled = v;
     316            0 :     emit isSnapEnabledChanged();
     317              : }
     318              : 
     319            0 : qreal PolylinePlot::snapThresholdPx() const
     320              : {
     321            0 :     return m_snapThresholdPx;
     322              : }
     323              : 
     324            0 : void PolylinePlot::setSnapThresholdPx(qreal v)
     325              : {
     326            0 :     if (m_snapThresholdPx == v) {
     327            0 :         return;
     328              :     }
     329              : 
     330            0 :     m_snapThresholdPx = v;
     331            0 :     emit snapThresholdPxChanged();
     332              : }
     333              : 
     334            0 : QVector<QPointF> PolylinePlot::points() const
     335              : {
     336            0 :     return m_points;
     337              : }
     338              : 
     339            0 : void PolylinePlot::setPoints(const QVector<QPointF>& pts)
     340              : {
     341            0 :     if (m_points == pts) {
     342            0 :         if (m_points.isEmpty()) {
     343            0 :             updateBaselineFromDefaultValue();
     344              : 
     345              :             // invalidate colors under line
     346            0 :             if (!m_colorsUnderLine.empty()) {
     347            0 :                 m_colorsUnderLine.clear();
     348            0 :                 emit colorsUnderLineChanged();
     349              :             }
     350              : 
     351            0 :             update();
     352              :         }
     353            0 :         return;
     354              :     }
     355            0 :     m_points = pts;
     356            0 :     emit pointsChanged();
     357              : 
     358              :     // invalidate colors under line
     359            0 :     if (!m_colorsUnderLine.empty()) {
     360            0 :         m_colorsUnderLine.clear();
     361            0 :         emit colorsUnderLineChanged();
     362              :     }
     363              : 
     364            0 :     if (m_points.isEmpty()) {
     365            0 :         updateBaselineFromDefaultValue();
     366              :     }
     367              : 
     368              :     // if there's pending point, find its index and mark as ready for drag
     369            0 :     if (m_pressed && m_pressedPointIndex == PENDING_POINT_IDX && m_pressedOnLine) {
     370            0 :         if (width() > 0 && height() > 0 && !m_points.isEmpty()) {
     371            0 :             const auto ghostPoint = ghostPointToPolylinePx(m_pressPx);
     372              : 
     373            0 :             QPointF pN(clamp01(ghostPoint.point.x() / width()),
     374            0 :                        1.0 - clamp01(ghostPoint.point.y() / height()));
     375              : 
     376            0 :             const QPointF targetDomain = domainFromNormalized(pN);
     377              : 
     378            0 :             int bestIdx = INVALID_POINT_IDX;
     379            0 :             double bestScore = std::numeric_limits<double>::max();
     380              : 
     381            0 :             for (int i = 0; i < m_points.size(); ++i) {
     382            0 :                 const double dx = std::abs(m_points[i].x() - targetDomain.x());
     383            0 :                 const double dy = std::abs(m_points[i].y() - targetDomain.y());
     384            0 :                 const double score = dx * 1000.0 + dy;
     385            0 :                 if (score < bestScore) {
     386            0 :                     bestScore = score;
     387            0 :                     bestIdx = i;
     388              :                 }
     389              :             }
     390              : 
     391            0 :             if (bestIdx >= 0) {
     392            0 :                 m_pressedOnPoint = true;
     393            0 :                 m_pressedPointIndex = bestIdx;
     394            0 :                 m_draggedPointDomain = m_points[bestIdx];
     395            0 :                 m_hasDraggedPointDomain = true;
     396              :             }
     397              :         }
     398              :     }
     399              : 
     400            0 :     rebuildVisiblePoints();
     401              : }
     402              : 
     403            0 : QVector<QColor> PolylinePlot::colorsUnderLine() const
     404              : {
     405            0 :     return m_colorsUnderLine;
     406              : }
     407              : 
     408            0 : void PolylinePlot::setColorsUnderLine(const QVector<QColor>& c)
     409              : {
     410            0 :     if (m_colorsUnderLine == c) {
     411            0 :         return;
     412              :     }
     413              : 
     414            0 :     m_colorsUnderLine = c;
     415            0 :     emit colorsUnderLineChanged();
     416              : 
     417            0 :     update();
     418              : }
     419              : 
     420            0 : qreal PolylinePlot::leftEdgeValue() const
     421              : {
     422            0 :     if (!m_leftEdgeValue.has_value()) {
     423            0 :         return 0.0;
     424              :     }
     425            0 :     return m_leftEdgeValue.value();
     426              : }
     427              : 
     428            0 : void PolylinePlot::setLeftEdgeValue(qreal v)
     429              : {
     430            0 :     if (m_leftEdgeValue == v) {
     431            0 :         return;
     432              :     }
     433              : 
     434            0 :     m_leftEdgeValue = v;
     435            0 :     emit leftEdgeValueChanged();
     436              : 
     437            0 :     rebuildVisiblePoints();
     438              : }
     439              : 
     440            0 : void PolylinePlot::resetLeftEdgeValue()
     441              : {
     442            0 :     if (!m_leftEdgeValue.has_value()) {
     443            0 :         return;
     444              :     }
     445              : 
     446            0 :     m_leftEdgeValue.reset();
     447            0 :     emit leftEdgeValueChanged();
     448              : 
     449            0 :     rebuildVisiblePoints();
     450              : }
     451              : 
     452            0 : qreal PolylinePlot::rightEdgeValue() const
     453              : {
     454            0 :     if (!m_rightEdgeValue.has_value()) {
     455            0 :         return 0.0;
     456              :     }
     457            0 :     return m_rightEdgeValue.value();
     458              : }
     459              : 
     460            0 : void PolylinePlot::setRightEdgeValue(qreal v)
     461              : {
     462            0 :     if (m_rightEdgeValue == v) {
     463            0 :         return;
     464              :     }
     465              : 
     466            0 :     m_rightEdgeValue = v;
     467            0 :     emit rightEdgeValueChanged();
     468              : 
     469            0 :     rebuildVisiblePoints();
     470              : }
     471              : 
     472            0 : void PolylinePlot::resetRightEdgeValue()
     473              : {
     474            0 :     if (!m_rightEdgeValue.has_value()) {
     475            0 :         return;
     476              :     }
     477              : 
     478            0 :     m_rightEdgeValue.reset();
     479            0 :     emit rightEdgeValueChanged();
     480              : 
     481            0 :     rebuildVisiblePoints();
     482              : }
     483              : 
     484            0 : qreal PolylinePlot::defaultValue() const
     485              : {
     486            0 :     return m_defaultValue;
     487              : }
     488              : 
     489            0 : void PolylinePlot::setXRangeFrom(qreal v)
     490              : {
     491            0 :     if (m_xFrom == v) {
     492            0 :         return;
     493              :     }
     494              : 
     495            0 :     m_xFrom = v;
     496            0 :     emit xRangeFromChanged();
     497              : 
     498            0 :     rebuildVisiblePoints();
     499              : }
     500              : 
     501            0 : qreal PolylinePlot::xRangeTo() const
     502              : {
     503            0 :     return m_xTo;
     504              : }
     505              : 
     506            0 : void PolylinePlot::setXRangeTo(qreal v)
     507              : {
     508            0 :     if (m_xTo == v) {
     509            0 :         return;
     510              :     }
     511              : 
     512            0 :     m_xTo = v;
     513            0 :     emit xRangeToChanged();
     514              : 
     515            0 :     rebuildVisiblePoints();
     516              : }
     517              : 
     518            0 : qreal PolylinePlot::yRangeFrom() const
     519              : {
     520            0 :     return m_yFrom;
     521              : }
     522              : 
     523            0 : void PolylinePlot::setYRangeFrom(qreal v)
     524              : {
     525            0 :     if (m_yFrom == v) {
     526            0 :         return;
     527              :     }
     528              : 
     529            0 :     m_yFrom = v;
     530            0 :     emit yRangeFromChanged();
     531              : 
     532            0 :     rebuildVisiblePoints();
     533              : }
     534              : 
     535            0 : qreal PolylinePlot::yRangeTo() const
     536              : {
     537            0 :     return m_yTo;
     538              : }
     539              : 
     540            0 : void PolylinePlot::setYRangeTo(qreal v)
     541              : {
     542            0 :     if (m_yTo == v) {
     543            0 :         return;
     544              :     }
     545              : 
     546            0 :     m_yTo = v;
     547            0 :     emit yRangeToChanged();
     548              : 
     549            0 :     rebuildVisiblePoints();
     550              : }
     551              : 
     552            0 : qreal PolylinePlot::ySplitNormalized() const
     553              : {
     554            0 :     return m_ySplitNormalized;
     555              : }
     556              : 
     557            0 : void PolylinePlot::setYSplitNormalized(qreal v)
     558              : {
     559            0 :     v = clamp01(v);
     560            0 :     if (m_ySplitNormalized == v) {
     561            0 :         return;
     562              :     }
     563              : 
     564            0 :     m_ySplitNormalized = v;
     565            0 :     emit ySplitNormalizedChanged();
     566              : 
     567            0 :     rebuildVisiblePoints();
     568              : }
     569              : 
     570            0 : qreal PolylinePlot::ySplitValue() const
     571              : {
     572            0 :     return m_ySplitValue;
     573              : }
     574              : 
     575            0 : void PolylinePlot::setYSplitValue(qreal v)
     576              : {
     577            0 :     const qreal low = std::min(m_yFrom, m_yTo);
     578            0 :     const qreal high = std::max(m_yFrom, m_yTo);
     579            0 :     qreal clampedValue = std::clamp(v, low, high);
     580              : 
     581            0 :     if (m_ySplitValue == clampedValue) {
     582            0 :         return;
     583              :     }
     584              : 
     585            0 :     m_ySplitValue = clampedValue;
     586            0 :     emit ySplitValueChanged();
     587              : 
     588            0 :     rebuildVisiblePoints();
     589              : }
     590              : 
     591            0 : bool PolylinePlot::yAxisInverse() const
     592              : {
     593            0 :     return m_yAxisInverse;
     594              : }
     595              : 
     596            0 : void PolylinePlot::setYAxisInverse(bool v)
     597              : {
     598            0 :     if (m_yAxisInverse == v) {
     599            0 :         return;
     600              :     }
     601              : 
     602            0 :     m_yAxisInverse = v;
     603            0 :     emit yAxisInverseChanged();
     604            0 :     rebuildVisiblePoints();
     605              : }
     606              : 
     607            0 : bool PolylinePlot::hasActivePoint() const
     608              : {
     609            0 :     return m_hasActivePoint;
     610              : }
     611              : 
     612            0 : qreal PolylinePlot::activePointX() const
     613              : {
     614            0 :     return m_activePointPx.x();
     615              : }
     616              : 
     617            0 : qreal PolylinePlot::activePointY() const
     618              : {
     619            0 :     return m_activePointPx.y();
     620              : }
     621              : 
     622            0 : qreal PolylinePlot::activePointValue() const
     623              : {
     624            0 :     return m_activePointValue;
     625              : }
     626              : 
     627            0 : void PolylinePlot::setDefaultValue(qreal v)
     628              : {
     629            0 :     if (m_defaultValue == v) {
     630            0 :         return;
     631              :     }
     632              : 
     633            0 :     m_defaultValue = v;
     634            0 :     emit defaultValueChanged();
     635              : 
     636              :     // if there are no points, baseline should reflect defaultY immediately
     637            0 :     if (m_points.isEmpty()) {
     638            0 :         updateBaselineFromDefaultValue();
     639            0 :         rebuildVisiblePoints();
     640              :     }
     641              : }
     642              : 
     643            0 : qreal PolylinePlot::xRangeFrom() const
     644              : {
     645            0 :     return m_xFrom;
     646              : }
     647              : 
     648            0 : qreal PolylinePlot::clamp01(qreal v) const
     649              : {
     650            0 :     return std::max<qreal>(0.0, std::min<qreal>(1.0, v));
     651              : }
     652              : 
     653            0 : QPointF PolylinePlot::clamp01(const QPointF& p) const
     654              : {
     655            0 :     return QPointF(clamp01(p.x()), clamp01(p.y()));
     656              : }
     657              : 
     658            0 : bool PolylinePlot::hasValidXRange() const
     659              : {
     660            0 :     return std::isfinite(m_xFrom) && std::isfinite(m_xTo) && std::abs(m_xTo - m_xFrom) > EPSILON;
     661              : }
     662              : 
     663            0 : bool PolylinePlot::hasValidYRange() const
     664              : {
     665            0 :     return std::isfinite(m_yFrom) && std::isfinite(m_yTo) && std::abs(m_yTo - m_yFrom) > EPSILON;
     666              : }
     667              : 
     668            0 : bool PolylinePlot::hasValidYSplit() const
     669              : {
     670            0 :     return hasValidYRange() && !muse::RealIsEqualOrLess(m_ySplitNormalized, 0.0) && !muse::RealIsEqualOrMore(m_ySplitNormalized, 1.0);
     671              : }
     672              : 
     673            0 : qreal PolylinePlot::yDomainFromNormalized(qreal yNormalized) const
     674              : {
     675            0 :     const qreal n = clamp01(yNormalized);
     676              : 
     677              :     // simple linear mapping (no split)
     678            0 :     if (!hasValidYSplit()) {
     679            0 :         return m_yFrom + n * (m_yTo - m_yFrom);
     680              :     }
     681              : 
     682            0 :     const qreal splitNormalized = m_ySplitNormalized;
     683            0 :     const qreal splitValue      = m_ySplitValue;
     684              : 
     685              :     // first segment (bottom → split)
     686            0 :     if (n <= splitNormalized) {
     687            0 :         if (splitNormalized <= EPSILON) {
     688            0 :             return splitValue;
     689              :         }
     690              : 
     691            0 :         const qreal segmentN = n / splitNormalized;
     692            0 :         return m_yFrom + segmentN * (splitValue - m_yFrom);
     693              :     }
     694              : 
     695              :     // second segment (split → top)
     696            0 :     const qreal secondSegmentSize = 1.0 - splitNormalized;
     697            0 :     if (secondSegmentSize <= EPSILON) {
     698            0 :         return splitValue;
     699              :     }
     700              : 
     701            0 :     const qreal segmentN = (n - splitNormalized) / secondSegmentSize;
     702            0 :     return splitValue + segmentN * (m_yTo - splitValue);
     703              : }
     704              : 
     705            0 : qreal PolylinePlot::yNormalizedFromDomain(qreal yDomain) const
     706              : {
     707              :     // simple linear mapping (no split)
     708            0 :     if (!hasValidYSplit()) {
     709            0 :         return (yDomain - m_yFrom) / (m_yTo - m_yFrom);
     710              :     }
     711              : 
     712            0 :     const qreal splitNormalized = m_ySplitNormalized;
     713            0 :     const qreal splitValue      = m_ySplitValue;
     714              : 
     715            0 :     const bool increasing = (m_yTo >= m_yFrom);
     716              : 
     717            0 :     const bool isInFirstSegment
     718            0 :         =increasing ? (yDomain <= splitValue)
     719              :           : (yDomain >= splitValue);
     720              : 
     721              :     // ---- first segment ----
     722            0 :     if (isInFirstSegment) {
     723            0 :         const qreal segmentSize = splitValue - m_yFrom;
     724            0 :         if (std::abs(segmentSize) <= EPSILON) {
     725            0 :             return splitNormalized;
     726              :         }
     727              : 
     728            0 :         const qreal segmentN = (yDomain - m_yFrom) / segmentSize;
     729            0 :         return segmentN * splitNormalized;
     730              :     }
     731              : 
     732              :     // ---- second segment ----
     733            0 :     const qreal segmentSize = m_yTo - splitValue;
     734            0 :     if (std::abs(segmentSize) <= EPSILON) {
     735            0 :         return splitNormalized;
     736              :     }
     737              : 
     738            0 :     const qreal segmentN = (yDomain - splitValue) / segmentSize;
     739            0 :     return splitNormalized + segmentN * (1.0 - splitNormalized);
     740              : }
     741              : 
     742            0 : void PolylinePlot::updateBaselineFromDefaultValue()
     743              : {
     744            0 :     const qreal baseline = normalizedFromDomain(QPointF(m_xFrom, m_defaultValue)).y();
     745            0 :     if (m_baselineN == baseline) {
     746            0 :         return;
     747              :     }
     748              : 
     749            0 :     m_baselineN = baseline;
     750            0 :     emit baselineNChanged();
     751              : }
     752              : 
     753            0 : void PolylinePlot::updateActivePoint()
     754              : {
     755            0 :     const int hoveredDomainIdx = pointIndexAtPx(m_hoverPx);
     756            0 :     int draggedDisplayDomainIdx = INVALID_POINT_IDX;
     757            0 :     if (m_pressed && m_hasDraggedPointDomain && !m_points.isEmpty()) {
     758            0 :         double bestScore = std::numeric_limits<double>::max();
     759            0 :         for (int i = 0; i < m_points.size(); ++i) {
     760            0 :             const double dx = std::abs(m_points[i].x() - m_draggedPointDomain.x());
     761            0 :             const double dy = std::abs(m_points[i].y() - m_draggedPointDomain.y());
     762            0 :             const double score = dx * 1000.0 + dy;
     763            0 :             if (score < bestScore) {
     764            0 :                 bestScore = score;
     765            0 :                 draggedDisplayDomainIdx = i;
     766              :             }
     767              :         }
     768              :     }
     769              : 
     770              :     // m_pressedPointIndex must stay at the original model index (so consumed neighbors can be restored), but selection indices
     771              :     // need to shift when other indices are deleted...
     772            0 :     if (m_selectedPointsEnabled && draggedDisplayDomainIdx >= 0) {
     773            0 :         if (m_selectedPointsIndices.size() != 1 || !muse::contains(m_selectedPointsIndices, draggedDisplayDomainIdx)) {
     774            0 :             m_selectedPointsIndices.clear();
     775            0 :             m_selectedPointsIndices.insert(draggedDisplayDomainIdx);
     776              :         }
     777              :     }
     778              : 
     779            0 :     const int activeDomainIdx = (draggedDisplayDomainIdx >= 0)
     780            0 :                                 ? draggedDisplayDomainIdx
     781            0 :                                 : ((m_pressedPointIndex >= 0) ? m_pressedPointIndex : hoveredDomainIdx);
     782              : 
     783            0 :     if (activeDomainIdx < 0) {
     784            0 :         if (m_hasActivePoint) {
     785            0 :             m_hasActivePoint = false;
     786            0 :             m_activePointPx = {};
     787            0 :             m_activePointValue = 0.0;
     788            0 :             emit activePointChanged();
     789              :         }
     790            0 :         return;
     791              :     }
     792              : 
     793            0 :     QPointF activeN;
     794            0 :     bool found = false;
     795            0 :     for (int i = 0; i < m_pointsNVisible.size() && i < m_visibleToDomainIndex.size(); ++i) {
     796            0 :         if (m_visibleToDomainIndex[i] == activeDomainIdx) {
     797            0 :             activeN = m_pointsNVisible[i];
     798            0 :             found = true;
     799            0 :             break;
     800              :         }
     801              :     }
     802              : 
     803            0 :     if (!found) {
     804            0 :         if (m_hasActivePoint) {
     805            0 :             m_hasActivePoint = false;
     806            0 :             m_activePointPx = {};
     807            0 :             m_activePointValue = 0.0;
     808            0 :             emit activePointChanged();
     809              :         }
     810            0 :         return;
     811              :     }
     812              : 
     813            0 :     const QPointF newPx(toPxX(this, activeN.x()), toPxY(this, activeN.y()));
     814            0 :     const qreal newValue = (draggedDisplayDomainIdx >= 0 && m_hasDraggedPointDomain)
     815            0 :                            ? m_draggedPointDomain.y()
     816            0 :                            : m_points[activeDomainIdx].y();
     817            0 :     const bool changed = (!m_hasActivePoint
     818            0 :                           || newPx != m_activePointPx
     819            0 :                           || std::abs(newValue - m_activePointValue) > EPSILON);
     820              : 
     821            0 :     m_hasActivePoint = true;
     822            0 :     m_activePointPx = newPx;
     823            0 :     m_activePointValue = newValue;
     824              : 
     825            0 :     if (changed) {
     826            0 :         emit activePointChanged();
     827              :     }
     828              : }
     829              : 
     830            0 : QPointF PolylinePlot::normalizedFromDomain(const QPointF& p) const
     831              : {
     832            0 :     if (!hasValidXRange() || !hasValidYRange()) {
     833            0 :         return clamp01(QPointF(0.0, 0.0));
     834              :     }
     835              : 
     836            0 :     const qreal xN = (p.x() - m_xFrom) / (m_xTo - m_xFrom);
     837            0 :     qreal yN = clamp01(yNormalizedFromDomain(p.y()));
     838              : 
     839            0 :     if (m_yAxisInverse) {
     840            0 :         yN = 1.0 - yN;
     841              :     }
     842              : 
     843            0 :     return clamp01(QPointF(xN, yN));
     844              : }
     845              : 
     846            0 : QPointF PolylinePlot::domainFromNormalized(const QPointF& pN) const
     847              : {
     848            0 :     if (!hasValidXRange() || !hasValidYRange()) {
     849            0 :         return QPointF(m_xFrom, m_yFrom);
     850              :     }
     851              : 
     852            0 :     const qreal x = m_xFrom + clamp01(pN.x()) * (m_xTo - m_xFrom);
     853              : 
     854            0 :     qreal yT = clamp01(pN.y());
     855            0 :     if (m_yAxisInverse) {
     856            0 :         yT = 1.0 - yT;
     857              :     }
     858            0 :     const qreal y = yDomainFromNormalized(yT);
     859              : 
     860            0 :     return QPointF(x, y);
     861              : }
     862              : 
     863            0 : QVector<QPointF> PolylinePlot::normalizedFromDomain(const QVector<QPointF>& pts) const
     864              : {
     865            0 :     QVector<QPointF> out;
     866            0 :     out.reserve(pts.size());
     867            0 :     for (const auto& p : pts) {
     868            0 :         out.push_back(normalizedFromDomain(p));
     869              :     }
     870              : 
     871            0 :     return out;
     872            0 : }
     873              : 
     874            0 : QVector<QPointF> PolylinePlot::domainFromNormalized(const QVector<QPointF>& ptsN) const
     875              : {
     876            0 :     QVector<QPointF> out;
     877            0 :     out.reserve(ptsN.size());
     878            0 :     for (const auto& pN : ptsN) {
     879            0 :         out.push_back(domainFromNormalized(pN));
     880              :     }
     881              : 
     882            0 :     return out;
     883            0 : }
     884              : 
     885            0 : void PolylinePlot::rebuildVisiblePoints()
     886              : {
     887            0 :     m_pointsNVisible.clear();
     888            0 :     m_visibleToDomainIndex.clear();
     889              : 
     890            0 :     const double xRange = (m_xTo - m_xFrom);
     891            0 :     const double yRange = (m_yTo - m_yFrom);
     892              : 
     893            0 :     if (width() <= 0 || height() <= 0 || std::abs(xRange) <= 0 || std::abs(yRange) <= 0) {
     894            0 :         if (m_points.isEmpty()) {
     895            0 :             updateBaselineFromDefaultValue();
     896              :         }
     897            0 :         update();
     898            0 :         return;
     899              :     }
     900              : 
     901            0 :     if (m_points.isEmpty()) {
     902            0 :         updateBaselineFromDefaultValue();
     903              :         // with no points the line is flat on the baseline, unless there are edge values to draw it between
     904            0 :         if (!m_leftEdgeValue && !m_rightEdgeValue) {
     905            0 :             update();
     906            0 :             return;
     907              :         }
     908              :     }
     909              : 
     910              :     struct P {
     911              :         QPointF p;
     912              :         int idx;
     913              :     };
     914              : 
     915            0 :     QVector<P> sortedPointsWithIndexes;
     916            0 :     sortedPointsWithIndexes.reserve(m_points.size());
     917            0 :     for (int i = 0; i < m_points.size(); ++i) {
     918            0 :         sortedPointsWithIndexes.push_back({ m_points[i], i });
     919              :     }
     920            0 :     std::sort(sortedPointsWithIndexes.begin(), sortedPointsWithIndexes.end(),
     921            0 :               [](const P& a, const P& b) { return a.p.x() < b.p.x(); });
     922              : 
     923            0 :     auto normY = [&](double yAbs) {
     924            0 :         double yn = yNormalizedFromDomain(yAbs);
     925            0 :         if (m_yAxisInverse) {
     926            0 :             yn = 1.0 - yn;
     927              :         }
     928            0 :         return yn;
     929            0 :     };
     930              : 
     931              :     // interpolate at window edges
     932            0 :     QVector<QPointF> sortedPointsN;
     933            0 :     sortedPointsN.reserve(sortedPointsWithIndexes.size());
     934            0 :     for (const auto& it : sortedPointsWithIndexes) {
     935            0 :         sortedPointsN.push_back(QPointF(it.p.x(), normY(it.p.y())));
     936              :     }
     937              : 
     938              :     // Synthetic boundary points must be interpolated in rendered Y-space.
     939              :     // Interpolating in domain Y breaks continuity for split mappings
     940              :     // (e.g. segments crossing 0 dB split value).
     941              : 
     942              :     // An edge value overrides the value "natural" edge value (if set)...
     943            0 :     auto edgeYN = [&](const std::optional<qreal>& edgeValue, const std::optional<qreal>& otherEdgeValue, double x) {
     944            0 :         if (edgeValue.has_value()) {
     945            0 :             return normY(edgeValue.value());
     946              :         }
     947            0 :         if (sortedPointsN.isEmpty()) {
     948            0 :             return normY(otherEdgeValue.value());
     949              :         }
     950            0 :         return valueAtX(sortedPointsN, x);
     951            0 :     };
     952              : 
     953            0 :     const double yAt0N = edgeYN(m_leftEdgeValue, m_rightEdgeValue, m_xFrom);
     954            0 :     const double yAt1N = edgeYN(m_rightEdgeValue, m_leftEdgeValue, m_xTo);
     955              : 
     956              :     // NOTE: synthetic boundary points are added just outside the visible range
     957              :     // so the polyline draws correctly: they allow horizontal segments
     958              :     // before the first real point and after the last real point. Especially in
     959              :     // cases where polyline is currently visible only partially the screen.
     960              :     // These are rendering-only points (no corresponding domain index).
     961              : 
     962              :     // left boundary (synthetic)
     963            0 :     m_pointsNVisible.push_back(QPointF(-0.1, yAt0N));
     964            0 :     m_visibleToDomainIndex.push_back(INVALID_POINT_IDX);
     965              : 
     966              :     // interior real points
     967            0 :     for (const auto& it : sortedPointsWithIndexes) {
     968            0 :         const auto& p = it.p;
     969              :         // NOTE: build visible points with a margin so points directly at the edges
     970              :         // of container do not flash on re-paint
     971            0 :         if (p.x() < (m_xFrom - BOUNDARY_MARGIN) || p.x() > (m_xTo + BOUNDARY_MARGIN)) {
     972            0 :             continue;
     973              :         }
     974            0 :         const double xN = (p.x() - m_xFrom) / xRange;
     975            0 :         m_pointsNVisible.push_back(QPointF(std::clamp(xN, 0.0, 1.0), normY(p.y())));
     976            0 :         m_visibleToDomainIndex.push_back(it.idx);
     977              :     }
     978              : 
     979              :     // right boundary (synthetic)
     980            0 :     m_pointsNVisible.push_back(QPointF(1.1, yAt1N));
     981            0 :     m_visibleToDomainIndex.push_back(INVALID_POINT_IDX);
     982              : 
     983            0 :     updateActivePoint();
     984            0 :     update();
     985            0 : }
     986              : 
     987            0 : QVector<QPointF> PolylinePlot::polylinePx() const
     988              : {
     989            0 :     QVector<QPointF> pts;
     990              : 
     991            0 :     if (width() <= 0 || height() <= 0) {
     992            0 :         return pts;
     993              :     }
     994              : 
     995              :     // 0 or 1 point -> horizontal baseline
     996            0 :     if (m_pointsNVisible.size() < 2) {
     997            0 :         qreal yN = m_baselineN;
     998            0 :         if (m_pointsNVisible.size() == 1) {
     999            0 :             yN = m_pointsNVisible[0].y();
    1000              :         }
    1001            0 :         yN = clamp01(yN);
    1002              : 
    1003            0 :         const qreal y = toPxY(this, yN);
    1004            0 :         pts.push_back(QPointF(0.0, y));
    1005            0 :         pts.push_back(QPointF(width(), y));
    1006              : 
    1007            0 :         return pts;
    1008              :     }
    1009              : 
    1010              :     // 2+ points -> polyline
    1011            0 :     QVector<QPointF> sorted = m_pointsNVisible;
    1012            0 :     std::sort(sorted.begin(), sorted.end(),
    1013            0 :               [](const QPointF& a, const QPointF& b) { return a.x() < b.x(); });
    1014              : 
    1015            0 :     const QPointF firstN = sorted.front();
    1016            0 :     const QPointF lastN  = sorted.back();
    1017              : 
    1018            0 :     const qreal firstYpx = toPxY(this, firstN.y());
    1019            0 :     const qreal lastYpx  = toPxY(this, lastN.y());
    1020              : 
    1021            0 :     pts.reserve(sorted.size() + 2);
    1022              : 
    1023              :     // left horizontal segment start
    1024            0 :     pts.push_back(QPointF(0.0, firstYpx));
    1025              : 
    1026              :     // actual points
    1027            0 :     for (const auto& pN : sorted) {
    1028            0 :         if (pN.x() < 0.0 || pN.x() > 1.0) {
    1029              :             // ignore synthetic boundary points
    1030            0 :             continue;
    1031              :         }
    1032            0 :         pts.push_back(QPointF(toPxX(this, clamp01(pN.x())),
    1033            0 :                               toPxY(this, pN.y())));
    1034              :     }
    1035              : 
    1036              :     // right horizontal segment end
    1037            0 :     pts.push_back(QPointF(width(), lastYpx));
    1038              : 
    1039            0 :     return pts;
    1040            0 : }
    1041              : 
    1042            0 : bool PolylinePlot::isNearLinePx(const QPointF& px) const
    1043              : {
    1044            0 :     const auto pts = polylinePx();
    1045            0 :     if (pts.size() < 2) {
    1046            0 :         return false;
    1047              :     }
    1048              : 
    1049            0 :     qreal best = std::numeric_limits<qreal>::max();
    1050            0 :     for (int i = 0; i < pts.size() - 1; ++i) {
    1051            0 :         best = std::min(best, pointToSegmentDistance(px, pts[i], pts[i + 1]));
    1052              :     }
    1053            0 :     return best <= m_hitRadius;
    1054            0 : }
    1055              : 
    1056            0 : int PolylinePlot::pointIndexAtPx(const QPointF& px) const
    1057              : {
    1058              :     // search in visible points, skip synthetic boundary points
    1059            0 :     for (int i = 0; i < m_pointsNVisible.size(); ++i) {
    1060            0 :         const int domainIdx = (i < m_visibleToDomainIndex.size()) ? m_visibleToDomainIndex[i] : INVALID_POINT_IDX;
    1061            0 :         if (domainIdx < 0) {
    1062            0 :             continue;
    1063              :         }
    1064              : 
    1065            0 :         QPointF pN = m_pointsNVisible[i];
    1066            0 :         const qreal x = toPxX(this, pN.x());
    1067            0 :         const qreal y = toPxY(this, pN.y());
    1068            0 :         const qreal dx = px.x() - x;
    1069            0 :         const qreal dy = px.y() - y;
    1070            0 :         if ((dx * dx + dy * dy) <= (m_hitRadius * m_hitRadius)) {
    1071            0 :             return domainIdx;
    1072              :         }
    1073              :     }
    1074              : 
    1075            0 :     return INVALID_POINT_IDX;
    1076              : }
    1077              : 
    1078            0 : GhostPoint PolylinePlot::ghostPointToPolylinePx(const QPointF& px) const
    1079              : {
    1080            0 :     GhostPoint best;
    1081              : 
    1082            0 :     const auto pts = polylinePx();
    1083            0 :     if (pts.size() < 2) {
    1084            0 :         best.point = px;
    1085            0 :         best.distToSegment = std::numeric_limits<qreal>::max();
    1086            0 :         return best;
    1087              :     }
    1088              : 
    1089            0 :     for (int i = 0; i < pts.size() - 1; ++i) {
    1090            0 :         const auto res = ghostPointToSegmentDistance(px, pts[i], pts[i + 1]);
    1091            0 :         if (res.distToSegment < best.distToSegment) {
    1092            0 :             best = res;
    1093              :         }
    1094              :     }
    1095              : 
    1096            0 :     return best;
    1097            0 : }
    1098              : 
    1099            0 : QPointF PolylinePlot::snapToNeighbor(qreal dragPxX, QPointF pDomain) const
    1100              : {
    1101            0 :     if (!m_isSnapEnabled || !m_hasDraggedPointDomain || m_points.size() < 2) {
    1102            0 :         return pDomain;
    1103              :     }
    1104              : 
    1105              :     // Find the dragged point's actual current index in m_points (which may
    1106              :     // have shifted after neighbor consumption) without modifying
    1107              :     // m_pressedPointIndex — the model needs that to stay at the original
    1108              :     // value so it can restore consumed points when dragging back.
    1109            0 :     int currentIdx = -1;
    1110            0 :     double bestDist = std::numeric_limits<double>::max();
    1111            0 :     for (int i = 0; i < m_points.size(); ++i) {
    1112            0 :         const double dx = std::abs(m_points[i].x() - m_draggedPointDomain.x());
    1113            0 :         const double dy = std::abs(m_points[i].y() - m_draggedPointDomain.y());
    1114            0 :         const double dist = dx + dy;
    1115            0 :         if (dist < bestDist) {
    1116            0 :             bestDist = dist;
    1117            0 :             currentIdx = i;
    1118              :         }
    1119              :     }
    1120              : 
    1121            0 :     if (currentIdx < 0) {
    1122            0 :         return pDomain;
    1123              :     }
    1124              : 
    1125              :     // Snap close to — but not exactly onto — a neighbor.
    1126              :     // Can't have multiple automation points at the exact same timepoint.
    1127              :     static constexpr double SNAP_EPSILON = 1e-9;
    1128              : 
    1129            0 :     if (currentIdx + 1 < m_points.size()) {
    1130            0 :         const qreal neighborPxX = toPxX(this, normalizedFromDomain(m_points[currentIdx + 1]).x());
    1131            0 :         if (std::abs(dragPxX - neighborPxX) <= m_snapThresholdPx) {
    1132            0 :             pDomain.setX(m_points[currentIdx + 1].x() - SNAP_EPSILON);
    1133              :         }
    1134              :     }
    1135              : 
    1136            0 :     if (currentIdx > 0) {
    1137            0 :         const qreal neighborPxX = toPxX(this, normalizedFromDomain(m_points[currentIdx - 1]).x());
    1138            0 :         if (std::abs(dragPxX - neighborPxX) <= m_snapThresholdPx) {
    1139            0 :             pDomain.setX(m_points[currentIdx - 1].x() + SNAP_EPSILON);
    1140              :         }
    1141              :     }
    1142              : 
    1143            0 :     return pDomain;
    1144              : }
    1145              : 
    1146            0 : void PolylinePlot::updateCursor()
    1147              : {
    1148            0 :     const bool interactive = m_hoveredOnLine || m_pressed || (m_pressedPointIndex >= 0);
    1149              : 
    1150            0 :     if (interactive) {
    1151            0 :         setCursor(Qt::CrossCursor);
    1152              :     } else {
    1153            0 :         unsetCursor();
    1154              :     }
    1155            0 : }
    1156              : 
    1157            0 : void PolylinePlot::resetGestureState()
    1158              : {
    1159            0 :     m_pressed = false;
    1160            0 :     m_pressedOnLine = false;
    1161            0 :     m_pressedOnPoint = false;
    1162            0 :     m_pressedPointIndex = INVALID_POINT_IDX;
    1163            0 :     m_hasDraggedPointDomain = false;
    1164            0 :     m_hoveredOnLine = false;
    1165            0 :     m_draggedPointDomain = {};
    1166            0 :     m_movedSincePress = false;
    1167            0 :     m_pressPx = QPointF(0.0, 0.0);
    1168              : 
    1169            0 :     updateCursor();
    1170            0 :     updateActivePoint();
    1171            0 :     update();
    1172            0 : }
    1173              : 
    1174            0 : void PolylinePlot::geometryChange(const QRectF& newG, const QRectF& oldG)
    1175              : {
    1176            0 :     QQuickPaintedItem::geometryChange(newG, oldG);
    1177            0 :     if (newG.size() != oldG.size()) {
    1178            0 :         rebuildVisiblePoints();
    1179              :     }
    1180            0 : }
    1181              : 
    1182            0 : void PolylinePlot::drawLinesAndFillUnder(QPainter* painter) const
    1183              : {
    1184            0 :     const auto pts = polylinePx();
    1185            0 :     if (pts.size() < 2) {
    1186            0 :         return;
    1187              :     }
    1188              : 
    1189            0 :     QPen pen(m_lineColor);
    1190            0 :     pen.setWidthF(m_lineWidth);
    1191            0 :     pen.setJoinStyle(Qt::MiterJoin);
    1192            0 :     painter->setPen(pen);
    1193            0 :     painter->setBrush(Qt::NoBrush);
    1194              : 
    1195            0 :     const qreal baselinePxY = toPxY(this, m_baselineN);
    1196              : 
    1197            0 :     for (int i = 0; i < pts.size() - 1; ++i) {
    1198            0 :         if (i < m_colorsUnderLine.size()) {
    1199              :             // fill area under line
    1200              :             const QVector<QPointF> areaUnderLine {
    1201            0 :                 QPointF(pts[i].x(), baselinePxY), // bottom left
    1202            0 :                 pts[i], // top left
    1203            0 :                 pts[i + 1], // top right
    1204            0 :                 QPointF(pts[i + 1].x(), baselinePxY), // bottom right
    1205            0 :             };
    1206            0 :             QPainterPath path;
    1207            0 :             path.addPolygon(QPolygonF(areaUnderLine));
    1208            0 :             painter->fillPath(path, m_colorsUnderLine.at(i));
    1209            0 :         }
    1210              :         // draw line
    1211            0 :         painter->drawLine(pts[i], pts[i + 1]);
    1212              :     }
    1213            0 : }
    1214              : 
    1215            0 : void PolylinePlot::paintPoint(QPainter* painter, const PolylinePointStyle* style, const QPointF& centre, bool useHoveredStyle) const
    1216              : {
    1217            0 :     IF_ASSERT_FAILED(painter && style) {
    1218            0 :         return;
    1219              :     }
    1220              : 
    1221            0 :     const qreal centerRadius = useHoveredStyle ? style->centerRadiusHovered() : style->centerRadius();
    1222            0 :     const QColor centerColor = useHoveredStyle ? style->centerColorHovered() : style->centerColor();
    1223              : 
    1224            0 :     const qreal middleRingWidth = useHoveredStyle ? style->middleRingWidthHovered() : style->middleRingWidth();
    1225            0 :     const QColor middleRingColor = useHoveredStyle ? style->middleRingColorHovered() : style->middleRingColor();
    1226              : 
    1227            0 :     const qreal outlineWidth = useHoveredStyle ? style->outlineWidthHovered() : style->outlineWidth();
    1228            0 :     const QColor outlineColor = useHoveredStyle ? style->outlineColorHovered() : style->outlineColor();
    1229              : 
    1230            0 :     if (middleRingWidth > 0.0) {
    1231            0 :         const qreal middleRadius = centerRadius + middleRingWidth;
    1232            0 :         const qreal outerRadius = middleRadius + outlineWidth;
    1233              : 
    1234            0 :         painter->setPen(Qt::NoPen);
    1235              : 
    1236            0 :         painter->setBrush(outlineColor);
    1237            0 :         painter->drawEllipse(centre, outerRadius, outerRadius);
    1238              : 
    1239            0 :         painter->setBrush(middleRingColor);
    1240            0 :         painter->drawEllipse(centre, middleRadius, middleRadius);
    1241              : 
    1242            0 :         painter->setBrush(centerColor);
    1243            0 :         painter->drawEllipse(centre, centerRadius, centerRadius);
    1244            0 :         return;
    1245              :     }
    1246              : 
    1247            0 :     painter->setPen(Qt::NoPen);
    1248            0 :     painter->setBrush(centerColor);
    1249            0 :     painter->drawEllipse(centre, centerRadius, centerRadius);
    1250              : 
    1251            0 :     if (outlineWidth > 0.0) {
    1252            0 :         QPen pen(outlineColor);
    1253            0 :         pen.setWidthF(outlineWidth);
    1254            0 :         painter->setPen(pen);
    1255            0 :         painter->setBrush(Qt::NoBrush);
    1256            0 :         painter->drawEllipse(centre, centerRadius, centerRadius);
    1257            0 :     }
    1258              : }
    1259              : 
    1260            0 : void PolylinePlot::paint(QPainter* painter)
    1261              : {
    1262            0 :     if (!painter) {
    1263            0 :         return;
    1264              :     }
    1265              : 
    1266            0 :     painter->setRenderHint(QPainter::Antialiasing, antialiasing());
    1267              : 
    1268              :     // draw background overlay
    1269            0 :     if (m_drawBackground) {
    1270            0 :         QColor overlay = uiConfiguration()->currentTheme().extra["black_color"].value<QColor>();
    1271            0 :         overlay.setAlphaF(0.25);
    1272              : 
    1273            0 :         painter->setPen(Qt::NoPen);
    1274            0 :         painter->setBrush(overlay);
    1275            0 :         painter->drawRect(boundingRect());
    1276              :     }
    1277              : 
    1278              :     // draw lines and fill area underneath
    1279            0 :     drawLinesAndFillUnder(painter);
    1280              : 
    1281              :     // draw points
    1282            0 :     IF_ASSERT_FAILED(m_standardPointStyle && m_ghostPointStyle && m_selectedPointStyle) {
    1283            0 :         return;
    1284              :     }
    1285              : 
    1286            0 :     const int n = m_pointsNVisible.size();
    1287            0 :     const int hoveredIndex = pointIndexAtPx(m_hoverPx);
    1288            0 :     for (int i = 0; i < n; ++i) {
    1289            0 :         QPointF pN = m_pointsNVisible[i];
    1290            0 :         const QPointF centre(toPxX(this, pN.x()), toPxY(this, pN.y()));
    1291              : 
    1292            0 :         const int domainIdx = (i < m_visibleToDomainIndex.size()) ? m_visibleToDomainIndex[i] : INVALID_POINT_IDX;
    1293            0 :         const bool isSelected = m_selectedPointsEnabled && domainIdx >= 0 && muse::contains(m_selectedPointsIndices, domainIdx);
    1294            0 :         const bool isHovered = !(m_pressed && isSelected) && domainIdx >= 0 && domainIdx == hoveredIndex;
    1295              : 
    1296            0 :         const PolylinePointStyle* style = isSelected ? m_selectedPointStyle : m_standardPointStyle;
    1297            0 :         paintPoint(painter, style, centre, /*useHoveredStyle*/ isHovered);
    1298              :     }
    1299              : 
    1300              :     // draw hover ghost point
    1301            0 :     if (m_ghostPointsEnabled && m_hoveredOnLine && m_pressedPointIndex < 0) {
    1302            0 :         QPointF hp = m_hoverGhostPx;
    1303              : 
    1304            0 :         if (m_pointsNVisible.size() < 2) {
    1305            0 :             const qreal yN = (m_pointsNVisible.size() == 1) ? m_pointsNVisible[0].y() : m_baselineN;
    1306            0 :             hp.setY(toPxY(this, yN));
    1307              :         }
    1308              : 
    1309            0 :         if (pointIndexAtPx(hp) < 0 && isNearLinePx(hp)) {
    1310            0 :             paintPoint(painter, m_ghostPointStyle, hp, /*useHoveredStyle*/ false);
    1311              :         }
    1312              :     }
    1313              : }
    1314              : 
    1315            0 : void PolylinePlot::hoverMoveEvent(QHoverEvent* e)
    1316              : {
    1317              :     // NOTE: even if mouse is still, Qt produces hoverMoveEvents constantly
    1318            0 :     if (m_hoverPx == e->position()) {
    1319              :         // IMPORTANT: let through hover events (otherwise double-click
    1320              :         // clip selection may be broken)
    1321            0 :         e->ignore();
    1322            0 :         return;
    1323              :     }
    1324              : 
    1325            0 :     m_hoverPx = e->position();
    1326              : 
    1327            0 :     const bool nearPoint = (pointIndexAtPx(m_hoverPx) >= 0);
    1328              : 
    1329            0 :     bool nearLine = false;
    1330            0 :     if (m_ghostPointsEnabled) {
    1331            0 :         const auto proj = ghostPointToPolylinePx(m_hoverPx);
    1332            0 :         nearLine = (proj.distToSegment <= m_hitRadius);
    1333              : 
    1334            0 :         if (m_pointsNVisible.size() >= 2) {
    1335            0 :             m_hoverGhostPx = proj.point;
    1336              :         } else {
    1337            0 :             m_hoverGhostPx = m_hoverPx;
    1338              :         }
    1339              :     } else {
    1340              :         // nearLine is still required for updateCursor (m_hoveredOnLine) and to accept events, but
    1341              :         // if ghost points are disabled we can skip the m_hoverGhostPx logic...
    1342            0 :         nearLine = isNearLinePx(m_hoverPx);
    1343              :     }
    1344              : 
    1345            0 :     m_hoveredOnLine = (nearPoint || nearLine);
    1346            0 :     updateCursor();
    1347              : 
    1348            0 :     updateActivePoint();
    1349            0 :     update();
    1350              : 
    1351              :     // IMPORTANT: only consume hover when automation is actually interactive.
    1352              :     // Otherwise let underlying ClipItem hover area keep containsMouse=true,
    1353              :     // so itemHovered stays correct for selection/double-click logic.
    1354            0 :     if (nearPoint || nearLine || m_pressed || m_pressedPointIndex >= 0) {
    1355            0 :         e->accept();
    1356              :     } else {
    1357            0 :         e->ignore();
    1358              :     }
    1359              : }
    1360              : 
    1361            0 : void PolylinePlot::hoverLeaveEvent(QHoverEvent* e)
    1362              : {
    1363              :     Q_UNUSED(e);
    1364            0 :     m_hoveredOnLine = false;
    1365            0 :     updateCursor();
    1366            0 :     updateActivePoint();
    1367            0 :     update();
    1368            0 : }
    1369              : 
    1370            0 : void PolylinePlot::mousePressEvent(QMouseEvent* e)
    1371              : {
    1372            0 :     if (e->button() != Qt::LeftButton) {
    1373            0 :         e->ignore();
    1374            0 :         return;
    1375              :     }
    1376              : 
    1377            0 :     if (!m_selectedPointsIndices.empty()) {
    1378            0 :         m_selectedPointsIndices.clear();
    1379              :     }
    1380              : 
    1381            0 :     const int pointIndex = pointIndexAtPx(e->position());
    1382            0 :     const bool onPoint = pointIndex >= 0;
    1383            0 :     const bool onLine  = isNearLinePx(e->position());
    1384              : 
    1385              :     // NOTE: allow clicks on the points and lines only
    1386            0 :     if (!onPoint && !onLine) {
    1387            0 :         e->ignore();
    1388            0 :         return;
    1389              :     }
    1390              : 
    1391            0 :     resetGestureState();
    1392              : 
    1393            0 :     e->accept();
    1394            0 :     updateCursor();
    1395              : 
    1396            0 :     m_pressed = true;
    1397            0 :     m_pressPx = e->position();
    1398              : 
    1399            0 :     if (onPoint) {
    1400            0 :         m_pressedOnPoint = true;
    1401            0 :         m_pressedPointIndex = pointIndex;
    1402            0 :         if (m_pressedPointIndex >= 0 && m_pressedPointIndex < m_points.size()) {
    1403            0 :             if (m_selectedPointsEnabled) {
    1404            0 :                 m_selectedPointsIndices.insert(m_pressedPointIndex);
    1405              :             }
    1406            0 :             m_draggedPointDomain = m_points[m_pressedPointIndex];
    1407            0 :             m_hasDraggedPointDomain = true;
    1408              :         }
    1409            0 :         updateActivePoint();
    1410            0 :         return;
    1411              :     }
    1412              : 
    1413            0 :     if (onLine) {
    1414            0 :         m_pressedOnLine = true;
    1415            0 :         m_pressedPointIndex = PENDING_POINT_IDX;
    1416            0 :         m_pressedOnPoint = false;
    1417              : 
    1418            0 :         if (width() > 0 && height() > 0) {
    1419            0 :             const auto ghostPoint = ghostPointToPolylinePx(e->position());
    1420              : 
    1421            0 :             QPointF pN(clamp01(ghostPoint.point.x() / width()),
    1422            0 :                        1.0 - clamp01(ghostPoint.point.y() / height()));
    1423              : 
    1424            0 :             const QPointF pDomain = domainFromNormalized(pN);
    1425            0 :             emit pointAdded(pDomain.x(), pDomain.y(), /*completed*/ false);
    1426              :         }
    1427              : 
    1428            0 :         updateActivePoint();
    1429            0 :         return;
    1430              :     }
    1431              : }
    1432              : 
    1433            0 : void PolylinePlot::mouseMoveEvent(QMouseEvent* e)
    1434              : {
    1435            0 :     if (!m_pressed) {
    1436            0 :         e->ignore();
    1437            0 :         return;
    1438              :     }
    1439              : 
    1440            0 :     e->accept();
    1441              : 
    1442            0 :     const QPointF pos = e->position();
    1443            0 :     if (!m_movedSincePress && (pos - m_pressPx).manhattanLength() > MOVE_THRESHOLD) {
    1444            0 :         m_movedSincePress = true;
    1445              :     }
    1446              : 
    1447              :     // drag point (2+ points only)
    1448            0 :     if (m_pressedPointIndex >= 0) {
    1449            0 :         if (width() <= 0 || height() <= 0) {
    1450            0 :             return;
    1451              :         }
    1452              : 
    1453            0 :         QPointF pN(pos.x() / width(), 1.0 - (pos.y() / height()));
    1454            0 :         pN = clamp01(pN);
    1455              : 
    1456            0 :         const QPointF pDomain = snapToNeighbor(pos.x(), domainFromNormalized(pN));
    1457              : 
    1458            0 :         m_draggedPointDomain = pDomain;
    1459            0 :         m_hasDraggedPointDomain = true;
    1460            0 :         emit pointMoved(m_pressedPointIndex, pDomain.x(), pDomain.y(), /*completed*/ false);
    1461            0 :         updateActivePoint();
    1462              : 
    1463            0 :         return;
    1464              :     }
    1465              : }
    1466              : 
    1467            0 : void PolylinePlot::mouseReleaseEvent(QMouseEvent* e)
    1468              : {
    1469            0 :     if (e->button() != Qt::LeftButton || !m_pressed) {
    1470            0 :         e->ignore();
    1471            0 :         return;
    1472              :     }
    1473            0 :     e->accept();
    1474              : 
    1475            0 :     const bool isClick = !m_movedSincePress;
    1476            0 :     const QPointF rel = e->position();
    1477              : 
    1478              :     // commit point drag
    1479            0 :     if (!isClick && m_pressedPointIndex >= 0) {
    1480            0 :         QPointF pN(rel.x() / width(), 1.0 - (rel.y() / height()));
    1481            0 :         pN = clamp01(pN);
    1482              : 
    1483            0 :         const QPointF pDomain = snapToNeighbor(rel.x(), domainFromNormalized(pN));
    1484              : 
    1485            0 :         m_draggedPointDomain = pDomain;
    1486            0 :         m_hasDraggedPointDomain = true;
    1487            0 :         emit pointMoved(m_pressedPointIndex, pDomain.x(), pDomain.y(), /*completed*/ true);
    1488            0 :         emit interactionFinished();
    1489            0 :         resetGestureState();
    1490            0 :         return;
    1491              :     }
    1492              : 
    1493              :     // click-without-drag on line: finalize the point that was added during press
    1494            0 :     if (isClick && m_pressedOnLine && m_pressedPointIndex >= 0 && m_pressedPointIndex < m_points.size()) {
    1495            0 :         const QPointF pDomain = m_points[m_pressedPointIndex];
    1496            0 :         emit pointAdded(pDomain.x(), pDomain.y(), /*completed*/ true);
    1497              :     }
    1498              : 
    1499            0 :     emit interactionFinished();
    1500            0 :     resetGestureState();
    1501              : }
    1502              : 
    1503            0 : void PolylinePlot::mouseDoubleClickEvent(QMouseEvent* e)
    1504              : {
    1505            0 :     if (e->button() != Qt::LeftButton) {
    1506            0 :         e->ignore();
    1507            0 :         return;
    1508              :     }
    1509              : 
    1510            0 :     const int idx = pointIndexAtPx(e->position());
    1511            0 :     if (idx < 0) {
    1512            0 :         e->ignore();
    1513            0 :         return;
    1514              :     }
    1515              : 
    1516            0 :     e->accept();
    1517              : 
    1518            0 :     emit pointRemoved(idx, /*completed*/ true);
    1519            0 :     emit interactionFinished();
    1520              : 
    1521            0 :     resetGestureState();
    1522              : }
        

Generated by: LCOV version 2.0-1