-
Notifications
You must be signed in to change notification settings - Fork 4
/
histoview.cpp
156 lines (133 loc) · 3.81 KB
/
histoview.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// C++ Implementation: histoview
//
// Description:
//
//
// Author: Nicholas Phillips <nicholas.G.Phillips@nasa.gov>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include <math.h>
#include <iomanip>
#include <QPainter>
#include "histogram.h"
#include "histoview.h"
#include "colortable.h"
using namespace std;
/* ------------------------------------------------------------------------------------
'HistoView' constructor.
Set the default size and background. Mark histogram as not set
Arguments:
parent: who wanted me to exist. You take responsibility for killing me.
Written by Nicholas Phillips, UMCP, 6 August 2008.
------------------------------------------------------------------------------------ */
HistoView::HistoView(QWidget *parent) : QWidget(parent)
{
histo = 0; // So no degfaults while we wait for some data to paint
ct = 0;
h = 128;
w = 256;
w1= 64;
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
/* ------------------------------------------------------------------------------------
'set'
What histogram to display. It is assumed the passed Histogram exists as
long as QPaintEvents may arrive.
Arguments:
h Pointer to the histogram to use
Returned:
Written by Nicholas Phillips, UMCP, 6 August 2008.
------------------------------------------------------------------------------------ */
void HistoView::set(const ColorTable *newct)
{
ct = newct;
update();
return;
}
/* ------------------------------------------------------------------------------------
'set'
What histogram to display. It is assumed the passed Histogram exists as
long as QPaintEvents may arrive.
Arguments:
h Pointer to the histogram to use
Returned:
Written by Nicholas Phillips, UMCP, 6 August 2008.
------------------------------------------------------------------------------------ */
void HistoView::set(const Histogram *h)
{
histo = h;
setCenterZoom(0.5,1);
return;
}
/* ------------------------------------------------------------------------------------
'setCenterZoom'
The new values are assumed to be within range.
The hilighted region is taken to span [c-z/2,c+z/2]
Arguments:
c_ New Center value
z_ New Zoom value
Returned:
Nothing
Written by Nicholas Phillips, UMCP, 6 August 2008.
------------------------------------------------------------------------------------ */
void HistoView::setCenterZoom( float c_, float z_)
{
c = c_;
z = z_;
update();
return;
}
/* ------------------------------------------------------------------------------------
'paintEvent'
Paint the actual histogram, hilighting the selected range as set by the
most recent call of setCenterZoom.
Assumes the histogram returns a value between 0 and 1
Arguments:
<unused QPaintEvent>
Returned:
Nothing.
Written by Nicholas Phillips, UMCP, 6 August 2008.
------------------------------------------------------------------------------------ */
void HistoView::paintEvent(QPaintEvent *)
{
if( ! histo ) return;
float a = c - (w1/(w-1.) + 0.5)*z;
float b = z/(w-1.);
QPainter painter(this);
painter.setPen(QPen(QColor("black")));
painter.drawLine(w1-1, 0, w1-1, h);
painter.drawLine(w+w1-1, 0, w+w1-1, h);
QColor color;
float x0,x1,r,cv;
for(int p = 0; p < w+2*w1-1; p ++)
{
x0 = a + b*p;
x1 = a + b*(p+1);
if( (x0 <= 0) || (x1 >= 1) )
r = 0;
else
r = (*histo)(x0,x1);
if( (p < w1) || (p >= w+w1) )
painter.setPen(QPen(QColor("black")));
else
{
if( ct != NULL )
{
cv = ((float)(p-w1))/(w-1);
painter.setPen(QPen((*ct)(cv)));
}
else
{
cv = ((float)(w+w1-p-1))/(1.5*w);
color.setHsvF(cv,1,1);
painter.setPen(QPen(color));
}
}
painter.drawLine(p,(int)(h*(1-r)),p,h);
}
return;
}