qSlicerVolumesIO.cxx 5.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*==============================================================================

  Program: 3D Slicer

  Copyright (c) 2010 Kitware Inc.

  See Doc/copyright/copyright.txt
  or http://www.slicer.org/copyright/copyright.txt for details.

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  This file was originally developed by Julien Finet, Kitware Inc.
  and was partially funded by NIH grant 3P41RR013218-12S1

==============================================================================*/

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Qt includes
#include <QDebug>
#include <QMap>
#include <QString>
#include <QStringList>
#include <QVariant>

// SlicerQt includes
#include "qSlicerAbstractModule.h"
#include "qSlicerCoreApplication.h"
#include "qSlicerModuleManager.h"
#include "qSlicerVolumesIO.h"
#include "qSlicerVolumesIOOptionsWidget.h"

// Logic includes
#include "vtkSlicerVolumesLogic.h"
37
#include "vtkSlicerApplicationLogic.h"
38

39
40
41
// MRMLLogic includes
#include <vtkMRMLApplicationLogic.h>

42
// MRML includes
43
#include <vtkMRMLScalarVolumeNode.h>
44
#include <vtkMRMLVolumeNode.h>
45
#include <vtkMRMLSelectionNode.h>
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

// VTK includes
#include <vtkSmartPointer.h>
#include <vtkStringArray.h>

//-----------------------------------------------------------------------------
qSlicerVolumesIO::qSlicerVolumesIO(QObject* _parent)
  :qSlicerIO(_parent)
{
}

//-----------------------------------------------------------------------------
QString qSlicerVolumesIO::description()const
{
  return "Volume";
}

//-----------------------------------------------------------------------------
qSlicerIO::IOFileType qSlicerVolumesIO::fileType()const
{
  return qSlicerIO::VolumeFile;
}

//-----------------------------------------------------------------------------
70
QStringList qSlicerVolumesIO::extensions()const
71
{
72
  // pic files are bio-rad images (see itkBioRadImageIO)
73
74
75
  return QStringList()
    << "Volume (*.hdr *.nhdr *.nrrd *.mhd *.mha *.vti *.nii *.gz *.mgz *.img *.pic)"
    << "Dicom (*.dcm)"
76
    << "Image (*.png *.tif *.tiff *.jpg *.jpeg)";
77
78
79
80
81
82
83
84
85
86
87
88
89
}

//-----------------------------------------------------------------------------
qSlicerIOOptions* qSlicerVolumesIO::options()const
{
  return new qSlicerVolumesIOOptionsWidget;
}

//-----------------------------------------------------------------------------
bool qSlicerVolumesIO::load(const IOProperties& properties)
{
  Q_ASSERT(properties.contains("fileName"));
  QString fileName = properties["fileName"].toString();
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
  QString name = fileName;
  if (properties.contains("name"))
    {
    name = properties["name"].toString();
    }
  int options = 0;
  if (properties.contains("labelmap"))
    {
    options |= properties["labelmap"].toBool() ? 0x1 : 0x0;
    }
  if (properties.contains("center"))
    {
    options |= properties["center"].toBool() ? 0x2 : 0x0;
    }
  if (properties.contains("singleFile"))
    {
    options |= properties["singleFile"].toBool() ? 0x4 : 0x0;
    }
  if (properties.contains("autowindowlevel"))
    {
    options |= properties["autowindowlevel"].toBool() ? 0x8 : 0x0;
    }
  if (properties.contains("discardorientation"))
    {
    options |= properties["discardorientation"].toBool() ? 0x10 : 0x0;
    }
  vtkSmartPointer<vtkStringArray> fileList;
  if (properties.contains("fileNames"))
    {
    fileList = vtkSmartPointer<vtkStringArray>::New();
    foreach(QString file, properties["fileNames"].toStringList())
      {
      fileList->InsertNextValue(file.toLatin1().data());
      }
    }
126
  vtkSlicerVolumesLogic* volumesLogic =
127
128
129
130
131
    vtkSlicerVolumesLogic::SafeDownCast(
      qSlicerCoreApplication::application()->moduleManager()
      ->module("volumes")->logic());
  Q_ASSERT(volumesLogic);
  vtkMRMLVolumeNode* node = volumesLogic->AddArchetypeVolume(
132
    fileName.toLatin1().data(),
133
134
135
136
137
    name.toLatin1().data(),
    options,
    fileList.GetPointer());
  if (node)
    {
138
139
140
    vtkMRMLApplicationLogic* mrmlAppLogic =
      qSlicerCoreApplication::application()->mrmlApplicationLogic();
    vtkSlicerApplicationLogic* slicerLogic =
141
      qSlicerCoreApplication::application()->appLogic();
142
143
144
145
146
147
148
149
    vtkMRMLSelectionNode* selectionNode =
      mrmlAppLogic ? mrmlAppLogic->GetSelectionNode() : 0;
    if (!selectionNode)
      {
      // support old way
      selectionNode = slicerLogic ? slicerLogic->GetSelectionNode() : 0;
      }
    if (selectionNode)
150
      {
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
      if (vtkMRMLScalarVolumeNode::SafeDownCast(node) &&
          vtkMRMLScalarVolumeNode::SafeDownCast(node)->GetLabelMap())
        {
        selectionNode->SetReferenceActiveLabelVolumeID(node->GetID());
        }
      else
        {
        selectionNode->SetReferenceActiveVolumeID(node->GetID());
        }
      if (mrmlAppLogic)
        {
        mrmlAppLogic->PropagateVolumeSelection();
        // TODO: slices should probably be fitting automatically..
        mrmlAppLogic->FitSliceToAll();
        }
      else if (slicerLogic)
        {
        slicerLogic->PropagateVolumeSelection();
        }
170
      }
171
172
173
174
175
176
177
178
    this->setLoadedNodes(QStringList(QString(node->GetID())));
    }
  else
    {
    this->setLoadedNodes(QStringList());
    }
  return node != 0;
}