Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QmlMitkPiecewiseFunctionCanvas.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 
22 
24 QmlMitkTransferFunctionCanvas(parent), m_PiecewiseFunction(nullptr)
25 {
26  instance = this;
27  setAcceptedMouseButtons(Qt::AllButtons);
28 }
29 
31 {
32  this->m_GreyValue = value;
33  this->SetX(value);
35 }
36 
38 {
39  return this->m_GreyValue;
40 }
41 
43 {
44  this->m_Opacity = opacity;
45  this->SetY(opacity);
47 }
48 
50 {
51  return this->m_Opacity;
52 }
53 
54 void QmlMitkPiecewiseFunctionCanvas::SetTitle(const QString& title)
55 {
56  m_Title=title;
57 }
58 
60 {
61  PaintHistogram(painter);
62 
63  if (m_Title.size()>0)
64  {
65  painter->setPen(Qt::black);
66  painter->drawText(QPoint(11,21),m_Title);
67  painter->setPen(Qt::white);
68  painter->drawText(QPoint(10,20),m_Title);
69  }
70 
71  {
72 
73  QString qs_min = QString::number( m_Min, 'g', 4 );
74  QString qs_max = QString::number( m_Max, 'g', 4 );
75 
76  QRect qr_min = painter->fontMetrics().boundingRect( qs_min );
77  QRect qr_max = painter->fontMetrics().boundingRect( qs_max );
78 
79  int y,x;
80 
81  y=this->boundingRect().height()-qr_min.height()+5;
82  //y = this->boundingRect().height()+5;
83  x=10;
84 
85  // Fill the tf presets in the generator widget
86  /*
87  painter->setPen(Qt::black);
88  painter->drawText(QPoint(x+1,y+1),qs_min);
89  painter->setPen(Qt::white);
90  painter->drawText(QPoint(x ,y ),qs_min);
91 
92  y=this->boundingRect().height()-qr_max.height()+5;
93  x=this->boundingRect().width()-qr_max.width()-6;
94 
95  painter->setPen(Qt::black);
96  painter->drawText(QPoint(x,y+1),qs_max);
97  painter->setPen(Qt::white);
98  painter->drawText(QPoint(x,y ),qs_max);
99  */
100  }
101 
102  painter->setPen(Qt::gray);
103 
104  QRectF contentsRect = this->boundingRect();
105  //painter->drawRect(0, 0, contentsRect.width()+1, contentsRect.height()+1);
106  if (m_PiecewiseFunction && this->isEnabled())
107  {
108  double* dp = m_PiecewiseFunction->GetDataPointer();
109 
110  // Render lines
111 
112  painter->setPen(Qt::black);
113 
114  for (int i = -1; i < m_PiecewiseFunction->GetSize(); i++)
115  {
116  std::pair<int,int> left;
117  std::pair<int,int> right;
118 
119  if(i < 0)
120  left = this->FunctionToCanvas(std::make_pair(-32768, dp[0 * 2 + 1]));
121  else
122  left = this->FunctionToCanvas(std::make_pair(dp[i * 2], dp[i * 2 + 1]));
123 
124  if(i+1 >= m_PiecewiseFunction->GetSize())
125  right = this->FunctionToCanvas(std::make_pair(32768, dp[(i ) * 2 + 1]));
126  else
127  right = this->FunctionToCanvas(std::make_pair(dp[(i+1) * 2], dp[(i+1) * 2 + 1]));
128 
129  painter->drawLine(left.first, left.second, right.first, right.second);
130  }
131 
132  // Render Points
133 
134  for (int i = 0; i < m_PiecewiseFunction->GetSize(); i++)
135  {
136  std::pair<int,int> point = this->FunctionToCanvas(std::make_pair(
137  dp[i * 2], dp[i * 2 + 1]));
138 
139  if (i == m_GrabbedHandle)
140  {
141  painter->setBrush(QBrush(Qt::red));
142 
143  this->m_Opacity = QString::number(GetFunctionY(m_GrabbedHandle), 'g', 4).toFloat();
144  this->m_GreyValue = QString::number(GetFunctionX(m_GrabbedHandle), 'g', 4).toFloat();
145 
146  emit this->sync();
147 
148  }
149  else
150  {
151  painter->setBrush(QBrush(Qt::green));
152  }
153  painter->drawEllipse(point.first - 4, point.second - 4, 8, 8);
154  }
155 
156  painter->setBrush(Qt::NoBrush);
157  }
158 }
159 
161  unsigned int maxSquaredDistance)
162 {
163  double* dp = m_PiecewiseFunction->GetDataPointer();
164  for (int i = 0; i < m_PiecewiseFunction->GetSize(); i++)
165  {
166  std::pair<int,int> point = this->FunctionToCanvas(std::make_pair(dp[i * 2],
167  dp[i * 2 + 1]));
168  if ((unsigned int) ((point.first - x) * (point.first - x) + (point.second
169  - y) * (point.second - y)) <= maxSquaredDistance)
170  {
171  return i;
172 
173  }
174  }
175  return -1;
176 }
177 
179  std::pair<double,double> pos)
180 {
182  m_GrabbedHandle = AddFunctionPoint(pos.first, pos.second);
183 }
184 
185 void QmlMitkPiecewiseFunctionCanvas::mousePressEvent( QMouseEvent* mouseEvent )
186 {
188 }
189 
190 void QmlMitkPiecewiseFunctionCanvas::mouseMoveEvent( QMouseEvent* mouseEvent )
191 {
193 }
194 
196 {
198 }
199 
201 {
203 }
204 
206 {
208 }
209 
211 {
212  qmlRegisterType<QmlMitkPiecewiseFunctionCanvas>("Mitk.Views", 1, 0, "PiecewiseItem");
213 }
void mousePressEvent(QMouseEvent *mouseEvent) override
void mouseMoveEvent(QMouseEvent *mouseEvent) override
void mouseDoubleClickEvent(QMouseEvent *mouseEvent) override
void paint(QPainter *painter) override
void mouseDoubleClickEvent(QMouseEvent *mouseEvent) override
void mouseMoveEvent(QMouseEvent *mouseEvent) override
static QmlMitkPiecewiseFunctionCanvas * instance
int GetNearHandle(int x, int y, unsigned int maxSquaredDistance=32) override
std::pair< int, int > FunctionToCanvas(std::pair< double, double >)
void mousePressEvent(QMouseEvent *mouseEvent) override
void MoveFunctionPoint(int index, std::pair< double, double > pos) override
void mouseReleaseEvent(QMouseEvent *mouseEvent) override
void mouseReleaseEvent(QMouseEvent *mouseEvent) override
static RenderingManager * GetInstance()
void keyPressEvent(QKeyEvent *event) override
void keyPressEvent(QKeyEvent *keyEvent) override
QmlMitkPiecewiseFunctionCanvas(QQuickPaintedItem *parent=nullptr)
int AddFunctionPoint(double x, double val) override
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)