Skip to content

Commit ce8d830

Browse files
committed
Add support for exporting dataset to Tiled
Signed-off-by: Chris Harris <chris.harris@kitware.com>
1 parent 3ffc0a4 commit ce8d830

11 files changed

Lines changed: 397 additions & 1 deletion

tomviz/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ set(SOURCES
6262
DataBrokerLoadDialog.h
6363
DataBrokerLoadReaction.cxx
6464
DataBrokerLoadReaction.h
65+
DataBrokerSaveDialog.cxx
66+
DataBrokerSaveDialog.h
67+
DataBrokerSaveReaction.cxx
68+
DataBrokerSaveReaction.h
6569
DataExchangeFormat.cxx
6670
DataExchangeFormat.h
6771
DataPropertiesModel.cxx

tomviz/DataBroker.cxx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,38 @@ LoadDataCall* DataBroker::loadVariable(const QString& catalog,
241241
return call;
242242
}
243243

244+
SaveDataCall* DataBroker::saveData(const QString& catalog, const QString& name,
245+
vtkImageData* data)
246+
{
247+
auto call = new SaveDataCall(this);
248+
249+
auto future = QtConcurrent::run([this, catalog, name, data, call]() {
250+
Python python;
251+
252+
auto saveFunc = m_dataBrokerModule.findFunction("save_data");
253+
if (!saveFunc.isValid()) {
254+
emit call->error("Failed to import tomviz.io._databroker.save_data");
255+
}
256+
257+
Python::Object pydata = Python::VTK::GetObjectFromPointer(data);
258+
Python::Tuple args(3);
259+
args.set(0, catalog.toStdString());
260+
args.set(1, name.toStdString());
261+
args.set(2, pydata);
262+
263+
auto res = saveFunc.call(args);
264+
265+
if (!res.isValid()) {
266+
emit call->error("Error calling save_data");
267+
return;
268+
}
269+
270+
auto id = res.toString();
271+
272+
emit call->complete(id);
273+
});
274+
275+
return call;
276+
}
277+
244278
} // namespace tomviz

tomviz/DataBroker.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ class LoadDataCall : public DataBrokerCall
5353
void complete(vtkSmartPointer<vtkImageData> imageData);
5454
};
5555

56+
class SaveDataCall : public DataBrokerCall
57+
{
58+
Q_OBJECT
59+
60+
public:
61+
explicit SaveDataCall(QObject* parent = 0) : DataBrokerCall(parent) {}
62+
63+
signals:
64+
void complete(const QString& id);
65+
};
66+
5667
class DataBroker : public QObject
5768
{
5869
Q_OBJECT
@@ -71,6 +82,8 @@ class DataBroker : public QObject
7182
const QString& table);
7283
LoadDataCall* loadVariable(const QString& catalog, const QString& runUid,
7384
const QString& table, const QString& variable);
85+
SaveDataCall* saveData(const QString& catalog, const QString& name,
86+
vtkImageData* data);
7487
};
7588

7689
} // namespace tomviz

tomviz/DataBrokerSaveDialog.cxx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 "DataBrokerSaveDialog.h"
5+
#include "DataBroker.h"
6+
#include "Utilities.h"
7+
8+
#include "ui_DataBrokerSaveDialog.h"
9+
10+
#include <QDateTime>
11+
#include <QDebug>
12+
#include <QProcessEnvironment>
13+
#include <QPushButton>
14+
#include <QTreeWidget>
15+
16+
#include <pqSettings.h>
17+
18+
namespace tomviz {
19+
20+
DataBrokerSaveDialog::DataBrokerSaveDialog(DataBroker* dataBroker,
21+
QWidget* parent)
22+
: QDialog(parent), m_ui(new Ui::DataBrokerSaveDialog),
23+
m_dataBroker(dataBroker)
24+
{
25+
m_ui->setupUi(this);
26+
27+
setEnabledOkButton(false);
28+
29+
connect(m_ui->nameLineEdit, &QLineEdit::textChanged, this,
30+
[this](const QString& name) {
31+
if (name.size() > 0) {
32+
m_name = name;
33+
setEnabledOkButton(true);
34+
}
35+
});
36+
}
37+
38+
DataBrokerSaveDialog::~DataBrokerSaveDialog() = default;
39+
40+
void DataBrokerSaveDialog::setEnabledOkButton(bool enabled)
41+
{
42+
auto okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
43+
okButton->setEnabled(enabled);
44+
}
45+
46+
QString DataBrokerSaveDialog::name()
47+
{
48+
return m_name;
49+
}
50+
51+
} // namespace tomviz

tomviz/DataBrokerSaveDialog.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
#ifndef tomvizDataBrokerSaveDialog_h
5+
#define tomvizDataBrokerSaveDialog_h
6+
7+
#include "ActiveObjects.h"
8+
9+
#include <QDialog>
10+
11+
#include <QDate>
12+
#include <QList>
13+
#include <QScopedPointer>
14+
#include <QVariantMap>
15+
16+
namespace Ui {
17+
class DataBrokerSaveDialog;
18+
}
19+
20+
class QTreeWidgetItem;
21+
22+
namespace tomviz {
23+
24+
class DataBroker;
25+
class ListResourceCall;
26+
27+
class DataBrokerSaveDialog : public QDialog
28+
{
29+
Q_OBJECT
30+
31+
public:
32+
explicit DataBrokerSaveDialog(DataBroker* dataBroker,
33+
QWidget* parent = nullptr);
34+
~DataBrokerSaveDialog() override;
35+
QString name();
36+
37+
private:
38+
Q_DISABLE_COPY(DataBrokerSaveDialog)
39+
QScopedPointer<Ui::DataBrokerSaveDialog> m_ui;
40+
DataBroker* m_dataBroker;
41+
42+
void setEnabledOkButton(bool enable);
43+
44+
QString m_name;
45+
};
46+
} // namespace tomviz
47+
48+
#endif

tomviz/DataBrokerSaveDialog.ui

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>DataBrokerSaveDialog</class>
4+
<widget class="QDialog" name="DataBrokerSaveDialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>400</width>
10+
<height>117</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Export to DataBroker</string>
15+
</property>
16+
<widget class="QDialogButtonBox" name="buttonBox">
17+
<property name="geometry">
18+
<rect>
19+
<x>30</x>
20+
<y>70</y>
21+
<width>341</width>
22+
<height>32</height>
23+
</rect>
24+
</property>
25+
<property name="orientation">
26+
<enum>Qt::Horizontal</enum>
27+
</property>
28+
<property name="standardButtons">
29+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
30+
</property>
31+
</widget>
32+
<widget class="QWidget" name="formLayoutWidget">
33+
<property name="geometry">
34+
<rect>
35+
<x>9</x>
36+
<y>9</y>
37+
<width>381</width>
38+
<height>51</height>
39+
</rect>
40+
</property>
41+
<layout class="QFormLayout" name="formLayout">
42+
<item row="0" column="0">
43+
<widget class="QLabel" name="label">
44+
<property name="text">
45+
<string>Name to associate with data:</string>
46+
</property>
47+
</widget>
48+
</item>
49+
<item row="0" column="1">
50+
<widget class="QLineEdit" name="nameLineEdit">
51+
<property name="text">
52+
<string/>
53+
</property>
54+
</widget>
55+
</item>
56+
</layout>
57+
</widget>
58+
</widget>
59+
<resources/>
60+
<connections>
61+
<connection>
62+
<sender>buttonBox</sender>
63+
<signal>accepted()</signal>
64+
<receiver>DataBrokerSaveDialog</receiver>
65+
<slot>accept()</slot>
66+
</connection>
67+
<connection>
68+
<sender>buttonBox</sender>
69+
<signal>rejected()</signal>
70+
<receiver>DataBrokerSaveDialog</receiver>
71+
<slot>reject()</slot>
72+
</connection>
73+
</connections>
74+
</ui>

tomviz/DataBrokerSaveReaction.cxx

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

tomviz/DataBrokerSaveReaction.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#ifndef tomvizDataBrokerSaveReaction_h
5+
#define tomvizDataBrokerSaveReaction_h
6+
7+
#include <pqReaction.h>
8+
9+
#include "DataBroker.h"
10+
#include "MainWindow.h"
11+
12+
namespace tomviz {
13+
class DataSource;
14+
15+
/// DataBrokerSaveReaction handles the "Export to DataBroker" action in
16+
/// tomviz.
17+
///
18+
class DataBrokerSaveReaction : public pqReaction
19+
{
20+
Q_OBJECT
21+
22+
public:
23+
DataBrokerSaveReaction(QAction* parentAction, MainWindow* mainWindow);
24+
~DataBrokerSaveReaction() override;
25+
26+
void setDataBrokerInstalled(bool installed);
27+
void saveData();
28+
29+
protected:
30+
/// Called when the action is triggered.
31+
void onTriggered() override;
32+
33+
bool m_dataBrokerInstalled = false;
34+
35+
private:
36+
Q_DISABLE_COPY(DataBrokerSaveReaction)
37+
MainWindow* m_mainWindow;
38+
};
39+
} // namespace tomviz
40+
41+
#endif

0 commit comments

Comments
 (0)