]>
Commit | Line | Data |
---|---|---|
ccdd3ef5 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
ccdd3ef5 JH |
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 | ||
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 | ||
1b5813fe JH |
71 | const double multiplier = pow(0.1, prefix * 3 + FirstSIPrefixPower); |
72 | ||
ccdd3ef5 JH |
73 | const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX, |
74 | Qt::AlignLeft | Qt::AlignTop, "8").height(); | |
75 | ||
76 | // Draw the tick marks | |
77 | p.setPen(Qt::black); | |
78 | ||
79 | const double minor_tick_period = tick_period / MinorTickSubdivision; | |
80 | const double first_major_division = | |
81 | floor(_view.offset() / tick_period); | |
82 | const double first_minor_division = | |
83 | ceil(_view.offset() / minor_tick_period); | |
84 | const double t0 = first_major_division * tick_period; | |
85 | ||
86 | int division = (int)round(first_minor_division - | |
87 | first_major_division * MinorTickSubdivision); | |
88 | while(1) | |
89 | { | |
90 | const double t = t0 + division * minor_tick_period; | |
91 | const double x = (t - _view.offset()) / _view.scale(); | |
92 | ||
93 | if(x >= width()) | |
94 | break; | |
95 | ||
96 | if(division % MinorTickSubdivision == 0) | |
97 | { | |
98 | // Draw a major tick | |
99 | QString s; | |
100 | QTextStream ts(&s); | |
1b5813fe | 101 | ts << (t * multiplier) << SIPrefixes[prefix] << "s"; |
ccdd3ef5 JH |
102 | p.drawText(x, 0, 0, text_height, Qt::AlignCenter | |
103 | Qt::AlignTop | Qt::TextDontClip, s); | |
104 | p.drawLine(x, text_height, x, height()); | |
105 | } | |
106 | else | |
107 | { | |
108 | // Draw a minor tick | |
109 | p.drawLine(x, (text_height + height()) / 2, | |
110 | x, height()); | |
111 | } | |
112 | ||
113 | division++; | |
114 | } | |
115 | ||
116 | p.end(); | |
117 | } | |
118 | ||
119 | } // namespace view | |
120 | } // namespace pv |