-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImageViewerPlugin.cpp
More file actions
485 lines (351 loc) · 18.1 KB
/
Copy pathImageViewerPlugin.cpp
File metadata and controls
485 lines (351 loc) · 18.1 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
#include "ImageViewerPlugin.h"
#include "SelectionAction.h"
#include "Layer.h"
#include "ConvertToImagesDatasetDialog.h"
#include <ImageData/Images.h>
#include <ImageData/ImageData.h>
#include <ClusterData/ClusterData.h>
#include <util/Exception.h>
#include <actions/PluginTriggerAction.h>
#include <DatasetsMimeData.h>
#include <QDebug>
#include <QSplitter>
#include <QMimeData>
#include <QMenu>
using namespace mv;
using namespace mv::gui;
using namespace mv::util;
Q_PLUGIN_METADATA(IID "nl.BioVault.ImageViewerPlugin")
ImageViewerPlugin::ImageViewerPlugin(mv::plugin::PluginFactory* factory) :
ViewPlugin(factory),
_layersModel(this),
_selectionModel(&_layersModel),
_imageViewerWidget(*this),
_dropWidget(&_imageViewerWidget),
_selectionToolbarAction(*this),
_interactionToolbarAction(this, "Zoom Toolbar"),
_settingsAction(this, "Settings")
{
setObjectName("Images");
getWidget().setContextMenuPolicy(Qt::CustomContextMenu);
//getWidget().setFocusPolicy(Qt::ClickFocus);
_interactionToolbarAction.initialize(this);
}
void ImageViewerPlugin::init()
{
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(_selectionToolbarAction.createWidget(&getWidget()));
layout->addWidget(&_imageViewerWidget, 1);
layout->addWidget(_interactionToolbarAction.createWidget(&getWidget()));
getWidget().setLayout(layout);
_dropWidget.setDropIndicatorWidget(new DropWidget::DropIndicatorWidget(&getWidget(), "No data loaded", "Drag an item from the data hierarchy and drop it here to visualize data..."));
_dropWidget.initialize([this](const QMimeData* mimeData) -> DropWidget::DropRegions {
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 = mv::DataTypes({ ImageType, PointType, ClusterType });
if (!dataTypes.contains(dataType))
dropRegions << new DropWidget::DropRegion(this, "Incompatible data", "This type of data is not supported", "exclamation-circle", false);
if (dataType == ImageType) {
dropRegions << new DropWidget::DropRegion(this, "Images", QString("Add an image layer for %1").arg(datasetGuiName), "images", true, [this, datasetGuiName, dataset]() {
try
{
getWidget().setFocus(Qt::OtherFocusReason);
loadData({ dataset });
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to load '%1'").arg(datasetGuiName), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to load '%1'").arg(datasetGuiName));
}
});
}
if (dataType == PointType) {
dropRegions << new DropWidget::DropRegion(this, "Points", QString("Convert %1 to image layer").arg(datasetGuiName), "braille", true, [this, dataset]() {
immigrateDataset(dataset);
});
}
if (dataType == ClusterType) {
dropRegions << new DropWidget::DropRegion(this, "Clusters", QString("Convert %1 to image layer").arg(datasetGuiName), "th-large", true, [this, dataset]() {
immigrateDataset(dataset);
});
}
return dropRegions;
});
connect(&_imageViewerWidget, &ImageViewerWidget::pixelSelectionStarted, this, [this]() {
const auto selectedRows = _selectionModel.selectedRows();
if (selectedRows.count() != 1)
return;
auto layer = _layersModel.getLayerFromIndex(selectedRows.first());
Q_ASSERT(layer != nullptr);
if (layer == nullptr)
return;
layer->startSelection();
});
connect(&_imageViewerWidget, &ImageViewerWidget::mousePositionsChanged, this, [this](const QVector<QPoint>& mousePositions) {
if (mousePositions.count() == 0)
return;
const auto selectedRows = _selectionModel.selectedRows();
if (selectedRows.count() != 1)
return;
auto layer = _layersModel.getLayerFromIndex(selectedRows.first());
Q_ASSERT(layer != nullptr);
if (layer == nullptr)
return;
layer->computeSelection(mousePositions);
auto& selectionAction = layer->getSelectionAction();
const auto isSampleSelection = layer->getSelectionAction().getPixelSelectionAction().getTypeAction().getCurrentIndex() == static_cast<std::int32_t>(PixelSelectionType::Sample);
if (selectionAction.getPixelSelectionAction().getNotifyDuringSelectionAction().isChecked() || isSampleSelection)
layer->publishSelection();
if (isSampleSelection)
layer->resetSelectionBuffer();
});
connect(&_imageViewerWidget, &ImageViewerWidget::pixelSelectionEnded, this, [this]() {
const auto selectedRows = _selectionModel.selectedRows();
if (selectedRows.count() != 1)
return;
auto layer = _layersModel.getLayerFromIndex(selectedRows.first());
Q_ASSERT(layer != nullptr);
if (layer == nullptr)
return;
layer->publishSelection();
layer->resetSelectionBuffer();
});
const auto layersInsertedRemovedChanged = [this]() {
_dropWidget.setShowDropIndicator(_layersModel.rowCount() == 0);
const auto hasVisibleLayers = _layersModel.rowCount() == 0 ? false : !_layersModel.match(_layersModel.index(0, static_cast<int>(LayersModel::Column::Visible)), Qt::EditRole, true, -1).isEmpty();
_selectionToolbarAction.setEnabled(hasVisibleLayers);
_interactionToolbarAction.setEnabled(hasVisibleLayers);
};
connect(&_layersModel, &LayersModel::rowsInserted, this, layersInsertedRemovedChanged);
connect(&_layersModel, &LayersModel::rowsRemoved, this, layersInsertedRemovedChanged);
connect(&_layersModel, &LayersModel::dataChanged, this, layersInsertedRemovedChanged);
layersInsertedRemovedChanged();
connect(&_selectionModel, &QItemSelectionModel::selectionChanged, this, &ImageViewerPlugin::onLayerSelectionChanged);
onLayerSelectionChanged();
connect(&_imageViewerWidget, &ImageViewerWidget::customContextMenuRequested, this, [this](const QPoint& point) {
if (_layersModel.rowCount() <= 0)
return;
_settingsAction.getContextMenu()->exec(getWidget().mapToGlobal(point));
});
_dropWidget.setShowDropIndicator(false);
_dropWidget.setShowDropIndicator(true);
addDockingAction(&_settingsAction, nullptr, DockAreaFlag::Left, true, AutoHideLocation::Right, QSize(300, 300));
}
void ImageViewerPlugin::loadData(const Datasets& datasets)
{
if (datasets.isEmpty())
return;
for (const auto& dataset : datasets)
addDataset(dataset);
}
void ImageViewerPlugin::arrangeLayers(LayersLayout layersLayout)
{
auto layers = _layersModel.getLayers();
const auto numberOfLayers = layers.size();
auto margin = 0.0f;
for (auto layer : layers) {
const auto candidateMargin = 0.15f * std::max(layer->getWorldBoundingRectangle().width(), layer->getWorldBoundingRectangle().height());
if (candidateMargin > margin)
margin = candidateMargin;
}
switch (layersLayout)
{
case LayersLayout::Stacked:
break;
case LayersLayout::Vertical:
{
auto offsetY = 0u;
for (int layerIndex = 0; layerIndex < numberOfLayers; ++layerIndex) {
auto currentLayer = layers[layerIndex];
auto nextLayer = layers[(layerIndex + 1) < numberOfLayers ? layerIndex + 1 : layerIndex];
layers[layerIndex]->getGeneralAction().getPositionAction().getYAction().setValue(offsetY);
offsetY += currentLayer->getWorldBoundingRectangle().height() / 2;
offsetY += margin;
offsetY += nextLayer->getWorldBoundingRectangle().height() / 2;
}
break;
}
case LayersLayout::Horizontal:
{
auto offsetX = 0u;
for (int layerIndex = 0; layerIndex < numberOfLayers; ++layerIndex) {
auto currentLayer = layers[layerIndex];
auto nextLayer = layers[(layerIndex + 1) < numberOfLayers ? layerIndex + 1 : layerIndex];
layers[layerIndex]->getGeneralAction().getPositionAction().getXAction().setValue(offsetX);
offsetX += currentLayer->getWorldBoundingRectangle().width() / 2;
offsetX += margin;
offsetX += nextLayer->getWorldBoundingRectangle().width() / 2;
}
break;
}
case LayersLayout::Grid:
{
const auto numberOfColumns = static_cast<std::uint32_t>(ceilf(sqrtf(static_cast<float>(numberOfLayers))));
const auto numberOfRows = static_cast<std::uint32_t>(ceilf(static_cast<float>(numberOfLayers) / static_cast<float>(numberOfColumns)));
QVector<float> columnWidths, rowHeights;
columnWidths.resize(numberOfColumns, 0.0f);
rowHeights.resize(numberOfRows, 0.0f);
for (int columnIndex = 0; columnIndex < numberOfColumns; ++columnIndex) {
for (int rowIndex = 0; rowIndex < numberOfRows; ++rowIndex) {
auto layer = layers[rowIndex * numberOfColumns + columnIndex];
const auto layerWidth = layer->getWorldBoundingRectangle().width();
if (layerWidth > columnWidths[columnIndex])
columnWidths[columnIndex] = layerWidth;
}
}
for (int rowIndex = 0; rowIndex < numberOfRows; ++rowIndex) {
for (int columnIndex = 0; columnIndex < numberOfColumns; ++columnIndex) {
auto layer = layers[rowIndex * numberOfColumns + columnIndex];
const auto layerHeight = layer->getWorldBoundingRectangle().height();
if (layerHeight > rowHeights[rowIndex])
rowHeights[rowIndex] = layerHeight;
}
}
QPoint offset;
for (int rowIndex = 0; rowIndex < numberOfRows; ++rowIndex) {
for (int columnIndex = 0; columnIndex < numberOfColumns; ++columnIndex) {
auto layer = layers[rowIndex * numberOfColumns + columnIndex];
layer->getGeneralAction().getPositionAction().getXAction().setValue(offset.x());
layer->getGeneralAction().getPositionAction().getYAction().setValue(offset.y());
offset.setX(offset.x() + columnWidths[columnIndex] + margin);
}
offset.setX(0);
offset.setY(offset.y() - rowHeights[rowIndex] - margin);
}
break;
}
}
getImageViewerWidget().getRenderer().setZoomRectangle(getImageViewerWidget().getWorldBoundingRectangle());
getImageViewerWidget().update();
}
void ImageViewerPlugin::addDataset(const Dataset<Images>& dataset)
{
auto layer = new Layer(&_settingsAction.getEditLayersAction(), dataset->text());
layer->initialize(this, dataset);
if (!projects().isOpeningProject() && !projects().isImportingProject())
layer->scaleToFit(_imageViewerWidget.getWorldBoundingRectangle(false));
_layersModel.addLayer(layer);
getImageViewerWidget().update();
}
void ImageViewerPlugin::onLayerSelectionChanged()
{
/*
// Get selected row and establish whether there is a valid selection
const auto selectedRows = _selectionModel.selectedRows();
const auto hasSelection = !selectedRows.isEmpty();
// Name of the currently selected layer
QString currentLayerName = "";
// Update current layer name when there is a valid selection
if (hasSelection) {
// Get pointer to layer that was selected
auto layer = static_cast<Layer*>(selectedRows.first().internalPointer());
// A layer is selected so change the current layer name
currentLayerName = layer->getGeneralAction().getNameAction().getString();
// Zoom to the extents of the layer if smart zoom is enabled
if (_interactionToolbarAction.getViewSettingsAction().getSmartZoomAction().isChecked() && layer->getGeneralAction().getVisibleAction().isChecked())
layer->zoomToExtents();
}
// Update the window title
getWidget().setWindowTitle(QString("%1%2").arg(getGuiName(), currentLayerName.isEmpty() ? "" : QString(": %1").arg(currentLayerName)));
*/
}
void ImageViewerPlugin::immigrateDataset(const Dataset<DatasetImpl>& dataset)
{
try {
ConvertToImagesDatasetDialog* dialog = new ConvertToImagesDatasetDialog(*this, const_cast<Dataset<DatasetImpl>&>(dataset));
connect(dialog, &ConvertToImagesDatasetDialog::accepted, this, [this, dialog]() -> void {
auto layer = new Layer(&_settingsAction.getEditLayersAction(), dialog->getTargetImagesDataset()->text());
layer->initialize(this, dialog->getTargetImagesDataset());
layer->scaleToFit(_imageViewerWidget.getWorldBoundingRectangle(false));
_layersModel.addLayer(layer);
_imageViewerWidget.updateWorldBoundingRectangle();
if (_interactionToolbarAction.getViewSettingsAction().getSmartZoomAction().isChecked() || _layersModel.rowCount() == 1)
layer->zoomToExtents();
dialog->deleteLater();
});
dialog->open();
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to immigrate dataset: %1").arg(dataset->text()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to immigrate dataset: %1").arg(dataset->text()));
}
}
void ImageViewerPlugin::fromVariantMap(const mv::VariantMap& variantMap)
{
ViewPlugin::fromVariantMap(variantMap);
_layersModel.fromParentVariantMap(variantMap);
_selectionToolbarAction.fromParentVariantMap(variantMap);
_interactionToolbarAction.fromParentVariantMap(variantMap);
}
mv::VariantMap ImageViewerPlugin::toVariantMap() const
{
auto variantMap = ViewPlugin::toVariantMap();
_layersModel.insertIntoVariantMap(variantMap);
_selectionToolbarAction.insertIntoVariantMap(variantMap);
_interactionToolbarAction.insertIntoVariantMap(variantMap);
return variantMap;
}
QIcon ImageViewerPluginFactory::getIcon(const QColor& color /*= Qt::black*/) const
{
return mv::Application::getIconFont("FontAwesome").getIcon("images", color);
}
ImageViewerPlugin* ImageViewerPluginFactory::produce()
{
return new ImageViewerPlugin(this);
}
PluginTriggerActions ImageViewerPluginFactory::getPluginTriggerActions(const mv::Datasets& datasets) const
{
PluginTriggerActions pluginTriggerActions;
const auto getInstance = [this]() -> ImageViewerPlugin* {
return dynamic_cast<ImageViewerPlugin*>(Application::core()->getPluginManager().requestViewPlugin(getKind()));
};
const auto numberOfDatasets = datasets.count();
if (PluginFactory::areAllDatasetsOfTheSameType(datasets, ImageType)) {
auto& fontAwesome = Application::getIconFont("FontAwesome");
if (numberOfDatasets == 1) {
if (datasets.first()->getDataType() == ImageType) {
auto pluginTriggerAction = new PluginTriggerAction(const_cast<ImageViewerPluginFactory*>(this), this, "Image Viewer", "Load dataset in image viewer", fontAwesome.getIcon("images"), [this, getInstance, datasets](PluginTriggerAction& pluginTriggerAction) -> void {
getInstance()->loadData(datasets);
});
pluginTriggerActions << pluginTriggerAction;
}
}
if (numberOfDatasets >= 2) {
auto viewTogetherAction = new PluginTriggerAction(const_cast<ImageViewerPluginFactory*>(this), this, "Images/Stacked", "View datasets in the image viewer arranged on top of each other", fontAwesome.getIcon("layer-group"), [this, getInstance, datasets](PluginTriggerAction& pluginTriggerAction) -> void {
getInstance()->loadData(datasets);
});
auto arrangeVerticallyAction = new PluginTriggerAction(const_cast<ImageViewerPluginFactory*>(this), this, "Images/Vertically", "View datasets in the image viewer arranged vertically", fontAwesome.getIcon("long-arrow-alt-down"), [this, getInstance, datasets](PluginTriggerAction& pluginTriggerAction) -> void {
auto plugin = getInstance();
plugin->loadData(datasets);
plugin->arrangeLayers(ImageViewerPlugin::LayersLayout::Vertical);
});
auto arrangeHorizontallyAction = new PluginTriggerAction(const_cast<ImageViewerPluginFactory*>(this), this, "Images/Horizontally", "View datasets in the image viewer arranged horizontally", fontAwesome.getIcon("long-arrow-alt-right"), [this, getInstance, datasets](PluginTriggerAction& pluginTriggerAction) -> void {
auto plugin = getInstance();
plugin->loadData(datasets);
plugin->arrangeLayers(ImageViewerPlugin::LayersLayout::Horizontal);
});
auto arrangeGridAction = new PluginTriggerAction(const_cast<ImageViewerPluginFactory*>(this), this, "Images/Grid", "View datasets in the image viewer arranged in a grid", fontAwesome.getIcon("th"), [this, getInstance, datasets](PluginTriggerAction& pluginTriggerAction) -> void {
auto plugin = getInstance();
plugin->loadData(datasets);
plugin->arrangeLayers(ImageViewerPlugin::LayersLayout::Grid);
});
pluginTriggerActions << viewTogetherAction << arrangeVerticallyAction << arrangeHorizontallyAction << arrangeGridAction;
}
}
return pluginTriggerActions;
}