-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClusteringAction.cpp
More file actions
126 lines (94 loc) · 5.31 KB
/
ClusteringAction.cpp
File metadata and controls
126 lines (94 loc) · 5.31 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
#include "ClusteringAction.h"
#include "ScatterplotPlugin.h"
#include <ClusterData/ClusterData.h>
#include <PointData/PointData.h>
#include <QHBoxLayout>
using namespace mv;
using namespace mv::gui;
ClusteringAction::ClusteringAction(QObject* parent, const QString& title) :
GroupAction(parent, title),
_scatterplotPlugin(dynamic_cast<ScatterplotPlugin*>(parent->parent())),
_nameAction(this, "Cluster name"),
_colorAction(this, "Cluster color"),
_addClusterAction(this, "Add cluster"),
_clusterDatasetPickerAction(this, "Add to"),
_clusterDatasetNameAction(this, "Dataset name"),
_createClusterDatasetAction(this, "Create"),
_clusterDatasetWizardAction(this, "Create cluster dataset"),
_clusterDatasetAction(this, "Target clusters dataset")
{
setIconByName("th-large");
setConnectionPermissionsToForceNone();
setConfigurationFlag(WidgetAction::ConfigurationFlag::ForceCollapsedInGroup);
setLabelSizingType(LabelSizingType::Auto);
addAction(&_nameAction);
addAction(&_colorAction);
addAction(&_clusterDatasetAction);
addAction(&_addClusterAction);
_clusterDatasetAction.setShowLabels(false);
_clusterDatasetAction.addAction(&_clusterDatasetPickerAction);
_clusterDatasetAction.addAction(&_clusterDatasetWizardAction, TriggerAction::Icon);
_nameAction.setToolTip("Name of the cluster");
_colorAction.setToolTip("Color of the cluster");
_addClusterAction.setToolTip("Add cluster");
_clusterDatasetPickerAction.setToolTip("Target cluster set");
_clusterDatasetNameAction.setToolTip("Name of the new cluster dataset");
_clusterDatasetNameAction.setClearable(true);
_createClusterDatasetAction.setToolTip("Create new cluster dataset");
_createClusterDatasetAction.setEnabled(false);
_clusterDatasetWizardAction.setIconByName("magic");
_clusterDatasetWizardAction.setToolTip("Create a new cluster dataset");
_clusterDatasetWizardAction.setConfigurationFlag(WidgetAction::ConfigurationFlag::ForceCollapsedInGroup);
_clusterDatasetWizardAction.setLabelSizingType(LabelSizingType::Auto);
_clusterDatasetWizardAction.addAction(&_clusterDatasetNameAction);
_clusterDatasetWizardAction.addAction(&_createClusterDatasetAction);
_clusterDatasetPickerAction.setFilterFunction([this](mv::Dataset<DatasetImpl> dataset) -> bool {
return dataset->getDataType() == ClusterType;
});
connect(& _clusterDatasetNameAction, & StringAction::stringChanged, this, [this](const QString& string) -> void {
_createClusterDatasetAction.setEnabled(!string.isEmpty());
});
connect(&_createClusterDatasetAction, &TriggerAction::triggered, this, [this]() -> void {
const auto clustersDataset = mv::data().createDataset<Clusters>("Cluster", _clusterDatasetNameAction.getString(), _scatterplotPlugin->getPositionDataset());
_clusterDatasetPickerAction.setCurrentDataset(clustersDataset);
});
connect(&_clusterDatasetPickerAction, &DatasetPickerAction::datasetPicked, this, [this](Dataset<DatasetImpl> dataset) -> void {
_scatterplotPlugin->getSettingsAction().getColoringAction().setCurrentColorDataset(dataset);
});
connect(&_addClusterAction, &TriggerAction::triggered, this, [this]() {
if (!_scatterplotPlugin->getPositionDataset().isValid())
return;
auto targetClusterDataset = _clusterDatasetPickerAction.getCurrentDataset<Clusters>();
if (!targetClusterDataset.isValid())
return;
auto selection = _scatterplotPlugin->getPositionDataset()->getSelection<Points>();
Cluster cluster;
cluster.setName(_nameAction.getString());
cluster.setColor(_colorAction.getColor());
cluster.setIndices(selection->indices);
targetClusterDataset->addCluster(cluster);
events().notifyDatasetDataChanged(targetClusterDataset);
randomizeClusterColor();
});
const auto updateActionsReadOnly = [this]() -> void {
const auto positionDataset = _scatterplotPlugin->getPositionDataset();
const auto numberOfSelectedPoints = positionDataset.isValid() ? positionDataset->getSelectionSize() : 0;
const auto hasSelection = numberOfSelectedPoints >= 1;
const auto canAddCluster = hasSelection && !_nameAction.getString().isEmpty();
setEnabled(_scatterplotPlugin->getPositionDataset().isValid() && hasSelection);
_nameAction.setEnabled(_clusterDatasetPickerAction.hasSelection());
_colorAction.setEnabled(_clusterDatasetPickerAction.hasSelection());
_addClusterAction.setEnabled(!_nameAction.getString().isEmpty());
};
updateActionsReadOnly();
connect(&_scatterplotPlugin->getPositionDataset(), &Dataset<Points>::changed, this, updateActionsReadOnly);
connect(&_scatterplotPlugin->getPositionDataset(), &Dataset<Points>::dataSelectionChanged, this, updateActionsReadOnly);
connect(&_nameAction, &StringAction::stringChanged, this, updateActionsReadOnly);
connect(&_clusterDatasetPickerAction, &DatasetPickerAction::datasetPicked, this, updateActionsReadOnly);
randomizeClusterColor();
}
void ClusteringAction::randomizeClusterColor()
{
auto rng = QRandomGenerator::global();
_colorAction.setColor(QColor::fromHsl(rng->bounded(360), rng->bounded(150, 255), rng->bounded(100, 200)));
}