Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkC3jsWidget.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include <QmitkC3jsWidget.h>
18 #include <QGridLayout>
19 #include <QWebChannel>
20 #include <QWebEngineView>
21 
22 #include <QmitkC3Data.h>
23 
24 #include <mitkIntensityProfile.h>
25 
26 #include "mitkImageTimeSelector.h"
27 
28 class QmitkC3jsWidget::Impl final
29 {
30 public:
31  explicit Impl(QWidget* parent);
32  ~Impl();
33 
34  Impl(const Impl&) = delete;
35  Impl& operator=(const Impl&) = delete;
36 
37  QWebChannel* GetWebChannel();
38 
39  void ClearJavaScriptChart();
40  void initializeJavaScriptChart();
41  void callJavaScriptFuntion(QString command);
42 
43  QmitkC3Data* GetC3Data() { return &m_c3Data; };
44 
45  mitk::Image::Pointer GetImage() { return m_Image; }; const
46  void SetImage(const mitk::Image::Pointer image) { m_Image = image; };
47 
48  mitk::PlanarFigure::ConstPointer GetPlanarFigure() { return m_PlanarFigure; }; const
49  void SetPlanarFigure(const mitk::PlanarFigure::ConstPointer planarFigure) { m_PlanarFigure = planarFigure; };
50 
51 private:
52  QWidget* m_Parent;
53  QWebChannel* m_WebChannel;
54  QWebEngineView* m_WebEngineView;
55 
56  QmitkC3Data m_c3Data;
57 
63  mitk::Image::Pointer m_Image;
64 
70  mitk::PlanarFigure::ConstPointer m_PlanarFigure;
71 };
72 
73 QmitkC3jsWidget::Impl::Impl(QWidget* parent)
74  : m_WebChannel(new QWebChannel(parent)),
75  m_WebEngineView(new QWebEngineView(parent)),
76  m_Parent(parent)
77 {
78  //disable context menu for QWebEngineView
79  m_WebEngineView->setContextMenuPolicy(Qt::NoContextMenu);
80 
81  //Set the webengineview to an initial empty page. The actual chart will be loaded once the data is calculated.
82  m_WebEngineView->setUrl(QUrl(QStringLiteral("qrc:///C3js/empty.html")));
83  m_WebEngineView->page()->setWebChannel(m_WebChannel);
84 
85  connect( m_WebEngineView, SIGNAL( loadFinished(bool) ), parent, SLOT( OnLoadFinished(bool) ) );
86 
87  auto layout = new QGridLayout(parent);
88  layout->setMargin(0);
89  layout->addWidget(m_WebEngineView);
90 
91  parent->setLayout(layout);
92 }
93 
94 QmitkC3jsWidget::Impl::~Impl()
95 {
96 }
97 
98 QWebChannel* QmitkC3jsWidget::Impl::GetWebChannel()
99 {
100  return m_WebChannel;
101 }
102 
104  : QWidget(parent),
105  m_Impl(new Impl(this))
106 {
108 }
109 
110 void QmitkC3jsWidget::Impl::callJavaScriptFuntion(QString command)
111 {
112  m_WebEngineView->page()->runJavaScript(command);
113 }
114 
115 void QmitkC3jsWidget::Impl::ClearJavaScriptChart()
116 {
117  m_WebEngineView->setUrl(QUrl(QStringLiteral("qrc:///C3js/empty.html")));
118 }
119 
120 void QmitkC3jsWidget::Impl::initializeJavaScriptChart()
121 {
122  m_WebChannel->registerObject(QStringLiteral("initValues"), &m_c3Data);
123  m_WebEngineView->load(QUrl(QStringLiteral("qrc:///C3js/QmitkC3jsWidget.html")));
124 }
125 
126 QmitkC3jsWidget::QmitkC3jsWidget(const QString& id, QObject* object, QWidget* parent)
127  : QWidget(parent),
128  m_Impl(new Impl(this))
129 {
130  if (!id.isEmpty() && object != nullptr)
131  m_Impl->GetWebChannel()->registerObject(id, object);
132 
134 }
135 
137 {
138  delete m_Impl;
139 }
140 
141 void QmitkC3jsWidget::OnLoadFinished(bool isLoadSuccessfull)
142 {
143  emit PageSuccessfullyLoaded();
144 }
145 
146 void QmitkC3jsWidget::TransformView(QString transformTo)
147 {
148  QString command = QString("transformView('" + transformTo + "')");
149  m_Impl->callJavaScriptFuntion(command);
150 }
151 
152 void QmitkC3jsWidget::SendCommand(QString command)
153 {
154  m_Impl->callJavaScriptFuntion(command);
155 }
156 
157 void QmitkC3jsWidget::SetAppearance(bool useLineChart, bool showSubChart)
158 {
159  this->m_Impl->GetC3Data()->SetAppearance(useLineChart, showSubChart);
160 }
161 
162 // method to expose data to JavaScript by using properties
163 void QmitkC3jsWidget::ComputeHistogram(HistogramType* histogram, bool useLineChart, bool showSubChart)
164 {
165  this->m_Impl->GetC3Data()->SetHistogram(histogram);
166 
167  SetAppearance(useLineChart, showSubChart);
168  HistogramConstIteratorType startIt = this->m_Impl->GetC3Data()->GetHistogram()->End();
169  HistogramConstIteratorType endIt = this->m_Impl->GetC3Data()->GetHistogram()->End();
170  HistogramConstIteratorType it = this->m_Impl->GetC3Data()->GetHistogram()->Begin();
171 
172 
173 
174  //Clear old data befor loading new data.
175  this->m_Impl->GetC3Data()->ClearData();
176 
177  unsigned int i = 0;
178  bool firstValue = false;
179  // removes frequencies of 0, which are outside the first and last bin
180  for (; it != this->m_Impl->GetC3Data()->GetHistogram()->End(); ++it)
181  {
182  if (it.GetFrequency() > 0.0)
183  {
184  endIt = it;
185  if (!firstValue)
186  {
187  firstValue = true;
188  startIt = it;
189  }
190  }
191  }
192  ++endIt;
193  // generating Lists of measurement and frequencies
194  for (it = startIt; it != endIt; ++it, ++i)
195  {
196  QVariant frequency = QVariant::fromValue(it.GetFrequency());
197  QVariant measurement = it.GetMeasurementVector()[0];
198  this->m_Impl->GetC3Data()->GetYDataPointer()->insert(i, frequency);
199  this->m_Impl->GetC3Data()->GetXDataPointer()->insert(i, measurement);
200  }
201 
202  m_Impl->initializeJavaScriptChart();
203 }
204 
205 void QmitkC3jsWidget::ComputeIntensityProfile(unsigned int timeStep, bool computeStatistics)
206 {
207  this->ClearHistogram();
208  //m_Impl->GetC3Data()->ClearData();
209  //m_ParametricPath->Initialize();
210 
211  if (m_Impl->GetPlanarFigure().IsNull())
212  {
213  mitkThrow() << "PlanarFigure not set!";
214  }
215 
216  if (m_Impl->GetImage().IsNull())
217  {
218  mitkThrow() << "Image not set!";
219  }
220 
221  mitk::Image::Pointer image;
222 
223  if (m_Impl->GetImage()->GetDimension() == 4)
224  {
226  timeSelector->SetInput(m_Impl->GetImage());
227  timeSelector->SetTimeNr(timeStep);
228  timeSelector->Update();
229 
230  image = timeSelector->GetOutput();
231  }
232  else
233  {
234  image = m_Impl->GetImage();
235  }
236 
238  image, const_cast<mitk::PlanarFigure*>(m_Impl->GetPlanarFigure().GetPointer()));
239 
240  //m_Frequency.clear();
241  //m_Measurement.clear();
242 
243  int i = -1;
244  mitk::IntensityProfile::ConstIterator end = intensityProfile->End();
245 
246  for (mitk::IntensityProfile::ConstIterator it = intensityProfile->Begin(); it != end; ++it)
247  {
248  m_Impl->GetC3Data()->GetYDataPointer()->push_back(it.GetMeasurementVector()[0]);
249  //m_Impl->GetC3Data()->GetFrequencyPointer()->push_back(50000);
250  m_Impl->GetC3Data()->GetXDataPointer()->push_back(++i);
251  }
252 
253  if (computeStatistics)
254  {
255  mitk::ComputeIntensityProfileStatistics(intensityProfile, m_Statistics);
256  }
257 
258  m_Impl->initializeJavaScriptChart();
259 }
260 
262 {
263  m_Impl->GetC3Data()->ClearData();
264  m_Impl->ClearJavaScriptChart();
265 }
266 
268 {
269  return m_Impl->GetImage();
270 }
271 
273 {
274  m_Impl->SetImage(image);
275 }
276 
278 {
279  return m_Impl->GetPlanarFigure();
280 }
281 
283 {
284  m_Impl->SetPlanarFigure(planarFigure);
285 }
itk::SmartPointer< Self > Pointer
void SetAppearance(bool useLineChart, bool showSubChart)
void ComputeIntensityProfile(unsigned int timeStep, bool computeStatistics)
mitk::Image::HistogramType HistogramType
MITKIMAGESTATISTICS_EXPORT IntensityProfile::Pointer ComputeIntensityProfile(Image::Pointer image, PlanarFigure::Pointer planarFigure)
Compute intensity profile of an image for each pixel along the first PolyLine of a given planar figur...
void ComputeHistogram(HistogramType *histogram, bool useLineChart, bool showSubChart)
Calculates the histogram.
mitk::PlanarFigure::ConstPointer GetPlanarFigure() const
mitk::Image::HistogramType::ConstIterator HistogramConstIteratorType
void TransformView(QString transformTo)
virtual ~QmitkC3jsWidget()
void SetPlanarFigure(const mitk::PlanarFigure::ConstPointer planarFigure)
void PageSuccessfullyLoaded()
void OnLoadFinished(bool isLoadSuccessfull)
mitk::Image::Pointer GetImage() const
#define mitkThrow()
void SendCommand(QString command)
MITKIMAGESTATISTICS_EXPORT void ComputeIntensityProfileStatistics(IntensityProfile::Pointer intensityProfile, ImageStatisticsCalculator::StatisticsContainer::Pointer stats)
Compute statistics of an intensity profile.
QmitkC3jsWidget(QWidget *parent=nullptr)
void SetImage(const mitk::Image::Pointer image)
static Pointer New()