-
Notifications
You must be signed in to change notification settings - Fork 0
/
DQHistogramWidget.cpp
110 lines (82 loc) · 3.34 KB
/
DQHistogramWidget.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*****************************************************************************
*************************** DQHistogramWidget.cpp ***************************
*****************************************************************************/
/*****************************************************************************
****************************** I N C L U D E *******************************
*****************************************************************************/
#include <QToolTip>
#include <QPainter>
#include <QMouseEvent>
#include "DQHistogramWidget.h"
/*****************************************************************************
*** class DQHistogramWidget
*****************************************************************************/
/*****************************************************************************
*
* DQHistogramWidget::DQHistogramWidget
*
*****************************************************************************/
DQHistogramWidget::DQHistogramWidget(QRgb clrLine /* = qRgb(0, 0, 0) */,
QWidget* pParent /* = nullptr */)
: QFrame(pParent)
{
m_RGBLine = clrLine;
setFrameStyle(QFrame::Box | QFrame::Plain);
setLineWidth(1);
setFixedWidth(m_Histogram.GetBinCount() + (2 * frameWidth()));
setMouseTracking(true);
show();
return;
} // End of function DQHistogramWidget::DQHistogramWidget
/*****************************************************************************
*
* DQHistogramWidget::paintEvent
*
*****************************************************************************/
void DQHistogramWidget::paintEvent(QPaintEvent* pEvent)
{
QFrame::paintEvent(pEvent);
QPainter Painter(this);
// Drawing has to be adjusted to fit in the contents rectangle
QRect CRect = contentsRect();
QPoint BL = CRect.bottomLeft();
QPen Pen(m_RGBLine);
Painter.setPen(Pen);
if (m_Histogram.GetMaxCount() > 0)
{
for (int i = 0 ; i < m_Histogram.GetBinCount() ; i++)
{
int x = CRect.x() + i;
int nCount = m_Histogram.GetBin(i);
// Scale the output so the largest bin is full scale
int nBar = ((CRect.height() - 1) * nCount) /
m_Histogram.GetMaxCount();
if (nCount > 0)
{
Painter.drawLine(x, BL.y(), x, BL.y() - nBar);
} // end if
} // end for
} // end if
return;
} // End of function DQHistogramWidget::paintEvent
/*****************************************************************************
*
* DQHistogramWidget::mouseMoveEvent
*
* On mouse over show the bin number and count.
*
*****************************************************************************/
void DQHistogramWidget::mouseMoveEvent(QMouseEvent* pEvent)
{
QString strData;
// Compenstate the x coordinate for the frame width
int x = pEvent->x() - frameWidth();
if ((x >= 0) && (x < m_Histogram.GetBinCount()))
{
// The string is the bin number and bin count. The histogram fills
// the control so this simple method works
strData.sprintf("(%d, %u)", x, m_Histogram.GetBin(x));
QToolTip::showText(pEvent->globalPos(), strData, this);
} // end if
return;
} // End of function DQHistogramWidget::mouseMoveEvent