Repository navigation
Expand file tree
/
Copy pathChartLegendViewPlugin.cpp
More file actions
669 lines (570 loc) · 27.8 KB
/
Copy pathChartLegendViewPlugin.cpp
File metadata and controls
669 lines (570 loc) · 27.8 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
#include "ChartLegendViewPlugin.h"
#include <ClusterData/ClusterData.h>
#include <PointData/PointData.h>
#include <util/Serialization.h>
#include <QBrush>
#include <QColor>
#include <QDebug>
#include <QFrame>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QGraphicsView>
#include <QHBoxLayout>
#include <QLabel>
#include <QLayout>
#include <QLinearGradient>
#include <QMap>
#include <QSet>
#include <QScrollArea>
#include <QScrollBar>
#include <QSizePolicy>
#include <QString>
#include <QStringList>
#include <QVariant>
#include <QVariantList>
#include <QVariantMap>
#include <QVBoxLayout>
#include <QWidget>
#include <algorithm>
#include <exception>
#include <vector>
using namespace mv;
using namespace mv::plugin;
using namespace mv::gui;
namespace {
constexpr int kNumericalLegendWidth = 70;
constexpr int kNumericalLegendHeight = 250;
constexpr int kLegendRowHeight = 30;
constexpr int kGradientBarHeight = 180;
constexpr int kGradientBarWidth = 15;
constexpr int kNumericalTickCount = 5;
constexpr int kColorScaleEndY = 200;
constexpr int kColorScaleSampleCount = 100;
constexpr int kColorScaleSampleInterval = 10;
/** Blend two colors using a normalized interpolation amount. */
QColor interpolateColor(double value, const QColor& startColor, const QColor& endColor)
{
value = std::clamp(value, 0.0, 1.0);
int red = static_cast<int>(startColor.red() + value * (endColor.red() - startColor.red()));
int green = static_cast<int>(startColor.green() + value * (endColor.green() - startColor.green()));
int blue = static_cast<int>(startColor.blue() + value * (endColor.blue() - startColor.blue()));
red = std::clamp(red, 0, 255);
green = std::clamp(green, 0, 255);
blue = std::clamp(blue, 0, 255);
return QColor(red, green, blue);
}
/** Map a numeric value to a color from one of the supported color schemes. */
QColor selectColorVal(double value, double min, double max, const QString& colorScheme, bool mirror)
{
double normalizedValue = (value - min) / (max - min);
if (mirror) {
normalizedValue = 1.0 - normalizedValue;
}
// Keep each scheme as an ordered list of stops for interpolation.
static const QMap<QString, QVector<QColor>> colorSchemes = {
{"qualitative", {QColor(255, 127, 14), QColor(44, 160, 44)}}, // Example qualitative scheme
{"RdYlBu", {QColor(44, 123, 182), QColor(171, 217, 233), QColor(255, 255, 191), QColor(253, 174, 97), QColor(215, 25, 28)}}, // Inversed Red-Yellow-Blue
{"YlGn", {QColor(255, 255, 204), QColor(161, 218, 180), QColor(65, 171, 93), QColor(0, 104, 55)}}, // Yellow-Green
{"RdYlGn", {QColor(0, 104, 55), QColor(166, 217, 106), QColor(255, 255, 191), QColor(253, 174, 97), QColor(215, 25, 28)}}, // Inversed Red-Yellow-Green
{"GnBu", {QColor(224, 243, 219), QColor(168, 221, 181), QColor(67, 162, 202), QColor(1, 97, 132)}}, // Green-Blue
{"YlGnBu", {QColor(255, 255, 204), QColor(161, 218, 180), QColor(65, 171, 93), QColor(44, 127, 184), QColor(8, 81, 156)}}, // Yellow-Green-Blue
{"Spectral", {QColor(94, 79, 162), QColor(50, 136, 189), QColor(102, 194, 165), QColor(171, 221, 164), QColor(230, 245, 152), QColor(255, 255, 191), QColor(254, 224, 139), QColor(253, 174, 97), QColor(244, 109, 67), QColor(158, 1, 66)}}, // Inversed Spectral
{"BrBG", {QColor(140, 81, 10), QColor(216, 179, 101), QColor(246, 232, 195), QColor(245, 245, 245), QColor(199, 234, 229), QColor(90, 180, 172), QColor(1, 133, 113)}}, // Brown-Blue-Green
{"YlOrBr", {QColor(255, 255, 212), QColor(254, 227, 145), QColor(254, 196, 79), QColor(254, 153, 41), QColor(217, 95, 14), QColor(127, 59, 8)}}, // Yellow-Orange-Brown
{"RdBu", {QColor(5, 113, 176), QColor(146, 197, 222), QColor(209, 229, 240), QColor(247, 247, 247), QColor(253, 219, 199), QColor(244, 165, 130), QColor(202, 0, 32)}}, // Inversed Red-Blue
{"RdPu", {QColor(254, 224, 210), QColor(252, 197, 192), QColor(250, 159, 181), QColor(247, 104, 161), QColor(221, 52, 151), QColor(174, 1, 126), QColor(122, 1, 119)}}, // Red-Purple
{"Plasma", {QColor(13, 8, 135), QColor(84, 2, 163), QColor(139, 10, 165), QColor(185, 50, 137), QColor(219, 92, 104), QColor(244, 136, 73), QColor(254, 188, 43), QColor(240, 249, 33)}}, // Plasma
{"PuOr", {QColor(45, 0, 75), QColor(84, 39, 136), QColor(128, 115, 172), QColor(178, 171, 210), QColor(216, 218, 235), QColor(247, 247, 247), QColor(254, 224, 182), QColor(253, 184, 99), QColor(224, 130, 20), QColor(179, 88, 6), QColor(127, 59, 8)}}, // Inversed Purple-Orange
{"BuPu", {QColor(237, 248, 251), QColor(191, 211, 230), QColor(158, 188, 218), QColor(140, 150, 198), QColor(140, 107, 177), QColor(136, 65, 157), QColor(129, 15, 124), QColor(77, 0, 75)}}, // Blue-Purple
{"Reds", {QColor(255, 245, 240), QColor(254, 224, 210), QColor(252, 187, 161), QColor(252, 146, 114), QColor(251, 106, 74), QColor(239, 59, 44), QColor(203, 24, 29), QColor(153, 0, 13), QColor(103, 0, 13)}}, // Reds
{"Viridis", {QColor(255, 255, 0), QColor(200, 220, 50), QColor(92, 200, 99), QColor(39, 173, 129), QColor(33, 144, 141), QColor(44, 114, 142), QColor(59, 82, 139), QColor(72, 35, 116), QColor(68, 1, 84)}}, // Reversed Viridis
{"Q_BlGrRd", {QColor(5, 48, 97), QColor(33, 102, 172), QColor(67, 147, 195), QColor(146, 197, 222), QColor(209, 229, 240), QColor(247, 247, 247), QColor(253, 219, 199), QColor(244, 165, 130), QColor(202, 0, 32), QColor(165, 0, 38)}}, // Custom Blue-Green-Red
{"Magma", {QColor(0, 0, 4), QColor(28, 16, 68), QColor(79, 18, 123), QColor(129, 37, 129), QColor(181, 54, 122), QColor(229, 80, 100), QColor(251, 135, 97), QColor(254, 194, 135), QColor(252, 253, 191)}}, // Magma
{"PiYG", {QColor(77, 172, 38), QColor(184, 225, 134), QColor(230, 245, 208), QColor(247, 247, 247), QColor(253, 224, 239), QColor(241, 182, 218), QColor(208, 28, 139)}}, // Inversed Pink-Yellow-Green
{"default", {QColor(0, 0, 0), QColor(255, 255, 255)}} // Black to white
};
QVector<QColor> colors = colorSchemes.value(colorScheme, colorSchemes["default"]);
int numSegments = colors.size() - 1;
double segmentSize = 1.0 / numSegments;
int segmentIndex = static_cast<int>(normalizedValue / segmentSize);
segmentIndex = std::clamp(segmentIndex, 0, numSegments - 1);
double segmentValue = (normalizedValue - segmentIndex * segmentSize) / segmentSize;
return interpolateColor(segmentValue, colors[segmentIndex], colors[segmentIndex + 1]);
}
QStringList convertToQStringList(const std::vector<QString>& vector)
{
QStringList qStringList;
for (const auto& str : vector) {
qStringList.append(str);
}
return qStringList;
}
int getIndexOfQString(const std::vector<QString>& vector, const QString& value)
{
auto it = std::find(vector.begin(), vector.end(), value);
if (it != vector.end()) {
return std::distance(vector.begin(), it);
}
else {
return -1;
}
}
} // namespace
ChartLegendViewPlugin::ChartLegendViewPlugin(const PluginFactory* factory) :
ViewPlugin(factory),
_verticalToolbarAction(this, "Settings Holder Vertical"),
_settingsAction(*this)
{
setSerializationName("Legend:Plugin");
getLearningCenterAction().addVideos(QStringList({ "Practitioner", "Developer" }));
}
void ChartLegendViewPlugin::init()
{
getWidget().setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
_legendWidget = new QFrame(&getWidget());
_legendWidget->setStyleSheet("background-color: white;");
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
_verticalToolbarAction.addAction(&_settingsAction.getColorSettingsHolder(), 3, GroupAction::Vertical);
layout->addWidget(_legendWidget, 9.5);
// Keep the displayed legend synchronized with data and dataset changes.
connect(&_currentDataset, &Dataset<DatasetImpl>::dataChanged, this, &ChartLegendViewPlugin::updateChart);
connect(&_currentDataset, &Dataset<DatasetImpl>::dataSelectionChanged, this, &ChartLegendViewPlugin::updateChart);
connect(&_settingsAction.getColorSettingsHolder().getColorDatasetAction(), &DatasetPickerAction::currentDatasetChanged,
this, [this](const Dataset<DatasetImpl>& dataset) {
_currentDataset = dataset;
updateChart();
});
getWidget().setLayout(layout);
const auto updateChartIfDatasetIsSelected = [this]() {
if (_settingsAction.getColorSettingsHolder().getColorDatasetAction().getCurrentDataset().isValid())
updateChart();
};
connect(&_settingsAction.getColorSettingsHolder().getSelectionStringList(),
&OptionsAction::selectedOptionsChanged,
this,
updateChartIfDatasetIsSelected);
connect(&_settingsAction.getColorSettingsHolder().getSelectionColorAction(),
&ColorAction::changed,
this,
updateChartIfDatasetIsSelected);
connect(&_settingsAction.getColorSettingsHolder().getChartTitleAction(),
&StringAction::stringChanged,
this,
updateChartIfDatasetIsSelected);
const auto clusterSelectionStringSelection = [this]() {
if (!_settingsAction.getColorSettingsHolder().getClusterSelectionString().getString().isEmpty())
{
const auto clusterNames = _settingsAction.getColorSettingsHolder().getClusterSelectionString().getString();
const auto options = _settingsAction.getColorSettingsHolder().getSelectionStringList().getOptions();
const auto delimiter = _settingsAction.getColorSettingsHolder().getDelimiterString().getString();
// Match linked cluster names against the available legend entries.
QStringList list = clusterNames.split(delimiter);
if (!list.isEmpty())
{
QSet<QString> listSet = QSet<QString>(list.begin(), list.end());
QStringList common;
for (const QString& option : options)
{
if (listSet.contains(option))
{
common.append(option);
}
}
if (!common.isEmpty())
{
_settingsAction.getColorSettingsHolder().getSelectionStringList().setSelectedOptions(common);
}
else
{
_settingsAction.getColorSettingsHolder().getSelectionStringList().setSelectedOptions({ "" });
}
}
}
else
{
_settingsAction.getColorSettingsHolder().getSelectionStringList().setSelectedOptions({ "" });
}
};
connect(&_settingsAction.getColorSettingsHolder().getClusterSelectionString(),
&StringAction::stringChanged,
this,
clusterSelectionStringSelection);
const auto delimiterStringSelection = [this]() {
if (!_settingsAction.getColorSettingsHolder().getClusterSelectionString().getString().isEmpty()) {
const auto clusterNames = _settingsAction.getColorSettingsHolder().getClusterSelectionString().getString();
_settingsAction.getColorSettingsHolder().getClusterSelectionString().setString("");
_settingsAction.getColorSettingsHolder().getClusterSelectionString().setString(clusterNames);
}
};
connect(&_settingsAction.getColorSettingsHolder().getDelimiterString(),
&StringAction::stringChanged,
this,
delimiterStringSelection);
const auto colorMapActionSelection = [this]() {
auto& colors = _settingsAction.getColorSettingsHolder();
if (!colors.getColorMapAction().getColorMap().isEmpty())
updateChart();
};
connect(&_settingsAction.getColorSettingsHolder().getColorMapAction(),
&ColorMap1DAction::imageChanged,
this,
colorMapActionSelection);
const auto dimSelectionStringSelection = [this]() {
if (!_settingsAction.getColorSettingsHolder().getDimSelectionString().getString().isEmpty()) {
const auto dimensionName = _settingsAction.getColorSettingsHolder().getDimSelectionString().getString();
const auto options = _settingsAction.getColorSettingsHolder().getDimensionSelectionAction().getDimensionNames();
const int currentIndex = _settingsAction.getColorSettingsHolder().getDimensionSelectionAction().getCurrentDimensionIndex();
if (currentIndex < 0 || currentIndex >= options.size() || options[currentIndex] != dimensionName) {
const int newIndex = options.indexOf(dimensionName);
if (newIndex != -1)
_settingsAction.getColorSettingsHolder().getDimensionSelectionAction().setCurrentDimensionName(dimensionName);
}
}
};
connect(&_settingsAction.getColorSettingsHolder().getDimSelectionString(),
&StringAction::stringChanged,
this,
dimSelectionStringSelection);
connect(&_settingsAction.getColorSettingsHolder().getDimensionSelectionAction(),
&DimensionPickerAction::currentDimensionIndexChanged,
this,
updateChartIfDatasetIsSelected);
}
void ChartLegendViewPlugin::loadData(const mv::Datasets& datasets)
{
if (datasets.isEmpty())
{
qDebug() << "LegendPlugin::loadData: No data set to load";
return;
}
auto dataset = datasets.first();
try {
_settingsAction.getColorSettingsHolder().getColorDatasetAction().setCurrentDataset(dataset);
}
catch (const std::exception& e) {
qCritical() << "Exception caught in loadData:" << e.what();
}
catch (...) {
qCritical() << "Unknown exception caught in loadData";
}
}
void ChartLegendViewPlugin::clearChartFrame()
{
if (_legendWidget) {
QLayout* layout = _legendWidget->layout();
if (layout) {
QLayoutItem* item;
while ((item = layout->takeAt(0)) != nullptr) {
delete item->widget();
delete item;
}
delete layout;
}
}
}
void ChartLegendViewPlugin::createNumericalChart(const QVariantList& chartDataNumerical)
{
clearChartFrame();
if (chartDataNumerical.isEmpty()) {
qDebug() << "Numerical chart data is empty.";
return;
}
QVariantMap data = chartDataNumerical[0].toMap();
double min = data["min"].toDouble();
double max = data["max"].toDouble();
_legendWidget->setCursor(Qt::ArrowCursor);
QScrollArea* scrollArea = new QScrollArea(_legendWidget);
scrollArea->setWidgetResizable(true);
scrollArea->setFrameStyle(QFrame::NoFrame);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QWidget* containerWidget = new QWidget();
containerWidget->setStyleSheet("border: none;");
QVBoxLayout* layout = new QVBoxLayout(containerWidget);
layout->setContentsMargins(0, 0, 0, 0);
layout->setAlignment(Qt::AlignTop);
QGraphicsScene* scene = new QGraphicsScene(containerWidget);
QGraphicsView* view = new QGraphicsView(scene, containerWidget);
view->setFixedSize(kNumericalLegendWidth, kNumericalLegendHeight);
view->setFrameStyle(QFrame::NoFrame);
// Build the gradient from the selected color map and add evenly spaced labels.
QLinearGradient gradient(0, 0, 0, kColorScaleEndY);
for (int sample = 0; sample <= kColorScaleSampleCount; sample += kColorScaleSampleInterval) {
const double normalizedSample = sample / static_cast<double>(kColorScaleSampleCount);
const double value = min + normalizedSample * (max - min);
QColor color = selectColor(value, min, max);
gradient.setColorAt(normalizedSample, color);
}
scene->addRect(0, 0, kGradientBarWidth, kGradientBarHeight, Qt::NoPen, QBrush(gradient));
for (int i = 0; i <= kNumericalTickCount; ++i) {
double value = min + i * (max - min) / kNumericalTickCount;
double y = kGradientBarHeight - (i * kGradientBarHeight / static_cast<double>(kNumericalTickCount));
QGraphicsTextItem* label = scene->addText(QString::number(value, 'f', 1));
label->setDefaultTextColor(Qt::black);
label->setPos(20, y - 10);
}
layout->addWidget(view);
scrollArea->setWidget(containerWidget);
QVBoxLayout* mainLayout = new QVBoxLayout(_legendWidget);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setAlignment(Qt::AlignTop);
QLabel* titleLabel = new QLabel(_settingsAction.getColorSettingsHolder().getChartTitleAction().getString(), _legendWidget);
titleLabel->setAlignment(Qt::AlignCenter);
titleLabel->setStyleSheet("font-weight: bold; font-size: 14px; color: black;");
mainLayout->addWidget(titleLabel);
mainLayout->addWidget(scrollArea);
}
void ChartLegendViewPlugin::createCategoricalChart(const QVariantList& chartDataCategorical)
{
// Preserve the user's scroll position when the categorical legend refreshes.
int scrollPos = 0;
if (_legendWidget->layout()) {
QScrollArea* existingScrollArea = _legendWidget->findChild<QScrollArea*>();
if (existingScrollArea) {
scrollPos = existingScrollArea->verticalScrollBar()->value();
}
}
clearChartFrame();
if (chartDataCategorical.isEmpty()) {
qDebug() << "Categorical chart data is empty.";
return;
}
QVariantMap data = chartDataCategorical[0].toMap();
QVariantList clusters = data["clusters"].toList();
QScrollArea* scrollArea = new QScrollArea(_legendWidget);
scrollArea->setWidgetResizable(true);
scrollArea->setFrameStyle(QFrame::NoFrame);
scrollArea->setContentsMargins(0, 0, 0, 0);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QWidget* containerWidget = new QWidget();
containerWidget->setStyleSheet("border: none;");
containerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QVBoxLayout* layout = new QVBoxLayout(containerWidget);
layout->setSpacing(1);
layout->setContentsMargins(0, 0, 0, 0);
const int itemHeight = kLegendRowHeight;
QStringList selectionStrings = _settingsAction.getColorSettingsHolder().getSelectionStringList().getSelectedOptions();
QColor colorSelection = _settingsAction.getColorSettingsHolder().getSelectionColorAction().getColor();
for (const QVariant& clusterItem : clusters) {
QVariantMap cluster = clusterItem.toMap();
QString name = cluster["name"].toString();
int count = cluster["count"].toInt();
QString color = cluster["color"].toString();
QWidget* legendRow = new QWidget(containerWidget);
legendRow->setFixedHeight(itemHeight);
legendRow->setStyleSheet("border: none;");
QHBoxLayout* rowLayout = new QHBoxLayout(legendRow);
rowLayout->setSpacing(5);
rowLayout->setContentsMargins(0, 0, 0, 0);
QLabel* colorBox = new QLabel(legendRow);
colorBox->setFixedSize(12, 12);
colorBox->setStyleSheet(QString("background-color: %1; border-radius: 4px;").arg(color));
QLabel* textLabel = new QLabel(QString("%1 (%2)").arg(name).arg(count), legendRow);
if (selectionStrings.contains(name)) {
textLabel->setStyleSheet(QString("color: %1; font-size: 13px;").arg(colorSelection.name()));
}
else {
textLabel->setStyleSheet("font-size: 12px; color: black;");
}
rowLayout->addWidget(colorBox);
rowLayout->addWidget(textLabel);
layout->addWidget(legendRow);
}
layout->addStretch();
scrollArea->setWidget(containerWidget);
QVBoxLayout* mainLayout = new QVBoxLayout(_legendWidget);
QLabel* titleLabel = new QLabel(_settingsAction.getColorSettingsHolder().getChartTitleAction().getString(), _legendWidget);
titleLabel->setAlignment(Qt::AlignCenter);
titleLabel->setStyleSheet("font-weight: bold; font-size: 14px; color: black;");
mainLayout->addWidget(titleLabel);
mainLayout->addWidget(scrollArea);
scrollArea->verticalScrollBar()->setValue(scrollPos);
}
QColor ChartLegendViewPlugin::selectColor(double value, double min, double max)
{
QString colorScheme = _settingsAction.getColorSettingsHolder().getColorMapAction().getColorMap();
bool mirror = _settingsAction.getColorSettingsHolder().getColorMapAction().getMirrorAction(ColorMapAction::Axis::X).isChecked();
return selectColorVal(value, min, max, colorScheme, mirror);
}
void ChartLegendViewPlugin::convertPointDataAndUpdateChart()
{
auto currentPointDataset = _settingsAction.getColorSettingsHolder().getColorDatasetAction().getCurrentDataset<Points>();
if (!currentPointDataset.isValid())
{
clearChartFrame();
return;
}
try {
std::vector<float> resultContainer(currentPointDataset->getNumPoints());
auto dimensionNames = currentPointDataset->getDimensionNames();
auto dimensionName = _settingsAction.getColorSettingsHolder().getDimensionSelectionAction().getCurrentDimensionName();
int dimensionIndex = getIndexOfQString(dimensionNames, dimensionName);
if (dimensionIndex != -1) {
currentPointDataset->extractDataForDimension(resultContainer, dimensionIndex);
}
else {
qDebug() << "Dimension name not found in dimension names.";
return;
}
if (resultContainer.empty())
{
qDebug() << "LegendPlugin::convertPointDataAndUpdateChart: No data found";
return;
}
float min = *std::min_element(resultContainer.begin(), resultContainer.end());
float max = *std::max_element(resultContainer.begin(), resultContainer.end());
QVariantList values;
for (const auto& value : resultContainer) {
values.append(value);
}
QVariantList visString = QVariantList{
QVariantMap{
{"type", "numerical"},
{"values", values},
{"min", min},
{"max", max}
}
};
createNumericalChart(visString);
}
catch (const std::exception& e) {
qCritical() << "Exception caught in convertPointDataAndUpdateChart:" << e.what();
}
catch (...) {
qCritical() << "Unknown exception caught in convertPointDataAndUpdateChart";
}
}
void ChartLegendViewPlugin::updateChart()
{
const auto dataset = _settingsAction.getColorSettingsHolder().getColorDatasetAction().getCurrentDataset();
if (!dataset.isValid())
{
clearChartFrame();
return;
}
const auto type = dataset->getDataType();
if (type == PointType) {
auto points = mv::data().getDataset<Points>(dataset->getId());
_settingsAction.getColorSettingsHolder().getDimensionSelectionAction().setPointsDataset(points);
convertPointDataAndUpdateChart();
} else if (type == ClusterType) {
auto clusters = mv::data().getDataset<Clusters>(dataset->getId());
const auto selectedOptions = _settingsAction.getColorSettingsHolder().getSelectionStringList().getSelectedOptions();
const auto clusterNames = clusters->getClusterNames();
_settingsAction.getColorSettingsHolder().getSelectionStringList().setOptions(convertToQStringList(clusterNames));
_settingsAction.getColorSettingsHolder().getSelectionStringList().setSelectedOptions(selectedOptions);
convertClusterDataAndUpdateChart();
}
}
void ChartLegendViewPlugin::convertClusterDataAndUpdateChart()
{
auto currentClusterDataset = _settingsAction.getColorSettingsHolder().getColorDatasetAction().getCurrentDataset<Clusters>();
if (!currentClusterDataset.isValid())
{
clearChartFrame();
return;
}
try {
const auto& clustersValues = currentClusterDataset->getClusters();
QVariantList clusterData;
if (clustersValues.empty())
{
qWarning() << "LegendPlugin::convertClusterDataAndUpdateChart: No clusters found";
return;
}
for (const auto& clusterValue : clustersValues)
{
QVariantMap clusterMap;
auto clusterName = clusterValue.getName();
auto clusterColor = clusterValue.getColor().name();
const auto clusterIndicesCount = clusterValue.getNumberOfIndices();
clusterMap.insert("name", clusterName);
clusterMap.insert("color", clusterColor);
clusterMap.insert("count", clusterIndicesCount);
clusterData.append(clusterMap);
}
std::sort(clusterData.begin(), clusterData.end(), [](const QVariant& first, const QVariant& second) {
return first.toMap().value("count").toInt() > second.toMap().value("count").toInt();
});
QVariantList visString = QVariantList{
QVariantMap{
{"type", "categorical"},
{"clusters", clusterData}
}
};
createCategoricalChart(visString);
}
catch (const std::exception& e) {
qCritical() << "Exception caught in convertClusterDataAndUpdateChart:" << e.what();
}
catch (...) {
qCritical() << "Unknown exception caught in convertClusterDataAndUpdateChart";
}
}
ChartLegendViewPluginFactory::ChartLegendViewPluginFactory()
{
setIconByName("palette");
getPluginMetadata().setDescription("Legend for cluster colors and numeric scales.");
getPluginMetadata().setSummary("See what the scatterplot colors represent and highlight selected entries.");
getPluginMetadata().setCopyrightHolder({ "BioVault (Biomedical Visual Analytics Unit LUMC - TU Delft)" });
getPluginMetadata().setAuthors({
{ "A. Vieth", { "Plugin developer", "Maintainer" }, { "LUMC", "TU Delft" } },
{ "T. Kroes", { "Lead software architect" }, { "LUMC" } },
{ "J. Thijssen", { "Software architect" }, { "LUMC", "TU Delft" } }
});
getPluginMetadata().setOrganizations({
{ "LUMC", "Leiden University Medical Center", "https://www.lumc.nl/en/" },
{ "TU Delft", "Delft university of technology", "https://www.tudelft.nl/" }
});
getPluginMetadata().setLicenseText("This plugin is distributed under the [LGPL v3.0](https://www.gnu.org/licenses/lgpl-3.0.en.html) license.");
}
ViewPlugin* ChartLegendViewPluginFactory::produce()
{
return new ChartLegendViewPlugin(this);
}
mv::DataTypes ChartLegendViewPluginFactory::supportedDataTypes() const
{
// Both numeric point data and categorical cluster data have legend views.
DataTypes supportedTypes;
supportedTypes.append(PointType);
supportedTypes.append(ClusterType);
return supportedTypes;
}
mv::gui::PluginTriggerActions ChartLegendViewPluginFactory::getPluginTriggerActions(const mv::Datasets& datasets) const
{
PluginTriggerActions pluginTriggerActions;
const auto getPluginInstance = [this]() -> ChartLegendViewPlugin* {
return dynamic_cast<ChartLegendViewPlugin*>(plugins().requestViewPlugin(getKind()));
};
const auto numberOfDatasets = datasets.count();
if (numberOfDatasets == 1)
{
auto type = datasets.at(0)->getDataType();
if (type == PointType || type == ClusterType)
{
auto* pluginTriggerAction = new PluginTriggerAction(
const_cast<ChartLegendViewPluginFactory*>(this),
this,
"Chart Legend View",
"Show the legend for the selected dataset",
mv::util::StyledIcon("list"),
[getPluginInstance, datasets](PluginTriggerAction&) {
getPluginInstance()->loadData(datasets);
});
pluginTriggerActions << pluginTriggerAction;
}
}
return pluginTriggerActions;
}
void ChartLegendViewPlugin::fromVariantMap(const QVariantMap& variantMap)
{
ViewPlugin::fromVariantMap(variantMap);
mv::util::variantMapMustContain(variantMap, "Legend:Settings");
_settingsAction.fromVariantMap(variantMap["Legend:Settings"].toMap());
}
QVariantMap ChartLegendViewPlugin::toVariantMap() const
{
QVariantMap variantMap = ViewPlugin::toVariantMap();
_settingsAction.insertIntoVariantMap(variantMap);
return variantMap;
}