Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkScalarBar.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 
13 #include "QmitkScalarBar.h"
14 
15 #include <QPaintEvent>
16 #include <QPainter>
17 
19  : QWidget(parent, Qt::Tool | Qt::FramelessWindowHint), m_Alignment(vertical), m_MainLine(nullptr)
20 {
22 
23  this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
25  this->setBackgroundRole(QPalette::Base);
26  this->setAttribute(Qt::WA_TranslucentBackground, true);
27  this->setAutoFillBackground(false);
28 
29  // X11 specific attributes
30  this->setAttribute(Qt::WA_X11NetWmWindowTypeUtility, true);
31 
32  // mac-specific attributes:
33  // making sure overlays are even visible if RenderWindow does not have the focus (not default for Qt::Tool on mac)
34  this->setAttribute(Qt::WA_MacAlwaysShowToolWindow, true);
35  // testing something
36  this->setAttribute(Qt::WA_MacShowFocusRect, false);
37 
38  this->resize(10, 61);
39  this->setFixedWidth(10);
40  this->setFixedHeight(61);
41 
42  m_Pen = QPen(Qt::red, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
43 }
44 
46 {
47  CleanUpLines();
48 }
49 
51 {
52  this->CleanUpLines();
53 
54  switch (align)
55  {
56  case vertical:
57  {
58  // draw subdivision
59  for (unsigned int i = 0; i < m_NumberOfSubDivisions; ++i)
60  {
61  int y = this->height() / (m_NumberOfSubDivisions - 1) * i;
62  if (i == 0)
63  {
64  // this is the first one -> move y 1 down to have this line completely drawn
65  y = 1;
66  }
67  else if (i == m_NumberOfSubDivisions - 1)
68  {
69  // this is the last one -> move y 1 up to have this line completely drawn
70  y = this->height() - 1;
71  }
72  m_SubDivisionLines.push_back(new QLine(QPoint(0, y), QPoint(width() - 2, y)));
73  }
74 
75  // draw mainline
76  if (m_SubDivisionLines.size() > 0)
77  {
78  m_MainLine = new QLine(QPoint(width() - 1, 0), QPoint(width() - 1, height()));
79  }
80  else
81  {
82  m_MainLine = new QLine(QPoint(0, 0), QPoint(0, 0)); // do not draw the line
83  }
84 
85  break;
86  }
87  case horizontal:
88  {
89  // draw subdivision
90  for (unsigned int i = 0; i < m_NumberOfSubDivisions; ++i)
91  {
92  int x = this->width() / (m_NumberOfSubDivisions - 1) * i;
93  if (i == 0)
94  {
95  x = 1;
96  }
97  else if (i == m_NumberOfSubDivisions - 1)
98  {
99  x = this->width() - 1;
100  }
101  m_SubDivisionLines.push_back(new QLine(QPoint(x, 0), QPoint(x, height())));
102  }
103 
104  // draw mainline
105  if (m_SubDivisionLines.size() > 0)
106  {
107  m_MainLine = new QLine(QPoint(0, height() / 2), QPoint(width(), height() / 2));
108  }
109  else
110  {
111  m_MainLine = new QLine(QPoint(0, 0), QPoint(0, 0)); // do not draw the line
112  }
113 
114  break;
115  }
116  }
117 }
118 
120 {
121  foreach (QLine *line, m_SubDivisionLines)
122  {
123  delete line; // QLine is not a QObject
124  line = nullptr;
125  }
126 
127  m_SubDivisionLines.clear();
128 
129  if (m_MainLine != nullptr)
130  {
131  delete m_MainLine;
132  m_MainLine = nullptr;
133  }
134 }
135 
137 {
138  m_ScaleFactor = scale;
139 
140  // Adopt the number of small, intersecting lines to the size of the widget.
141  if (this->parentWidget() != nullptr && this->parentWidget()->parentWidget() != nullptr)
142  {
143  // If the widget is larger than 80% of the size of the parent -> reduce number by two (must not be smaller than 3)
144  if (this->height() > this->parentWidget()->parentWidget()->height() * 0.7 && m_NumberOfSubDivisions >= 3)
145  {
147  }
148  // If the widget is smaller than 30% of the size of the parent -> increase number by two
149  else if (this->height() < this->parentWidget()->parentWidget()->height() * 0.4 &&
150  (m_NumberOfSubDivisions < 7 && m_NumberOfSubDivisions > 0))
151  {
153  }
154 
155  if (m_NumberOfSubDivisions == 1)
156  {
157  this->resize(0, 0);
158  this->setFixedWidth(0);
159  this->setFixedHeight(0);
160  }
161  else
162  {
163  this->resize(10, (m_NumberOfSubDivisions - 1) * 10 / m_ScaleFactor);
164  this->setFixedWidth(10);
165  this->setFixedHeight((m_NumberOfSubDivisions - 1) * 10 / m_ScaleFactor);
166  this->SetupGeometry(m_Alignment);
167  }
168 
169  if (this->height() > this->parentWidget()->parentWidget()->height() * 0.7 && m_NumberOfSubDivisions >= 3)
170  SetScaleFactor(scale);
171  }
172 }
173 
175 {
176  m_NumberOfSubDivisions = subs;
177 }
178 
180 {
181  return m_NumberOfSubDivisions;
182 }
183 
184 void QmitkScalarBar::paintEvent(QPaintEvent * /*event*/)
185 {
186  if (m_NumberOfSubDivisions > 1)
187  {
188  try
189  {
190  // QPainter shadowPainter( this );
191  // shadowPainter.setPen( QPen( QColor(0,0,0,255) ) );
192  // shadowPainter.setBrush( Qt::SolidPattern );
193  // shadowPainter.setRenderHint( QPainter::Antialiasing, true );
194 
195  // shadowPainter.drawLine( m_VerticalLine->p1()+QPoint(1,1), m_VerticalLine->p2()+QPoint(1,1) );
196  // foreach( QLine* line, m_HorizontalLines )
197  //{
198  // shadowPainter.drawLine( line->p1()+QPoint(1,1), line->p2()+QPoint(1,1) );
199  //}
200 
201  QPainter painter(this);
202  painter.setPen(m_Pen);
203  painter.setBrush(Qt::SolidPattern);
204  painter.setRenderHint(QPainter::Antialiasing, true);
205 
206  painter.drawLine(m_MainLine->p1(), m_MainLine->p2());
207 
208  foreach (QLine *line, m_SubDivisionLines)
209  {
210  painter.drawLine(line->p1(), line->p2());
211  }
212  }
213  catch (...)
214  {
215  MITK_ERROR << "ScalarBar cannot be drawn.";
216  }
217  }
218 }
219 
221 {
222  m_Alignment = align;
223  this->SetupGeometry(align);
224 }
225 
226 void QmitkScalarBar::SetPen(const QPen &pen)
227 {
228  m_Pen = pen;
229 }
static char * line
Definition: svm.cpp:2870
void SetPen(const QPen &pen)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
QmitkScalarBar(QWidget *parent=nullptr)
Default Constructor.
void paintEvent(QPaintEvent *event) override
std::vector< QLine * > m_SubDivisionLines
unsigned int m_NumberOfSubDivisions
void SetupGeometry(alignment align)
alignment m_Alignment
double m_ScaleFactor
void SetNumberOfSubdivisions(unsigned int subs)
QLine * m_MainLine
virtual void SetScaleFactor(double scale)
virtual void SetAlignment(alignment align)
~QmitkScalarBar() override
Default Destructor.
unsigned int GetNumberOfSubdivisions()