-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleViewJSPlugin.cpp
More file actions
289 lines (219 loc) · 10.2 KB
/
ExampleViewJSPlugin.cpp
File metadata and controls
289 lines (219 loc) · 10.2 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
#include "ExampleViewJSPlugin.h"
#include "../Common/common.h"
#include "ChartWidget.h"
#include <DatasetsMimeData.h>
#include <vector>
#include <random>
#include <QString>
#include <QStringList>
#include <QVariant>
#include <QVariantList>
#include <QVariantMap>
#include <QMimeData>
#include <QDebug>
Q_PLUGIN_METADATA(IID "studio.manivault.ExampleViewJSPlugin")
using namespace mv;
ExampleViewJSPlugin::ExampleViewJSPlugin(const PluginFactory* factory) :
ViewPlugin(factory),
_chartWidget(nullptr),
_dropWidget(nullptr),
_currentDataSet(nullptr)
{
getLearningCenterAction().addVideos(QStringList({ "Practitioner", "Developer" }));
}
void ExampleViewJSPlugin::init()
{
getWidget().setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
// Create layout
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
// Create chart widget and set html contents of webpage
_chartWidget = new ChartWidget(this);
_chartWidget->setPage(":example_chart/radar_chart.html", "qrc:/example_chart/");
// Add widget to layout
layout->addWidget(_chartWidget);
// Apply the layout
getWidget().setLayout(layout);
// Instantiate new drop widget: See ExampleViewPlugin for details
_dropWidget = new DropWidget(_chartWidget);
_dropWidget->setDropIndicatorWidget(new DropWidget::DropIndicatorWidget(&getWidget(), "No data loaded", "Drag the ExampleViewJSData in this view"));
_dropWidget->initialize([this](const QMimeData* mimeData) -> DropWidget::DropRegions {
// A drop widget can contain zero or more drop regions
DropWidget::DropRegions dropRegions;
const auto datasetsMimeData = dynamic_cast<const DatasetsMimeData*>(mimeData);
if (datasetsMimeData == nullptr)
return dropRegions;
if (datasetsMimeData->getDatasets().count() > 1)
return dropRegions;
const auto dataset = datasetsMimeData->getDatasets().first();
const auto datasetGuiName = dataset->text();
const auto datasetId = dataset->getId();
const auto dataType = dataset->getDataType();
const auto dataTypes = DataTypes({ PointType });
if (dataTypes.contains(dataType)) {
if (datasetId == getCurrentDataSetID()) {
dropRegions << new DropWidget::DropRegion(this, "Warning", "Data already loaded", "exclamation-circle", false);
}
else {
auto candidateDataset = mv::data().getDataset<Points>(datasetId);
dropRegions << new DropWidget::DropRegion(this, "Points", QString("Visualize %1 as parallel coordinates").arg(datasetGuiName), "map-marker-alt", true, [this, candidateDataset]() {
loadData({ candidateDataset });
_dropWidget->setShowDropIndicator(false);
});
}
}
else {
dropRegions << new DropWidget::DropRegion(this, "Incompatible data", "This type of data is not supported", "exclamation-circle", false);
}
return dropRegions;
});
// update data when data set changed
connect(&_currentDataSet, &Dataset<Points>::dataChanged, this, &ExampleViewJSPlugin::convertDataAndUpdateChart);
// Update the selection (coming from PCP) in core
connect(&_chartWidget->getCommunicationObject(), &ChartCommObject::passSelectionToCore, this, &ExampleViewJSPlugin::publishSelection);
// Create data so that we do not need to load any in this example
createData();
addNotification(getExampleNotificationMessage());
}
void ExampleViewJSPlugin::loadData(const mv::Datasets& datasets)
{
// Exit if there is nothing to load
if (datasets.isEmpty())
return;
qDebug() << "ExampleViewJSPlugin::loadData: Load data set from ManiVault core";
// Load the first dataset, changes to _currentDataSet are connected with convertDataAndUpdateChart
_currentDataSet = datasets.first();
events().notifyDatasetDataChanged(_currentDataSet);
}
void ExampleViewJSPlugin::convertDataAndUpdateChart()
{
if (!_currentDataSet.isValid())
return;
qDebug() << "ExampleViewJSPlugin::convertDataAndUpdateChart: Prepare payload";
// convert data from ManiVault PointData to a JSON structure
QVariantList payload;
QVariantMap entry;
_currentDataSet->visitFromBeginToEnd([&entry, &payload, this](auto beginOfData, auto endOfData)
{
auto pointNames = _currentDataSet->getProperty("PointNames");
auto dimNames = _currentDataSet->getDimensionNames();
auto numDims = dimNames.size();
for (std::uint64_t pointId = 0; pointId < _currentDataSet->getNumPoints(); pointId++)
{
entry.clear();
entry["className"] = pointNames.isValid() ? pointNames.value<QStringList>()[pointId] : QString::number(pointId);
QVariantList values;
for (uint32_t dimId = 0; dimId < numDims; dimId++)
{
QVariantMap axval;
axval["axis"] = dimNames[dimId];
axval["value"] = static_cast<float>(beginOfData[pointId * numDims + dimId]);
values.append(axval);
}
entry["axes"] = values;
payload.append(entry);
}
});
qDebug() << "ExampleViewJSPlugin::convertDataAndUpdateChart: Send data from Qt cpp to D3 js";
emit _chartWidget->getCommunicationObject().qt_js_setDataAndPlotInJS(payload);
}
void ExampleViewJSPlugin::publishSelection(const std::vector<unsigned int>& selectedIDs)
{
// ask core for the selection set for the current data set
auto selectionSet = _currentDataSet->getSelection<Points>();
auto& selectionIndices = selectionSet->indices;
// clear the selection and add the new points
selectionIndices.clear();
selectionIndices.reserve(_currentDataSet->getNumPoints());
for (const auto id : selectedIDs) {
selectionIndices.push_back(id);
}
// notify core about the selection change
if (_currentDataSet->isDerivedData())
events().notifyDatasetDataSelectionChanged(_currentDataSet->getSourceDataset<DatasetImpl>());
else
events().notifyDatasetDataSelectionChanged(_currentDataSet);
}
QString ExampleViewJSPlugin::getCurrentDataSetID() const
{
if (_currentDataSet.isValid())
return _currentDataSet->getId();
else
return QString{};
}
void ExampleViewJSPlugin::createData()
{
// Here, we create a random data set, so that we do not need
// to use other plugins for loading when trying out this example
auto points = mv::data().createDataset<Points>("Points", "ExampleViewJSData");
int numPoints = 2;
int numDimensions = 5;
const std::vector<QString> dimNames {"Dim 1", "Dim 2", "Dim 3", "Dim 4", "Dim 5", };
const QVariant pointNames = QStringList{ "Data point 1", "Data point 2" };
std::vector<float> exampleData;
qDebug() << "ExampleViewJSPlugin::createData: Create some example data. 2 points, each with 5 dimensions";
// Create random example data
{
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(0.0, 10.0);
for (int i = 0; i < numPoints * numDimensions; i++)
{
exampleData.push_back(distribution(generator));
qDebug() << "exampleData[" << i << "]: " << exampleData[i];
}
}
// Passing example data with 1000 points and 2 dimensions
points->setData(exampleData.data(), numPoints, numDimensions);
points->setDimensionNames(dimNames);
points->setProperty("PointNames", pointNames);
// Notify the core system of the new data
events().notifyDatasetDataChanged(points);
events().notifyDatasetDataDimensionsChanged(points);
}
// =============================================================================
// Plugin Factory
// =============================================================================
ExampleViewJSPluginFactory::ExampleViewJSPluginFactory()
{
setIconByName("bullseye");
getPluginMetadata().setDescription("Example Javascript view plugin");
getPluginMetadata().setSummary("This plugin shows how to implement a basic Javascript-based view plugin in ManiVault Studio.");
getPluginMetadata().setCopyrightHolder({ "BioVault (Biomedical Visual Analytics Unit LUMC - TU Delft)" });
getPluginMetadata().setAuthors({
{ "A. Vieth", { "Plugin developer", "Maintainer" }, { "LUMC", "TU Delft" } },
{ "J. Thijssen", { "Software architect" }, { "LUMC", "TU Delft" } },
{ "T. Kroes", { "Lead software architect" }, { "LUMC" } }
});
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* ExampleViewJSPluginFactory::produce()
{
return new ExampleViewJSPlugin(this);
}
mv::DataTypes ExampleViewJSPluginFactory::supportedDataTypes() const
{
// This example analysis plugin is compatible with points datasets
DataTypes supportedTypes;
supportedTypes.append(PointType);
return supportedTypes;
}
mv::gui::PluginTriggerActions ExampleViewJSPluginFactory::getPluginTriggerActions(const mv::Datasets& datasets) const
{
PluginTriggerActions pluginTriggerActions;
const auto getPluginInstance = [this]() -> ExampleViewJSPlugin* {
return dynamic_cast<ExampleViewJSPlugin*>(plugins().requestViewPlugin(getKind()));
};
const auto numberOfDatasets = datasets.count();
if (numberOfDatasets >= 1 && PluginFactory::areAllDatasetsOfTheSameType(datasets, PointType)) {
auto pluginTriggerAction = new PluginTriggerAction(const_cast<ExampleViewJSPluginFactory*>(this), this, "Example JS", "View JavaScript visualization", icon(), [this, getPluginInstance, datasets](PluginTriggerAction& pluginTriggerAction) -> void {
for (auto dataset : datasets)
getPluginInstance()->loadData(Datasets({ dataset }));
});
pluginTriggerActions << pluginTriggerAction;
}
return pluginTriggerActions;
}