-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
73 lines (57 loc) · 1.48 KB
/
node.cpp
File metadata and controls
73 lines (57 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <QtMath>
#include <QPainter>
#include <QVariant>
#include <QKeyEvent>
#include "node.h"
const Node::ColorHandler Node::m_colors = ColorHandler();
Node::Node(quint16 value, QWidget *parent)
: QWidget (parent)
{
setValue(value);
}
void Node::setValue(const quint16 &value)
{
m_value = value;
m_color = m_colors[qRound(log2(value))];
refreshPix();
}
const QColor& Node::color() const
{
return m_color;
}
void Node::paintEvent(QPaintEvent *)
{
QPainter painter (this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawPixmap (0, 0, m_pixmap);
}
void Node::resizeEvent(QResizeEvent *event)
{
m_size = event->size().height();
m_font = this ->font();
m_font.setPointSizeF(m_size/3);
m_digitY = (m_size + m_font.pointSize())*0.5;
refreshPix();
}
void Node::refreshPix()
{
m_pixmap = QPixmap(size());
m_pixmap.fill(Qt::transparent);
QPainter painter(&m_pixmap);
painter.initFrom(this);
painter.setPen (m_color);
painter.setBrush(m_color);
QRect rect(0, 0, m_size, m_size);
Style::getInstance().drawRect(painter, rect, m_size/5);
QFontMetrics fontMetrics (m_font);
QString valueStr = QVariant(m_value).toString();
painter.setPen ( m_colors[0]);
painter.setFont ( m_font);
painter.drawText((m_size - fontMetrics.width(valueStr))*0.5,
m_digitY,
valueStr);
}
qreal Node::log2(qreal val)
{
return qLn(val)/qLn(2);
}