Render ruler and signals with sub-pixel accuracy
[pulseview.git] / pv / view / ruler.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include "ruler.h"
22 #include "view.h"
23
24 #include <extdef.h>
25
26 #include <assert.h>
27 #include <math.h>
28
29 #include <QPainter>
30 #include <QTextStream>
31
32 namespace pv {
33 namespace view {
34
35 const int Ruler::MinorTickSubdivision = 4;
36 const int Ruler::ScaleUnits[3] = {1, 2, 5};
37
38 const QString Ruler::SIPrefixes[9] =
39         {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
40 const int Ruler::FirstSIPrefixPower = -15;
41
42 Ruler::Ruler(View &parent) :
43         QWidget(&parent),
44         _view(parent)
45 {
46 }
47
48 void Ruler::paintEvent(QPaintEvent *event)
49 {
50         QPainter p(this);
51         p.setRenderHint(QPainter::Antialiasing);
52
53         const double MinSpacing = 80;
54
55         const double min_period = _view.scale() * MinSpacing;
56
57         const int order = (int)floorf(log10f(min_period));
58         const double order_decimal = pow(10, order);
59
60         unsigned int unit = 0;
61         double tick_period = 0.0f;
62
63         do
64         {
65                 tick_period = order_decimal * ScaleUnits[unit++];
66         } while(tick_period < min_period && unit < countof(ScaleUnits));
67
68         const unsigned int prefix = (order - FirstSIPrefixPower) / 3;
69         assert(prefix >= 0);
70         assert(prefix < countof(SIPrefixes));
71
72         const double multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
73
74         const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
75                 Qt::AlignLeft | Qt::AlignTop, "8").height();
76
77         // Draw the tick marks
78         p.setPen(Qt::black);
79
80         const double minor_tick_period = tick_period / MinorTickSubdivision;
81         const double first_major_division =
82                 floor(_view.offset() / tick_period);
83         const double first_minor_division =
84                 ceil(_view.offset() / minor_tick_period);
85         const double t0 = first_major_division * tick_period;
86
87         int division = (int)round(first_minor_division -
88                 first_major_division * MinorTickSubdivision);
89         while(1)
90         {
91                 const double t = t0 + division * minor_tick_period;
92                 const double x = (t - _view.offset()) / _view.scale();
93
94                 if(x >= width())
95                         break;
96
97                 if(division % MinorTickSubdivision == 0)
98                 {
99                         // Draw a major tick
100                         QString s;
101                         QTextStream ts(&s);
102                         ts << (t * multiplier) << SIPrefixes[prefix] << "s";
103                         p.drawText(x, 0, 0, text_height, Qt::AlignCenter |
104                                 Qt::AlignTop | Qt::TextDontClip, s);
105                         p.drawLine(QPointF(x, text_height),
106                                 QPointF(x, height()));
107                 }
108                 else
109                 {
110                         // Draw a minor tick
111                         p.drawLine(QPointF(x, (text_height + height()) / 2),
112                                 QPointF(x, height()));
113                 }
114
115                 division++;
116         }
117
118         p.end();
119 }
120
121 } // namespace view
122 } // namespace pv