Skip to content

Commit 7f8b113

Browse files
committed
feat: add simple hover data to frequency plot
1 parent 81fb8a7 commit 7f8b113

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/hobbits-plugins/displays/FrequencyPlot/frequencyplotwidget.cpp

+77-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <QPainter>
44
#include <QImage>
55
#include "displayhelper.h"
6+
#include <QMouseEvent>
67

78
FrequencyPlotWidget::FrequencyPlotWidget(
89
QSharedPointer<DisplayHandle> displayHandle,
@@ -11,7 +12,8 @@ FrequencyPlotWidget::FrequencyPlotWidget(
1112
DisplayBase(displayHandle, pluginRef, parent),
1213
m_windowSize(10000),
1314
m_wordSize(8),
14-
m_scale(2)
15+
m_scale(2),
16+
m_mouseHover(-1, -1)
1517
{
1618

1719
}
@@ -74,6 +76,7 @@ void FrequencyPlotWidget::paintEvent(QPaintEvent*) {
7476

7577

7678
QPainter painter(this);
79+
painter.save();
7780
painter.setRenderHint(QPainter::Antialiasing, false);
7881
painter.scale(m_scale, m_scale);
7982

@@ -85,10 +88,82 @@ void FrequencyPlotWidget::paintEvent(QPaintEvent*) {
8588
double amount = double(barFrequencies.at(x))*ratio;
8689
painter.fillRect(QRectF(x, plotHeight, 1, amount), foreground);
8790
}
91+
painter.restore();
92+
93+
if (m_mouseHover.x() >= 0 && m_mouseHover.y() >= 0) {
94+
95+
int barIdx = m_mouseHover.x() / m_scale;
96+
int x = m_mouseHover.x();
97+
int y = m_mouseHover.y();
98+
99+
if (!barMax.contains(barIdx)) {
100+
return;
101+
}
102+
if (barIdx >= barFrequencies.size()) {
103+
return;
104+
}
105+
106+
quint64 value = barMax.value(barIdx);
107+
int frequency = barFrequencies.at(barIdx);
108+
109+
QString decimal = QString("%1").arg(value, 0, 10);
110+
QString hex = QString("0x%1").arg(value, 0, 16);
111+
QString freq = QString("Count: %1").arg(frequency);
112+
113+
int pad = 4;
114+
int textHeight = 10 + 3;
115+
int boxWidth = 10 * qMax(decimal.size(), freq.size()) + pad*2;
116+
int boxHeight = textHeight * 3 + pad*2;
117+
118+
119+
QRect box;
120+
if (x > (plotSize * m_scale) / 2) {
121+
box.setX(x - pad - boxWidth);
122+
}
123+
else {
124+
box.setX(x + pad);
125+
}
126+
if (y > (plotHeight * m_scale) / 2) {
127+
box.setY(y - pad - boxHeight);
128+
}
129+
else {
130+
box.setY(y + pad);
131+
}
132+
box.setWidth(boxWidth);
133+
box.setHeight(boxHeight);
134+
135+
painter.save();
136+
painter.fillRect(box, QColor(0x00, 0x00, 0x00, 0x99));
137+
QFont font = QFont("monospace", 10);
138+
font.setStyleStrategy(QFont::ForceIntegerMetrics);
139+
painter.setFont(font);
140+
painter.setPen(QColor(0xff, 0xff, 0xff));
141+
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
142+
painter.drawText(box.x() + pad, box.y() + pad, box.width(), textHeight, Qt::AlignLeft, decimal);
143+
painter.drawText(box.x() + pad, box.y() + pad + textHeight, box.width(), textHeight, Qt::AlignLeft, hex);
144+
painter.drawText(box.x() + pad, box.y() + pad + (2 * textHeight), box.width(), textHeight, Qt::AlignLeft, freq);
145+
146+
painter.restore();
147+
}
148+
}
149+
150+
void FrequencyPlotWidget::leaveEvent(QEvent *event)
151+
{
152+
m_mouseHover = {-1, -1};
153+
repaint();
88154
}
89155

90156
void FrequencyPlotWidget::mouseMoveEvent(QMouseEvent *event) {
91-
Q_UNUSED(event)
157+
m_mouseHover = {-1, -1};
158+
159+
if (m_displayHandle->getContainer().isNull()) {
160+
return;
161+
}
162+
163+
m_mouseHover.setX(event->x());
164+
m_mouseHover.setY(event->y());
165+
166+
repaint();
92167
}
93168

94169
void FrequencyPlotWidget::setWindowSize(int size)

src/hobbits-plugins/displays/FrequencyPlot/frequencyplotwidget.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class FrequencyPlotWidget : public DisplayBase
1414
QWidget *parent = nullptr);
1515

1616
void paintEvent(QPaintEvent*) override;
17+
void leaveEvent(QEvent *event) override;
1718
void mouseMoveEvent(QMouseEvent *event) override;
1819
void adjustScrollbars() override;
1920

@@ -26,6 +27,7 @@ public slots:
2627
qint64 m_windowSize;
2728
qint64 m_wordSize;
2829
int m_scale;
30+
QPoint m_mouseHover;
2931
};
3032

3133
#endif // FREQUENCYPLOTWIDGET_H

0 commit comments

Comments
 (0)