]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
Made pv::view::View::zoom public
[pulseview.git] / pv / view / ruler.cpp
CommitLineData
ccdd3ef5
JH
1/*
2 * This file is part of the sigrok 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
32namespace pv {
33namespace view {
34
35const int Ruler::MinorTickSubdivision = 4;
36const int Ruler::ScaleUnits[3] = {1, 2, 5};
37
38const QString Ruler::SIPrefixes[9] =
39 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
40const int Ruler::FirstSIPrefixPower = -15;
41
42Ruler::Ruler(View &parent) :
43 QWidget(&parent),
44 _view(parent)
45{
46}
47
48void Ruler::paintEvent(QPaintEvent *event)
49{
50 QPainter p(this);
51
52 const double MinSpacing = 80;
53
54 const double min_period = _view.scale() * MinSpacing;
55
56 const int order = (int)floorf(log10f(min_period));
57 const double order_decimal = pow(10, order);
58
59 int unit = 0;
60 double tick_period = 0.0f;
61
62 do
63 {
64 tick_period = order_decimal * ScaleUnits[unit++];
65 } while(tick_period < min_period && unit < countof(ScaleUnits));
66
67 const int prefix = (order - FirstSIPrefixPower) / 3;
68 assert(prefix >= 0);
69 assert(prefix < countof(SIPrefixes));
70
71 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
72 Qt::AlignLeft | Qt::AlignTop, "8").height();
73
74 // Draw the tick marks
75 p.setPen(Qt::black);
76
77 const double minor_tick_period = tick_period / MinorTickSubdivision;
78 const double first_major_division =
79 floor(_view.offset() / tick_period);
80 const double first_minor_division =
81 ceil(_view.offset() / minor_tick_period);
82 const double t0 = first_major_division * tick_period;
83
84 int division = (int)round(first_minor_division -
85 first_major_division * MinorTickSubdivision);
86 while(1)
87 {
88 const double t = t0 + division * minor_tick_period;
89 const double x = (t - _view.offset()) / _view.scale();
90
91 if(x >= width())
92 break;
93
94 if(division % MinorTickSubdivision == 0)
95 {
96 // Draw a major tick
97 QString s;
98 QTextStream ts(&s);
99 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
100 p.drawText(x, 0, 0, text_height, Qt::AlignCenter |
101 Qt::AlignTop | Qt::TextDontClip, s);
102 p.drawLine(x, text_height, x, height());
103 }
104 else
105 {
106 // Draw a minor tick
107 p.drawLine(x, (text_height + height()) / 2,
108 x, height());
109 }
110
111 division++;
112 }
113
114 p.end();
115}
116
117} // namespace view
118} // namespace pv