diff --git a/sdrgui/gui/connectionoverlay.cpp b/sdrgui/gui/connectionoverlay.cpp index 9baee8672..94f1fd1a3 100644 --- a/sdrgui/gui/connectionoverlay.cpp +++ b/sdrgui/gui/connectionoverlay.cpp @@ -27,18 +27,35 @@ #include #include #include -#include +#include +#include +#include +#include +#include +#include +#include #include +#include #include #include +// Opacity of links not touching the hovered window. +static constexpr qreal dimmedOpacity = 0.5; +// Channel, device, spectrum and feature title bars: a 2 px border, then a 20 px row. +static constexpr int titleBarMiddle = 12; +static constexpr int titleBarHeight = 24; + ConnectionOverlay::ConnectionOverlay(QMdiArea *mdi) : - QWidget(mdi), + QOpenGLWidget(mdi), m_mdi(mdi) { // QMdiArea scrolls its viewport children; keep the overlay fixed above the viewport. setAttribute(Qt::WA_TransparentForMouseEvents); - setAttribute(Qt::WA_TranslucentBackground); + // Composited last, with its alpha, over everything including OpenGL spectra. + setAttribute(Qt::WA_AlwaysStackOnTop); + QSurfaceFormat surfaceFormat = format(); + surfaceFormat.setAlphaBufferSize(8); + setFormat(surfaceFormat); setGeometry(mdi->viewport()->geometry()); mdi->viewport()->installEventFilter(this); m_animationTimer.setInterval(33); @@ -47,6 +64,50 @@ ConnectionOverlay::ConnectionOverlay(QMdiArea *mdi) : hide(); } +ConnectionOverlay::~ConnectionOverlay() +{ + releaseGL(); +} + +void ConnectionOverlay::initializeGL() +{ + // The context is recreated when the window changes, e.g. a workspace is floated. + connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &ConnectionOverlay::releaseGL, Qt::UniqueConnection); + m_unpremultiply = new QOpenGLShaderProgram(); + m_unpremultiply->addShaderFromSourceCode(QOpenGLShader::Vertex, + "attribute highp vec2 position;\n" + "varying highp vec2 coordinate;\n" + "void main() {\n" + " coordinate = position * 0.5 + 0.5;\n" + " gl_Position = vec4(position, 0.0, 1.0);\n" + "}\n"); + m_unpremultiply->addShaderFromSourceCode(QOpenGLShader::Fragment, + "varying highp vec2 coordinate;\n" + "uniform sampler2D source;\n" + "void main() {\n" + " highp vec4 colour = texture2D(source, coordinate);\n" + " gl_FragColor = colour.a > 0.0 ? vec4(colour.rgb / colour.a, colour.a) : vec4(0.0);\n" + "}\n"); + m_unpremultiply->link(); +} + +void ConnectionOverlay::releaseGL() +{ + if (!m_paintBuffer && !m_resolveBuffer && !m_unpremultiply) { + return; + } + makeCurrent(); + if (m_resolveBuffer != m_paintBuffer) { + delete m_resolveBuffer; + } + delete m_paintBuffer; + delete m_unpremultiply; + m_paintBuffer = nullptr; + m_resolveBuffer = nullptr; + m_unpremultiply = nullptr; + doneCurrent(); +} + bool ConnectionOverlay::sameConnections(const QList& a, const QList& b) { if (a.size() != b.size()) { @@ -56,11 +117,11 @@ bool ConnectionOverlay::sameConnections(const QList& a, const QList< { const Connection& x = a[i]; const Connection& y = b[i]; - if (x.source.data() != y.source.data() || x.target.data() != y.target.data() || - x.sourceLabel != y.sourceLabel || x.targetLabel != y.targetLabel || - x.flowLabel != y.flowLabel || x.sourceWorkspace != y.sourceWorkspace || - x.targetWorkspace != y.targetWorkspace || x.kind != y.kind || - x.direction != y.direction || x.color != y.color) { + if (x.m_source.data() != y.m_source.data() || x.m_target.data() != y.m_target.data() || + x.m_sourceLabel != y.m_sourceLabel || x.m_targetLabel != y.m_targetLabel || + x.m_flowLabel != y.m_flowLabel || x.m_sourceWorkspace != y.m_sourceWorkspace || + x.m_targetWorkspace != y.m_targetWorkspace || x.m_kind != y.m_kind || + x.m_direction != y.m_direction || x.m_color != y.m_color) { return false; } } @@ -74,14 +135,13 @@ void ConnectionOverlay::setConnections(const QList& connections) return; } m_connections = connections; - m_indexLabels.clear(); - // Cache each title ID widget so paths can anchor beside it and avoid painting over it. + m_windows.clear(); for (const Connection& connection : m_connections) { - for (QMdiSubWindow *window : {connection.source.data(), connection.target.data()}) + for (QMdiSubWindow *window : {connection.m_source.data(), connection.m_target.data()}) { - if (window && !m_indexLabels.contains(window)) { - m_indexLabels.insert(window, window->findChild("connectionIndexLabel")); + if (window && !m_windows.contains(window)) { + m_windows.append(window); } } } @@ -91,7 +151,7 @@ void ConnectionOverlay::setConnections(const QList& connections) void ConnectionOverlay::watchWindows() { - // Cached routes depend on the connected windows' geometry and their ID labels. + // Cached routes depend on the connected windows' geometry. for (const QPointer& object : m_watched) { if (object) { @@ -99,15 +159,12 @@ void ConnectionOverlay::watchWindows() } } m_watched.clear(); - for (auto it = m_indexLabels.constBegin(); it != m_indexLabels.constEnd(); ++it) + for (const QPointer& window : m_windows) { - for (QObject *object : {static_cast(it.key()), static_cast(it.value().data())}) + if (window) { - if (object) - { - object->installEventFilter(this); - m_watched.append(object); - } + window->installEventFilter(this); + m_watched.append(window.data()); } } } @@ -126,7 +183,6 @@ void ConnectionOverlay::setOverlayVisible(bool visible) else { m_animationTimer.stop(); - m_pulseRegion = QRegion(); } } @@ -148,12 +204,28 @@ bool ConnectionOverlay::eventFilter(QObject *object, QEvent *event) invalidateLayout(); // A window joined or left this workspace. } } + else if (type == QEvent::Enter || type == QEvent::Leave) + { + // Qt sends Enter to a window and all its ancestors, and does not send Leave + // when the mouse moves on to a child, so this tracks the whole window. + if (QMdiSubWindow *window = qobject_cast(object)) + { + if (type == QEvent::Enter) { + setHovered(window); + } else if (m_hovered == window) { + setHovered(nullptr); + } + } + } else if (type == QEvent::Move || type == QEvent::Resize || type == QEvent::Show || type == QEvent::Hide || type == QEvent::WindowStateChange || type == QEvent::ParentChange) { - invalidateLayout(); // A connected window or its ID label moved, e.g. when scrolling. + if (type == QEvent::Hide && object == m_hovered) { + setHovered(nullptr); // A hidden window gets no Leave event. + } + invalidateLayout(); // A connected window moved, e.g. when scrolling. } - return QWidget::eventFilter(object, event); + return QOpenGLWidget::eventFilter(object, event); } void ConnectionOverlay::changeEvent(QEvent *event) @@ -161,7 +233,7 @@ void ConnectionOverlay::changeEvent(QEvent *event) if (event->type() == QEvent::FontChange || event->type() == QEvent::PaletteChange) { invalidateLayout(); } - QWidget::changeEvent(event); + QOpenGLWidget::changeEvent(event); } void ConnectionOverlay::invalidateLayout() @@ -188,7 +260,7 @@ void ConnectionOverlay::relayout() void ConnectionOverlay::showEvent(QShowEvent *event) { - QWidget::showEvent(event); + QOpenGLWidget::showEvent(event); // Changes while hidden were not laid out. The viewport's own Show event arrives // before this widget is visible, so it cannot trigger the relayout itself. if (m_layoutDirty) @@ -241,88 +313,399 @@ static QPainterPath roundedRoute(const QVector& points) return path; } -static int crossingCost(const QPainterPath& path, const QRectF& sourceBox, const QRectF& targetBox) +// Evenly spaced points along a path. Flattening once is much cheaper than calling +// QPainterPath::pointAtPercent() per sample, which matters as every candidate route +// of every link is scored on each relayout, e.g. while a window is dragged. +static QVector samplePath(const QPainterPath& path, int samples, qreal& total) +{ + total = 0; + QPolygonF line; + for (const QPolygonF& subpath : path.toSubpathPolygons()) { + line += subpath; + } + QVector points; + if (line.isEmpty()) { + return points; + } + QVector cumulative(line.size(), 0.0); + for (int i = 1; i < line.size(); ++i) { + cumulative[i] = cumulative[i - 1] + QLineF(line[i - 1], line[i]).length(); + } + total = cumulative.last(); + points.reserve(samples + 1); + int segment = 1; + for (int i = 0; i <= samples; ++i) + { + if (line.size() == 1 || total <= 0) + { + points.append(line.first()); + continue; + } + const qreal distance = total * i / samples; + while (segment < line.size() - 1 && cumulative[segment] < distance) { + ++segment; + } + const qreal segmentLength = cumulative[segment] - cumulative[segment - 1]; + const qreal t = segmentLength > 0 ? (distance - cumulative[segment - 1]) / segmentLength : 0.0; + points.append(line[segment - 1] + (line[segment] - line[segment - 1]) * t); + } + return points; +} + +// Cells of a grid over the overlay covered by routes already placed, and which way those +// routes run through them, so later routes take their own lane rather than drawing on +// top of an earlier one. Crossing another route at right angles costs a little. +// Counts per cell let one route be removed and added again cheaply while links are +// rerouted, and a flat grid makes each lookup an array access. +constexpr quint8 occupancyHorizontal = 1; +constexpr quint8 occupancyVertical = 2; +constexpr qreal occupancyCell = 3; // Fine enough to tell lanes 4 px apart, as tiled windows leave narrow gaps + +// Which way a sampled route runs at a point, from its neighbouring samples. +static quint8 occupancyDirection(const QVector& points, int i) { - // Count samples drawn over a window body. An endpoint can be inside a window, - // e.g. the source's own title bar, or an ID label covered by an overlapping - // window. The route may then leave that window at the start, or arrive in it - // at the end, but any other part inside a window is a crossing. + const QPointF delta = points[qMin(i + 1, points.size() - 1)] - points[qMax(i - 1, 0)]; + return qAbs(delta.x()) >= qAbs(delta.y()) ? occupancyHorizontal : occupancyVertical; +} + +class Occupancy +{ +public: + explicit Occupancy(const QSize& size) : + m_columns(qMax(1, qCeil(size.width() / occupancyCell))), + m_rows(qMax(1, qCeil(size.height() / occupancyCell))), + m_horizontal(m_columns * m_rows, 0), + m_vertical(m_columns * m_rows, 0) + {} + + // The cells a route reserves, each as (cell index << 1) | vertical. Computed once + // per route, and passed to add() to add or remove the route. + QVector cells(const QPainterPath& path) const + { + // Reserve a band either side of the route, so a parallel route keeps about + // 16 px away rather than running right beside it. A little under 16 px, so the + // bracket lanes, 16 px apart, are clear of each other's bands. A line of cells + // across the route at every cell along it fills the band. + constexpr int band = 4; + // Only the middle of a route reserves its lane. Near its ends it turns into + // its window, where other routes need to pass too. + constexpr qreal freeEnds = 40; + qreal length = path.length(); + const int samples = qMax(2, qCeil(length / occupancyCell)); + const QVector points = samplePath(path, samples, length); + const qreal step = length / qMax(1, points.size() - 1); + QVector result; + for (int i = 0; i < points.size(); ++i) + { + if (i * step < freeEnds || (points.size() - 1 - i) * step < freeEnds) { + continue; + } + const quint8 direction = occupancyDirection(points, i); + const int column = qFloor(points[i].x() / occupancyCell); + const int row = qFloor(points[i].y() / occupancyCell); + for (int k = -band; k <= band; ++k) + { + const int c = direction == occupancyVertical ? column + k : column; + const int r = direction == occupancyVertical ? row : row + k; + if (c >= 0 && c < m_columns && r >= 0 && r < m_rows) { + result.append((quint32(r * m_columns + c) << 1) | (direction == occupancyVertical ? 1 : 0)); + } + } + } + return result; + } + + // Add (delta 1) or remove (delta -1) a route's cells. + void add(const QVector& cells, int delta) + { + for (quint32 cell : cells) + { + QVector& counts = (cell & 1) ? m_vertical : m_horizontal; + counts[cell >> 1] += delta; + } + m_routes += delta; + } + + bool isEmpty() const { return m_routes <= 0; } + + // The directions of the routes whose bands cover a point. + quint8 directions(const QPointF& point) const + { + const int column = qFloor(point.x() / occupancyCell); + const int row = qFloor(point.y() / occupancyCell); + if (column < 0 || column >= m_columns || row < 0 || row >= m_rows) { + return 0; + } + const int index = row * m_columns + column; + return (m_horizontal[index] ? occupancyHorizontal : 0) | (m_vertical[index] ? occupancyVertical : 0); + } + +private: + int m_columns; + int m_rows; + QVector m_horizontal; + QVector m_vertical; + int m_routes = 0; +}; + +// How good a route is: its length, and how much of it is drawn over windows or +// outside the viewport. Measured in pixels, so a longer detour does not look +// better just because its crossing is a smaller fraction of it. +struct RouteScore +{ + qreal crossing = 0; + qreal length = 0; + int bends = 0; + // A detour is worth taking when it saves a third of its extra length in crossing. + // Each bend costs a little, so simpler shapes win when all else is equal. + qreal value() const { return 3 * crossing + length + 24 * bends; } +}; + +// A route sampled for scoring, with the part of its score that does not depend on +// other routes worked out once, so it can be scored cheaply as they move. +struct ScoredPath +{ + QPainterPath path; + QVector points; + qreal step = 0; + int fixedCount = 0; // Half samples over windows or outside the viewport + RouteScore score; // Ignoring other routes +}; + +static ScoredPath scorePath(const QPainterPath& path, const QRectF& sourceBox, const QRectF& targetBox, + const QVector& obstacles, const QRectF& bounds) +{ + // Crossing covers the two linked windows, and every other window in the + // workspace, so routes prefer gaps and open space. An endpoint can be inside a + // window, e.g. where another window overlaps the edge it is on, or when it was + // pulled onto its own title bar. The route may then leave that window at the start, or + // arrive in it at the end, for up to a short distance. Running along a window's + // border costs fully, as a line on a seam between tiled windows is drawn on top + // of their frames; running just inside an edge costs half. constexpr int samples = 80; - QPointF points[samples + 1]; - for (int i = 0; i <= samples; ++i) { - points[i] = path.pointAtPercent(qreal(i) / samples); + constexpr qreal freeRun = 30; // Pixels a route may run inside its start or end window + ScoredPath scored; + scored.path = path; + scored.points = samplePath(path, samples, scored.score.length); + const QVector& points = scored.points; + if (points.isEmpty() || scored.score.length <= 0) { + return scored; } - int cost = 0; - for (const QRectF& box : {sourceBox, targetBox}) + const qreal step = scored.score.length / samples; + scored.step = step; + const int last = points.size() - 1; + const int maxFree = qCeil(freeRun / step); + qreal left = points.first().x(), right = left, top = points.first().y(), bottom = top; + for (const QPointF& point : points) { + left = qMin(left, point.x()); + right = qMax(right, point.x()); + top = qMin(top, point.y()); + bottom = qMax(bottom, point.y()); + } + const QRectF pathBounds(QPointF(left, top), QPointF(right, bottom)); + auto boxSamples = [&](const QRectF& box, bool edgeDiscount) -> int { if (box.isEmpty()) { - continue; + return 0; + } + // The window, plus a thin strip either side of its border. + constexpr qreal border = 4; + const QRectF body = box.adjusted(-border, -border, border, border); + if (!body.intersects(pathBounds.adjusted(-1, -1, 1, 1))) { + return 0; // Most windows are nowhere near a given route } - const QRectF body = box.adjusted(-2, -2, 2, 2); int first = 0; - while (first <= samples && body.contains(points[first])) { + while (first <= last && first < maxFree && body.contains(points[first])) { ++first; // Leading run inside, leaving the start window } - int last = samples; - while (last >= first && body.contains(points[last])) { - --last; // Trailing run inside, arriving in the end window + int end = last; + while (end >= first && last - end < maxFree && body.contains(points[end])) { + --end; // Trailing run inside, arriving in the end window } - for (int i = first; i <= last; ++i) + // Counted in half samples. On the border itself counts fully, so routes do + // not follow seams between windows. Running vertically just inside a left or + // right edge, where the lanes beside a column run, is much less intrusive + // than cutting across the middle, so it counts half. Along the top edge it + // would cover the title bar, so that counts fully. + constexpr qreal edgeBand = 64; // Room for four lanes 16 px apart + const QRectF inner = box.adjusted(border, border, -border, -border); + const QRectF deep = inner.adjusted(edgeBand, 0, -edgeBand, 0); + int count = 0; + for (int i = first; i <= end; ++i) { - if (body.contains(points[i])) { - ++cost; + if (!body.contains(points[i])) { + continue; } + const bool besideEdge = edgeDiscount && inner.contains(points[i]) && !deep.contains(points[i]) && + occupancyDirection(points, i) == occupancyVertical; + count += besideEdge ? 1 : 2; } + return count; + }; + // Crossing the linked windows counts double, with no discount near their + // edges: a line over a linked window looks like it connects there. + int count = 2 * (boxSamples(sourceBox, false) + boxSamples(targetBox, false)); + for (const QRectF& obstacle : obstacles) { + count += boxSamples(obstacle, true); } - return cost; + // Parts outside the viewport cannot be seen, so they count as crossing too. + const QRectF visible = bounds.adjusted(-4, -4, 4, 4); + for (const QPointF& point : points) + { + if (!visible.contains(point)) { + count += 2; + } + } + scored.fixedCount = count; + scored.score.crossing = count * step / 2; // Half samples to pixels + // Corners of rounded routes, and the direct curve, are cubic segments. + for (int e = 0; e < path.elementCount(); ++e) + { + if (path.elementAt(e).type == QPainterPath::CurveToElement) { + ++scored.score.bends; + } + } + return scored; } -static QPainterPath connectionPath(const QPointF& from, const QPointF& to, - int fromSide, int toSide, const QRectF& sourceBox, - const QRectF& targetBox, const QRectF& bounds) +// The score of a sampled route among the routes already placed. +static RouteScore scoreAmong(const ScoredPath& scored, const Occupancy *occupied) +{ + RouteScore score = scored.score; + if (!occupied || occupied->isEmpty() || scored.points.isEmpty() || score.length <= 0) { + return score; + } + // Running alongside an earlier route, in the same direction, counts as crossing + // too; crossing it at right angles costs a little, so crossings are avoided when + // that is cheap, e.g. by nesting two links the other way round. Sharing its line costs a lot: more than running + // into the edge of a neighbouring window, so parallel links keep their spacing + // even where windows are tiled with narrow gaps. Ends are exempt, as several + // links can leave the same anchor. + constexpr qreal exemptEnds = 20; + constexpr int overlapWeight = 4; + const QVector& points = scored.points; + const qreal step = scored.step; + const int last = points.size() - 1; + int count = scored.fixedCount; + for (int i = 0; i <= last; ++i) + { + if (i * step < exemptEnds || (last - i) * step < exemptEnds) { + continue; + } + const quint8 occupiedDirections = occupied->directions(points[i]); + if (occupiedDirections & occupancyDirection(points, i)) { + count += 2 * overlapWeight; + } else if (occupiedDirections) { + count += 4; // Across the other route's band: about a 150 px detour per crossing + } + } + score.crossing = count * step / 2; // Half samples to pixels + return score; +} + +static RouteScore scoreRoute(const QPainterPath& path, const QRectF& sourceBox, const QRectF& targetBox, + const QVector& obstacles, const QRectF& bounds, const Occupancy *occupied) +{ + return scoreAmong(scorePath(path, sourceBox, targetBox, obstacles, bounds), occupied); +} + +// The simplest route: a curve, or for both ends on the same side a bracket. +static QPainterPath directPath(const QPointF& from, const QPointF& to, int fromSide, int toSide, + const QRectF& sourceBox, const QRectF& targetBox) { - constexpr qreal labelLead = 14; + constexpr qreal labelLead = 20; // Straight run out from the window before any turn const QPointF curveFrom = from + QPointF(fromSide * labelLead, 0); const QPointF curveTo = to + QPointF(toSide * labelLead, 0); const QPointF delta = curveTo - curveFrom; - const qreal handle = qBound(32, qAbs(delta.x()) * 0.35 + qAbs(delta.y()) * 0.3, 180); + // With both ends on the same side, e.g. windows stacked in a column, a large + // handle bulges far into neighbouring windows, so keep the curve tight. + const bool sameSide = fromSide != 0 && fromSide == toSide; + const qreal handle = qBound(32, qAbs(delta.x()) * 0.35 + qAbs(delta.y()) * 0.3, sameSide ? 40 : 180); const QPointF firstControl = fromSide == 0 ? curveFrom + delta * 0.3 : curveFrom + QPointF(fromSide * handle, 0); const QPointF lastControl = toSide == 0 ? curveTo - delta * 0.3 : curveTo + QPointF(toSide * handle, 0); QPainterPath direct(from); - if (fromSide != 0) { - direct.lineTo(curveFrom); - } - direct.cubicTo(firstControl, lastControl, curveTo); - if (toSide != 0) { - direct.lineTo(to); - } - if (sourceBox.isEmpty() || targetBox.isEmpty()) { - return direct; + if (sameSide) + { + // Both ends on the same side, e.g. windows stacked in a column: a straight + // bracket 16 px out from the windows. A curve here bulges across several + // lanes, leaving no room for the links beside it. + const qreal edge = fromSide > 0 ? qMax(qMax(from.x(), to.x()), qMax(sourceBox.right(), targetBox.right())) : + qMin(qMin(from.x(), to.x()), qMin(sourceBox.left(), targetBox.left())); + const qreal laneX = edge + fromSide * 16; + direct = roundedRoute({from, QPointF(laneX, from.y()), QPointF(laneX, to.y()), to}); } - // Prefer the route that crosses windows least, then the shortest. Crossing can - // be unavoidable (e.g. an endpoint covered by the other window), and then the - // direct route is usually clearer than a long detour around both windows. - QPainterPath bestRoute = direct; - int bestCost = crossingCost(direct, sourceBox, targetBox); - qreal bestLength = direct.length(); - if (bestCost == 0) { - return direct; + else + { + if (fromSide != 0) { + direct.lineTo(curveFrom); + } + direct.cubicTo(firstControl, lastControl, curveTo); + if (toSide != 0) { + direct.lineTo(to); + } } + return direct; +} + +// Routes around windows, for when the direct route crosses them. +static QVector detourPaths(const QPointF& from, const QPointF& to, int fromSide, int toSide, + const QRectF& sourceBox, const QRectF& targetBox, const QRectF& bounds) +{ + const bool sameSide = fromSide != 0 && fromSide == toSide; + QVector detours; auto consider = [&](const QPainterPath& candidate) { - const qreal length = candidate.length(); - if (bestCost == 0 && length >= bestLength) { - return; // Cannot beat a shorter clear route + detours.append(candidate); + }; + + // Both ends on the same side: a bracket that steps out, runs along the gap + // beside the windows, and steps back in. + if (sameSide) + { + // Measured from the outermost window edge, so both windows are cleared. At + // least 16 px out, so the link visibly steps away from the window first; then + // every 16 px for parallel lanes. + const qreal edge = fromSide > 0 ? qMax(qMax(from.x(), to.x()), qMax(sourceBox.right(), targetBox.right())) : + qMin(qMin(from.x(), to.x()), qMin(sourceBox.left(), targetBox.left())); + for (qreal out : {16.0, 32.0, 48.0, 64.0, 80.0}) + { + const qreal laneX = edge + fromSide * out; + if (laneX >= bounds.left() + 2 && laneX <= bounds.right() - 2) { + consider(roundedRoute({from, QPointF(laneX, from.y()), QPointF(laneX, to.y()), to})); + } + } + } + + // Elbow routes: out from the source, one vertical run, and into the target. The + // simplest shape after the direct curve, tried with the vertical run just past + // the source, just before the target, midway, and along either window's frame. + { + constexpr qreal elbowLead = 24; + QVector elbowXs; + // Midway, except for same-side links: their anchors share an edge, so the + // midpoint runs straight down beside the windows without stepping out. + if (!sameSide) { + elbowXs.append((from.x() + to.x()) / 2); + } + // Along the windows' frames, with room for parallel lanes 16 px apart. + for (qreal lane : {16.0, 24.0, 32.0, 40.0, 48.0, 56.0, 64.0}) { + elbowXs << sourceBox.left() - lane << sourceBox.right() + lane + << targetBox.left() - lane << targetBox.right() + lane; + } + if (fromSide != 0) { + elbowXs.append(from.x() + fromSide * elbowLead); + } + if (toSide != 0) { + elbowXs.append(to.x() + toSide * elbowLead); } - const int cost = crossingCost(candidate, sourceBox, targetBox); - if (cost < bestCost || (cost == bestCost && length < bestLength)) + for (qreal elbowX : elbowXs) { - bestRoute = candidate; - bestCost = cost; - bestLength = length; + if (elbowX >= bounds.left() + 2 && elbowX <= bounds.right() - 2) { + consider(roundedRoute({from, QPointF(elbowX, from.y()), QPointF(elbowX, to.y()), to})); + } } - }; + } // Try an outer route around both windows, and nearer horizontal lanes. constexpr qreal clearance = 24; @@ -403,10 +786,82 @@ static QPainterPath connectionPath(const QPointF& from, const QPointF& to, })); } } + return detours; +} + +// Every route for a link, direct first, sampled for scoring. +static QVector routeCandidates(const QPointF& from, const QPointF& to, int fromSide, int toSide, + const QRectF& sourceBox, const QRectF& targetBox, const QRectF& bounds, + const QVector& obstacles) +{ + QVector candidates; + candidates.append(scorePath(directPath(from, to, fromSide, toSide, sourceBox, targetBox), + sourceBox, targetBox, obstacles, bounds)); + if (!sourceBox.isEmpty() && !targetBox.isEmpty()) + { + for (const QPainterPath& detour : detourPaths(from, to, fromSide, toSide, sourceBox, targetBox, bounds)) { + candidates.append(scorePath(detour, sourceBox, targetBox, obstacles, bounds)); + } + } + return candidates; +} + +// The best of a link's candidate routes among the routes already placed. Take the +// best scoring route: short, and clear of windows. Crossing can be unavoidable (e.g. +// an endpoint covered by the other window), and then the direct route is usually +// clearer than a long detour around both windows. Returns its index, and its score. +static int bestCandidate(const QVector& candidates, bool sameSide, const Occupancy *occupied, + qreal& bestScore) +{ + const RouteScore directScore = scoreAmong(candidates.first(), occupied); + bestScore = directScore.value(); + if (directScore.crossing <= 0 && !sameSide) { + return 0; // Clear, and nothing is shorter. A same-side bracket clears the + // outermost window, so a lane nearer the other may be shorter. + } + int best = 0; + for (int i = 1; i < candidates.size(); ++i) + { + const qreal score = scoreAmong(candidates[i], occupied).value(); + if (score < bestScore) + { + best = i; + bestScore = score; + } + } + return best; +} + +static QPainterPath connectionPath(const QPointF& from, const QPointF& to, + int fromSide, int toSide, const QRectF& sourceBox, + const QRectF& targetBox, const QRectF& bounds, + const QVector& obstacles, const Occupancy *occupied = nullptr) +{ + const bool sameSide = fromSide != 0 && fromSide == toSide; + const QPainterPath direct = directPath(from, to, fromSide, toSide, sourceBox, targetBox); + if (sourceBox.isEmpty() || targetBox.isEmpty()) { + return direct; + } + // The direct route alone first, as it is often clear, sparing the detours. + const RouteScore directScore = scoreRoute(direct, sourceBox, targetBox, obstacles, bounds, occupied); + if (directScore.crossing <= 0 && !sameSide) { + return direct; + } + QPainterPath bestRoute = direct; + qreal bestScore = directScore.value(); + for (const QPainterPath& detour : detourPaths(from, to, fromSide, toSide, sourceBox, targetBox, bounds)) + { + const qreal detourScore = scoreRoute(detour, sourceBox, targetBox, obstacles, bounds, occupied).value(); + if (detourScore < bestScore) + { + bestRoute = detour; + bestScore = detourScore; + } + } return bestRoute; } -bool ConnectionOverlay::computeLayout() +void ConnectionOverlay::computeLayout() { m_layoutDirty = false; m_routes.clear(); @@ -414,9 +869,7 @@ bool ConnectionOverlay::computeLayout() const QRectF bounds = rect().adjusted(8, 8, -8, -8); const QPoint viewportOffset = m_mdi->viewport()->pos() - pos(); const QFontMetrics metrics(font()); - m_clipRegion = QRegion(rect()); - QRegion paintedRegion; - QVector labelObstacles; // Title ID labels, then each label as it is placed + QVector labelObstacles; // Each label as it is placed // Stubs to other workspaces that leave the same anchor fan out vertically, so // their lines and labels do not sit on top of each other. QHash, int> stubCounts; @@ -426,184 +879,450 @@ bool ConnectionOverlay::computeLayout() const int step = (n + 1) / 2; // 0, 1, 1, 2, 2, ... return (n % 2 ? step : -step) * stubSpacing; // 0, +1, -1, +2, -2, ... }; - QPainterPathStroker maskStroker; - maskStroker.setWidth(20); - maskStroker.setCapStyle(Qt::RoundCap); - maskStroker.setJoinStyle(Qt::RoundJoin); - for (auto it = m_indexLabels.constBegin(); it != m_indexLabels.constEnd(); ++it) - { - QMdiSubWindow *window = it.key(); - QWidget *indexLabel = it.value().data(); - if (!window || !localWindows.contains(window) || !indexLabel || !indexLabel->isVisible()) { - continue; + // Stubs placed above (vertical < 0) or below (vertical > 0) a window stack away + // from it. Returns false, claiming nothing, if the next slot and its label would + // not fit inside the viewport, or if the space it needs is covered by another window. + auto claimVertical = [&](QMdiSubWindow *window, int side, int vertical, qreal anchorX, qreal markerWidth, qreal& tipY) -> bool { + const QPair key(window, side * (vertical < 0 ? 2 : 3)); // Separate from the level fan + const QRectF box = QRectF(window->geometry().translated(viewportOffset)); + const qreal offset = 16 + stubCounts.value(key) * stubSpacing; + const qreal y = vertical < 0 ? box.top() - offset : box.bottom() + offset; + const qreal labelHeight = metrics.height() + 4; + if (y - labelHeight / 2 < bounds.top() + 4 || y + labelHeight / 2 > bounds.bottom() - 4) { + return false; + } + // Stub and label, whichever side of the tip the label ends up on. + const qreal bandTop = vertical < 0 ? y - labelHeight / 2 : box.bottom(); + const qreal bandBottom = vertical < 0 ? box.top() : y + labelHeight / 2; + const QRectF band(anchorX - markerWidth - 40, bandTop, 2 * markerWidth + 80, bandBottom - bandTop); + for (QMdiSubWindow *other : localWindows) + { + if (other != window && other->isVisible() && !other->isMinimized() && + band.intersects(QRectF(other->geometry().translated(viewportOffset)))) { + return false; + } } - const QPoint labelTopLeft = window->geometry().topLeft() + - indexLabel->mapTo(window, QPoint(0, 0)) + viewportOffset; - const QRect indexRect = QRect(labelTopLeft, indexLabel->size()).adjusted(-2, -2, 2, 2); - m_clipRegion -= indexRect; - labelObstacles.append(indexRect); - } - // The overlay is a sibling of the scrolling viewport, so translate MDI - // window coordinates before using their title labels as endpoints. - auto anchorFor = [this, &viewportOffset](QMdiSubWindow *window, int side) -> QPointF { - QWidget *indexLabel = m_indexLabels.value(window).data(); - const QPoint insideWindow = indexLabel && indexLabel->isVisible() ? - indexLabel->mapTo(window, QPoint(side < 0 ? -10 : indexLabel->width() + 10, - indexLabel->height() / 2)) : - QPoint(side < 0 ? -10 : 48, 12); - return QPointF(window->geometry().topLeft() + insideWindow + viewportOffset); + stubCounts[key]++; + tipY = y; + return true; + }; + // A window can be connected on either side, just outside its left edge (side -1) + // or its right edge (side 1), level with the middle of its title bar. Each link + // uses whichever sides give it the best route. The overlay is a sibling of the + // scrolling viewport, so MDI window coordinates are translated. + auto anchorFor = [&viewportOffset](QMdiSubWindow *window, int side) -> QPointF { + const QPoint inWindow(side < 0 ? -6 : window->width() + 6, titleBarMiddle); + return QPointF(window->geometry().topLeft() + inWindow + viewportOffset); }; - for (const Connection& connection : m_connections) + // A flow label sits beside its route's midpoint, so labels of fanned stubs separate too. + auto placeFlowLabel = [&](Route& route) { + const qreal labelWidth = metrics.horizontalAdvance(route.m_flowLabel) + 10; + const qreal labelHeight = metrics.height() + 4; + const QPointF midpoint = route.m_path.pointAtPercent(0.5); + const qreal labelX = midpoint.x() - labelWidth / 2; + const qreal labelY = midpoint.y() - labelHeight - 6; + route.m_flowLabelRect = QRectF(qBound(bounds.left() + 4, labelX, + qMax(bounds.left() + 4, bounds.right() - labelWidth - 4)), + qBound(bounds.top() + 4, labelY, + qMax(bounds.top() + 4, bounds.bottom() - labelHeight - 4)), + labelWidth, labelHeight); + }; + + // Route links so they can keep out of each other's way: stubs to other workspaces + // first, as their place is fixed, then links within this workspace from the + // smallest vertical span up. Short links take the lanes nearest the windows, + // and longer ones nest around them without crossing. + Occupancy occupancy(size()); + // Candidate routes of each link within this workspace. They do not change while + // other links move, so they are made and sampled once, and only their scores + // among the other links are worked out again when rerouting. + struct Reroute + { + ScoredPath current; + QPointF anchors[2][2]; // [source/target][left/right] + QVector candidates[2][2]; // [source side][target side], empty where an anchor is off screen + QVector> cells[2][2]; // Occupancy cells of each candidate, made when needed + }; + struct Choice { - if (!connection.source || !connection.target) { + int si = -1; + int ti = -1; + int index = -1; + qreal score = 0; + }; + QVector reroutes; // Parallel to m_routes + QVector order; + QVector orderKey(m_connections.size(), 0); + for (int i = 0; i < m_connections.size(); ++i) + { + const Connection& connection = m_connections[i]; + order.append(i); + if (connection.m_source && connection.m_target && localWindows.contains(connection.m_source.data()) && + localWindows.contains(connection.m_target.data())) { + orderKey[i] = 1 + qAbs(connection.m_source->geometry().top() - connection.m_target->geometry().top()); + } + } + std::stable_sort(order.begin(), order.end(), [&orderKey](int a, int b) { return orderKey[a] < orderKey[b]; }); + + for (int connectionIndex : order) + { + const Connection& connection = m_connections[connectionIndex]; + if (!connection.m_source || !connection.m_target) { continue; } - const bool sourceLocal = localWindows.contains(connection.source.data()); - const bool targetLocal = localWindows.contains(connection.target.data()); + const bool sourceLocal = localWindows.contains(connection.m_source.data()); + const bool targetLocal = localWindows.contains(connection.m_target.data()); if (!sourceLocal && !targetLocal) { continue; } - if (sourceLocal && (connection.source->parentWidget() != m_mdi->viewport() || - !connection.source->isVisible() || connection.source->isMinimized())) { + if (sourceLocal && (connection.m_source->parentWidget() != m_mdi->viewport() || + !connection.m_source->isVisible() || connection.m_source->isMinimized())) { continue; } - if (targetLocal && (connection.target->parentWidget() != m_mdi->viewport() || - !connection.target->isVisible() || connection.target->isMinimized())) { + if (targetLocal && (connection.m_target->parentWidget() != m_mdi->viewport() || + !connection.m_target->isVisible() || connection.m_target->isMinimized())) { continue; } - const int sourceSide = 1; // Device ID, or the producer of a pipe. - const int targetSide = -1; // Inputs meet the left side of channel, spectrum, and feature IDs. - const QPointF sourceAnchor = sourceLocal ? anchorFor(connection.source.data(), sourceSide) : QPointF(); - const QPointF targetAnchor = targetLocal ? anchorFor(connection.target.data(), targetSide) : QPointF(); - const bool sourceVisible = sourceLocal && bounds.contains(sourceAnchor); - const bool targetVisible = targetLocal && bounds.contains(targetAnchor); + // Anchors on both sides of each local endpoint; index 0 is the left, 1 the right. + // A side can be used when its anchor is on screen. If neither is, e.g. a window + // wider than the viewport, but its title bar is visible, pull the left anchor + // inside, onto the ID label end of the title bar, rather than dropping the + // window's links. + auto placeAnchors = [&](QMdiSubWindow *window, QPointF anchors[2], bool on[2]) { + for (int i = 0; i < 2; ++i) + { + anchors[i] = anchorFor(window, i ? 1 : -1); + on[i] = bounds.contains(anchors[i]); + } + const QRectF titleBar(QPointF(window->geometry().topLeft() + viewportOffset), + QSizeF(window->width(), titleBarHeight)); + if (!on[0] && !on[1] && titleBar.intersects(bounds)) + { + anchors[0] = viewportEdge(bounds, anchors[0]); + on[0] = true; + } + }; + QPointF sourceAnchors[2], targetAnchors[2]; + bool sourceOn[2] = {false, false}, targetOn[2] = {false, false}; + if (sourceLocal) { + placeAnchors(connection.m_source.data(), sourceAnchors, sourceOn); + } + if (targetLocal) { + placeAnchors(connection.m_target.data(), targetAnchors, targetOn); + } + const bool sourceVisible = sourceOn[0] || sourceOn[1]; + const bool targetVisible = targetOn[0] || targetOn[1]; // This workspace draws a link only when at least one endpoint is on screen. if (!sourceVisible && !targetVisible) { continue; } Route route; + Reroute reroute; QPointF markerEdge; bool crossWorkspace = false; - int stubDirection = 0; // Which way a cross-workspace stub goes from its window + int labelDirection = 0; // Which side of a stub's tip its label goes + route.m_sourceBox = sourceVisible ? + QRectF(connection.m_source->geometry().translated(viewportOffset)) : QRectF(); + route.m_targetBox = targetVisible ? + QRectF(connection.m_target->geometry().translated(viewportOffset)) : QRectF(); + for (QMdiSubWindow *window : localWindows) + { + if (window != connection.m_source && window != connection.m_target && window->isVisible() && + !window->isMinimized() && window->parentWidget() == m_mdi->viewport()) { + route.m_obstacles.append(QRectF(window->geometry().translated(viewportOffset))); + } + } + + // Nearest on-screen anchor of an endpoint to a point. + auto nearestSide = [](const QPointF anchors[2], const bool on[2], const QPointF& point) -> int { + if (!on[0] || !on[1]) { + return on[1] ? 1 : -1; + } + return QLineF(anchors[1], point).length() < QLineF(anchors[0], point).length() ? 1 : -1; + }; + + // A stub to another workspace beside a local window: from the side with more + // room, level with the title bar; else above or below the window; else as far + // out as the viewport allows on the roomier side. + auto placeStub = [&](QMdiSubWindow *window, const QPointF anchors[2], const bool on[2], + qreal markerWidth, QPointF& local, QPointF& tip, int& side) { + const qreal stubLength = 90; + const qreal roomLeft = on[0] ? anchors[0].x() - bounds.left() : -1; + const qreal roomRight = on[1] ? bounds.right() - anchors[1].x() : -1; + const int roomier = roomRight >= roomLeft ? 1 : -1; + for (int s : {roomier, -roomier}) + { + if ((s > 0 ? roomRight : roomLeft) >= stubLength + markerWidth + 10) + { + side = labelDirection = s; + local = anchors[s > 0 ? 1 : 0]; + const qreal y = qBound(bounds.top() + 4, local.y() + fanOffset(window, s), bounds.bottom() - 4); + tip = QPointF(local.x() + s * stubLength, y); + return; + } + } + side = roomier; + local = anchors[side > 0 ? 1 : 0]; + qreal y = 0; + if (claimVertical(window, side, -1, local.x(), markerWidth, y) || + claimVertical(window, side, 1, local.x(), markerWidth, y)) + { + tip = QPointF(side > 0 ? qMin(bounds.right() - 4, local.x() + 30) : qMax(bounds.left() + 4, local.x() - 30), y); + const bool fitsOutwards = side > 0 ? tip.x() + 6 + markerWidth <= bounds.right() - 4 : + tip.x() - 6 - markerWidth >= bounds.left() + 4; + labelDirection = fitsOutwards ? side : -side; + return; + } + labelDirection = side; + y = qBound(bounds.top() + 4, local.y() + fanOffset(window, side), bounds.bottom() - 4); + tip = QPointF(side > 0 ? qMin(bounds.right() - markerWidth - 10, local.x() + stubLength) : + qMax(bounds.left() + markerWidth + 10, local.x() - stubLength), y); + }; + // Scrolled endpoints point to a viewport edge. An endpoint in another // workspace instead gets a short labeled stub beside the local window. if (sourceVisible && targetVisible) { - route.from = sourceAnchor; - route.to = targetAnchor; + route.m_local = true; + for (int i = 0; i < 2; ++i) + { + reroute.anchors[0][i] = sourceAnchors[i]; + reroute.anchors[1][i] = targetAnchors[i]; + } + // Try each pair of sides; keep the best scoring route. + Choice best; + for (int si = 0; si < 2; ++si) + { + for (int ti = 0; ti < 2; ++ti) + { + if (!sourceOn[si] || !targetOn[ti]) { + continue; + } + QVector& candidates = reroute.candidates[si][ti]; + candidates = routeCandidates(sourceAnchors[si], targetAnchors[ti], si ? 1 : -1, ti ? 1 : -1, + route.m_sourceBox, route.m_targetBox, bounds, route.m_obstacles); + reroute.cells[si][ti].resize(candidates.size()); + qreal score = 0; + const int index = bestCandidate(candidates, si == ti, &occupancy, score); + if (best.index < 0 || score < best.score) { + best = Choice{si, ti, index, score}; + } + } + } + reroute.current = reroute.candidates[best.si][best.ti][best.index]; + route.m_from = sourceAnchors[best.si]; + route.m_to = targetAnchors[best.ti]; + route.m_fromSide = best.si ? 1 : -1; + route.m_toSide = best.ti ? 1 : -1; + route.m_path = reroute.current.path; } else if (sourceVisible && targetLocal) { - route.from = sourceAnchor; - route.to = viewportEdge(bounds, targetAnchor); - markerEdge = route.to; - route.marker = scrollArrow(bounds, targetAnchor) + " " + connection.targetLabel; + route.m_to = viewportEdge(bounds, targetAnchors[0]); + route.m_fromSide = nearestSide(sourceAnchors, sourceOn, route.m_to); + route.m_from = sourceAnchors[route.m_fromSide > 0 ? 1 : 0]; + markerEdge = route.m_to; + route.m_marker = scrollArrow(bounds, targetAnchors[0]) + " " + connection.m_targetLabel; } else if (targetVisible && sourceLocal) { - route.from = viewportEdge(bounds, sourceAnchor); - route.to = targetAnchor; - markerEdge = route.from; - route.marker = scrollArrow(bounds, sourceAnchor) + " " + connection.sourceLabel; + route.m_from = viewportEdge(bounds, sourceAnchors[0]); + route.m_toSide = nearestSide(targetAnchors, targetOn, route.m_from); + route.m_to = targetAnchors[route.m_toSide > 0 ? 1 : 0]; + markerEdge = route.m_from; + route.m_marker = scrollArrow(bounds, sourceAnchors[0]) + " " + connection.m_sourceLabel; } else if (sourceVisible) { - route.from = sourceAnchor; - route.marker = QString("W%1 · %2").arg(connection.targetWorkspace).arg(connection.targetLabel); - if (!connection.flowLabel.isEmpty()) { - route.marker += " · " + connection.flowLabel; // One label per stub, at its tip + route.m_marker = QString("W%1 · %2").arg(connection.m_targetWorkspace).arg(connection.m_targetLabel); + if (!connection.m_flowLabel.isEmpty()) { + route.m_marker += " · " + connection.m_flowLabel; // One label per stub, at its tip } - const qreal markerWidth = metrics.horizontalAdvance(route.marker) + 8; - const qreal stubLength = 90; - const qreal stubY = qBound(bounds.top() + 4, route.from.y() + fanOffset(connection.source.data(), sourceSide), - bounds.bottom() - 4); - // Stubs leave to the right. Near the right edge there is no room for the - // stub and its label, so go left instead, past the window's ID label. - stubDirection = route.from.x() + stubLength + markerWidth + 10 <= bounds.right() ? 1 : -1; - const qreal tipX = stubDirection > 0 ? route.from.x() + stubLength : - qMax(bounds.left() + markerWidth + 10, route.from.x() - stubLength - 60); - route.to = QPointF(tipX, stubY); - route.stubTip = 1; + placeStub(connection.m_source.data(), sourceAnchors, sourceOn, metrics.horizontalAdvance(route.m_marker) + 8, + route.m_from, route.m_to, route.m_fromSide); + route.m_stubTip = 1; crossWorkspace = true; } else { - route.to = targetAnchor; - route.marker = QString("W%1 · %2").arg(connection.sourceWorkspace).arg(connection.sourceLabel); - if (!connection.flowLabel.isEmpty()) { - route.marker += " · " + connection.flowLabel; // One label per stub, at its tip + route.m_marker = QString("W%1 · %2").arg(connection.m_sourceWorkspace).arg(connection.m_sourceLabel); + if (!connection.m_flowLabel.isEmpty()) { + route.m_marker += " · " + connection.m_flowLabel; // One label per stub, at its tip } - const qreal markerWidth = metrics.horizontalAdvance(route.marker) + 8; - const qreal stubLength = 90; - // Stubs arrive from the left. Near the left edge there is no room for the - // stub and its label, so come from the right instead, past the ID label. - stubDirection = route.to.x() - stubLength - markerWidth - 10 >= bounds.left() ? -1 : 1; - const qreal tipX = stubDirection < 0 ? route.to.x() - stubLength : - qMin(bounds.right() - markerWidth - 10, route.to.x() + stubLength + 60); - const qreal stubY = qBound(bounds.top() + 4, route.to.y() + fanOffset(connection.target.data(), targetSide), - bounds.bottom() - 4); - route.from = QPointF(tipX, stubY); - route.stubTip = -1; + placeStub(connection.m_target.data(), targetAnchors, targetOn, metrics.horizontalAdvance(route.m_marker) + 8, + route.m_to, route.m_from, route.m_toSide); + route.m_stubTip = -1; crossWorkspace = true; } - const QPointF& from = route.from; - const QPointF& to = route.to; - - route.color = connection.color.isValid() ? connection.color.lighter(150) : - (connection.kind == PipeLink ? QColor(238, 183, 77) : QColor(56, 204, 228)); - route.kind = connection.kind; - route.direction = connection.direction; - route.sourceBox = sourceVisible ? - QRectF(connection.source->geometry().translated(viewportOffset)) : QRectF(); - route.targetBox = targetVisible ? - QRectF(connection.target->geometry().translated(viewportOffset)) : QRectF(); - // A stub flipped to the other side goes straight out, behind its ID label. - route.fromSide = sourceVisible && stubDirection >= 0 ? sourceSide : 0; - route.toSide = targetVisible && stubDirection <= 0 ? targetSide : 0; - route.path = connectionPath(from, to, route.fromSide, route.toSide, - route.sourceBox, route.targetBox, bounds); - route.length = route.path.length(); - - if (!connection.flowLabel.isEmpty() && !crossWorkspace) // Stubs include it in their marker - { - const qreal labelWidth = metrics.horizontalAdvance(connection.flowLabel) + 10; - const qreal labelHeight = metrics.height() + 4; - // Beside each route's own midpoint, so labels of fanned stubs separate too. - const QPointF midpoint = route.path.pointAtPercent(0.5); - const qreal labelX = midpoint.x() - labelWidth / 2; - const qreal labelY = midpoint.y() - labelHeight - 6; - route.flowLabel = connection.flowLabel; - route.flowLabelRect = QRectF(qBound(bounds.left() + 4, labelX, - qMax(bounds.left() + 4, bounds.right() - labelWidth - 4)), - qBound(bounds.top() + 4, labelY, - qMax(bounds.top() + 4, bounds.bottom() - labelHeight - 4)), - labelWidth, labelHeight); + const QPointF& from = route.m_from; + const QPointF& to = route.m_to; + + route.m_source = connection.m_source.data(); + route.m_target = connection.m_target.data(); + route.m_color = connection.m_color.isValid() ? connection.m_color.lighter(150) : + (connection.m_kind == PipeLink ? QColor(238, 183, 77) : QColor(56, 204, 228)); + route.m_kind = connection.m_kind; + route.m_direction = connection.m_direction; + if (route.m_path.isEmpty()) { + route.m_path = connectionPath(from, to, route.m_fromSide, route.m_toSide, + route.m_sourceBox, route.m_targetBox, bounds, route.m_obstacles, &occupancy); } + route.m_length = route.m_path.length(); - if (!route.marker.isEmpty()) + if (!connection.m_flowLabel.isEmpty() && !crossWorkspace) // Stubs include it in their marker { - const qreal labelWidth = metrics.horizontalAdvance(route.marker) + 8; + route.m_flowLabel = connection.m_flowLabel; + placeFlowLabel(route); + } + + if (!route.m_marker.isEmpty()) + { + const qreal labelWidth = metrics.horizontalAdvance(route.m_marker) + 8; const qreal labelHeight = metrics.height() + 4; const QPointF remoteAnchor = sourceVisible ? to : from; - // A stub's label continues from its tip, in the direction the stub goes. + // A stub's label continues from its tip, usually in the direction the stub goes. const qreal labelX = crossWorkspace ? - (stubDirection > 0 ? remoteAnchor.x() + 6 : remoteAnchor.x() - labelWidth - 6) : + (labelDirection > 0 ? remoteAnchor.x() + 6 : remoteAnchor.x() - labelWidth - 6) : (markerEdge.x() >= bounds.right() - 1 ? markerEdge.x() - labelWidth - 8 : markerEdge.x() + 8); const qreal labelY = crossWorkspace ? remoteAnchor.y() - labelHeight / 2 : (markerEdge.y() >= bounds.bottom() - 1 ? markerEdge.y() - labelHeight - 8 : markerEdge.y() + 8); - route.markerRect = QRectF(qBound(bounds.left() + 4, labelX, + route.m_markerRect = QRectF(qBound(bounds.left() + 4, labelX, qMax(bounds.left() + 4, bounds.right() - labelWidth - 4)), qBound(bounds.top() + 4, labelY, qMax(bounds.top() + 4, bounds.bottom() - labelHeight - 4)), labelWidth, labelHeight); } + route.m_cells = occupancy.cells(route.m_path); + occupancy.add(route.m_cells, 1); m_routes.append(route); + reroutes.append(reroute); + } + + // Reroute links within this workspace with every other link in place, so the order + // they were first placed in does not decide which gets the better lane, e.g. + // whether two links from one device nest without crossing. + // Links are taken out of the occupancy while they are rerouted, and put back after. + auto scoreCurrent = [&](int r) -> qreal { + return scoreAmong(reroutes[r].current, &occupancy).value(); + }; + // Best route for a link around the others in the occupancy, trying each pair of sides. + auto routeAmong = [&](int r) -> Choice { + Choice best; + for (int si = 0; si < 2; ++si) + { + for (int ti = 0; ti < 2; ++ti) + { + const QVector& candidates = reroutes[r].candidates[si][ti]; + if (candidates.isEmpty()) { + continue; + } + qreal score = 0; + const int index = bestCandidate(candidates, si == ti, &occupancy, score); + if (best.index < 0 || score < best.score) { + best = Choice{si, ti, index, score}; + } + } + } + return best; + }; + auto cellsOf = [&](int r, const Choice& choice) -> const QVector& { + QVector& cells = reroutes[r].cells[choice.si][choice.ti][choice.index]; + if (cells.isEmpty()) { + cells = occupancy.cells(reroutes[r].candidates[choice.si][choice.ti][choice.index].path); + } + return cells; + }; + auto scoreChoice = [&](int r, const Choice& choice) -> qreal { + return scoreAmong(reroutes[r].candidates[choice.si][choice.ti][choice.index], &occupancy).value(); + }; + auto apply = [&](int r, const Choice& choice) { + Route& route = m_routes[r]; + Reroute& reroute = reroutes[r]; + reroute.current = reroute.candidates[choice.si][choice.ti][choice.index]; + route.m_from = reroute.anchors[0][choice.si]; + route.m_to = reroute.anchors[1][choice.ti]; + route.m_fromSide = choice.si ? 1 : -1; + route.m_toSide = choice.ti ? 1 : -1; + route.m_path = reroute.current.path; + route.m_length = route.m_path.length(); + route.m_cells = cellsOf(r, choice); + }; + for (int pass = 0; pass < 2; ++pass) + { + bool changed = false; + // Each link on its own. + for (int r = 0; r < m_routes.size(); ++r) + { + if (!m_routes[r].m_local) { + continue; + } + occupancy.add(m_routes[r].m_cells, -1); + const Choice choice = routeAmong(r); + if (choice.index >= 0 && choice.score < scoreCurrent(r) - 1) + { + apply(r, choice); + changed = true; + } + occupancy.add(m_routes[r].m_cells, 1); + } + // Nearby pairs that get in each other's way, placed again in the opposite order. + // Rerouting one link at a time cannot swap two links' lanes, as each would first + // have to overlap the other. + for (int a = 0; a < m_routes.size(); ++a) + { + for (int b = 0; b < m_routes.size(); ++b) + { + const Route& routeA = m_routes[a]; + const Route& routeB = m_routes[b]; + if (a == b || !routeA.m_local || !routeB.m_local || + !routeA.m_path.boundingRect().adjusted(-32, -32, 32, 32).intersects(routeB.m_path.boundingRect())) { + continue; + } + occupancy.add(routeA.m_cells, -1); + occupancy.add(routeB.m_cells, -1); + const qreal aAlone = scoreCurrent(a); + const qreal bAlone = scoreCurrent(b); + occupancy.add(routeB.m_cells, 1); + const qreal aWithB = scoreCurrent(a); + occupancy.add(routeB.m_cells, -1); + occupancy.add(routeA.m_cells, 1); + const qreal bWithA = scoreCurrent(b); + occupancy.add(routeA.m_cells, -1); + if (aWithB > aAlone + 1 || bWithA > bAlone + 1) + { + const Choice newB = routeAmong(b); + occupancy.add(cellsOf(b, newB), 1); + const Choice newA = routeAmong(a); + occupancy.add(cellsOf(b, newB), -1); + occupancy.add(cellsOf(a, newA), 1); + const qreal newBScore = scoreChoice(b, newB); + occupancy.add(cellsOf(a, newA), -1); + if (newA.score + newBScore < aWithB + bWithA - 1) + { + apply(a, newA); + apply(b, newB); + changed = true; + } + } + occupancy.add(m_routes[a].m_cells, 1); + occupancy.add(m_routes[b].m_cells, 1); + } + } + if (!changed) { + break; + } + } + for (Route& route : m_routes) + { + if (route.m_local && !route.m_flowLabel.isEmpty()) { + placeFlowLabel(route); + } } // Resolve remaining label overlaps. Place workspace and scroll markers first, as @@ -640,59 +1359,23 @@ bool ConnectionOverlay::computeLayout() }; for (Route& route : m_routes) { - const qreal before = route.markerRect.center().y(); - placeLabel(route.markerRect); - const qreal dy = route.markerRect.center().y() - before; - if (route.stubTip != 0 && !qFuzzyIsNull(dy)) + const qreal before = route.m_markerRect.center().y(); + placeLabel(route.m_markerRect); + const qreal dy = route.m_markerRect.center().y() - before; + if (route.m_stubTip != 0 && !qFuzzyIsNull(dy)) { // Keep a moved stub label on the end of its own line. - QPointF& tip = route.stubTip > 0 ? route.to : route.from; + QPointF& tip = route.m_stubTip > 0 ? route.m_to : route.m_from; tip.ry() += dy; - route.path = connectionPath(route.from, route.to, route.fromSide, route.toSide, - route.sourceBox, route.targetBox, bounds); - route.length = route.path.length(); + route.m_path = connectionPath(route.m_from, route.m_to, route.m_fromSide, route.m_toSide, + route.m_sourceBox, route.m_targetBox, bounds, route.m_obstacles); + route.m_length = route.m_path.length(); } } for (Route& route : m_routes) { - placeLabel(route.flowLabelRect); - } - // Only now are the routes final, so build the painted region. - for (Route& route : m_routes) - { - QRegion routeRegion(maskStroker.createStroke(route.path).toFillPolygon().toPolygon(), Qt::WindingFill); - routeRegion += QRectF(route.from.x() - 9, route.from.y() - 9, 18, 18).toAlignedRect(); - routeRegion += QRectF(route.to.x() - 9, route.to.y() - 9, 18, 18).toAlignedRect(); - for (const QRectF& label : {route.markerRect, route.flowLabelRect}) - { - if (!label.isEmpty()) { - routeRegion += label.toAlignedRect().adjusted(-2, -2, 2, 2); - } - } - route.bounds = routeRegion.boundingRect(); - paintedRegion += routeRegion; - } - - paintedRegion &= m_clipRegion; - // A full-size widget above QOpenGLWidget spectra can occlude their entire - // viewport. Give the overlay only the pixels needed by its drawing. - if (paintedRegion.isEmpty()) { - paintedRegion = QRegion(0, 0, 1, 1); + placeLabel(route.m_flowLabelRect); } - if (mask() != paintedRegion) - { - // An empty mask means the whole widget was painted. - const QRegion oldMask = mask().isEmpty() ? QRegion(rect()) : mask(); - setMask(paintedRegion); - // setMask() asks the QMdiArea to repaint what the overlay no longer covers, - // but the opaque viewport above it is not repainted, leaving trails of old - // routes. Repaint the viewport (and its sub windows) there explicitly. - const QRegion exposed = oldMask - paintedRegion; - if (!exposed.isEmpty()) { - m_mdi->viewport()->update(exposed.translated(pos() - m_mdi->viewport()->pos())); - } - return true; - } - return false; + updateHighlight(); } QVector ConnectionOverlay::pulses() const @@ -700,14 +1383,14 @@ QVector ConnectionOverlay::pulses() const QVector result; for (const Route& route : m_routes) { - if (route.direction == NoDirection || route.length <= 32) { + if (route.m_direction == NoDirection || route.m_length <= 32 || isDimmed(route)) { continue; } - const int flowCount = route.direction == BothDirections ? 2 : 1; - const int pulsesPerFlow = route.length > 360 ? 2 : 1; + const int flowCount = route.m_direction == BothDirections ? 2 : 1; + const int pulsesPerFlow = route.m_length > 360 ? 2 : 1; for (int flow = 0; flow < flowCount; ++flow) { - const bool reverse = route.direction == Reverse || (route.direction == BothDirections && flow == 1); + const bool reverse = route.m_direction == Reverse || (route.m_direction == BothDirections && flow == 1); for (int pulse = 0; pulse < pulsesPerFlow; ++pulse) { qreal progress = m_phase + qreal(pulse) / pulsesPerFlow; @@ -718,101 +1401,179 @@ QVector ConnectionOverlay::pulses() const progress = 1.0 - progress; } const qreal tailProgress = qBound(0, progress + (reverse ? 0.025 : -0.025), 1); - result.append({route.path.pointAtPercent(tailProgress), route.path.pointAtPercent(progress), route.color}); + result.append({route.m_path.pointAtPercent(tailProgress), route.m_path.pointAtPercent(progress), route.m_color}); } } } return result; } -QRegion ConnectionOverlay::pulsesRegion(const QVector& pulses) const -{ - QRegion region; - for (const Pulse& pulse : pulses) { - region += QRectF(pulse.tail, pulse.head).normalized().adjusted(-9, -9, 9, 9).toAlignedRect(); - } - return region; -} - void ConnectionOverlay::animate() { if (!isVisible()) { return; // Workspace is a background tab, or minimized. } - // Only the pulses move, so repaint just where they were and where they are now. + // An OpenGL widget repaints in full, so only repaint when something is moving. m_phase = (m_clock.elapsed() % 2400) / 2400.0; - const QRegion region = pulsesRegion(pulses()); - const QRegion dirty = m_pulseRegion | region; - m_pulseRegion = region; - if (!dirty.isEmpty()) { - update(dirty); + if (!pulses().isEmpty()) { + update(); } } -void ConnectionOverlay::paintEvent(QPaintEvent *event) +void ConnectionOverlay::paintGL() { - // Paint current geometry rather than a stale cache. This paint is clipped to the - // old mask, so repaint again only if the mask changed. - if (m_layoutDirty && computeLayout()) { - update(); + // Paint current geometry rather than a stale cache. + if (m_layoutDirty) { + computeLayout(); + } + QOpenGLFunctions *gl = context()->functions(); + const qreal ratio = devicePixelRatioF(); + const QSize pixels = size() * ratio; + if (!m_paintBuffer || m_paintBuffer->size() != pixels) + { + if (m_resolveBuffer != m_paintBuffer) { + delete m_resolveBuffer; + } + delete m_paintBuffer; + // QPainter's OpenGL engine needs multisampling for smooth lines, and a + // stencil buffer for paths. Without framebuffer blits, paint unsampled. + QOpenGLFramebufferObjectFormat bufferFormat; + bufferFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); + const bool blit = QOpenGLFramebufferObject::hasOpenGLFramebufferBlit(); + bufferFormat.setSamples(blit ? 4 : 0); + m_paintBuffer = new QOpenGLFramebufferObject(pixels, bufferFormat); + m_resolveBuffer = blit ? new QOpenGLFramebufferObject(pixels) : m_paintBuffer; + } + + m_paintBuffer->bind(); + gl->glViewport(0, 0, pixels.width(), pixels.height()); + gl->glClearColor(0, 0, 0, 0); + gl->glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + { + QOpenGLPaintDevice device(pixels); + device.setDevicePixelRatio(ratio); + QPainter painter(&device); + paintRoutes(painter); } - QPainter painter(this); + if (m_resolveBuffer != m_paintBuffer) { + QOpenGLFramebufferObject::blitFramebuffer(m_resolveBuffer, m_paintBuffer); + } + + // Copy into the widget, converting to straight alpha. + gl->glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject()); + gl->glViewport(0, 0, pixels.width(), pixels.height()); + gl->glDisable(GL_BLEND); + gl->glDisable(GL_SCISSOR_TEST); + gl->glDisable(GL_DEPTH_TEST); + gl->glDisable(GL_STENCIL_TEST); + gl->glBindBuffer(GL_ARRAY_BUFFER, 0); + gl->glActiveTexture(GL_TEXTURE0); + gl->glBindTexture(GL_TEXTURE_2D, m_resolveBuffer->texture()); + m_unpremultiply->bind(); + m_unpremultiply->setUniformValue("source", 0); + static const GLfloat quad[] = {-1, -1, 1, -1, -1, 1, 1, 1}; + const int position = m_unpremultiply->attributeLocation("position"); + m_unpremultiply->enableAttributeArray(position); + m_unpremultiply->setAttributeArray(position, quad, 2); + gl->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + m_unpremultiply->disableAttributeArray(position); + m_unpremultiply->release(); +} + +void ConnectionOverlay::paintRoutes(QPainter& painter) +{ painter.setRenderHint(QPainter::Antialiasing); - painter.setClipRegion(m_clipRegion & event->region()); - const QRegion& dirty = event->region(); for (const Route& route : m_routes) { - if (!dirty.intersects(route.bounds)) { - continue; - } - const QColor& color = route.color; + const QColor& color = route.m_color; + const bool highlighted = m_highlightActive && !isDimmed(route); + painter.setOpacity(isDimmed(route) ? dimmedOpacity : 1.0); // drawPath also fills with the current brush. The preceding edge draws // its endpoint dots with a solid brush, so clear it before every path. painter.setBrush(Qt::NoBrush); - painter.setPen(QPen(QColor(color.red(), color.green(), color.blue(), 35), 6, + painter.setPen(QPen(QColor(color.red(), color.green(), color.blue(), highlighted ? 90 : 35), highlighted ? 8 : 6, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); - painter.drawPath(route.path); + painter.drawPath(route.m_path); painter.setPen(QPen(QColor(color.red(), color.green(), color.blue(), 180), 2.2, - route.kind == PipeLink ? Qt::DashLine : Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); - painter.drawPath(route.path); + route.m_kind == PipeLink ? Qt::DashLine : Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + painter.drawPath(route.m_path); painter.setBrush(QColor(color.red(), color.green(), color.blue(), 220)); painter.setPen(QPen(palette().window().color(), 1.5)); - painter.drawEllipse(route.from, 4, 4); - painter.drawEllipse(route.to, 4, 4); + painter.drawEllipse(route.m_from, 4, 4); + painter.drawEllipse(route.m_to, 4, 4); } + painter.setOpacity(1.0); for (const Pulse& pulse : pulses()) { - const QColor& color = pulse.color; + const QColor& color = pulse.m_color; painter.setBrush(Qt::NoBrush); painter.setPen(QPen(QColor(color.red(), color.green(), color.blue(), 210), 4, Qt::SolidLine, Qt::RoundCap)); - painter.drawLine(pulse.tail, pulse.head); + painter.drawLine(pulse.m_tail, pulse.m_head); painter.setPen(Qt::NoPen); painter.setBrush(QColor(color.red(), color.green(), color.blue(), 130)); - painter.drawEllipse(pulse.head, 7, 7); + painter.drawEllipse(pulse.m_head, 7, 7); painter.setBrush(QColor(255, 255, 255, 245)); - painter.drawEllipse(pulse.head, 3, 3); + painter.drawEllipse(pulse.m_head, 3, 3); } for (const Route& route : m_routes) { - if (!route.flowLabel.isEmpty() && dirty.intersects(route.flowLabelRect.toAlignedRect())) + painter.setOpacity(isDimmed(route) ? dimmedOpacity : 1.0); + if (!route.m_flowLabel.isEmpty()) { - painter.setPen(QPen(route.color, 1)); + painter.setPen(QPen(route.m_color, 1)); painter.setBrush(QColor(24, 28, 32, 235)); - painter.drawRoundedRect(route.flowLabelRect, 3, 3); + painter.drawRoundedRect(route.m_flowLabelRect, 3, 3); painter.setPen(Qt::white); - painter.drawText(route.flowLabelRect, Qt::AlignCenter, route.flowLabel); + painter.drawText(route.m_flowLabelRect, Qt::AlignCenter, route.m_flowLabel); } - if (!route.marker.isEmpty() && dirty.intersects(route.markerRect.toAlignedRect())) + if (!route.m_marker.isEmpty()) { painter.setPen(Qt::NoPen); painter.setBrush(QColor(24, 28, 32, 235)); - painter.drawRoundedRect(route.markerRect, 3, 3); + painter.drawRoundedRect(route.m_markerRect, 3, 3); painter.setPen(Qt::white); - painter.drawText(route.markerRect, Qt::AlignCenter, route.marker); + painter.drawText(route.m_markerRect, Qt::AlignCenter, route.m_marker); } } } + +void ConnectionOverlay::setHovered(QMdiSubWindow *window) +{ + if (m_hovered == window) { + return; + } + m_hovered = window; + const bool wasActive = m_highlightActive; + updateHighlight(); + if (wasActive || m_highlightActive) { + update(); // Opacity changes affect every route + } +} + +void ConnectionOverlay::updateHighlight() +{ + // Only highlight when the hovered window has links drawn in this workspace; + // hovering an unconnected window leaves every link at full strength. + bool active = false; + if (m_hovered) + { + for (const Route& route : m_routes) + { + if (route.m_source == m_hovered || route.m_target == m_hovered) + { + active = true; + break; + } + } + } + m_highlightActive = active; +} + +bool ConnectionOverlay::isDimmed(const Route& route) const +{ + return m_highlightActive && route.m_source != m_hovered && route.m_target != m_hovered; +} diff --git a/sdrgui/gui/connectionoverlay.h b/sdrgui/gui/connectionoverlay.h index fb04620a1..08061574c 100644 --- a/sdrgui/gui/connectionoverlay.h +++ b/sdrgui/gui/connectionoverlay.h @@ -28,13 +28,18 @@ #include #include #include -#include +#include class QMdiArea; class QMdiSubWindow; +class QOpenGLFramebufferObject; +class QOpenGLShaderProgram; +class QPainter; // A non-interactive drawing layer above one workspace's MDI windows, showing connections between windows. -class ConnectionOverlay : public QWidget +// It is an OpenGL widget stacked on top, so it blends over OpenGL spectra too; a translucent +// ordinary widget shows black where it overlaps them. +class ConnectionOverlay : public QOpenGLWidget { Q_OBJECT public: @@ -42,19 +47,20 @@ public: enum Direction { NoDirection, Forward, Reverse, BothDirections }; struct Connection { - QPointer source; - QPointer target; - QString sourceLabel; - QString targetLabel; - QString flowLabel; - int sourceWorkspace = -1; - int targetWorkspace = -1; - Kind kind = DeviceLink; - Direction direction = Forward; - QColor color; + QPointer m_source; + QPointer m_target; + QString m_sourceLabel; + QString m_targetLabel; + QString m_flowLabel; + int m_sourceWorkspace = -1; + int m_targetWorkspace = -1; + Kind m_kind = DeviceLink; + Direction m_direction = Forward; + QColor m_color; }; explicit ConnectionOverlay(QMdiArea *mdi); + ~ConnectionOverlay() override; void setConnections(const QList& connections); void setOverlayVisible(bool visible); @@ -62,57 +68,72 @@ protected: bool eventFilter(QObject *object, QEvent *event) override; void changeEvent(QEvent *event) override; void showEvent(QShowEvent *event) override; - void paintEvent(QPaintEvent *event) override; + void initializeGL() override; + void paintGL() override; private: // Geometry for one drawn connection, cached until windows move or connections change. struct Route { - QPainterPath path; - qreal length = 0; - QRect bounds; - QPointF from; - QPointF to; - QColor color; - Kind kind = DeviceLink; - Direction direction = Forward; - QString flowLabel; - QRectF flowLabelRect; - QString marker; - QRectF markerRect; + QPainterPath m_path; + qreal m_length = 0; + QPointF m_from; + QPointF m_to; + QColor m_color; + Kind m_kind = DeviceLink; + Direction m_direction = Forward; + QString m_flowLabel; + QRectF m_flowLabelRect; + QString m_marker; + QRectF m_markerRect; // Inputs to connectionPath(), kept so a stub can be rerouted to follow its label. - int fromSide = 0; - int toSide = 0; - QRectF sourceBox; - QRectF targetBox; - int stubTip = 0; // Cross-workspace stub: 1 if to is its tip, -1 if from is + int m_fromSide = 0; + int m_toSide = 0; + QRectF m_sourceBox; + QRectF m_targetBox; + QVector m_obstacles; // Other windows the route should avoid + int m_stubTip = 0; // Cross-workspace stub: 1 if m_to is its tip, -1 if m_from is + QMdiSubWindow *m_source = nullptr; // For hover highlighting; compared, never dereferenced + QMdiSubWindow *m_target = nullptr; + bool m_local = false; // Both ends in this workspace, so it can be rerouted around other links + QVector m_cells; // Lane cells the route reserves, see Occupancy in the .cpp }; struct Pulse { - QPointF tail; - QPointF head; - QColor color; + QPointF m_tail; + QPointF m_head; + QColor m_color; }; QMdiArea *m_mdi; QList m_connections; - QHash> m_indexLabels; + QList> m_windows; // Windows with links, to watch for moves QList> m_watched; QVector m_routes; - QRegion m_clipRegion; // Everything except the title ID labels - QRegion m_pulseRegion; // Area covered by the pulses last painted qreal m_phase = 0; + QPointer m_hovered; // Window under the mouse, if it has links + bool m_highlightActive = false; // Dim links that do not touch m_hovered bool m_layoutDirty = true; // Cached routes no longer match the windows bool m_relayoutPending = false; // A queued relayout() is outstanding QElapsedTimer m_clock; QTimer m_animationTimer; + // QPainter draws premultiplied alpha, but Qt composites an OpenGL widget as straight + // alpha, which darkens anything translucent. So paint offscreen, then copy through a + // shader that converts to straight alpha. + QOpenGLFramebufferObject *m_paintBuffer = nullptr; // Multisampled, painted by QPainter + QOpenGLFramebufferObject *m_resolveBuffer = nullptr; // Plain texture the shader reads + QOpenGLShaderProgram *m_unpremultiply = nullptr; static bool sameConnections(const QList& a, const QList& b); void watchWindows(); void invalidateLayout(); void relayout(); - bool computeLayout(); // Returns true if the mask changed + void computeLayout(); QVector pulses() const; - QRegion pulsesRegion(const QVector& pulses) const; void animate(); + void setHovered(QMdiSubWindow *window); + void updateHighlight(); + bool isDimmed(const Route& route) const; + void paintRoutes(QPainter& painter); + void releaseGL(); }; #endif // SDRGUI_GUI_CONNECTIONOVERLAY_H_ diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index a6e720933..cf1321c93 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -2801,15 +2801,15 @@ void MainWindow::updateConnectionOverlays() return; } ConnectionOverlay::Connection connection; - connection.source = device; - connection.target = target; - connection.sourceLabel = deviceLabel; - connection.targetLabel = label; - connection.flowLabel = flowLabel; - connection.sourceWorkspace = device->getWorkspaceIndex(); - connection.targetWorkspace = targetWorkspace; - connection.direction = direction; - connection.color = color; + connection.m_source = device; + connection.m_target = target; + connection.m_sourceLabel = deviceLabel; + connection.m_targetLabel = label; + connection.m_flowLabel = flowLabel; + connection.m_sourceWorkspace = device->getWorkspaceIndex(); + connection.m_targetWorkspace = targetWorkspace; + connection.m_direction = direction; + connection.m_color = color; connections.append(connection); }; @@ -2907,21 +2907,21 @@ void MainWindow::updateConnectionOverlays() if (existingIndex >= 0) { if (audio) { - connections[existingIndex].flowLabel = audioLabel(source); + connections[existingIndex].m_flowLabel = audioLabel(source); } continue; } ConnectionOverlay::Connection connection; - connection.source = source.window; - connection.target = target.window; - connection.sourceLabel = source.label; - connection.targetLabel = target.label; - connection.flowLabel = audio ? audioLabel(source) : QString("Data"); - connection.sourceWorkspace = source.workspace; - connection.targetWorkspace = target.workspace; - connection.kind = ConnectionOverlay::PipeLink; - connection.direction = ConnectionOverlay::Forward; - connection.color = source.linkColor.isValid() ? source.linkColor : target.linkColor; + connection.m_source = source.window; + connection.m_target = target.window; + connection.m_sourceLabel = source.label; + connection.m_targetLabel = target.label; + connection.m_flowLabel = audio ? audioLabel(source) : QString("Data"); + connection.m_sourceWorkspace = source.workspace; + connection.m_targetWorkspace = target.workspace; + connection.m_kind = ConnectionOverlay::PipeLink; + connection.m_direction = ConnectionOverlay::Forward; + connection.m_color = source.linkColor.isValid() ? source.linkColor : target.linkColor; pipeConnectionIndices[source.window].insert(target.window, connections.size()); connections.append(connection); }