CustusX  15.4.0-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxDicomWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include <QApplication>
34 #include <QDesktopWidget>
35 #include <QDir>
36 #undef REGISTERED
37 #include "ctkServiceTracker.h"
38 #include "ctkDICOMBrowser.h"
39 #include "ctkDICOMAppWidget.h"
40 #include "cxDICOMAppWidget.h"
41 #include "ctkDICOMTableManager.h"
42 #include "ctkDICOMObjectListWidget.h"
43 #include "ctkPluginContext.h"
44 #include "cxDicomWidget.h"
45 //#include "cxDataLocations.h"
46 #include "cxProfile.h"
47 #include "cxTypeConversions.h"
48 #include "cxDicomConverter.h"
49 #include "cxLogger.h"
50 
51 #include "cxPatientModelService.h"
52 #include "cxDicomImageReader.h"
53 
54 namespace cx
55 {
56 
57 DicomWidget::DicomWidget(ctkPluginContext *context, QWidget *parent) :
58  BaseWidget(parent, "DicomWidget", "Dicom"),
59  mVerticalLayout(new QVBoxLayout(this)),
60  mBrowser(NULL),
61  mContext(context)
62 {
63  this->setModified();
64 }
65 
67 {
68  if (!mBrowser)
69  {
70  this->createUI();
71  }
72 }
73 
74 void DicomWidget::createUI()
75 {
76  if (mBrowser)
77  return;
78 
79  this->setToolTip("Import DICOM data");
80  //Add detailed button
81  mViewHeaderAction = this->createAction(this,
82  QIcon(),
83  "View", "View header info for first selected series",
84  SLOT(onViewHeader()));
85  mImportIntoCustusXAction = this->createAction(this,
86  QIcon(),
87  "Import", "Import selected series into application",
88  SLOT(onImportIntoCustusXAction()));
89 
90  mBrowser = new DICOMAppWidget;
91  mBrowser->addActionToToolbar(mViewHeaderAction);
92  mBrowser->addActionToToolbar(mImportIntoCustusXAction);
93 
94  mVerticalLayout->setMargin(0);
95  mVerticalLayout->addWidget(mBrowser);
96 
97  this->setupDatabaseDirectory();
98 }
99 
101 {
102 }
103 
104 void DicomWidget::setupDatabaseDirectory()
105 {
106  QString databaseDirectory = profile()->getSettingsPath() + "/DICOMDatabase";
107 
108  QDir qdir(databaseDirectory);
109  if ( !qdir.exists(databaseDirectory) )
110  {
111  if ( !qdir.mkpath(databaseDirectory) )
112  {
113  CX_LOG_CHANNEL_ERROR("dicom") << "Could not create database directory \"" << databaseDirectory;
114 // std::cerr << "Could not create database directory \"" << databaseDirectory.toLatin1().data() << "\".\n";
115  }
116  }
117 
118 // CX_LOG_CHANNEL_INFO("dicom") << "databaseDirectory: " << databaseDirectory;
119 // std::cout << "databaseDirectory: " << databaseDirectory << std::endl;
120  mBrowser->setDatabaseDirectory(databaseDirectory);
121 }
122 
123 QStringList DicomWidget::currentSeriesSelection()
124 {
125  return mBrowser->getSelectedSeries();
126 }
127 
128 void DicomWidget::onViewHeader()
129 {
130  QStringList series = this->currentSeriesSelection();
131  std::cout << series.join("\n").toStdString() << std::endl;
132 
133  QStringList files;
134  for (int i=0; i<series.size(); ++i)
135  {
136  QStringList current = this->getDatabase()->filesForSeries(series[i]);
137  files.append(current);
138  }
139  files.sort();
140 // std::cout << "files:" << std::endl;
141 // std::cout << files.join("\n").toStdString() << std::endl;
142 
143  ctkDICOMObjectListWidget* window = new ctkDICOMObjectListWidget;
144  window->setWindowTitle("DICOM File Header");
145  window->setFileList(files);
146 
147  QWidget* screen = qApp->desktop()->screen(qApp->desktop()->screenNumber(this));
148  QRect rect = screen->geometry();
149  rect.setWidth(rect.width()*0.66);
150  window->setGeometry(rect);
151 
152  window->show();
153 }
154 
155 void DicomWidget::onImportIntoCustusXAction()
156 {
157  QStringList series = this->currentSeriesSelection();
158 
159  for (unsigned i=0; i<series.size(); ++i)
160  {
161  this->importSeries(series[i]);
162  }
163 }
164 
165 void DicomWidget::importSeries(QString seriesUid)
166 {
167  cx::DicomConverter converter;
168  converter.setDicomDatabase(this->getDatabase());
169  cx::ImagePtr convertedImage = converter.convertToImage(seriesUid);
170 
171  if (!convertedImage)
172  {
173  reportError(QString("Failed to convert DICOM series %1").arg(seriesUid));
174  return;
175  }
176 
177  this->loadIntoPatientModel(convertedImage, seriesUid);
178 }
179 
180 void DicomWidget::loadIntoPatientModel(ImagePtr image, QString seriesUid)
181 {
182  ctkServiceTracker<PatientModelService*> tracker(mContext);
183  tracker.open();
184  PatientModelService* service = tracker.getService(); // get arbitrary instance of this type
185 
186  if (service)
187  {
188  service->insertData(image);
189  report(QString("Loaded DICOM series %1 as %2").arg(seriesUid).arg(image->getName()));
190  }
191  else
192  {
193  reportWarning(QString("Failed to load DICOM series %1 as %2: no PatientModelService.").arg(seriesUid).arg(image->getName()));
194  }
195 }
196 
197 ctkDICOMDatabase* DicomWidget::getDatabase() const
198 {
199  return mBrowser->database();
200 }
201 
202 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
QStringList getSelectedSeries()
void reportError(QString msg)
Definition: cxLogger.cpp:92
ctkDICOMDatabase * database()
virtual void prePaintEvent()
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
void setDicomDatabase(ctkDICOMDatabase *database)
void addActionToToolbar(QAction *action)
DicomWidget(ctkPluginContext *context, QWidget *parent=0)
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:129
ImagePtr convertToImage(QString seriesUid)
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
void report(QString msg)
Definition: cxLogger.cpp:90
virtual ~DicomWidget()
void setDatabaseDirectory(const QString &directory)
#define CX_LOG_CHANNEL_ERROR(channel)
Definition: cxLogger.h:124