Slicer 5.13
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
Loading...
Searching...
No Matches
qSlicerNodeWriter.h
Go to the documentation of this file.
1/*==============================================================================
2
3 Program: 3D Slicer
4
5 Copyright (c) Kitware Inc.
6
7 See COPYRIGHT.txt
8 or http://www.slicer.org/copyright/copyright.txt for details.
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 This file was originally developed by Julien Finet, Kitware Inc.
17 and was partially funded by NIH grant 3P41RR013218-12S1
18
19==============================================================================*/
20
21#ifndef __qSlicerNodeWriter_h
22#define __qSlicerNodeWriter_h
23
24// QtGUI includes
25#include "qSlicerBaseQTGUIExport.h"
28
29// Slicer includes
30#include <vtkMRMLIOProperties.h>
31#include <vtkMRMLNodeWriter.h>
32#include <vtkMRMLStorableNode.h>
33
34// VTK includes
35#include <vtkNew.h>
36
37class vtkMRMLNode;
38
42class Q_SLICER_BASE_QTGUI_EXPORT qSlicerNodeWriter : public qSlicerVTKFileWriter
43{
44 Q_OBJECT
47
48public:
50 qSlicerNodeWriter(const QString& description, const qSlicerIO::IOFileType& fileType, const QStringList& nodeClassNames, bool supportUseCompression, QObject* parent)
51 : Superclass(nullptr, parent)
52 {
53 vtkNew<vtkMRMLNodeWriter> writer;
54 writer->SetDescription(description.toStdString());
55 writer->SetFileType(fileType.toStdString());
56 writer->SetSupportUseCompression(supportUseCompression);
57 // The writer is set in this constructor so that subclasses that may override methods are detected
58 this->setFileWriter(writer);
59 this->setNodeClassNames(nodeClassNames);
60 }
61
64
65 void setSupportUseCompression(bool support) { this->nodeWriter()->SetSupportUseCompression(support); }
66 bool supportUseCompression() const { return this->nodeWriter()->GetSupportUseCompression(); }
67
68 virtual vtkMRMLNode* getNodeByID(const char* id) const { return id ? this->nodeWriter()->GetNodeByID(id) : nullptr; }
69
73 bool write(const qSlicerIO::IOProperties& properties) override
74 {
75 vtkMRMLNodeWriter* writer = this->nodeWriter();
76 if (!writer)
77 {
78 return false;
79 }
80 writer->ClearWrittenNodeIDs();
81 writer->SetScene(this->mrmlScene());
82 vtkMRMLStorableNode* node = vtkMRMLStorableNode::SafeDownCast(this->getNodeByID(properties.value("nodeID").toString().toUtf8().constData()));
83 if (!node || this->canWriteObjectConfidence(node) <= 0.0)
84 {
85 return false;
86 }
87 vtkNew<vtkMRMLIOProperties> vtkProperties;
88 qSlicerIO::toVTKProperties(properties, vtkProperties);
89 if (node->GetID() && writer->GetNodeByID(node->GetID()) == node && writer->CanWriteObjectConfidence(node) > 0.0)
90 {
91 // The VTK-based writer can write the node: use Write(), which may be overridden in vtkMRMLNodeWriter subclasses
92 return writer->Write(vtkProperties);
93 }
94 // This writer accepts a node that the VTK-based writer would not (getNodeByID() or canWriteObject() is overridden)
95 return writer->WriteNode(node, vtkProperties);
96 }
97
99 qSlicerIOOptions* options() const override
100 {
102 options->setShowUseCompression(this->supportUseCompression());
103 return options;
104 }
105
108 explicit qSlicerNodeWriter(QObject* parent)
109 : Superclass(nullptr, parent)
110 {
111 vtkNew<vtkMRMLNodeWriter> writer;
112 this->setFileWriter(writer);
113 }
114
115protected:
116 void setNodeClassNames(const QStringList& nodeClassNames)
117 {
118 std::vector<std::string> classNames;
119 for (const QString& className : nodeClassNames)
120 {
121 classNames.push_back(className.toStdString());
122 }
123 this->nodeWriter()->SetNodeClassNames(classNames);
124 }
125 QStringList nodeClassNames() const
126 {
127 QStringList classNames;
128 for (const std::string& className : this->nodeWriter()->GetNodeClassNames())
129 {
130 classNames << QString::fromStdString(className);
131 }
132 return classNames;
133 }
134};
135
136#endif
QString IOFileType
Definition qSlicerIO.h:60
QVariantMap IOProperties
Definition qSlicerIO.h:61
static void toVTKProperties(const IOProperties &properties, vtkMRMLIOProperties *vtkProperties)
qSlicerNodeWriter(QObject *parent)
vtkMRMLNodeWriter * nodeWriter() const
VTK-based writer that performs all tasks.
virtual vtkMRMLNode * getNodeByID(const char *id) const
void setSupportUseCompression(bool support)
bool write(const qSlicerIO::IOProperties &properties) override
bool supportUseCompression() const
bool supportUseCompression
Some storage nodes don't support the compression option.
qSlicerVTKFileWriter Superclass
QStringList nodeClassNames() const
qSlicerIOOptions * options() const override
Options widget (for writers that call this method from their options() implementation).
void setNodeClassNames(const QStringList &nodeClassNames)
qSlicerNodeWriter(const QString &description, const qSlicerIO::IOFileType &fileType, const QStringList &nodeClassNames, bool supportUseCompression, QObject *parent)
vtkMRMLScene * mrmlScene() const
Return a pointer on the MRML scene.
QString description() const override
Unique name of the reader/writer.
double canWriteObjectConfidence(vtkObject *object) const override
qSlicerVTKFileWriter(vtkMRMLFileWriter *writer, QObject *parent=nullptr)
Q_INVOKABLE vtkMRMLFileWriter * fileWriter() const
VTK-based writer that performs all the tasks of this class.
IOFileType fileType() const override
Multiple readers can share the same file type.
void setFileWriter(vtkMRMLFileWriter *writer)
virtual void SetScene(vtkMRMLScene *scene)
Where the nodes go, and where messages for the user are collected.
virtual double CanWriteObjectConfidence(vtkObject *object)
void ClearWrittenNodeIDs()
virtual vtkMRMLNode * GetNodeByID(const std::string &id)
Get node from the scene by node ID.
bool Write(vtkMRMLIOProperties *properties) override
bool WriteNode(vtkMRMLStorableNode *node, vtkMRMLIOProperties *properties)
static vtkMRMLNodeWriter * SafeDownCast(vtkObject *o)
Abstract Superclass for all specific types of MRML nodes.
virtual char * GetID()
ID use by other nodes to reference this node in XML.
MRML node to represent a 3D surface model.
static vtkMRMLStorableNode * SafeDownCast(vtkObject *o)