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