Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
QmitkTransferFunctionCanvas.h
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 
13 #ifndef QmitkTransferFunctionCanvas_h
14 #define QmitkTransferFunctionCanvas_h
15 
17 
18 #include <mitkRenderingManager.h>
19 #include <mitkSimpleHistogram.h>
20 
21 #include <QLineEdit>
22 #include <QWidget>
23 
38 {
39  Q_OBJECT
40 
41 public:
47  QmitkTransferFunctionCanvas(QWidget *parent = nullptr, Qt::WindowFlags f = {});
48 
53  mitk::SimpleHistogram *GetHistogram() { return m_Histogram; }
54 
59  void SetHistogram(mitk::SimpleHistogram *histogram) { m_Histogram = histogram; }
60 
65  double GetMin() { return m_Min; }
66 
71  void SetMin(double min)
72  {
73  this->m_Min = min;
74  SetLower(min);
75  }
76 
81  double GetMax() { return m_Max; }
82 
87  void SetMax(double max)
88  {
89  this->m_Max = max;
90  SetUpper(max);
91  }
92 
97  double GetLower() { return m_Lower; }
98 
103  void SetLower(double lower) { this->m_Lower = lower; }
104 
109  double GetUpper() { return m_Upper; }
110 
115  void SetUpper(double upper) { this->m_Upper = upper; }
116 
118  void mousePressEvent(QMouseEvent *mouseEvent) override;
119 
121  void paintEvent(QPaintEvent *e) override;
122 
127  virtual void DoubleClickOnHandle(int handle) = 0;
128 
130  void mouseMoveEvent(QMouseEvent *mouseEvent) override;
131 
133  void mouseReleaseEvent(QMouseEvent *mouseEvent) override;
134 
136  void mouseDoubleClickEvent(QMouseEvent *mouseEvent) override;
137 
142  void PaintHistogram(QPainter &p);
143 
151  virtual int GetNearHandle(int x, int y, unsigned int maxSquaredDistance = 100) = 0;
152 
159  virtual int AddFunctionPoint(double x, double val) = 0;
160 
165  virtual void RemoveFunctionPoint(double x) = 0;
166 
172  virtual void MoveFunctionPoint(int index, std::pair<double, double> pos) = 0;
173 
179  virtual double GetFunctionX(int index) = 0;
180 
186  virtual double GetFunctionY(int index) = 0;
187 
192  virtual int GetFunctionSize() = 0;
193 
196 
197  double m_Lower;
198  double m_Upper;
199  double m_Min;
200  double m_Max;
201 
207  std::pair<int, int> FunctionToCanvas(std::pair<double, double> functionPoint);
208 
214  std::pair<double, double> CanvasToFunction(std::pair<int, int> canvasPoint);
215 
217 
219  void keyPressEvent(QKeyEvent *e) override;
220 
225  void SetImmediateUpdate(bool state);
226 
232  std::pair<double, double> ValidateCoord(std::pair<double, double> x)
233  {
234  double max = m_Histogram->GetMax();
235  double min = m_Histogram->GetMin();
236  if (x.first < min)
237  x.first = min;
238  if (x.first > max)
239  x.first = max;
240  if (x.second < 0)
241  x.second = 0;
242  if (x.second > 1)
243  x.second = 1;
244  return x;
245  }
246 
251  void SetX(float x)
252  {
253  if (m_GrabbedHandle != -1)
254  {
255  this->MoveFunctionPoint(m_GrabbedHandle, ValidateCoord(std::make_pair(x, GetFunctionY(m_GrabbedHandle))));
256  update();
258  }
259  }
260 
265  void SetY(float y)
266  {
267  if (m_GrabbedHandle != -1)
268  {
269  this->MoveFunctionPoint(m_GrabbedHandle, ValidateCoord(std::make_pair(GetFunctionX(m_GrabbedHandle), y)));
270  update();
272  }
273  }
274 
280  void SetQLineEdits(QLineEdit *xEdit, QLineEdit *yEdit)
281  {
282  m_XEdit = xEdit;
283  m_YEdit = yEdit;
284  m_LineEditAvailable = true;
285  }
286 
287 protected:
289  float m_Range;
290 
292  QLineEdit *m_XEdit;
293  QLineEdit *m_YEdit;
294 };
295 
296 #endif
#define MITKQTWIDGETSEXT_EXPORT
Abstract base class for transfer function editing canvases.
void SetMin(double min)
Set the minimum scalar value and update the lower display bound.
virtual int GetFunctionSize()=0
Get the number of function points.
double m_Min
Minimum data range value.
double GetMax()
Get the maximum scalar value of the data range.
double m_Max
Maximum data range value.
void SetUpper(double upper)
Set the upper display bound (visible range end).
virtual int AddFunctionPoint(double x, double val)=0
Add a function point at (x, val).
virtual double GetFunctionX(int index)=0
Get the x (scalar) value of a function point.
void PaintHistogram(QPainter &p)
Paint the histogram as a background overlay.
void SetMax(double max)
Set the maximum scalar value and update the upper display bound.
void SetX(float x)
Set the x coordinate of the currently grabbed handle.
void SetQLineEdits(QLineEdit *xEdit, QLineEdit *yEdit)
Connect QLineEdit widgets for displaying/editing the selected handle coordinates.
void paintEvent(QPaintEvent *e) override
Default paint event handler.
void SetY(float y)
Set the y coordinate of the currently grabbed handle.
virtual void MoveFunctionPoint(int index, std::pair< double, double > pos)=0
Move a function point to a new position.
void keyPressEvent(QKeyEvent *e) override
Handle keyboard input for point manipulation (Delete, arrow keys).
std::pair< double, double > ValidateCoord(std::pair< double, double > x)
Clamp coordinates to valid histogram range and [0, 1] for y.
void mouseMoveEvent(QMouseEvent *mouseEvent) override
Handle mouse move: drag the grabbed handle.
void mousePressEvent(QMouseEvent *mouseEvent) override
Handle mouse press: add or remove function points, select handles.
virtual void DoubleClickOnHandle(int handle)=0
Called when a handle is double-clicked (e.g. to open a color dialog).
QmitkTransferFunctionCanvas(QWidget *parent=nullptr, Qt::WindowFlags f={})
Construct the transfer function canvas.
double m_Lower
Lower display bound.
int m_GrabbedHandle
Index of the currently grabbed (selected) handle, or -1.
void SetHistogram(mitk::SimpleHistogram *histogram)
Set the histogram to display as background.
void SetImmediateUpdate(bool state)
Enable or disable immediate rendering updates during interaction.
double GetLower()
Get the lower display bound.
virtual int GetNearHandle(int x, int y, unsigned int maxSquaredDistance=100)=0
Find a handle near the given canvas position.
virtual void RemoveFunctionPoint(double x)=0
Remove the function point at position x.
void mouseDoubleClickEvent(QMouseEvent *mouseEvent) override
Handle double-click on a handle.
mitk::SimpleHistogram * GetHistogram()
Get the associated histogram.
double m_Upper
Upper display bound.
std::pair< double, double > CanvasToFunction(std::pair< int, int > canvasPoint)
Convert canvas pixel coordinates to function coordinates.
std::pair< int, int > FunctionToCanvas(std::pair< double, double > functionPoint)
Convert function coordinates to canvas pixel coordinates.
double GetMin()
Get the minimum scalar value of the data range.
virtual double GetFunctionY(int index)=0
Get the y (function) value of a function point.
void SetLower(double lower)
Set the lower display bound (visible range start).
mitk::SimpleHistogram * m_Histogram
The background histogram.
void mouseReleaseEvent(QMouseEvent *mouseEvent) override
Handle mouse release: trigger repaint.
double GetUpper()
Get the upper display bound.
static RenderingManager * GetInstance()
Get the RenderingManager singleton instance.
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
Request a deferred update for all registered render windows.
Abstract base class for simple histograms with double values.
virtual double GetMin() const =0
Returns the minimum value of the histogram.
virtual double GetMax() const =0
Returns the maximum value of the histogram.