-
Notifications
You must be signed in to change notification settings - Fork 0
/
Histogram.cpp
352 lines (295 loc) · 9.49 KB
/
Histogram.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* @file Histogram.cpp
* @author Dan R. Lipsa
* @date 22 July 2010
*
* Definition of the Histogram class
*/
#include "Application.h"
#include "Debug.h"
#include "Histogram.h"
#include "HistogramSettings.h"
#include "HistogramStatistics.h"
#include "HistogramItem.h"
const QSize Histogram::SIZE_HINT (200, 200);
/**
* @todo Add an option to show percentage per bin instead of count per
* bin for the y axis of the histogram.
*/
Histogram::Histogram (QWidget* parent) :
QwtPlot (parent),
m_histogramItem (new HistogramItem ()),
m_plotPicker (QwtPlot::xBottom, QwtPlot::yLeft,
QwtPicker::RectSelection | QwtPicker::DragSelection,
QwtPlotPicker::NoRubberBand,
QwtPicker::AlwaysOff,
canvas()),
m_selectionTool (ERASER),
m_histogramHeight (new HistogramSettings (this)),
m_sizeHint (SIZE_HINT)
{
setCanvasBackground(QColor(Qt::white));
alignScales ();
setAutoReplot ();
setAxisTitleDefaultFont (QwtPlot::yLeft, "Count per bin");
setAxisDefaultFont (QwtPlot::yLeft);
setAxisDefaultFont (QwtPlot::xBottom);
m_grid.setMajPen(QPen(Qt::black, 0, Qt::DotLine));
m_grid.setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
m_grid.attach(this);
SetGridEnabled (true);
m_histogramItem->setFocusColor(Qt::darkCyan);
m_histogramItem->setContextColor(QColor(Qt::lightGray).lighter (110));
m_histogramItem->setOutOfBoundsColor(Qt::red);
m_histogramItem->attach(this);
m_plotPicker.setEnabled (false);
connect(&m_plotPicker, SIGNAL(appended(const QPoint&)),
this, SLOT(SelectionPointAppended(const QPoint&)));
connect(&m_plotPicker, SIGNAL(moved(const QPoint&)),
this, SLOT(SelectionPointMoved(const QPoint&)));
connect(&m_plotPicker, SIGNAL(selected(const QwtPolygon&)),
this, SLOT(PolygonSelected (const QwtPolygon&)));
}
void Histogram::alignScales()
{
// The code below shows how to align the scales to
// the canvas frame, but is also a good example demonstrating
// why the spreaded API needs polishing.
canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
canvas()->setLineWidth(1);
for ( int i = 0; i < QwtPlot::axisCnt; i++ )
{
QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i);
if ( scaleWidget )
scaleWidget->setMargin(0);
QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i);
if ( scaleDraw )
scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
}
}
size_t Histogram::getBin (double value)
{
const QwtIntervalData& data = m_histogramItem->data ();
size_t binCount = data.size ();
return HistogramStatistics::GetBin (
value, binCount,
data.interval (0).minValue (), data.interval (binCount - 1).maxValue ());
}
void Histogram::SelectionPointAppended (const QPoint &canvasPos)
{
double value = invTransform(QwtPlot::xBottom, canvasPos.x());
m_beginBinSelection = getBin (value);
m_histogramItem->setSelected (m_selectionTool == BRUSH,
m_beginBinSelection, m_beginBinSelection + 1);
}
void Histogram::SelectionPointMoved (const QPoint& canvasPos)
{
double value = invTransform(QwtPlot::xBottom, canvasPos.x());
size_t begin = m_beginBinSelection;
size_t end = getBin (value);
if (begin > end)
swap (begin, end);
m_histogramItem->setSelected (m_selectionTool == BRUSH, begin, end + 1);
}
void Histogram::PolygonSelected (const QwtPolygon& poly)
{
static_cast<void> (poly);
Q_EMIT SelectionChanged ();
}
void Histogram::SetAllItemsSelection (bool selected)
{
m_histogramItem->setAllItemsSelected (selected);
Q_EMIT SelectionChanged ();
}
bool Histogram::AreAllItemsSelected () const
{
BinRegions selectedBins;
GetSelectedBins (&selectedBins);
return selectedBins.size () == 1 &&
selectedBins[0].first == 0 &&
selectedBins[0].second == m_histogramItem->data ().size ();
}
void Histogram::SetSelectionTool (SelectionTool selectionTool)
{
m_selectionTool = selectionTool;
m_plotPicker.setEnabled (selectionTool == NONE ? false : true);
}
void Histogram::setAxisTitleDefaultFont (int axisId, const char* s)
{
QFont defaultFont = Application::Get ()->font ();
defaultFont.setBold (true);
QwtText at;
if (s != 0)
at.setText (s);
else
at = axisTitle (axisId);
at.setFont (defaultFont);
setAxisTitle (axisId, at);
}
void Histogram::setAxisDefaultFont (int axisId)
{
QFont defaultFont = Application::Get ()->font ();
setAxisFont (axisId, defaultFont);
}
void Histogram::SetDefaultFont ()
{
setAxisTitleDefaultFont (QwtPlot::xBottom);
setAxisTitleDefaultFont (QwtPlot::yLeft);
setAxisDefaultFont (QwtPlot::xBottom);
setAxisDefaultFont (QwtPlot::yLeft);
}
void Histogram::SetDataAllBinsSelected (
const QwtIntervalData& intervalData, double maxValue, const char* axisTitle)
{
setData (intervalData, maxValue);
setAxisTitleDefaultFont (QwtPlot::xBottom, axisTitle);
replot ();
Q_EMIT SelectionChanged ();
}
void Histogram::SetSelectedBinsNoSignal (const BinRegions& bins)
{
m_histogramItem->setAllItemsSelected (false);
m_histogramItem->setSelectedBins (bins);
replot ();
}
void Histogram::SetDataKeepBinSelection (
const QwtIntervalData& intervalData, double maxValue,
const char* axisTitle, const BinRegions& selectedBins)
{
setData (intervalData, maxValue, &selectedBins);
setAxisTitleDefaultFont (QwtPlot::xBottom, axisTitle);
replot ();
}
bool Histogram::HasData () const
{
return m_histogramItem->HasData ();
}
void Histogram::setData (
const QwtIntervalData& intervalData, double maxValue,
const BinRegions* selectedBins)
{
m_histogramItem->setData(intervalData, maxValue, selectedBins);
setAxisScale(QwtPlot::yLeft, GetYAxisMinValue (), maxValue);
SetXAxisMinValue (intervalData.interval (0).minValue ());
SetXAxisMaxValue (
intervalData.interval (intervalData.size () - 1).maxValue ());
setAxisScale(QwtPlot::xBottom, GetXAxisMinValue (), GetXAxisMaxValue ());
}
void Histogram::SetYAxisMaxValue (double maxValueAxis)
{
m_histogramItem->SetYAxisMaxValue (maxValueAxis);
setAxisScale(QwtPlot::yLeft, GetYAxisMinValue (), maxValueAxis);
}
void Histogram::SetGridEnabled (bool enabled)
{
m_grid.enableX (enabled);
m_grid.enableY (enabled);
m_grid.enableXMin (enabled);
m_grid.enableYMin (enabled);
}
bool Histogram::IsGridEnabled () const
{
return m_grid.xEnabled ();
}
void Histogram::GetSelectedIntervals (
vector<QwtDoubleInterval>* intervals) const
{
m_histogramItem->getSelectedIntervals (intervals);
}
size_t Histogram::GetYAxisMaxValueData () const
{
QwtDoubleRect rect = m_histogramItem->boundingRect ();
return rect.y () + rect.height ();
}
void Histogram::SetYAxisLogScale (bool logYAxis)
{
m_histogramItem->SetYAxisLogScale (logYAxis);
SetYAxisMaxValue (GetYAxisMaxValue ());
if (logYAxis)
setAxisScaleEngine (QwtPlot::yLeft, new QwtLog10ScaleEngine);
else
setAxisScaleEngine (QwtPlot::yLeft, new QwtLinearScaleEngine);
}
void Histogram::SetXAxisLogScale (bool logXAxis)
{
m_histogramItem->SetXAxisLogScale (logXAxis);
if (logXAxis)
setAxisScaleEngine (QwtPlot::xBottom, new QwtLog10ScaleEngine);
else
setAxisScaleEngine (QwtPlot::xBottom, new QwtLinearScaleEngine);
}
void Histogram::SetDisplayColorBar (bool displayColorBar)
{
m_displayColorBar = displayColorBar;
QwtScaleWidget* scaleWidget = axisWidget (QwtPlot::xBottom);
scaleWidget->setColorBarEnabled (displayColorBar);
}
void Histogram::SetColorTransferFunction (const QwtDoubleInterval& interval,
const QwtLinearColorMap& colorMap)
{
m_histogramItem->setColorMap (colorMap);
QwtScaleWidget* scaleWidget = axisWidget (QwtPlot::xBottom);
scaleWidget->setColorMap (interval, m_histogramItem->getColorMap ());
}
void Histogram::HistogramSettingsDialog ()
{
m_histogramHeight->SetYValue (GetYAxisMaxValue ());
m_histogramHeight->SetYAxisMaxValue (GetYAxisMaxValueData ());
m_histogramHeight->SetYAxisLogScale (IsYAxisLogScale ());
if (m_histogramHeight->exec () == QDialog::Accepted)
{
SetYAxisLogScale (m_histogramHeight->IsYAxisLogScale ());
SetYAxisMaxValue (m_histogramHeight->GetYValue ());
}
}
void Histogram::SetItemsSelectionHigh (bool selected, double value)
{
size_t begin = getBin (value);
size_t binCount = m_histogramItem->data ().size ();
m_histogramItem->setSelected (selected, begin, binCount);
}
void Histogram::SetItemsSelectionLow (bool selected, double value)
{
size_t end = getBin (value);
m_histogramItem->setSelected (selected, 0, end + 1);
}
QSize Histogram::sizeHint () const
{
return m_sizeHint;
}
void Histogram::SetColorCoded (bool colorCoded)
{
m_histogramItem->setColorCoded (colorCoded);
}
double Histogram::GetYAxisMaxValue () const
{
return m_histogramItem->GetYAxisMaxValue ();
}
double Histogram::GetYAxisMinValue () const
{
return m_histogramItem->GetYAxisMinValue ();
}
double Histogram::GetXAxisMaxValue () const
{
return m_histogramItem->GetXAxisMaxValue ();
}
void Histogram::SetXAxisMaxValue (double value)
{
m_histogramItem->SetXAxisMaxValue (value);
}
double Histogram::GetXAxisMinValue () const
{
return m_histogramItem->GetXAxisMinValue ();
}
void Histogram::SetXAxisMinValue (double value)
{
m_histogramItem->SetXAxisMinValue (value);
}
bool Histogram::IsYAxisLogScale () const
{
return m_histogramItem->IsYAxisLogScale ();
}
void Histogram::GetSelectedBins (BinRegions* intervals, bool selected) const
{
m_histogramItem->getSelectedBins (intervals, selected);
}