Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmlMitkColorTransferFunctionCanvas.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 <QPainter>
20 #include <QColorDialog>
21 
23 QmlMitkTransferFunctionCanvas(parent), m_ColorTransferFunction(nullptr)
24 {
25 }
26 
28 {
29  m_Title=title;
30 }
31 
33 {
34  this->m_GreyValue = value;
35  this->SetX(value);
37 }
38 
40 {
41  return this->m_GreyValue;
42 }
43 
45 {
46  // Render gray background
47  QRectF contentsRect = this->boundingRect();
48  painter->setPen(Qt::gray);
49 
51  {
52  for (int x = contentsRect.x(); x < contentsRect.x() + contentsRect.width(); x++)
53  {
54  double xVal = m_Min + ((float) x) / contentsRect.width() * (m_Max
55  - m_Min);
56  QColor col((int) (m_ColorTransferFunction->GetRedValue(xVal) * 255),
57  (int) (m_ColorTransferFunction->GetGreenValue(xVal) * 255),
58  (int) (m_ColorTransferFunction->GetBlueValue(xVal) * 255));
59  painter->setPen(col);
60  painter->drawLine(x, 1, x, contentsRect.height());
61  }
62  }
63 
64  //paint title
65  if (m_Title.size()>0)
66  {
67  painter->setPen(Qt::black);
68  painter->drawText(QPoint(11,21),m_Title);
69  painter->setPen(Qt::white);
70  painter->drawText(QPoint(10,20),m_Title);
71  }
72 
73  //paint min and max
74  QString qs_min = QString::number( m_Min );
75  QString qs_max = QString::number( m_Max );
76 
77  QRect qr_min = painter->fontMetrics().boundingRect( qs_min );
78  QRect qr_max = painter->fontMetrics().boundingRect( qs_max );
79 
80  int y,x;
81 
82  y=this->boundingRect().height()-qr_min.height()+10;
83  x=10;
84 
85  painter->setPen(Qt::black);
86  painter->drawText(QPoint(x+1,y+1),qs_min);
87  painter->setPen(Qt::white);
88  painter->drawText(QPoint(x ,y ),qs_min);
89 
90  y=this->boundingRect().height()-qr_max.height()+10;
91  x=this->boundingRect().width()-qr_max.width()-6;
92 
93  painter->setPen(Qt::black);
94  painter->drawText(QPoint(x,y+1),qs_max);
95  painter->setPen(Qt::white);
96  painter->drawText(QPoint(x,y ),qs_max);
97 
98  if (m_ColorTransferFunction && this->isEnabled())
99  {
100  // now paint the handles
101  painter->setBrush(Qt::black);
102  painter->setPen(Qt::black);
103  for (int i = 0; i < this->GetFunctionSize(); i++)
104  {
105  int handleHeight = (i == m_GrabbedHandle) ? (int) (contentsRect.height() / 1.5)
106  : contentsRect.height() / 2;
107  int handleWidth = (i == m_GrabbedHandle) ? 6 : 4;
108  std::pair<int,int> point = this->FunctionToCanvas(std::make_pair(
109  GetFunctionX(i), 0.0f));
110  int y = height() / 2;
111  painter->drawRoundRect(point.first - handleWidth / 2,
112  y - handleHeight / 2, handleWidth, handleHeight, 50, 50);
113 
114  if (i == m_GrabbedHandle)
115  {
116  this->m_GreyValue = QString::number(GetFunctionX(m_GrabbedHandle), 'g', 4).toFloat();
117  emit this->sync();
118  }
119  }
120  }
121 }
122 
124  unsigned int maxSquaredDistance)
125 {
126  for (int i = 0; i < this->GetFunctionSize(); i++)
127  {
128  std::pair<int,int> point = this->FunctionToCanvas(std::make_pair(
129  GetFunctionX(i), (double) 0.0));
130  if ((unsigned int) ((point.first - x) * (point.first - x))
131  < maxSquaredDistance)
132  {
133  return i;
134  }
135  }
136  return -1;
137 }
138 
140 {
141  double xVal = GetFunctionX(handle);
142  QColor col((int) (m_ColorTransferFunction->GetRedValue(xVal) * 255),
143  (int) (m_ColorTransferFunction->GetGreenValue(xVal) * 255),
144  (int) (m_ColorTransferFunction->GetBlueValue(xVal) * 255));
145  QColor result = QColorDialog::getColor(col);
146  if (result.isValid())
147  {
148  m_ColorTransferFunction->AddRGBPoint(xVal, result.red() / 255.0,
149  result.green() / 255.0, result.blue() / 255.0);
150  this->update();
152  }
153 }
154 
156  std::pair<double,double> pos)
157 {
158  double color[3];
159  m_ColorTransferFunction->GetColor(GetFunctionX(index), color);
161  m_ColorTransferFunction->AddRGBPoint(pos.first, color[0], color[1], color[2]);
162 }
163 
164 void QmlMitkColorTransferFunctionCanvas::AddRGB(double x, double r, double g,
165  double b)
166 {
167  m_ColorTransferFunction->AddRGBPoint(x, r, g, b);
168 }
169 
171 {
172  qmlRegisterType<QmlMitkColorTransferFunctionCanvas>("Mitk.Views", 1, 0, "ColorItem");
173 }
void AddRGB(double x, double r, double g, double b)
QmlMitkColorTransferFunctionCanvas(QQuickPaintedItem *parent=nullptr)
std::pair< int, int > FunctionToCanvas(std::pair< double, double >)
int GetNearHandle(int x, int y, unsigned int maxSquaredDistance=32) override
static RenderingManager * GetInstance()
void MoveFunctionPoint(int index, std::pair< double, double > pos) override
virtual void paint(QPainter *painter) override
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)