-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScatterplotWidget.cpp
More file actions
890 lines (677 loc) · 25.4 KB
/
ScatterplotWidget.cpp
File metadata and controls
890 lines (677 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
#include "ScatterplotWidget.h"
#include <CoreInterface.h>
#include <util/Exception.h>
#include <vector>
#include <QDebug>
#include <QGuiApplication>
#include <QMatrix4x4>
#include <QOpenGLFramebufferObject>
#include <QPainter>
#include <QSize>
#include <QWheelEvent>
#include <QWindow>
#include <QRectF>
#include <math.h>
#include "ScatterplotPlugin.h"
using namespace mv;
namespace
{
Bounds getDataBounds(const std::vector<Vector2f>& points)
{
Bounds bounds = Bounds::Max;
for (const Vector2f& point : points)
{
bounds.setLeft(std::min(point.x, bounds.getLeft()));
bounds.setRight(std::max(point.x, bounds.getRight()));
bounds.setBottom(std::min(point.y, bounds.getBottom()));
bounds.setTop(std::max(point.y, bounds.getTop()));
}
return bounds;
}
void translateBounds(Bounds& b, float x, float y)
{
b.setLeft(b.getLeft() + x);
b.setRight(b.getRight() + x);
b.setBottom(b.getBottom() + y);
b.setTop(b.getTop() + y);
}
}
ScatterplotWidget::ScatterplotWidget() :
QOpenGLWidget(),
_pointRenderer(),
_densityRenderer(DensityRenderer::RenderMode::DENSITY),
_isInitialized(false),
_renderMode(SCATTERPLOT),
_backgroundColor(255, 255, 255, 255),
_coloringMode(ColoringMode::Constant),
_widgetSizeInfo(),
_dataRectangleAction(this, "Data rectangle"),
_navigationAction(this, "Navigation"),
_colorMapImage(),
_pixelSelectionTool(this),
_samplerPixelSelectionTool(this),
_pixelRatio(1.0),
_mousePositions(),
_isNavigating(false),
_weightDensity(false)
{
setContextMenuPolicy(Qt::CustomContextMenu);
setAcceptDrops(true);
setMouseTracking(true);
setFocusPolicy(Qt::ClickFocus);
grabGesture(Qt::PinchGesture);
//setAttribute(Qt::WA_TranslucentBackground);
installEventFilter(this);
_navigationAction.initialize(this);
_pixelSelectionTool.setEnabled(true);
_pixelSelectionTool.setMainColor(QColor(Qt::black));
_pixelSelectionTool.setFixedBrushRadiusModifier(Qt::AltModifier);
_samplerPixelSelectionTool.setEnabled(true);
_samplerPixelSelectionTool.setMainColor(QColor(Qt::black));
_samplerPixelSelectionTool.setFixedBrushRadiusModifier(Qt::AltModifier);
connect(&_pixelSelectionTool, &PixelSelectionTool::shapeChanged, [this]() {
if (isInitialized())
update();
});
connect(&_samplerPixelSelectionTool, &PixelSelectionTool::shapeChanged, [this]() {
if (isInitialized())
update();
});
QSurfaceFormat surfaceFormat;
surfaceFormat.setRenderableType(QSurfaceFormat::OpenGL);
surfaceFormat.setVersion(3, 3);
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
surfaceFormat.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
surfaceFormat.setSamples(16);
surfaceFormat.setStencilBufferSize(8);
#ifdef _DEBUG
surfaceFormat.setOption(QSurfaceFormat::DebugContext);
#endif
setFormat(surfaceFormat);
// Call updatePixelRatio when the window is moved between hi and low dpi screens
// e.g., from a laptop display to a projector
// Wait with the connection until we are sure that the window is created
connect(this, &ScatterplotWidget::created, this, [this](){
[[maybe_unused]] auto windowID = this->window()->winId(); // This is needed to produce a valid windowHandle on some systems
QWindow* winHandle = windowHandle();
// On some systems we might need to use a different windowHandle
if(!winHandle)
{
const QWidget* nativeParent = nativeParentWidget();
winHandle = nativeParent->windowHandle();
}
if(winHandle == nullptr)
{
qDebug() << "ScatterplotWidget: Not connecting updatePixelRatio - could not get window handle";
return;
}
QObject::connect(winHandle, &QWindow::screenChanged, this, &ScatterplotWidget::updatePixelRatio, Qt::UniqueConnection);
});
connect(&_navigationAction.getZoomRectangleAction(), &DecimalRectangleAction::rectangleChanged, this, [this]() -> void {
auto& zoomRectangleAction = _navigationAction.getZoomRectangleAction();
const auto zoomBounds = zoomRectangleAction.getBounds();
_pointRenderer.setViewBounds(zoomBounds);
_densityRenderer.setBounds(zoomBounds);
_navigationAction.getZoomDataExtentsAction().setEnabled(zoomBounds != _dataRectangleAction.getBounds());
update();
});
}
bool ScatterplotWidget::event(QEvent* event)
{
// Interactions when Alt is pressed
if (isInitialized() && QGuiApplication::keyboardModifiers() == Qt::AltModifier) {
switch (event->type())
{
case QEvent::Wheel:
{
// Scroll to zoom
if (auto* wheelEvent = static_cast<QWheelEvent*>(event))
zoomAround(wheelEvent->position().toPoint(), wheelEvent->angleDelta().x() / 1200.f);
break;
}
case QEvent::MouseButtonPress:
{
if (auto* mouseEvent = static_cast<QMouseEvent*>(event))
{
if(mouseEvent->button() == Qt::MiddleButton)
resetView();
// Navigation
if (mouseEvent->buttons() == Qt::LeftButton)
{
_isNavigating = true;
_pixelSelectionTool.setEnabled(false);
setCursor(Qt::ClosedHandCursor);
_mousePositions << mouseEvent->pos();
update();
}
}
break;
}
case QEvent::MouseButtonRelease:
{
if (_isNavigating)
{
_isNavigating = false;
_pixelSelectionTool.setEnabled(true);
setCursor(Qt::ArrowCursor);
_mousePositions.clear();
update();
}
break;
}
case QEvent::MouseMove:
{
if (auto* mouseEvent = static_cast<QMouseEvent*>(event))
{
if (_isNavigating)
{
_mousePositions << mouseEvent->pos();
if (mouseEvent->buttons() & Qt::LeftButton && _mousePositions.size() >= 2) {
const auto& previousMousePosition = _mousePositions[_mousePositions.size() - 2];
const auto& currentMousePosition = _mousePositions[_mousePositions.size() - 1];
const auto panVector = currentMousePosition - previousMousePosition;
panBy(panVector);
}
}
}
break;
}
case QEvent::KeyRelease:
{
if (auto* keyEvent = static_cast<QKeyEvent*>(event))
{
// Reset navigation
if (keyEvent && keyEvent->key() == Qt::Key_Alt)
{
_isNavigating = false;
}
}
break;
}
}
}
return QOpenGLWidget::event(event);
}
void ScatterplotWidget::resetView()
{
_navigationAction.getZoomRectangleAction().setBounds(_dataRectangleAction.getBounds());
}
void ScatterplotWidget::panBy(const QPointF& to)
{
auto& zoomRectangleAction = _navigationAction.getZoomRectangleAction();
const auto moveBy = QPointF(to.x() / _widgetSizeInfo.width * zoomRectangleAction.getWidth() * _widgetSizeInfo.ratioWidth * -1.f,
to.y() / _widgetSizeInfo.height * zoomRectangleAction.getHeight() * _widgetSizeInfo.ratioHeight);
zoomRectangleAction.translateBy({ moveBy.x(), moveBy.y() });
update();
}
void ScatterplotWidget::zoomAround(const QPointF& at, float factor)
{
auto& zoomRectangleAction = _navigationAction.getZoomRectangleAction();
// the widget might have a different aspect ratio than the square opengl viewport
const auto offsetBounds = QPointF(zoomRectangleAction.getWidth() * (0.5f * (1 - _widgetSizeInfo.ratioWidth)),
zoomRectangleAction.getHeight() * (0.5f * (1 - _widgetSizeInfo.ratioHeight)) * -1.f);
const auto originBounds = QPointF(zoomRectangleAction.getLeft(), zoomRectangleAction.getTop());
// translate mouse point in widget to mouse point in bounds coordinates
const auto atTransformed = QPointF(at.x() / _widgetSizeInfo.width * zoomRectangleAction.getWidth() * _widgetSizeInfo.ratioWidth,
at.y() / _widgetSizeInfo.height * zoomRectangleAction.getHeight() * _widgetSizeInfo.ratioHeight * -1.f);
const auto atInBounds = originBounds + offsetBounds + atTransformed;
// ensure mouse position is the same after zooming
const auto currentBoundCenter = zoomRectangleAction.getCenter();
float moveMouseX = (atInBounds.x() - currentBoundCenter.first) * factor;
float moveMouseY = (atInBounds.y() - currentBoundCenter.second) * factor;
// zoom and move view
zoomRectangleAction.translateBy({ moveMouseX, moveMouseY });
zoomRectangleAction.expandBy(-1.f * factor);
update();
}
bool ScatterplotWidget::isInitialized() const
{
return _isInitialized;
}
ScatterplotWidget::RenderMode ScatterplotWidget::getRenderMode() const
{
return _renderMode;
}
void ScatterplotWidget::setRenderMode(const RenderMode& renderMode)
{
if (renderMode == _renderMode)
return;
_renderMode = renderMode;
emit renderModeChanged(_renderMode);
switch (_renderMode)
{
case ScatterplotWidget::SCATTERPLOT:
break;
case ScatterplotWidget::DENSITY:
computeDensity();
break;
case ScatterplotWidget::LANDSCAPE:
computeDensity();
break;
default:
break;
}
update();
}
ScatterplotWidget::ColoringMode ScatterplotWidget::getColoringMode() const
{
return _coloringMode;
}
void ScatterplotWidget::setColoringMode(const ColoringMode& coloringMode)
{
if (coloringMode == _coloringMode)
return;
_coloringMode = coloringMode;
emit coloringModeChanged(_coloringMode);
}
PixelSelectionTool& ScatterplotWidget::getPixelSelectionTool()
{
return _pixelSelectionTool;
}
PixelSelectionTool& ScatterplotWidget::getSamplerPixelSelectionTool()
{
return _samplerPixelSelectionTool;
}
void ScatterplotWidget::computeDensity()
{
emit densityComputationStarted();
_densityRenderer.computeDensity();
emit densityComputationEnded();
update();
}
// Positions need to be passed as a pointer as we need to store them locally in order
// to be able to find the subset of data that's part of a selection. If passed
// by reference then we can upload the data to the GPU, but not store it in the widget.
void ScatterplotWidget::setData(const std::vector<Vector2f>* points)
{
auto dataBounds = getDataBounds(*points);
// pass un-adjusted data bounds to renderer for 2D colormapping
_pointRenderer.setDataBounds(dataBounds);
// Adjust data points for projection matrix creation (add a little white space around data)
dataBounds.ensureMinimumSize(1e-07f, 1e-07f);
dataBounds.makeSquare();
dataBounds.expand(0.1f);
const auto shouldSetBounds = (mv::projects().isOpeningProject() || mv::projects().isImportingProject()) ? false : !_navigationAction.getFreezeZoomAction().isChecked();
if (shouldSetBounds)
_pointRenderer.setViewBounds(dataBounds);
_densityRenderer.setBounds(dataBounds);
_dataRectangleAction.setBounds(dataBounds);
if (shouldSetBounds)
_navigationAction.getZoomRectangleAction().setBounds(dataBounds);
_pointRenderer.setData(*points);
_densityRenderer.setData(points);
switch (_renderMode)
{
case ScatterplotWidget::SCATTERPLOT:
break;
case ScatterplotWidget::DENSITY:
case ScatterplotWidget::LANDSCAPE:
{
_densityRenderer.computeDensity();
break;
}
default:
break;
}
// _pointRenderer.setSelectionOutlineColor(Vector3f(1, 0, 0));
update();
}
QColor ScatterplotWidget::getBackgroundColor() const
{
return _backgroundColor;
}
void ScatterplotWidget::setBackgroundColor(QColor color)
{
_backgroundColor = color;
update();
}
void ScatterplotWidget::setHighlights(const std::vector<char>& highlights, const std::int32_t& numSelectedPoints)
{
_pointRenderer.setHighlights(highlights, numSelectedPoints);
update();
}
void ScatterplotWidget::setScalars(const std::vector<float>& scalars)
{
_pointRenderer.setColorChannelScalars(scalars);
update();
}
void ScatterplotWidget::setColors(const std::vector<Vector3f>& colors)
{
_pointRenderer.setColors(colors);
_pointRenderer.setScalarEffect(None);
update();
}
void ScatterplotWidget::setPointSizeScalars(const std::vector<float>& pointSizeScalars)
{
if (pointSizeScalars.empty())
return;
_pointRenderer.setSizeChannelScalars(pointSizeScalars);
_pointRenderer.setPointSize(*std::max_element(pointSizeScalars.begin(), pointSizeScalars.end()));
update();
}
void ScatterplotWidget::setPointOpacityScalars(const std::vector<float>& pointOpacityScalars)
{
_pointRenderer.setOpacityChannelScalars(pointOpacityScalars);
update();
}
void ScatterplotWidget::setPointScaling(mv::gui::PointScaling scalingMode)
{
_pointRenderer.setPointScaling(scalingMode);
update();
}
void ScatterplotWidget::setScalarEffect(PointEffect effect)
{
_pointRenderer.setScalarEffect(effect);
update();
}
void ScatterplotWidget::setSigma(const float sigma)
{
_densityRenderer.setSigma(sigma);
update();
}
void ScatterplotWidget::setWeightDensity(bool useWeights)
{
_weightDensity = useWeights;
const std::vector<float>* weights = nullptr;
if (_weightDensity)
weights = &_pointRenderer.getGpuPoints().getSizeScalars();
_densityRenderer.setWeights(weights);
}
mv::Vector3f ScatterplotWidget::getColorMapRange() const
{
switch (_renderMode) {
case SCATTERPLOT:
return _pointRenderer.getColorMapRange();
case LANDSCAPE:
return _densityRenderer.getColorMapRange();
default:
break;
}
return Vector3f();
}
void ScatterplotWidget::setColorMapRange(const float& min, const float& max)
{
switch (_renderMode) {
case SCATTERPLOT:
{
_pointRenderer.setColorMapRange(min, max);
break;
}
case LANDSCAPE:
{
_densityRenderer.setColorMapRange(min, max);
break;
}
default:
break;
}
update();
}
void ScatterplotWidget::showHighlights(bool show)
{
_pointRenderer.setSelectionOutlineScale(show ? 0.5f : 0);
update();
}
void ScatterplotWidget::createScreenshot(std::int32_t width, std::int32_t height, const QString& fileName, const QColor& backgroundColor)
{
// Exit if the viewer is not initialized
if (!_isInitialized)
return;
// Exit prematurely if the file name is invalid
if (fileName.isEmpty())
return;
makeCurrent();
try {
// Use custom FBO format
QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setTextureTarget(GL_TEXTURE_2D);
fboFormat.setInternalTextureFormat(GL_RGB);
QOpenGLFramebufferObject fbo(width, height, fboFormat);
// Bind the FBO and render into it when successfully bound
if (fbo.bind()) {
// Clear the widget to the background color
glClearColor(backgroundColor.redF(), backgroundColor.greenF(), backgroundColor.blueF(), backgroundColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the blending function
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Resize OpenGL to intended screenshot size
resizeGL(width, height);
switch (_renderMode)
{
case SCATTERPLOT:
{
_pointRenderer.setPointScaling(Relative);
_pointRenderer.render();
_pointRenderer.setPointScaling(Absolute);
break;
}
case DENSITY:
case LANDSCAPE:
_densityRenderer.setRenderMode(_renderMode == DENSITY ? DensityRenderer::DENSITY : DensityRenderer::LANDSCAPE);
_densityRenderer.render();
break;
}
// Save FBO image to disk
//fbo.toImage(false, QImage::Format_RGB32).convertToFormat(QImage::Format_RGB32).save(fileName);
//fbo.toImage(false, QImage::Format_ARGB32).save(fileName);
QImage fboImage(fbo.toImage());
QImage image(fboImage.constBits(), fboImage.width(), fboImage.height(), QImage::Format_ARGB32);
image.save(fileName);
// Resize OpenGL back to original OpenGL widget size
resizeGL(this->width(), this->height());
fbo.release();
}
}
catch (std::exception& e)
{
exceptionMessageBox("Rendering failed", e);
}
catch (...) {
exceptionMessageBox("Rendering failed");
}
}
PointSelectionDisplayMode ScatterplotWidget::getSelectionDisplayMode() const
{
return _pointRenderer.getSelectionDisplayMode();
}
void ScatterplotWidget::setSelectionDisplayMode(PointSelectionDisplayMode selectionDisplayMode)
{
_pointRenderer.setSelectionDisplayMode(selectionDisplayMode);
update();
}
QColor ScatterplotWidget::getSelectionOutlineColor() const
{
QColor haloColor;
haloColor.setRedF(_pointRenderer.getSelectionOutlineColor().x);
haloColor.setGreenF(_pointRenderer.getSelectionOutlineColor().y);
haloColor.setBlueF(_pointRenderer.getSelectionOutlineColor().z);
return haloColor;
}
void ScatterplotWidget::setSelectionOutlineColor(const QColor& selectionOutlineColor)
{
_pointRenderer.setSelectionOutlineColor(Vector3f(selectionOutlineColor.redF(), selectionOutlineColor.greenF(), selectionOutlineColor.blueF()));
update();
}
bool ScatterplotWidget::getSelectionOutlineOverrideColor() const
{
return _pointRenderer.getSelectionOutlineOverrideColor();
}
void ScatterplotWidget::setSelectionOutlineOverrideColor(bool selectionOutlineOverrideColor)
{
_pointRenderer.setSelectionOutlineOverrideColor(selectionOutlineOverrideColor);
update();
}
float ScatterplotWidget::getSelectionOutlineScale() const
{
return _pointRenderer.getSelectionOutlineScale();
}
void ScatterplotWidget::setSelectionOutlineScale(float selectionOutlineScale)
{
_pointRenderer.setSelectionOutlineScale(selectionOutlineScale);
update();
}
float ScatterplotWidget::getSelectionOutlineOpacity() const
{
return _pointRenderer.getSelectionOutlineOpacity();
}
void ScatterplotWidget::setSelectionOutlineOpacity(float selectionOutlineOpacity)
{
_pointRenderer.setSelectionOutlineOpacity(selectionOutlineOpacity);
update();
}
bool ScatterplotWidget::getSelectionOutlineHaloEnabled() const
{
return _pointRenderer.getSelectionHaloEnabled();
}
void ScatterplotWidget::setSelectionOutlineHaloEnabled(bool selectionOutlineHaloEnabled)
{
_pointRenderer.setSelectionHaloEnabled(selectionOutlineHaloEnabled);
update();
}
void ScatterplotWidget::setRandomizedDepthEnabled(bool randomizedDepth)
{
_pointRenderer.setRandomizedDepthEnabled(randomizedDepth);
update();
}
bool ScatterplotWidget::getRandomizedDepthEnabled() const
{
return _pointRenderer.getRandomizedDepthEnabled();
}
void ScatterplotWidget::initializeGL()
{
initializeOpenGLFunctions();
#ifdef SCATTER_PLOT_WIDGET_VERBOSE
qDebug() << "Initializing scatterplot widget with context: " << context();
std::string versionString = std::string((const char*) glGetString(GL_VERSION));
qDebug() << versionString.c_str();
#endif
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &ScatterplotWidget::cleanup);
// Initialize renderers
_pointRenderer.init();
_densityRenderer.init();
// Set a default color map for both renderers
_pointRenderer.setScalarEffect(PointEffect::Color);
_pointRenderer.setPointScaling(Absolute);
_pointRenderer.setSelectionOutlineColor(Vector3f(1, 0, 0));
// OpenGL is initialized
_isInitialized = true;
// Initialize the point and density renderer with a color map
setColorMap(_colorMapImage);
emit initialized();
}
void ScatterplotWidget::resizeGL(int w, int h)
{
_widgetSizeInfo.width = static_cast<float>(w);
_widgetSizeInfo.height = static_cast<float>(h);
_widgetSizeInfo.minWH = _widgetSizeInfo.width < _widgetSizeInfo.height ? _widgetSizeInfo.width : _widgetSizeInfo.height;
_widgetSizeInfo.ratioWidth = _widgetSizeInfo.width / _widgetSizeInfo.minWH;
_widgetSizeInfo.ratioHeight = _widgetSizeInfo.height / _widgetSizeInfo.minWH;
// we need this here as we do not have the screen yet to get the actual devicePixelRatio when the view is created
_pixelRatio = devicePixelRatio();
// Pixel ratio tells us how many pixels map to a point
// That is needed as macOS calculates in points and we do in pixels
// On macOS high dpi displays pixel ration is 2
w *= _pixelRatio;
h *= _pixelRatio;
_pointRenderer.resize(QSize(w, h));
_densityRenderer.resize(QSize(w, h));
}
void ScatterplotWidget::paintGL()
{
try {
QPainter painter;
// Begin mixed OpenGL/native painting
if (!painter.begin(this))
throw std::runtime_error("Unable to begin painting");
// Draw layers with OpenGL
painter.beginNativePainting();
{
// Bind the framebuffer belonging to the widget
// glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject());
// Clear the widget to the background color
glClearColor(_backgroundColor.redF(), _backgroundColor.greenF(), _backgroundColor.blueF(), _backgroundColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the blending function
glEnable(GL_BLEND);
if (getRandomizedDepthEnabled())
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
switch (_renderMode)
{
case SCATTERPLOT:
_pointRenderer.render();
break;
case DENSITY:
case LANDSCAPE:
_densityRenderer.setRenderMode(_renderMode == DENSITY ? DensityRenderer::DENSITY : DensityRenderer::LANDSCAPE);
_densityRenderer.render();
break;
}
}
painter.endNativePainting();
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
QImage pixelSelectionToolsImage(size(), QImage::Format_ARGB32);
pixelSelectionToolsImage.fill(Qt::transparent);
paintPixelSelectionToolNative(_pixelSelectionTool, pixelSelectionToolsImage, painter);
paintPixelSelectionToolNative(_samplerPixelSelectionTool, pixelSelectionToolsImage, painter);
painter.drawImage(0, 0, pixelSelectionToolsImage);
painter.end();
}
catch (std::exception& e)
{
exceptionMessageBox("Rendering failed", e);
}
catch (...) {
exceptionMessageBox("Rendering failed");
}
}
void ScatterplotWidget::paintPixelSelectionToolNative(PixelSelectionTool& pixelSelectionTool, QImage& image, QPainter& painter) const
{
if (!pixelSelectionTool.isEnabled())
return;
QPainter pixelSelectionToolImagePainter(&image);
pixelSelectionToolImagePainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
pixelSelectionToolImagePainter.drawPixmap(rect(), pixelSelectionTool.getShapePixmap());
pixelSelectionToolImagePainter.drawPixmap(rect(), pixelSelectionTool.getAreaPixmap());
}
void ScatterplotWidget::cleanup()
{
qDebug() << "Deleting scatterplot widget, performing clean up...";
_isInitialized = false;
makeCurrent();
_pointRenderer.destroy();
_densityRenderer.destroy();
}
void ScatterplotWidget::setColorMap(const QImage& colorMapImage)
{
_colorMapImage = colorMapImage;
// Do not update color maps of the renderers when OpenGL is not initialized
if (!_isInitialized)
return;
// Apply color maps to renderers
_pointRenderer.setColormap(_colorMapImage);
_densityRenderer.setColormap(_colorMapImage);
// Render
update();
}
void ScatterplotWidget::updatePixelRatio()
{
float pixelRatio = devicePixelRatio();
#ifdef SCATTER_PLOT_WIDGET_VERBOSE
qDebug() << "Window moved to screen " << window()->screen() << ".";
qDebug() << "Pixelratio before was " << _pixelRatio << ". New pixelratio is: " << pixelRatio << ".";
#endif // SCATTER_PLOT_WIDGET_VERBOSE
// we only update if the ratio actually changed
if( _pixelRatio != pixelRatio )
{
_pixelRatio = pixelRatio;
resizeGL(width(), height());
update();
}
}
ScatterplotWidget::~ScatterplotWidget()
{
disconnect(QOpenGLWidget::context(), &QOpenGLContext::aboutToBeDestroyed, this, &ScatterplotWidget::cleanup);
cleanup();
}