Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkPiecewiseFunctionCanvas.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 
18 
19 #include <QLineEdit>
20 #include <QPainter>
21 
23  : QmitkTransferFunctionCanvas(parent, f), m_PiecewiseFunction(nullptr)
24 {
25  // used for drawing a border
26  setContentsMargins(1, 1, 1, 1);
27 }
28 
29 void QmitkPiecewiseFunctionCanvas::SetTitle(const QString &title)
30 {
31  m_Title = title;
32 }
33 
35 {
36  QPainter painter(this);
37 
38  PaintHistogram(painter);
39 
40  if (m_Title.size() > 0)
41  {
42  painter.setPen(Qt::black);
43  painter.drawText(QPoint(11, 21), m_Title);
44  painter.setPen(Qt::white);
45  painter.drawText(QPoint(10, 20), m_Title);
46  }
47 
48  {
49  QString qs_min = QString::number(m_Min, 'g', 4);
50  QString qs_max = QString::number(m_Max, 'g', 4);
51 
52  QRect qr_min = painter.fontMetrics().boundingRect(qs_min);
53  QRect qr_max = painter.fontMetrics().boundingRect(qs_max);
54 
55  int y, x;
56 
57  y = this->contentsRect().height() - qr_min.height() + 5;
58  x = 10;
59 
60  // Fill the tf presets in the generator widget
61 
62  painter.setPen(Qt::black);
63  painter.drawText(QPoint(x + 1, y + 1), qs_min);
64  painter.setPen(Qt::white);
65  painter.drawText(QPoint(x, y), qs_min);
66 
67  y = this->contentsRect().height() - qr_max.height() + 5;
68  x = this->contentsRect().width() - qr_max.width() - 6;
69 
70  painter.setPen(Qt::black);
71  painter.drawText(QPoint(x, y + 1), qs_max);
72  painter.setPen(Qt::white);
73  painter.drawText(QPoint(x, y), qs_max);
74  }
75 
76  painter.setPen(Qt::gray);
77 
78  QRect contentsRect = this->contentsRect();
79  painter.drawRect(0, 0, contentsRect.width() + 1, contentsRect.height() + 1);
80  if (m_PiecewiseFunction && this->isEnabled())
81  {
82  double *dp = m_PiecewiseFunction->GetDataPointer();
83 
84  // Render lines
85 
86  painter.setPen(Qt::black);
87 
88  for (int i = -1; i < m_PiecewiseFunction->GetSize(); i++)
89  {
90  std::pair<int, int> left;
91  std::pair<int, int> right;
92 
93  if (i < 0)
94  left = this->FunctionToCanvas(std::make_pair(-32768, dp[0 * 2 + 1]));
95  else
96  left = this->FunctionToCanvas(std::make_pair(dp[i * 2], dp[i * 2 + 1]));
97 
98  if (i + 1 >= m_PiecewiseFunction->GetSize())
99  right = this->FunctionToCanvas(std::make_pair(32768, dp[(i)*2 + 1]));
100  else
101  right = this->FunctionToCanvas(std::make_pair(dp[(i + 1) * 2], dp[(i + 1) * 2 + 1]));
102 
103  painter.drawLine(left.first, left.second, right.first, right.second);
104  }
105 
106  // Render Points
107 
108  for (int i = 0; i < m_PiecewiseFunction->GetSize(); i++)
109  {
110  std::pair<int, int> point = this->FunctionToCanvas(std::make_pair(dp[i * 2], dp[i * 2 + 1]));
111 
112  if (i == m_GrabbedHandle)
113  {
114  painter.setBrush(QBrush(Qt::red));
116  {
117  int xCursor = m_XEdit->cursorPosition();
118  int yCursor = m_YEdit->cursorPosition();
119  m_XEdit->setText(QString::number(GetFunctionX(m_GrabbedHandle), 'g', 4));
120  m_YEdit->setText(QString::number(GetFunctionY(m_GrabbedHandle), 'g', 4));
121  m_XEdit->setCursorPosition(xCursor);
122  m_YEdit->setCursorPosition(yCursor);
123  }
124  }
125  else
126  {
127  painter.setBrush(QBrush(Qt::green));
128  }
129  painter.drawEllipse(point.first - 4, point.second - 4, 8, 8);
130  }
131 
132  painter.setBrush(Qt::NoBrush);
133  }
134 }
135 
136 int QmitkPiecewiseFunctionCanvas::GetNearHandle(int x, int y, unsigned int maxSquaredDistance)
137 {
138  double *dp = m_PiecewiseFunction->GetDataPointer();
139  for (int i = 0; i < m_PiecewiseFunction->GetSize(); i++)
140  {
141  std::pair<int, int> point = this->FunctionToCanvas(std::make_pair(dp[i * 2], dp[i * 2 + 1]));
142  if ((unsigned int)((point.first - x) * (point.first - x) + (point.second - y) * (point.second - y)) <=
143  maxSquaredDistance)
144  {
145  return i;
146  }
147  }
148  return -1;
149 }
150 
151 void QmitkPiecewiseFunctionCanvas::MoveFunctionPoint(int index, std::pair<double, double> pos)
152 {
154  m_GrabbedHandle = AddFunctionPoint(pos.first, pos.second);
155 }
double GetFunctionY(int index) override
virtual void paintEvent(QPaintEvent *e) override
double GetFunctionX(int index) override
int GetNearHandle(int x, int y, unsigned int maxSquaredDistance=32) override
QmitkPiecewiseFunctionCanvas(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
void MoveFunctionPoint(int index, std::pair< double, double > pos) override
void RemoveFunctionPoint(double x) override
int AddFunctionPoint(double x, double val) override
std::pair< int, int > FunctionToCanvas(std::pair< double, double >)