3
3
#include < QPainter>
4
4
#include < QImage>
5
5
#include " displayhelper.h"
6
+ #include < QMouseEvent>
6
7
7
8
FrequencyPlotWidget::FrequencyPlotWidget (
8
9
QSharedPointer<DisplayHandle> displayHandle,
@@ -11,7 +12,8 @@ FrequencyPlotWidget::FrequencyPlotWidget(
11
12
DisplayBase(displayHandle, pluginRef, parent),
12
13
m_windowSize(10000 ),
13
14
m_wordSize(8 ),
14
- m_scale(2 )
15
+ m_scale(2 ),
16
+ m_mouseHover(-1 , -1 )
15
17
{
16
18
17
19
}
@@ -74,6 +76,7 @@ void FrequencyPlotWidget::paintEvent(QPaintEvent*) {
74
76
75
77
76
78
QPainter painter (this );
79
+ painter.save ();
77
80
painter.setRenderHint (QPainter::Antialiasing, false );
78
81
painter.scale (m_scale, m_scale);
79
82
@@ -85,10 +88,82 @@ void FrequencyPlotWidget::paintEvent(QPaintEvent*) {
85
88
double amount = double (barFrequencies.at (x))*ratio;
86
89
painter.fillRect (QRectF (x, plotHeight, 1 , amount), foreground);
87
90
}
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 ();
88
154
}
89
155
90
156
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 ();
92
167
}
93
168
94
169
void FrequencyPlotWidget::setWindowSize (int size)
0 commit comments