CustusX  15.3.4-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  //Add detailed button
80  mViewHeaderAction = this->createAction(this,
81  QIcon(),
82  "View", "View header info for first selected series",
83  SLOT(onViewHeader()));
84  mImportIntoCustusXAction = this->createAction(this,
85  QIcon(),
86  "Import", "Import selected series into application",
87  SLOT(onImportIntoCustusXAction()));
88 
89  mBrowser = new DICOMAppWidget;
90  mBrowser->addActionToToolbar(mViewHeaderAction);
91  mBrowser->addActionToToolbar(mImportIntoCustusXAction);
92 
93  mVerticalLayout->addWidget(mBrowser);
94 
95  this->setupDatabaseDirectory();
96 }
97 
99 {
100 }
101 
103 {
104  return "<html>"
105  "<h3>Dicom plugin.</h3>"
106  "<p>Import data from dicom</p>"
107  "<p>The dicom widgets are taken from the ctk project.</p>"
108  "</html>";
109 }
110 
111 void DicomWidget::setupDatabaseDirectory()
112 {
113  QString databaseDirectory = profile()->getSettingsPath() + "/DICOMDatabase";
114 
115  QDir qdir(databaseDirectory);
116  if ( !qdir.exists(databaseDirectory) )
117  {
118  if ( !qdir.mkpath(databaseDirectory) )
119  {
120  CX_LOG_CHANNEL_ERROR("dicom") << "Could not create database directory \"" << databaseDirectory;
121 // std::cerr << "Could not create database directory \"" << databaseDirectory.toLatin1().data() << "\".\n";
122  }
123  }
124 
125  CX_LOG_CHANNEL_INFO("dicom") << "databaseDirectory: " << databaseDirectory;
126 // std::cout << "databaseDirectory: " << databaseDirectory << std::endl;
127  mBrowser->setDatabaseDirectory(databaseDirectory);
128 }
129 
130 QStringList DicomWidget::currentSeriesSelection()
131 {
132  return mBrowser->getSelectedSeries();
133 }
134 
135 void DicomWidget::onViewHeader()
136 {
137  QStringList series = this->currentSeriesSelection();
138  std::cout << series.join("\n").toStdString() << std::endl;
139 
140  QStringList files;
141  for (int i=0; i<series.size(); ++i)
142  {
143  QStringList current = this->getDatabase()->filesForSeries(series[i]);
144  files.append(current);
145  }
146  files.sort();
147 // std::cout << "files:" << std::endl;
148 // std::cout << files.join("\n").toStdString() << std::endl;
149 
150  ctkDICOMObjectListWidget* window = new ctkDICOMObjectListWidget;
151  window->setWindowTitle("DICOM File Header");
152  window->setFileList(files);
153 
154  QWidget* screen = qApp->desktop()->screen(qApp->desktop()->screenNumber(this));
155  QRect rect = screen->geometry();
156  rect.setWidth(rect.width()*0.66);
157  window->setGeometry(rect);
158 
159  window->show();
160 }
161 
162 void DicomWidget::onImportIntoCustusXAction()
163 {
164  QStringList series = this->currentSeriesSelection();
165 
166  for (unsigned i=0; i<series.size(); ++i)
167  {
168  this->importSeries(series[i]);
169  }
170 }
171 
172 void DicomWidget::importSeries(QString seriesUid)
173 {
174  cx::DicomConverter converter;
175  converter.setDicomDatabase(this->getDatabase());
176  cx::ImagePtr convertedImage = converter.convertToImage(seriesUid);
177 
178  if (!convertedImage)
179  {
180  reportError(QString("Failed to convert DICOM series %1").arg(seriesUid));
181  return;
182  }
183 
184  this->loadIntoPatientModel(convertedImage, seriesUid);
185 }
186 
187 void DicomWidget::loadIntoPatientModel(ImagePtr image, QString seriesUid)
188 {
189  ctkServiceTracker<PatientModelService*> tracker(mContext);
190  tracker.open();
191  PatientModelService* service = tracker.getService(); // get arbitrary instance of this type
192 
193  if (service)
194  {
195  service->insertData(image);
196  report(QString("Loaded DICOM series %1 as %2").arg(seriesUid).arg(image->getName()));
197  }
198  else
199  {
200  reportWarning(QString("Failed to load DICOM series %1 as %2: no PatientModelService.").arg(seriesUid).arg(image->getName()));
201  }
202 }
203 
204 ctkDICOMDatabase* DicomWidget::getDatabase() const
205 {
206  return mBrowser->database();
207 }
208 
209 } /* namespace cx */
#define CX_LOG_CHANNEL_INFO(channel)
Definition: cxLogger.h:121
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
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:130
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