forked from shundhammer/qdirstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistogramDraw.cpp
340 lines (246 loc) · 8.85 KB
/
HistogramDraw.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
/*
* File name: HistogramDraw.cpp
* Summary: Draw routines for file size histogram
* License: GPL V2 - See file LICENSE for details.
*
* Author: Stefan Hundhammer <[email protected]>
*/
#include <math.h>
#include "HistogramView.h"
#include "HistogramItems.h"
#include "FileInfo.h"
#include "Logger.h"
#include "Exception.h"
#define UnicodeMathSigma 0x2211 // 'n-ary summation'
using namespace QDirStat;
void HistogramView::addHistogram()
{
addHistogramBackground();
addAxes();
addYAxisLabel();
addXAxisLabel();
addXStartEndLabels();
addQuartileText();
addHistogramBars();
addMarkers();
}
void HistogramView::addHistogramBackground()
{
QRectF rect( -_leftBorder,
_bottomBorder,
_histogramWidth + _leftBorder + _rightBorder,
-( _histogramHeight + _topBorder + _bottomBorder ) );
_histogramPanel = scene()->addRect( rect, QPen( Qt::NoPen ), _panelBackground );
_histogramPanel->setZValue( PanelBackgroundLayer );
}
void HistogramView::addAxes()
{
QPen pen( QColor( Qt::black ), 1 );
QGraphicsItem * xAxis = scene()->addLine( 0, 0, _histogramWidth, 0, pen );
QGraphicsItem * yAxis = scene()->addLine( 0, 0, 0, -_histogramHeight, pen );
xAxis->setZValue( AxisLayer );
yAxis->setZValue( AxisLayer );
}
void HistogramView::addYAxisLabel()
{
QString labelText = _useLogHeightScale ? "log<sub>2</sub>(n) -->" : "n";
QGraphicsTextItem * item = scene()->addText( "" );
item->setHtml( labelText );
QFont font( item->font() );
font.setBold( true );
item->setFont( font );
qreal textWidth = item->boundingRect().width();
qreal textHeight = item->boundingRect().height();
QPointF labelCenter = QPoint( -_leftBorder / 2, -_histogramHeight / 2 );
if ( _useLogHeightScale )
{
item->setRotation( 270 );
item->setPos( labelCenter.x() - textHeight / 2,
labelCenter.y() + textWidth / 2 );
}
else
{
item->setPos( labelCenter.x() - textWidth / 2,
labelCenter.y() - textHeight / 2 );
}
item->setZValue( TextLayer );
}
void HistogramView::addXAxisLabel()
{
QString labelText = tr( "File Size" ) + " -->";
QGraphicsSimpleTextItem * item = scene()->addSimpleText( labelText );
QFont font( item->font() );
font.setBold( true );
item->setFont( font );
qreal textWidth = item->boundingRect().width();
qreal textHeight = item->boundingRect().height();
QPointF labelCenter = QPoint( _histogramWidth / 2, _bottomBorder );
item->setPos( labelCenter.x() - textWidth / 2,
labelCenter.y() - textHeight ); // Align bottom
item->setZValue( TextLayer );
}
void HistogramView::addXStartEndLabels()
{
QString startLabel = tr( "Min" );
if ( _startPercentile > 0 )
startLabel = QString( "P%1" ).arg( _startPercentile );
startLabel += "\n" + formatSize( percentile( _startPercentile ) );
QString endLabel = _endPercentile == 100 ?
tr( "Max" ) :
QString( "P%1" ).arg( _endPercentile );
QString endSizeLabel = formatSize( percentile( _endPercentile ) );
QGraphicsSimpleTextItem * startItem = scene()->addSimpleText( startLabel );
QGraphicsSimpleTextItem * endItem = scene()->addSimpleText( endLabel );
QGraphicsSimpleTextItem * endSizeItem = scene()->addSimpleText( endSizeLabel );
qreal endTextHeight = endItem->boundingRect().height();
qreal endTextWidth = endItem->boundingRect().width();
qreal endSizeWidth = endSizeItem->boundingRect().width();
qreal y = _bottomBorder - 2 * endTextHeight;
startItem->setPos( 0, _bottomBorder - startItem->boundingRect().height() );
endItem->setPos( _histogramWidth - endTextWidth, y );
endSizeItem->setPos( _histogramWidth - endSizeWidth, y + endTextHeight );
startItem->setZValue( TextLayer );
endItem->setZValue( TextLayer );
endSizeItem->setZValue( TextLayer );
}
void HistogramView::addQuartileText()
{
qreal textBorder = 10.0;
qreal textSpacing = 30.0;
qreal x = 0;
qreal y = -_histogramHeight - _topBorder - textBorder;
qreal n = bucketsTotalSum();
if ( n > 0 ) // Only useful if there are any data at all
{
QString q1Text = tr( "Q1: %1" ).arg( formatSize( percentile( 25 ) ) );
QString q3Text = tr( "Q3: %1" ).arg( formatSize( percentile( 75 ) ) );
QString medianText = tr( "Median: %1" ).arg( formatSize( percentile( 50 ) ) );
QGraphicsSimpleTextItem * q1Item = scene()->addSimpleText( q1Text );
QGraphicsSimpleTextItem * q3Item = scene()->addSimpleText( q3Text );
QGraphicsSimpleTextItem * medianItem = scene()->addSimpleText( medianText );
q1Item->setBrush( _quartilePen.color() );
q3Item->setBrush( _quartilePen.color() );
medianItem->setBrush( _medianPen.color() );
QFont font( medianItem->font() );
font.setBold( true );
medianItem->setFont( font );
q1Item->setFont( font );
q3Item->setFont( font );
y -= medianItem->boundingRect().height();
qreal q1Width = q1Item->boundingRect().width();
qreal q3Width = q3Item->boundingRect().width();
qreal medianWidth = medianItem->boundingRect().width();
q1Item->setPos( x, y ); x += q1Width + textSpacing;
medianItem->setPos( x, y ); x += medianWidth + textSpacing;
q3Item->setPos( x, y ); x += q3Width + textSpacing;
q1Item->setZValue( TextLayer );
q3Item->setZValue( TextLayer );
medianItem->setZValue( TextLayer );
}
//
// Add text for the total number of files
//
QString nText = tr( "Files (n): %1" ).arg( n );
QGraphicsSimpleTextItem * nTextItem = scene()->addSimpleText( nText );
QFont font( nTextItem->font() );
font.setBold( true );
nTextItem->setFont( font );
QFontMetrics metrics( font );
QChar sigma( UnicodeMathSigma );
if ( metrics.inFont( sigma ) )
nTextItem->setText( QString( "%1n: %2" ).arg( sigma ).arg( n ) );
if ( n == 0 )
y -= nTextItem->boundingRect().height();
nTextItem->setPos( x, y );
nTextItem->setZValue( TextLayer );
}
void HistogramView::addHistogramBars()
{
qreal barWidth = _histogramWidth / _buckets.size();
qreal maxVal = _bucketMaxValue;
if ( _useLogHeightScale )
maxVal = log2( maxVal );
for ( int i=0; i < _buckets.size(); ++i )
{
// logDebug() << "Adding bar #" << i << " with value " << _buckets[ i ] << endl;
QRectF rect;
rect.setX( i * barWidth );
rect.setY( 0 );
rect.setHeight( -_histogramHeight );
rect.setWidth( barWidth );
qreal val = _buckets[i];
if ( _useLogHeightScale && val > 1.0 )
val = log2( val );
qreal fillHeight = maxVal == 0 ?
0.0 :
val / maxVal * _histogramHeight;
HistogramBar * bar = new HistogramBar( this, i, rect, fillHeight );
CHECK_NEW( bar );
}
}
void HistogramView::addMarkers()
{
qreal totalWidth =
_percentiles[ _endPercentile ] -
_percentiles[ _startPercentile ];
if ( totalWidth < 1 )
return;
QLineF zeroLine( 0, _markerExtraHeight,
0, -( _histogramHeight + _markerExtraHeight ) );
// Show ordinary percentiles (all except Q1, Median, Q3)
for ( int i = _startPercentile + 1; i < _endPercentile; ++i )
{
if ( i == 0 || i == 100 )
continue;
if ( i == 50 && _showMedian )
continue;
if ( ( i == 25 || i == 75 ) && _showQuartiles )
continue;
if ( _percentileStep != 1 )
{
bool skip = true;
if ( i <= _startPercentile + _leftMarginPercentiles && i < 25 )
skip = false;
if ( i >= _endPercentile - _rightMarginPercentiles && i > 75 )
skip = false;
if ( skip && _percentileStep != 0 && i % _percentileStep == 0 )
skip = false;
if ( skip )
continue;
}
QPen pen = _percentilePen;
if ( _percentileStep != 0 && _percentileStep != 5 && i % 10 == 0 )
pen = _decilePen;
// logDebug() << "Adding marker for P" << i << endl;
new PercentileMarker( this, i, "", zeroLine, pen );
}
if ( _showQuartiles )
{
if ( percentileDisplayed( 25 ) )
new PercentileMarker( this, 25, tr( "Q1 (1st Quartile)" ),
zeroLine, _quartilePen );
if ( percentileDisplayed( 75 ) )
new PercentileMarker( this, 75, tr( "Q3 (3rd Quartile)" ),
zeroLine, _quartilePen );
}
if ( _showMedian && percentileDisplayed( 50 ) )
{
new PercentileMarker( this, 50, tr( "Median" ),
zeroLine, _medianPen );
}
}
QPointF HistogramView::addText( const QPointF & pos, const QStringList & lines )
{
QGraphicsTextItem * textItem = scene()->addText( lines.join( "\n" ) );
textItem->setPos( pos );
return QPoint( pos.x(), pos.y() + textItem->boundingRect().height() );
}
QPointF HistogramView::addBoldText( const QPointF & pos, const QString & text )
{
QGraphicsTextItem * textItem = scene()->addText( text );
textItem->setPos( pos );
QFont boldFont( textItem->font() );
boldFont.setBold( true );
textItem->setFont( boldFont );
return QPoint( pos.x(), pos.y() + textItem->boundingRect().height() );
}