Skip to content

Commit

Permalink
Add support for exporting dataset to Tiled
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Harris <chris.harris@kitware.com>
  • Loading branch information
cjh1 committed Jun 22, 2022
1 parent 3ffc0a4 commit a430002
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tomviz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ set(SOURCES
DataBrokerLoadDialog.h
DataBrokerLoadReaction.cxx
DataBrokerLoadReaction.h
DataBrokerSaveDialog.cxx
DataBrokerSaveDialog.h
DataBrokerSaveReaction.cxx
DataBrokerSaveReaction.h
DataExchangeFormat.cxx
DataExchangeFormat.h
DataPropertiesModel.cxx
Expand Down
34 changes: 34 additions & 0 deletions tomviz/DataBroker.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,38 @@ LoadDataCall* DataBroker::loadVariable(const QString& catalog,
return call;
}

SaveDataCall* DataBroker::saveData(const QString& catalog, const QString& name,
vtkImageData* data)
{
auto call = new SaveDataCall(this);

auto future = QtConcurrent::run([this, catalog, name, data, call]() {
Python python;

auto saveFunc = m_dataBrokerModule.findFunction("save_data");
if (!saveFunc.isValid()) {
emit call->error("Failed to import tomviz.io._databroker.save_data");
}

Python::Object pydata = Python::VTK::GetObjectFromPointer(data);
Python::Tuple args(3);
args.set(0, catalog.toStdString());
args.set(1, name.toStdString());
args.set(2, pydata);

auto res = saveFunc.call(args);

if (!res.isValid()) {
emit call->error("Error calling save_data");
return;
}

auto id = res.toString();

emit call->complete(id);
});

return call;
}

} // namespace tomviz
13 changes: 13 additions & 0 deletions tomviz/DataBroker.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ class LoadDataCall : public DataBrokerCall
void complete(vtkSmartPointer<vtkImageData> imageData);
};

class SaveDataCall : public DataBrokerCall
{
Q_OBJECT

public:
explicit SaveDataCall(QObject* parent = 0) : DataBrokerCall(parent) {}

signals:
void complete(const QString& id);
};

class DataBroker : public QObject
{
Q_OBJECT
Expand All @@ -71,6 +82,8 @@ class DataBroker : public QObject
const QString& table);
LoadDataCall* loadVariable(const QString& catalog, const QString& runUid,
const QString& table, const QString& variable);
SaveDataCall* saveData(const QString& catalog, const QString& name,
vtkImageData* data);
};

} // namespace tomviz
Expand Down
51 changes: 51 additions & 0 deletions tomviz/DataBrokerSaveDialog.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* This source file is part of the Tomviz project, https://tomviz.org/.
It is released under the 3-Clause BSD License, see "LICENSE". */

#include "DataBrokerSaveDialog.h"
#include "DataBroker.h"
#include "Utilities.h"

#include "ui_DataBrokerSaveDialog.h"

#include <QDateTime>
#include <QDebug>
#include <QProcessEnvironment>
#include <QPushButton>
#include <QTreeWidget>

#include <pqSettings.h>

namespace tomviz {

DataBrokerSaveDialog::DataBrokerSaveDialog(DataBroker* dataBroker,
QWidget* parent)
: QDialog(parent), m_ui(new Ui::DataBrokerSaveDialog),
m_dataBroker(dataBroker)
{
m_ui->setupUi(this);

setEnabledOkButton(false);

connect(m_ui->nameLineEdit, &QLineEdit::textChanged, this,
[this](const QString& name) {
if (name.size() > 0) {
m_name = name;
setEnabledOkButton(true);
}
});
}

DataBrokerSaveDialog::~DataBrokerSaveDialog() = default;

void DataBrokerSaveDialog::setEnabledOkButton(bool enabled)
{
auto okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
okButton->setEnabled(enabled);
}

QString DataBrokerSaveDialog::name()
{
return m_name;
}

} // namespace tomviz
48 changes: 48 additions & 0 deletions tomviz/DataBrokerSaveDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* This source file is part of the Tomviz project, https://tomviz.org/.
It is released under the 3-Clause BSD License, see "LICENSE". */

#ifndef tomvizDataBrokerSaveDialog_h
#define tomvizDataBrokerSaveDialog_h

#include "ActiveObjects.h"

#include <QDialog>

#include <QDate>
#include <QList>
#include <QScopedPointer>
#include <QVariantMap>

namespace Ui {
class DataBrokerSaveDialog;
}

class QTreeWidgetItem;

namespace tomviz {

class DataBroker;
class ListResourceCall;

class DataBrokerSaveDialog : public QDialog
{
Q_OBJECT

public:
explicit DataBrokerSaveDialog(DataBroker* dataBroker,
QWidget* parent = nullptr);
~DataBrokerSaveDialog() override;
QString name();

private:
Q_DISABLE_COPY(DataBrokerSaveDialog)
QScopedPointer<Ui::DataBrokerSaveDialog> m_ui;
DataBroker* m_dataBroker;

void setEnabledOkButton(bool enable);

QString m_name;
};
} // namespace tomviz

#endif
74 changes: 74 additions & 0 deletions tomviz/DataBrokerSaveDialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DataBrokerSaveDialog</class>
<widget class="QDialog" name="DataBrokerSaveDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>117</height>
</rect>
</property>
<property name="windowTitle">
<string>Export to DataBroker</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>30</x>
<y>70</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QWidget" name="formLayoutWidget">
<property name="geometry">
<rect>
<x>9</x>
<y>9</y>
<width>381</width>
<height>51</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Name to associate with data:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="nameLineEdit">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DataBrokerSaveDialog</receiver>
<slot>accept()</slot>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>DataBrokerSaveDialog</receiver>
<slot>reject()</slot>
</connection>
</connections>
</ui>
102 changes: 102 additions & 0 deletions tomviz/DataBrokerSaveReaction.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* This source file is part of the Tomviz project, https://tomviz.org/.
It is released under the 3-Clause BSD License, see "LICENSE". */

#include "DataBrokerSaveReaction.h"
#include "DataBrokerSaveDialog.h"

#include "ActiveObjects.h"
#include "DataSource.h"
#include "GenericHDF5Format.h"
#include "LoadDataReaction.h"
#include "Utilities.h"

#include <vtkImageData.h>

#include <QDebug>
#include <QMessageBox>

namespace tomviz {

DataBrokerSaveReaction::DataBrokerSaveReaction(QAction* parentObject,
MainWindow* mainWindow)
: pqReaction(parentObject), m_mainWindow(mainWindow)
{
QObject::connect(
&ActiveObjects::instance(),
QOverload<DataSource*>::of(&ActiveObjects::dataSourceChanged), this,
[this](DataSource* dataSource) {
parentAction()->setEnabled(dataSource != nullptr && m_installed);
});
}

DataBrokerSaveReaction::~DataBrokerSaveReaction() = default;

void DataBrokerSaveReaction::onTriggered()
{
saveData();
}

void DataBrokerSaveReaction::setInstalled(bool installed)
{
m_installed = installed;
}

void DataBrokerSaveReaction::saveData()
{
auto dataBroker = new DataBroker(tomviz::mainWidget());
DataBrokerSaveDialog dialog(dataBroker, tomviz::mainWidget());

if (dialog.exec() == QDialog::Accepted) {
auto name = dialog.name();

auto ds = ActiveObjects::instance().activeDataSource();
if (ds == nullptr) {
qWarning() << "No active data source!";
return;
}

auto data = ds->imageData();

vtkNew<vtkImageData> permutedData;
if (DataSource::hasTiltAngles(data)) {
// No deep copies of data needed. Just re-label the axes.
permutedData->ShallowCopy(data);
relabelXAndZAxes(permutedData);
} else {
// Need to re-order to C ordering before writing
GenericHDF5Format::reorderData(data, permutedData,
ReorderMode::FortranToC);
}

tomviz::mainWidget()->setCursor(Qt::WaitCursor);
auto call = dataBroker->saveData("fxi", name, data);
connect(
call, &SaveDataCall::complete, dataBroker,
[dataBroker, this](const QString& id) {
dataBroker->deleteLater();
tomviz::mainWidget()->unsetCursor();
QMessageBox messageBox(
QMessageBox::Information, "tomviz",
QString(
"The active dataset was successfully exported to DataBroker: %1")
.arg(id),
QMessageBox::Ok, m_mainWindow);
messageBox.exec();
});

connect(call, &DataBrokerCall::error, dataBroker,
[dataBroker, this](const QString& errorMessage) {
tomviz::mainWidget()->unsetCursor();
dataBroker->deleteLater();
QMessageBox messageBox(
QMessageBox::Warning, "tomviz",
QString("Error export data to DataBroker: %1. Please check "
"message log for details.")
.arg(errorMessage),
QMessageBox::Ok, m_mainWindow);
messageBox.exec();
});
}
}

} // namespace tomviz
41 changes: 41 additions & 0 deletions tomviz/DataBrokerSaveReaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* This source file is part of the Tomviz project, https://tomviz.org/.
It is released under the 3-Clause BSD License, see "LICENSE". */

#ifndef tomvizDataBrokerSaveReaction_h
#define tomvizDataBrokerSaveReaction_h

#include <pqReaction.h>

#include "DataBroker.h"
#include "MainWindow.h"

namespace tomviz {
class DataSource;

/// DataBrokerSaveReaction handles the "Export to DataBroker" action in
/// tomviz.
///
class DataBrokerSaveReaction : public pqReaction
{
Q_OBJECT

public:
DataBrokerSaveReaction(QAction* parentAction, MainWindow* mainWindow);
~DataBrokerSaveReaction() override;

void setInstalled(bool installed);
void saveData();

protected:
/// Called when the action is triggered.
void onTriggered() override;

bool m_installed = false;

private:
Q_DISABLE_COPY(DataBrokerSaveReaction)
MainWindow* m_mainWindow;
};
} // namespace tomviz

#endif
Loading

0 comments on commit a430002

Please sign in to comment.