• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphia-app / graphia / 42082325281

05 Nov 2026 06:13PM UTC coverage: 71.439% (+0.08%) from 71.364%
42082325281

push

github

timangus
Use QCustomPlot's split sources from its repository

11786 of 18050 branches covered (65.3%)

Branch coverage included in aggregate %.

30011 of 40457 relevant lines covered (74.18%)

300661.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/source/app/rendering/graphcomponentrenderer.cpp
1
/* Copyright © 2013-2025 Tim Angus
2
 * Copyright © 2013-2025 Tom Freeman
3
 *
4
 * This file is part of Graphia.
5
 *
6
 * Graphia is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Graphia is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with Graphia.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

20
#include "graphcomponentrenderer.h"
21
#include "graphrenderer.h"
22

23
#include "app/graph/graph.h"
24
#include "app/graph/graphmodel.h"
25

26
#include "app/layout/collision.h"
27
#include "app/layout/nodepositions.h"
28

29
#include "app/maths/boundingsphere.h"
30
#include "app/maths/plane.h"
31

32
#include "app/ui/selectionmanager.h"
33
#include "app/ui/visualisations/elementvisual.h"
34

35
#include "app/preferences.h"
36

37
#include "shared/graph/elementid_debug.h"
38
#include "shared/graph/igraphcomponent.h"
39
#include "shared/utils/utils.h"
40

41
#include <QDebug>
42
#include <QObject>
43
#include <QtGlobal>
44
#include <QKeyEvent>
45
#include <QMouseEvent>
46
#include <QtMath>
47

48
#include <cmath>
49
#include <algorithm>
50
#include <cstddef>
51
#include <iterator>
52
#include <limits>
53

54
void GraphComponentRenderer::initialise(GraphModel* graphModel, ComponentId componentId,
55
                                        SelectionManager* selectionManager,
56
                                        GraphRenderer* graphRenderer)
57
{
×
58
    if(_initialised)
×
59
    {
×
60
        synchronise();
×
61
        return;
×
62
    }
×
63

64
    _graphModel = graphModel;
×
65
    _componentId = componentId;
×
66
    _selectionManager = selectionManager;
×
67
    _graphRenderer = graphRenderer;
×
68

69
    _viewData.reset();
×
70
    _targetZoomDistance = _viewData._zoomDistance;
×
71
    _moveFocusToCentreOfComponentLater = true;
×
72
    _projection = graphRenderer->projection();
×
73

74
    synchronise();
×
75

76
    QObject::connect(&_zoomTransition, &Transition::started, graphRenderer,
×
77
                     &GraphRenderer::rendererStartedTransition, Qt::DirectConnection);
×
78
    QObject::connect(&_zoomTransition, &Transition::finished, graphRenderer,
×
79
                     &GraphRenderer::rendererFinishedTransition, Qt::DirectConnection);
×
80

81
    _initialised = true;
×
82
    _visible = false;
×
83
}
×
84

85
void GraphComponentRenderer::setVisible(bool visible)
86
{
×
87
    _visible = visible;
×
88
}
×
89

90
void GraphComponentRenderer::cleanup()
91
{
×
92
    if(!_initialised)
×
93
        return;
×
94

95
    if(_frozen)
×
96
    {
×
97
        _cleanupWhenThawed = true;
×
98
        return;
×
99
    }
×
100

101
    _zoomTransition.disconnect(); // clazy:exclude=unused-result-check
×
102

103
    _graphRenderer->onComponentCleanup(_componentId);
×
104

105
    _nodeIds.clear();
×
106
    _edges.clear();
×
107

108
    _graphModel = nullptr;
×
109
    _componentId.setToNull();
×
110
    _selectionManager = nullptr;
×
111
    _graphRenderer = nullptr;
×
112

113
    _initialised = false;
×
114
    _visible = false;
×
115

116
    _viewData.reset();
×
117
    _savedViewData.reset();
×
118
}
×
119

120
void GraphComponentRenderer::synchronise()
121
{
×
122
    if(_frozen)
×
123
    {
×
124
        _synchroniseWhenThawed = true;
×
125
        return;
×
126
    }
×
127

128
    _nodeIds.clear();
×
129
    _edges.clear();
×
130

131
    const auto* component = _graphModel->graph().componentById(_componentId);
×
132
    Q_ASSERT(component != nullptr);
×
133

134
    _nodeIds = component->nodeIds();
×
135

136
    // If the saved focus node has gone away (e.g. it was merged with
137
    // another node), then reset the view data
138
    if(!_savedViewData.isReset() && !u::contains(_nodeIds, _savedViewData._focusNodeId))
×
139
        _savedViewData.reset();
×
140

141
    const auto& edgeIds = component->edgeIds();
×
142
    std::ranges::transform(edgeIds, std::back_inserter(_edges),
×
143
    [this](auto edgeId)
×
144
    {
×
145
        return &_graphModel->graph().edgeById(edgeId);
×
146
    });
×
147
}
×
148

149
void GraphComponentRenderer::cloneViewDataFrom(const GraphComponentRenderer& other)
150
{
×
151
    _viewData = other._viewData;
×
152
}
×
153

154
void GraphComponentRenderer::restoreViewData()
155
{
×
156
    _viewData._autoZooming = _savedViewData._autoZooming;
×
157
    _viewData._focusNodeId = _savedViewData._focusNodeId;
×
158

159
    Q_ASSERT(_viewData._focusNodeId.isNull() || u::contains(_nodeIds, _viewData._focusNodeId));
×
160

161
    updateCentreAndZoomDistance();
×
162
    updateCameraProjection(_viewData.camera());
×
163
    zoomToDistance(_savedViewData._zoomDistance);
×
164

165
    if(_savedViewData._focusNodeId.isNull())
×
166
        centrePositionInViewport(_viewData._componentCentre, _viewData._zoomDistance);
×
167
    else
×
168
        centreNodeInViewport(_savedViewData._focusNodeId, _viewData._zoomDistance);
×
169

170
    _savedViewData.reset();
×
171
}
×
172

173
void GraphComponentRenderer::freeze()
174
{
×
175
    _frozen = true;
×
176
}
×
177

178
void GraphComponentRenderer::thaw()
179
{
×
180
    if(!_frozen)
×
181
        return;
×
182

183
    _frozen = false;
×
184

185
    if(_synchroniseWhenThawed && !_cleanupWhenThawed)
×
186
    {
×
187
        synchronise();
×
188
        _synchroniseWhenThawed = false;
×
189
    }
×
190

191
    if(_cleanupWhenThawed)
×
192
    {
×
193
        cleanup();
×
194
        _cleanupWhenThawed = false;
×
195
    }
×
196
}
×
197

198
float GraphComponentRenderer::maxNodeDistanceFromPoint(const GraphModel& graphModel,
199
    const QVector3D& centre, const std::vector<NodeId>& nodeIds)
200
{
×
201
    float maxDistance = std::numeric_limits<float>::lowest();
×
202
    auto nodePositions = graphModel.nodePositions().get(nodeIds);
×
203
    auto nodeVisuals = graphModel.nodeVisuals(nodeIds);
×
204
    Q_ASSERT(nodePositions.size() == nodeVisuals.size());
×
205

206
    for(size_t i = 0; i < nodeIds.size(); i++)
×
207
    {
×
208
        const QVector3D nodePosition = nodePositions.at(i);
×
209
        const auto& nodeVisual = nodeVisuals.at(i);
×
210
        const float distance = (centre - nodePosition).length() + nodeVisual._size;
×
211

212
        maxDistance = std::max(distance, maxDistance);
×
213
    }
×
214

215
    // HACK: allow single node components to be zoomed out beyond their natural maximum
216
    if(nodeIds.size() == 1)
×
217
        maxDistance *= 2.2f;
×
218

219
    return maxDistance;
×
220
}
×
221

222
QMatrix4x4 GraphComponentRenderer::subViewportMatrix(QRectF sub, QRect viewport)
223
{
×
224
    auto subCentre = sub.center();
×
225
    auto viewportF = QRectF(viewport);
×
226

227
    // The translation values effectively represent the centre of
228
    // sub in terms of viewport, using NDC
229
    auto xNormalised = static_cast<float>((subCentre.x() - viewportF.x()) / viewportF.width());
×
230
    auto yNormalised = static_cast<float>((subCentre.y() - viewportF.y()) / viewportF.height());
×
231
    auto xTranslation = (2.0f * xNormalised) - 1.0f;
×
232
    auto yTranslation = (2.0f * yNormalised) - 1.0f;
×
233

234
    QMatrix4x4 m;
×
235
    m.translate(xTranslation, -yTranslation);
×
236

237
    auto xScale = sub.width() / viewportF.width();
×
238
    auto yScale = sub.height() / viewportF.height();
×
239
    m.scale(static_cast<float>(xScale), static_cast<float>(yScale));
×
240

241
    return m;
×
242
}
×
243

244
float GraphComponentRenderer::zoomDistanceForRadius(float radius, Projection projection) const
245
{
×
246
    if(projection == Projection::Unset)
×
247
        projection = _projection;
×
248

249
    if(projection == Projection::Perspective)
×
250
    {
×
251
        const float minHalfFov = qDegreesToRadians(std::min(_fovx, _fovy) * 0.5f);
×
252

253
        if(minHalfFov > 0.0f)
×
254
            return std::max(radius / std::sin(minHalfFov), MINIMUM_ZOOM_DISTANCE);
×
255

256
        qDebug() << "WARNING: ComponentId" << _componentId << "GraphComponentRenderer fov not set";
×
257
        return MINIMUM_ZOOM_DISTANCE;
×
258
    }
×
259

260
    return std::max(radius, MINIMUM_ZOOM_DISTANCE);
×
261
}
×
262

263
float GraphComponentRenderer::maxDistanceFor(NodeId nodeId) const
264
{
×
265
    if(!componentIsValid())
×
266
        return -1.0f;
×
267

268
    Q_ASSERT(!_frozen);
×
269
    const auto& nodeIds = _graphModel->graph().componentById(_componentId)->nodeIds();
×
270

271
    const QVector3D position = !nodeId.isNull() ?
×
272
        _graphModel->nodePositions().get(nodeId) :
×
273
        _viewData._componentCentre;
×
274

275
    return maxNodeDistanceFromPoint(*_graphModel, position, nodeIds);
×
276
}
×
277

278
float GraphComponentRenderer::entireComponentZoomDistanceFor(NodeId nodeId) const
279
{
×
280
    auto maxDistance = maxDistanceFor(nodeId);
×
281
    return zoomDistanceForRadius(maxDistance);
×
282
}
×
283

284
void GraphComponentRenderer::updateCentreAndZoomDistance()
285
{
×
286
    if(!componentIsValid())
×
287
        return;
×
288

289
    Q_ASSERT(!_frozen);
×
290
    const auto& nodeIds = _graphModel->graph().componentById(_componentId)->nodeIds();
×
291

292
    _viewData._componentCentre = _graphModel->nodePositions().centreOfMass(nodeIds);
×
293

294
    auto maxDistance = maxDistanceFor(_viewData._focusNodeId);
×
295
    if(maxDistance >= 0.0f)
×
296
    {
×
297
        _entireComponentZoomDistance = zoomDistanceForRadius(maxDistance);
×
298
        _orthoCameraDistance = zoomDistanceForRadius(maxDistance, Projection::Perspective);
×
299
        _maxDistanceFromFocus = maxDistance;
×
300
    }
×
301
}
×
302

303
void GraphComponentRenderer::update(float t)
304
{
×
305
    Q_ASSERT(_initialised);
×
306

307
    if(_graphModel != nullptr)
×
308
    {
×
309
        Q_ASSERT(_graphRenderer != nullptr);
×
310

311
        if(!_frozen)
×
312
        {
×
313
            if(_graphRenderer->layoutChanged())
×
314
            {
×
315
                updateCentreAndZoomDistance();
×
316
            }
×
317
            else if(_moveFocusToCentreOfComponentLater)
×
318
            {
×
319
                moveFocusToCentreOfComponent();
×
320
                _moveFocusToCentreOfComponentLater = false;
×
321
            }
×
322
        }
×
323

324
        _zoomTransition.update(t);
×
325

326
        if(!_graphRenderer->transition().active() && _trackFocus)
×
327
        {
×
328
            if(trackingCentreOfComponent())
×
329
            {
×
330
                if(!_zoomTransition.active() && _viewData._autoZooming)
×
331
                    zoomToDistance(_entireComponentZoomDistance);
×
332

333
                centrePositionInViewport(_viewData._componentCentre, _viewData._zoomDistance);
×
334
            }
×
335
            else
×
336
                centreNodeInViewport(_viewData._focusNodeId, _viewData._zoomDistance);
×
337
        }
×
338
    }
×
339
}
×
340

341
void GraphComponentRenderer::updateCameraProjection(Camera& camera)
342
{
×
343
    auto aspectRatio = static_cast<float>(_dimensions.width() / _dimensions.height());
×
344

345
    auto minCameraDistance = _viewData.camera().distance();
×
346

347
    if(_zoomTransition.active())
×
348
        minCameraDistance = std::min(minCameraDistance, _targetZoomDistance);
×
349
    else if(_graphRenderer->transition().active())
×
350
        minCameraDistance = std::min(minCameraDistance, _viewData._transitionEnd._camera.distance());
×
351

352
    // Keep the near clipping plane as far away as possible, and in
353
    // so doing maximise the available depth buffer resolution
354
    const auto nearClip = std::max(minCameraDistance - _maxDistanceFromFocus, 0.3f);
×
355

356
    // Nodes further away than this are too small to see. In theory it could
357
    // be calculated too, but the gain in depth precision is questionable
358
    const auto farClip = 50000.0f;
×
359

360
    if(_projection == Projection::Perspective)
×
361
    {
×
362
        camera.setPerspectiveProjection(_fovy, aspectRatio, nearClip, farClip);
×
363
    }
×
364
    else
×
365
    {
×
366
        auto horizontal = _viewData._zoomDistance * aspectRatio;
×
367
        auto vertical = _viewData._zoomDistance;
×
368

369
        camera.setOrthographicProjection(-horizontal, horizontal,
×
370
            -vertical, vertical, nearClip, farClip);
×
371
    }
×
372
}
×
373

374
QMatrix4x4 GraphComponentRenderer::modelViewMatrix() const
375
{
×
376
    return _viewData.camera().viewMatrix();
×
377
}
×
378

379
QMatrix4x4 GraphComponentRenderer::projectionMatrix() const
380
{
×
381
    return subViewportMatrix(_dimensions, {0, 0, _viewportWidth, _viewportHeight}) *
×
382
        _viewData.camera().projectionMatrix();
×
383
}
×
384

385
void GraphComponentRenderer::setViewportSize(int viewportWidth, int viewportHeight)
386
{
×
387
    if(_initialised && viewportWidth > 0 && viewportHeight > 0)
×
388
    {
×
389
        _viewportWidth = viewportWidth;
×
390
        _viewportHeight = viewportHeight;
×
391
    }
×
392
    else
×
393
    {
×
394
        qWarning() << "GraphComponentRenderer::resize(" << viewportWidth << "," << viewportHeight <<
×
395
                    ") failed _initialised:" << _initialised;
×
396
    }
×
397
}
×
398

399
void GraphComponentRenderer::setDimensions(const QRectF& dimensions)
400
{
×
401
    _dimensions = dimensions;
×
402

403
    auto aspectRatio = static_cast<float>(_dimensions.width() / _dimensions.height());
×
404
    _fovy = 60.0f;
×
405
    _fovx = _fovy * aspectRatio;
×
406

407
    _viewData.camera().setViewport(_dimensions);
×
408
}
×
409

410
bool GraphComponentRenderer::transitionActive()
411
{
×
412
    return _graphRenderer->transition().active() || _zoomTransition.active();
×
413
}
×
414

415
void GraphComponentRenderer::setAlpha(float alpha)
416
{
×
417
    if(_alpha != alpha)
×
418
    {
×
419
        _alpha = alpha;
×
420

421
        Q_ASSERT(_graphRenderer != nullptr);
×
422
        _graphRenderer->onComponentAlphaChanged(_componentId);
×
423
    }
×
424
}
×
425

426
void GraphComponentRenderer::zoom(float delta, NamedBool<"doTransition"> doTransition)
427
{
×
428
    Q_ASSERT(_graphRenderer != nullptr);
×
429

430
    if(delta == 0.0f || _graphRenderer->transition().active())
×
431
        return;
×
432

433
    // Don't allow zooming out if autozooming
434
    if(delta < 0.0f && _viewData._autoZooming)
×
435
        return;
×
436

437
    float size = 0.0f;
×
438

439
    _viewData._autoZooming = false;
×
440

441
    if(!_viewData._focusNodeId.isNull())
×
442
        size = _graphModel->nodeVisual(_viewData._focusNodeId)._size;
×
443

444
    const float INTERSECTION_AVOIDANCE_OFFSET = 1.0f;
×
445
    const float MIN_SEPARATION = 2.0f;
×
446
    auto separation = std::max(((_targetZoomDistance - size) - INTERSECTION_AVOIDANCE_OFFSET), MIN_SEPARATION);
×
447
    auto step = delta * separation;
×
448

449
    _targetZoomDistance -= step;
×
450
    _targetZoomDistance = std::max(_targetZoomDistance, MINIMUM_ZOOM_DISTANCE);
×
451

452
    if(_targetZoomDistance > _entireComponentZoomDistance)
×
453
    {
×
454
        _targetZoomDistance = _entireComponentZoomDistance;
×
455

456
        // If we zoom out all the way then use autozoom mode
457
        if(_viewData._focusNodeId.isNull())
×
458
            _viewData._autoZooming = true;
×
459
    }
×
460

461
    const float startZoomDistance = _viewData._zoomDistance;
×
462

463
    // Don't do anything if there would be no change
464
    if(qFuzzyCompare(startZoomDistance, _targetZoomDistance))
×
465
        return;
×
466

467
    if(visible() && doTransition)
×
468
    {
×
469
        _zoomTransition.start(0.1f, Transition::Type::Linear,
×
470
        [this, startZoomDistance](float f)
×
471
        {
×
472
            _viewData._zoomDistance = startZoomDistance + ((_targetZoomDistance - startZoomDistance) * f);
×
473
        });
×
474
    }
×
475
    else
×
476
        _viewData._zoomDistance = _targetZoomDistance;
×
477
}
×
478

479
void GraphComponentRenderer::zoomToDistance(float distance)
480
{
×
481
    distance = std::clamp(distance, MINIMUM_ZOOM_DISTANCE, _entireComponentZoomDistance);
×
482
    _viewData._zoomDistance = _targetZoomDistance = distance;
×
483
}
×
484

485
void GraphComponentRenderer::centreNodeInViewport(NodeId nodeId, float zoomDistance, QQuaternion rotation)
486
{
×
487
    if(nodeId.isNull())
×
488
        return;
×
489

490
    centrePositionInViewport(_graphModel->nodePositions().get(nodeId), zoomDistance, rotation);
×
491
}
×
492

493
void GraphComponentRenderer::centrePositionInViewport(const QVector3D& focus,
494
    float zoomDistance, QQuaternion rotation)
495
{
×
496
    if(zoomDistance < 0.0f)
×
497
    {
×
498
        if(_projection == Projection::Perspective)
×
499
        {
×
500
            if(_viewData._autoZooming)
×
501
                zoomDistance = _entireComponentZoomDistance;
×
502
            else
×
503
            {
×
504
                // In the perspective case we need to calculate the zoom distance based on
505
                // the current camera position and its target...
506

507
                const QVector3D& oldPosition = _viewData.camera().position();
×
508
                QVector3D newPosition;
×
509
                const Plane translationPlane(focus, _viewData.camera().viewVector());
×
510

511
                if(translationPlane.sideForPoint(oldPosition) == Plane::Side::Back)
×
512
                {
×
513
                    // We're behind the translation plane, so move along it
514
                    const QVector3D cameraPlaneIntersection = translationPlane.rayIntersection(
×
515
                                Ray(oldPosition, _viewData.camera().viewVector()));
×
516
                    const QVector3D translation = focus - cameraPlaneIntersection;
×
517

518
                    newPosition = oldPosition + translation;
×
519
                }
×
520
                else
×
521
                {
×
522
                    // We're in front of the translation plane, so move directly to the target
523
                    newPosition = focus + (oldPosition - _viewData.camera().focus());
×
524
                }
×
525

526
                zoomDistance = newPosition.distanceToPoint(focus);
×
527
            }
×
528
        }
×
529
        else
×
530
        {
×
531
            // ...but in the ortho case the camera is fixed, so we don't need to bother
532
            zoomDistance = _viewData._zoomDistance;
×
533

534
            if(_viewData._autoZooming || _entireComponentZoomDistance < zoomDistance)
×
535
                zoomDistance = _entireComponentZoomDistance;
×
536
        }
×
537
    }
×
538

539
    zoomDistance = std::clamp(zoomDistance, MINIMUM_ZOOM_DISTANCE, _entireComponentZoomDistance);
×
540

541
    if(!_zoomTransition.active())
×
542
        zoomToDistance(zoomDistance);
×
543

544
    auto cameraDistance = _projection == Projection::Perspective ?
×
545
        // In perspective, camera moves
546
        zoomDistance :
×
547
        // In ortho, camera is fixed in a non-intersecting position
548
        _orthoCameraDistance;
×
549

550
    Q_ASSERT(_graphRenderer != nullptr);
×
551

552
    if(!_graphRenderer->transition().active() || !_viewData.camera().valid())
×
553
    {
×
554
        _viewData.camera().setDistance(cameraDistance);
×
555
        _viewData.camera().setFocus(focus);
×
556

557
        if(!rotation.isNull())
×
558
            _viewData.camera().setRotation(rotation);
×
559

560
        updateCameraProjection(_viewData.camera());
×
561
        _viewData._cameraAndLighting._lightScale = _orthoCameraDistance;
×
562

563
        _viewData._transitionStart = _viewData._transitionEnd =
×
564
            _viewData._cameraAndLighting;
×
565
    }
×
566
    else
×
567
    {
×
568
        _viewData._transitionStart = _viewData._cameraAndLighting;
×
569

570
        _viewData._transitionEnd = _viewData._cameraAndLighting;
×
571
        _viewData._transitionEnd._camera.setDistance(cameraDistance);
×
572
        _viewData._transitionEnd._camera.setFocus(focus);
×
573

574
        if(!rotation.isNull())
×
575
            _viewData._transitionEnd._camera.setRotation(rotation);
×
576

577
        updateCameraProjection(_viewData._transitionEnd._camera);
×
578
        _viewData._transitionEnd._lightScale = _orthoCameraDistance;
×
579
    }
×
580
}
×
581

582
void GraphComponentRenderer::moveFocusToNode(NodeId nodeId, float radius)
583
{
×
584
    if(!componentIsValid())
×
585
        return;
×
586

587
    Q_ASSERT(u::contains(_nodeIds, nodeId));
×
588

589
    _viewData._focusNodeId = nodeId;
×
590
    _viewData._autoZooming = false;
×
591
    updateCentreAndZoomDistance();
×
592

593
    float zoomDistance = -1.0f;
×
594
    if(radius >= 0.0f)
×
595
        zoomDistance = zoomDistanceForRadius(radius);
×
596

597
    centreNodeInViewport(nodeId, zoomDistance);
×
598
}
×
599

600
void GraphComponentRenderer::moveSavedFocusToNode(NodeId nodeId, float radius)
601
{
×
602
    if(!componentIsValid())
×
603
        return;
×
604

605
    Q_ASSERT(u::contains(_nodeIds, nodeId));
×
606

607
    _savedViewData._focusNodeId = nodeId;
×
608
    _savedViewData._autoZooming = false;
×
609

610
    float zoomDistance = -1.0f;
×
611
    if(radius >= 0.0f)
×
612
        zoomDistance = zoomDistanceForRadius(radius);
×
613
    else
×
614
        zoomDistance = entireComponentZoomDistanceFor(_savedViewData._focusNodeId);
×
615

616
    _savedViewData._zoomDistance = _targetZoomDistance = zoomDistance;
×
617
}
×
618

619
void GraphComponentRenderer::resetView()
620
{
×
621
    if(!componentIsValid())
×
622
        return;
×
623

624
    _viewData._autoZooming = true;
×
625

626
    moveFocusToCentreOfComponent();
×
627
}
×
628

629
void GraphComponentRenderer::moveFocusToCentreOfComponent()
630
{
×
631
    if(!componentIsValid())
×
632
        return;
×
633

634
    _viewData._focusNodeId.setToNull();
×
635
    updateCentreAndZoomDistance();
×
636

637
    centrePositionInViewport(_viewData._componentCentre);
×
638
}
×
639

640
void GraphComponentRenderer::moveFocusToNodeClosestCameraVector()
641
{
×
642
    if(!componentIsValid())
×
643
        return;
×
644

645
    Collision collision(*_graphModel, _componentId);
×
646
    const NodeId closestNodeId = collision.nodeClosestToLine(_viewData.camera().position(), _viewData.camera().viewVector());
×
647
    if(!closestNodeId.isNull())
×
648
        moveFocusToNode(closestNodeId);
×
649
}
×
650

651
void GraphComponentRenderer::moveFocusTo(const QVector3D& position, float radius, const QQuaternion& rotation)
652
{
×
653
    _viewData._focusNodeId.setToNull();
×
654
    _viewData._componentCentre = position;
×
655

656
    _entireComponentZoomDistance = zoomDistanceForRadius(radius);
×
657
    _orthoCameraDistance = zoomDistanceForRadius(radius, Projection::Perspective);
×
658
    _maxDistanceFromFocus = radius;
×
659
    zoomToDistance(_entireComponentZoomDistance);
×
660

661
    centrePositionInViewport(_viewData._componentCentre, _viewData._zoomDistance, rotation);
×
662
}
×
663

664
void GraphComponentRenderer::doProjectionTransition()
665
{
×
666
    if(!componentIsValid())
×
667
        return;
×
668

669
    updateCentreAndZoomDistance();
×
670

671
    QQuaternion rotation = nullQuaternion();
×
672

673
    if(_projection == Projection::TwoDee)
×
674
    {
×
675
        // When moving to 2D mode, rotate to look along the Z axis, so that
676
        // there is no perceptible change when the layout is flattened
677
        auto delta = QQuaternion::rotationTo(camera()->viewVector(), {0.0f, 0.0f, -1.0f});
×
678
        rotation = delta * camera()->rotation();
×
679
    }
×
680

681
    if(_viewData._focusNodeId.isNull())
×
682
        centrePositionInViewport(_viewData._componentCentre, -1.0f, rotation);
×
683
    else
×
684
        centreNodeInViewport(_viewData._focusNodeId, -1.0f, rotation);
×
685
}
×
686

687
bool GraphComponentRenderer::transitionRequired() const
688
{
×
689
    if(trackingCentreOfComponent() || !focusNodeIsVisible())
×
690
        return true;
×
691

692
    Q_ASSERT(!_viewData._focusNodeId.isNull());
×
693
    auto entireComponentZoomDistance =
×
694
        entireComponentZoomDistanceFor(_viewData._focusNodeId);
×
695

696
    return _viewData.camera().distance() > entireComponentZoomDistance;
×
697
}
×
698

699
void GraphComponentRenderer::computeTransition()
700
{
×
701
    if(trackingCentreOfComponent() || !focusNodeIsVisible())
×
702
        moveFocusToCentreOfComponent();
×
703
    else
×
704
        moveFocusToNode(focusNodeId());
×
705
}
×
706

707
static QMatrix4x4 interpolateProjectionMatrices(const QMatrix4x4& a, const QMatrix4x4& b, float f)
708
{
×
709
    auto m = u::interpolate(a, b, f);
×
710

711
    // If both matrices are affine they are (probably) ortho projections, and we're
712
    // (probably) zooming, in which case we decompose the scaling and use it for
713
    // the interpolation, rather than using the cells of the matrix directly
714
    if(a.isAffine() && b.isAffine())
×
715
    {
×
716
        for(int column = 0; column < 2; column++)
×
717
        {
×
718
            for(int row = 0; row < 2; row++)
×
719
            {
×
720
                auto ai = a(row, column);
×
721
                auto bi = b(row, column);
×
722

723
                ai = ai != 0.0f ? 1.0f / ai : ai;
×
724
                bi = bi != 0.0f ? 1.0f / bi : bi;
×
725
                auto mi = u::interpolate(ai, bi, f);
×
726
                m(row, column) = mi != 0.0f ? 1.0f / mi : mi;
×
727
            }
×
728
        }
×
729
    }
×
730

731
    return m;
×
732
}
×
733

734
void GraphComponentRenderer::updateTransition(float f)
735
{
×
736
    _viewData.camera().setDistance(u::interpolate(
×
737
        _viewData._transitionStart._camera.distance(),
×
738
        _viewData._transitionEnd._camera.distance(),
×
739
        f));
×
740

741
    _viewData.camera().setFocus(u::interpolate(
×
742
        _viewData._transitionStart._camera.focus(),
×
743
        _viewData._transitionEnd._camera.focus(),
×
744
        f));
×
745

746
    _viewData.camera().setRotation(QQuaternion::slerp(
×
747
        _viewData._transitionStart._camera.rotation(),
×
748
        _viewData._transitionEnd._camera.rotation(),
×
749
        f));
×
750

751
    _viewData.camera().setProjectionMatrix(interpolateProjectionMatrices(
×
752
        _viewData._transitionStart._camera.projectionMatrix(),
×
753
        _viewData._transitionEnd._camera.projectionMatrix(),
×
754
        f));
×
755

756
    _viewData._cameraAndLighting._lightScale = u::interpolate(
×
757
        _viewData._transitionStart._lightScale,
×
758
        _viewData._transitionEnd._lightScale,
×
759
        f);
×
760
}
×
761

762
NodeId GraphComponentRenderer::focusNodeId() const
763
{
×
764
    return _viewData._focusNodeId;
×
765
}
×
766

767
bool GraphComponentRenderer::focusNodeIsVisible() const
768
{
×
769
    return _graphModel->graph().typeOf(focusNodeId()) != MultiElementType::Tail;
×
770
}
×
771

772
QVector3D GraphComponentRenderer::focusPosition() const
773
{
×
774
    if(_viewData._focusNodeId.isNull())
×
775
        return _viewData._componentCentre;
×
776

777
    return _graphModel->nodePositions().get(_viewData._focusNodeId);
×
778
}
×
779

780
bool GraphComponentRenderer::focusedOnNodeAtRadius(NodeId nodeId, float radius) const
781
{
×
782
    const bool zoomMatchesRadius = qFuzzyCompare(zoomDistanceForRadius(radius),
×
783
        _projection == Projection::Perspective ? camera()->distance() : _viewData._zoomDistance);
×
784

785
    return focusNodeId() == nodeId && zoomMatchesRadius;
×
786
}
×
787

788
bool GraphComponentRenderer::trackingCentreOfComponent() const
789
{
×
790
    return _viewData._focusNodeId.isNull();
×
791
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc