|
| 1 | +/* This source file is part of the Tomviz project, https://tomviz.org/. |
| 2 | + It is released under the 3-Clause BSD License, see "LICENSE". */ |
| 3 | + |
| 4 | +#include "DataBrokerSaveReaction.h" |
| 5 | +#include "DataBrokerSaveDialog.h" |
| 6 | + |
| 7 | +#include "ActiveObjects.h" |
| 8 | +#include "DataSource.h" |
| 9 | +#include "GenericHDF5Format.h" |
| 10 | +#include "LoadDataReaction.h" |
| 11 | +#include "Utilities.h" |
| 12 | + |
| 13 | +#include <vtkImageData.h> |
| 14 | + |
| 15 | +#include <QDebug> |
| 16 | +#include <QMessageBox> |
| 17 | + |
| 18 | +namespace tomviz { |
| 19 | + |
| 20 | +DataBrokerSaveReaction::DataBrokerSaveReaction(QAction* parentObject, |
| 21 | + MainWindow* mainWindow) |
| 22 | + : pqReaction(parentObject), m_mainWindow(mainWindow) |
| 23 | +{ |
| 24 | + QObject::connect( |
| 25 | + &ActiveObjects::instance(), |
| 26 | + QOverload<DataSource*>::of(&ActiveObjects::dataSourceChanged), this, |
| 27 | + [this](DataSource* dataSource) { |
| 28 | + parentAction()->setEnabled(dataSource != nullptr && |
| 29 | + m_dataBrokerInstalled); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +DataBrokerSaveReaction::~DataBrokerSaveReaction() = default; |
| 34 | + |
| 35 | +void DataBrokerSaveReaction::onTriggered() |
| 36 | +{ |
| 37 | + saveData(); |
| 38 | +} |
| 39 | + |
| 40 | +void DataBrokerSaveReaction::setDataBrokerInstalled(bool installed) |
| 41 | +{ |
| 42 | + m_dataBrokerInstalled = installed; |
| 43 | +} |
| 44 | + |
| 45 | +void DataBrokerSaveReaction::saveData() |
| 46 | +{ |
| 47 | + auto dataBroker = new DataBroker(tomviz::mainWidget()); |
| 48 | + DataBrokerSaveDialog dialog(dataBroker, tomviz::mainWidget()); |
| 49 | + |
| 50 | + if (dialog.exec() == QDialog::Accepted) { |
| 51 | + auto name = dialog.name(); |
| 52 | + |
| 53 | + auto ds = ActiveObjects::instance().activeDataSource(); |
| 54 | + if (ds == nullptr) { |
| 55 | + qWarning() << "No active data source!"; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + auto data = ds->imageData(); |
| 60 | + |
| 61 | + vtkNew<vtkImageData> permutedData; |
| 62 | + if (DataSource::hasTiltAngles(data)) { |
| 63 | + // No deep copies of data needed. Just re-label the axes. |
| 64 | + permutedData->ShallowCopy(data); |
| 65 | + relabelXAndZAxes(permutedData); |
| 66 | + } else { |
| 67 | + // Need to re-order to C ordering before writing |
| 68 | + GenericHDF5Format::reorderData(data, permutedData, |
| 69 | + ReorderMode::FortranToC); |
| 70 | + } |
| 71 | + |
| 72 | + tomviz::mainWidget()->setCursor(Qt::WaitCursor); |
| 73 | + auto call = dataBroker->saveData("fxi", name, data); |
| 74 | + connect( |
| 75 | + call, &SaveDataCall::complete, dataBroker, |
| 76 | + [dataBroker, this](const QString& id) { |
| 77 | + dataBroker->deleteLater(); |
| 78 | + tomviz::mainWidget()->unsetCursor(); |
| 79 | + QMessageBox messageBox( |
| 80 | + QMessageBox::Information, "tomviz", |
| 81 | + QString( |
| 82 | + "The active dataset was successfully exported to DataBroker: %1") |
| 83 | + .arg(id), |
| 84 | + QMessageBox::Ok, m_mainWindow); |
| 85 | + messageBox.exec(); |
| 86 | + }); |
| 87 | + |
| 88 | + connect(call, &DataBrokerCall::error, dataBroker, |
| 89 | + [dataBroker, this](const QString& errorMessage) { |
| 90 | + tomviz::mainWidget()->unsetCursor(); |
| 91 | + dataBroker->deleteLater(); |
| 92 | + QMessageBox messageBox( |
| 93 | + QMessageBox::Warning, "tomviz", |
| 94 | + QString("Error export data to DataBroker: %1. Please check " |
| 95 | + "message log for details.") |
| 96 | + .arg(errorMessage), |
| 97 | + QMessageBox::Ok, m_mainWindow); |
| 98 | + messageBox.exec(); |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace tomviz |
0 commit comments