Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkColorTransferFunctionCanvas.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include <QColorDialog>
16 #include <QLineEdit>
17 #include <QPainter>
18 
20  : QmitkTransferFunctionCanvas(parent, f), m_ColorTransferFunction(nullptr)
21 {
22  // used for drawing a border
23  setContentsMargins(1, 1, 1, 1);
24 }
25 
27 {
28  m_Title = title;
29 }
30 
32 {
33  QPainter painter(this);
34 
35  // Render gray background
36  QRect contentsRect = this->contentsRect();
37  painter.setPen(Qt::gray);
38  painter.drawRect(0, 0, contentsRect.width() + 1, contentsRect.height() + 1);
39 
40  if (!this->isEnabled())
41  return;
42 
44  {
45  for (int x = contentsRect.x(); x < contentsRect.x() + contentsRect.width(); x++)
46  {
47  double xVal = m_Min + ((float)x) / contentsRect.width() * (m_Max - m_Min);
48  QColor col((int)(m_ColorTransferFunction->GetRedValue(xVal) * 255),
49  (int)(m_ColorTransferFunction->GetGreenValue(xVal) * 255),
50  (int)(m_ColorTransferFunction->GetBlueValue(xVal) * 255));
51  painter.setPen(col);
52  painter.drawLine(x, 1, x, contentsRect.height());
53  }
54  }
55 
56  // paint title
57  if (m_Title.size() > 0)
58  {
59  painter.setPen(Qt::black);
60  painter.drawText(QPoint(11, 21), m_Title);
61  painter.setPen(Qt::white);
62  painter.drawText(QPoint(10, 20), m_Title);
63  }
64 
65  // paint min and max
66  QString qs_min = QString::number(m_Min);
67  QString qs_max = QString::number(m_Max);
68 
69  QRect qr_min = painter.fontMetrics().boundingRect(qs_min);
70  QRect qr_max = painter.fontMetrics().boundingRect(qs_max);
71 
72  int y, x;
73 
74  y = this->contentsRect().height() - qr_min.height() + 5;
75  x = 10;
76 
77  painter.setPen(Qt::black);
78  painter.drawText(QPoint(x + 1, y + 1), qs_min);
79  painter.setPen(Qt::white);
80  painter.drawText(QPoint(x, y), qs_min);
81 
82  y = this->contentsRect().height() - qr_max.height() + 5;
83  x = this->contentsRect().width() - qr_max.width() - 6;
84 
85  painter.setPen(Qt::black);
86  painter.drawText(QPoint(x, y + 1), qs_max);
87  painter.setPen(Qt::white);
88  painter.drawText(QPoint(x, y), qs_max);
89 
91  {
92  // now paint the handles
93  painter.setBrush(Qt::black);
94  painter.setPen(Qt::black);
95  for (int i = 0; i < this->GetFunctionSize(); i++)
96  {
97  int handleHeight = (i == m_GrabbedHandle) ? (int)(contentsRect.height() / 1.5) : contentsRect.height() / 2;
98  int handleWidth = (i == m_GrabbedHandle) ? 6 : 4;
99  std::pair<int, int> point = this->FunctionToCanvas(std::make_pair(GetFunctionX(i), 0.0f));
100  int y = height() / 2;
101  painter.drawRoundRect(point.first - handleWidth / 2, y - handleHeight / 2, handleWidth, handleHeight, 50, 50);
102 
104  {
105  int xCursor = m_XEdit->cursorPosition();
106  m_XEdit->setText(QString::number(GetFunctionX(m_GrabbedHandle), 'g', 4));
107  m_XEdit->setCursorPosition(xCursor);
108  }
109  }
110  }
111 }
112 
113 int QmitkColorTransferFunctionCanvas::GetNearHandle(int x, int, unsigned int maxSquaredDistance)
114 {
115  for (int i = 0; i < this->GetFunctionSize(); i++)
116  {
117  std::pair<int, int> point = this->FunctionToCanvas(std::make_pair(GetFunctionX(i), (double)0.0));
118  if ((unsigned int)((point.first - x) * (point.first - x)) < maxSquaredDistance)
119  {
120  return i;
121  }
122  }
123  return -1;
124 }
125 
127 {
128  double xVal = GetFunctionX(handle);
129  QColor col((int)(m_ColorTransferFunction->GetRedValue(xVal) * 255),
130  (int)(m_ColorTransferFunction->GetGreenValue(xVal) * 255),
131  (int)(m_ColorTransferFunction->GetBlueValue(xVal) * 255));
132  QColor result = QColorDialog::getColor(col);
133  if (result.isValid())
134  {
135  m_ColorTransferFunction->AddRGBPoint(xVal, result.red() / 255.0, result.green() / 255.0, result.blue() / 255.0);
136  this->update();
138  }
139 }
140 
141 void QmitkColorTransferFunctionCanvas::MoveFunctionPoint(int index, std::pair<double, double> pos)
142 {
143  double color[3];
144  m_ColorTransferFunction->GetColor(GetFunctionX(index), color);
146  m_ColorTransferFunction->AddRGBPoint(pos.first, color[0], color[1], color[2]);
147 }
148 
149 void QmitkColorTransferFunctionCanvas::AddRGB(double x, double r, double g, double b)
150 {
151  m_ColorTransferFunction->AddRGBPoint(x, r, g, b);
152 }
int GetNearHandle(int x, int y, unsigned int maxSquaredDistance=32) override
void MoveFunctionPoint(int index, std::pair< double, double > pos) override
void AddRGB(double x, double r, double g, double b)
static RenderingManager * GetInstance()
QmitkColorTransferFunctionCanvas(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
std::pair< int, int > FunctionToCanvas(std::pair< double, double >)
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)