v2.0.0
Loading...
Searching...
No Matches
bar.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35//=============================================================================================================
36// INCLUDES
37//=============================================================================================================
38
39#include "bar.h"
40
41//=============================================================================================================
42// QT INCLUDES
43//=============================================================================================================
44
45#include <QPainter>
46#include <QPaintEvent>
47
48//=============================================================================================================
49// EIGEN INCLUDES
50//=============================================================================================================
51
52//=============================================================================================================
53// USED NAMESPACES
54//=============================================================================================================
55
56using namespace DISPLIB;
57
58//=============================================================================================================
59// DEFINE MEMBER METHODS
60//=============================================================================================================
61
62Bar::Bar(const QString& title,
63 QWidget* parent)
64: QWidget(parent)
65, m_sTitle(title)
66, m_iMaxFrequency(0)
67{
68 setMinimumSize(200, 150);
69 setBackgroundRole(QPalette::Base);
70 setAutoFillBackground(true);
71}
72
73//=============================================================================================================
74
75void Bar::paintEvent(QPaintEvent * /*event*/)
76{
77 QPainter painter(this);
78 painter.setRenderHint(QPainter::Antialiasing);
79
80 const int w = width();
81 const int h = height();
82 const int marginLeft = 60;
83 const int marginRight = 20;
84 const int marginTop = 40;
85 const int marginBottom = 100;
86 const int plotW = w - marginLeft - marginRight;
87 const int plotH = h - marginTop - marginBottom;
88
89 if (plotW <= 0 || plotH <= 0)
90 return;
91
92 // Title
93 if (!m_sTitle.isEmpty()) {
94 painter.setPen(Qt::black);
95 QFont titleFont = painter.font();
96 titleFont.setBold(true);
97 titleFont.setPointSize(titleFont.pointSize() + 2);
98 painter.setFont(titleFont);
99 painter.drawText(QRect(0, 0, w, marginTop), Qt::AlignCenter, m_sTitle);
100 painter.setFont(QFont());
101 }
102
103 if (m_frequencies.isEmpty())
104 return;
105
106 const int nBars = m_frequencies.size();
107 const int maxFreq = (m_iMaxFrequency > 0) ? m_iMaxFrequency : 1;
108
109 // Draw axes
110 painter.setPen(QPen(Qt::black, 1));
111 painter.drawLine(marginLeft, marginTop, marginLeft, marginTop + plotH);
112 painter.drawLine(marginLeft, marginTop + plotH, marginLeft + plotW, marginTop + plotH);
113
114 // Y-axis ticks
115 const int nYTicks = 5;
116 for (int i = 0; i <= nYTicks; ++i) {
117 int yVal = maxFreq * i / nYTicks;
118 int y = marginTop + plotH - (plotH * i / nYTicks);
119 painter.drawLine(marginLeft - 5, y, marginLeft, y);
120 painter.drawText(QRect(0, y - 10, marginLeft - 8, 20), Qt::AlignRight | Qt::AlignVCenter, QString::number(yVal));
121 }
122
123 // Draw bars
124 double barWidth = static_cast<double>(plotW) / nBars;
125 double barPadding = barWidth * 0.1;
126
127 for (int i = 0; i < nBars; ++i) {
128 double barH = (static_cast<double>(m_frequencies[i]) / maxFreq) * plotH;
129 double x = marginLeft + i * barWidth + barPadding;
130 double bw = barWidth - 2.0 * barPadding;
131 double y = marginTop + plotH - barH;
132
133 painter.setBrush(QColor(70, 130, 180));
134 painter.setPen(QPen(QColor(50, 100, 150), 1));
135 painter.drawRect(QRectF(x, y, bw, barH));
136 }
137
138 // X-axis labels (rotated)
139 painter.setPen(Qt::black);
140 QFont smallFont = painter.font();
141 smallFont.setPointSize(smallFont.pointSize() - 1);
142 painter.setFont(smallFont);
143
144 for (int i = 0; i < nBars && i < m_categories.size(); ++i) {
145 double cx = marginLeft + (i + 0.5) * barWidth;
146 painter.save();
147 painter.translate(cx, marginTop + plotH + 5);
148 painter.rotate(45);
149 painter.drawText(0, 0, m_categories[i]);
150 painter.restore();
151 }
152
153 // Legend
154 if (!m_sLegend.isEmpty()) {
155 painter.setFont(QFont());
156 painter.setPen(Qt::black);
157 painter.drawText(QRect(0, h - 20, w, 20), Qt::AlignCenter, m_sLegend);
158 }
159}
Bar class declaration.
2-D display widgets and visualisation helpers (charts, topography, colour maps).
void paintEvent(QPaintEvent *event) override
Definition bar.cpp:75
Bar(const QString &title="", QWidget *parent=nullptr)
Definition bar.cpp:62