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