]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
RowItem: Renamed get_v_offset to v_offset
[pulseview.git] / pv / view / ruler.cpp
CommitLineData
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"
ca4ec3ea 22
ccdd3ef5 23#include "view.h"
f0c9f81c 24#include "pv/util.h"
ccdd3ef5 25
cef18fc6 26#include <extdef.h>
ccdd3ef5 27
819f4c25 28using namespace Qt;
f76af637 29
ccdd3ef5
JH
30namespace pv {
31namespace view {
32
a6c1726e 33const int Ruler::RulerHeight = 30;
ccdd3ef5
JH
34const int Ruler::MinorTickSubdivision = 4;
35const int Ruler::ScaleUnits[3] = {1, 2, 5};
36
b3a7c013
JH
37const int Ruler::HoverArrowSize = 5;
38
ccdd3ef5 39Ruler::Ruler(View &parent) :
84a0d458 40 MarginWidget(parent)
ccdd3ef5 41{
b3a7c013
JH
42 setMouseTracking(true);
43
44 connect(&_view, SIGNAL(hover_point_changed()),
45 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
46}
47
a6c1726e
JH
48QSize Ruler::sizeHint() const
49{
50 return QSize(0, RulerHeight);
51}
52
e314eca4 53void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 54{
f260e3bf 55 const int ValueMargin = 3;
3f509c1f 56
ccdd3ef5 57 QPainter p(this);
ce6e73a8 58 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 59
abad24e2
JS
60 std::pair<double, unsigned int> spacing =
61 calculate_tick_spacing(p, _view.scale(), _view.offset());
3f509c1f 62
abad24e2
JS
63 double tick_period = spacing.first;
64 unsigned int prefix = spacing.second;
1b5813fe 65
ccdd3ef5 66 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
3af62a24 67 AlignLeft | AlignTop, "8").height();
ccdd3ef5
JH
68
69 // Draw the tick marks
d2caed8d 70 p.setPen(palette().color(foregroundRole()));
ccdd3ef5
JH
71
72 const double minor_tick_period = tick_period / MinorTickSubdivision;
73 const double first_major_division =
74 floor(_view.offset() / tick_period);
75 const double first_minor_division =
76 ceil(_view.offset() / minor_tick_period);
77 const double t0 = first_major_division * tick_period;
78
79 int division = (int)round(first_minor_division -
64b678ba 80 first_major_division * MinorTickSubdivision) - 1;
f260e3bf
JH
81
82 const int major_tick_y1 = text_height + ValueMargin * 2;
83 const int tick_y2 = height();
84 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
85
64b678ba 86 double x;
ccdd3ef5 87
64b678ba
JH
88 do {
89 const double t = t0 + division * minor_tick_period;
90 x = (t - _view.offset()) / _view.scale();
ccdd3ef5 91
333d5bbc 92 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
93 {
94 // Draw a major tick
f260e3bf
JH
95 p.drawText(x, ValueMargin, 0, text_height,
96 AlignCenter | AlignTop | TextDontClip,
f0c9f81c 97 pv::util::format_time(t, prefix));
f260e3bf
JH
98 p.drawLine(QPointF(x, major_tick_y1),
99 QPointF(x, tick_y2));
ccdd3ef5
JH
100 }
101 else
102 {
103 // Draw a minor tick
f260e3bf
JH
104 p.drawLine(QPointF(x, minor_tick_y1),
105 QPointF(x, tick_y2));
ccdd3ef5
JH
106 }
107
108 division++;
64b678ba
JH
109
110 } while (x < width());
ccdd3ef5 111
b3a7c013
JH
112 // Draw the hover mark
113 draw_hover_mark(p);
ca4ec3ea
JH
114}
115
b3a7c013
JH
116void Ruler::draw_hover_mark(QPainter &p)
117{
118 const int x = _view.hover_point().x();
333d5bbc 119
84a0d458 120 if (x == -1)
b3a7c013
JH
121 return;
122
123 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 124 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013
JH
125
126 const int b = height() - 1;
127 const QPointF points[] = {
128 QPointF(x, b),
129 QPointF(x - HoverArrowSize, b - HoverArrowSize),
130 QPointF(x + HoverArrowSize, b - HoverArrowSize)
131 };
132 p.drawPolygon(points, countof(points));
133}
134
135void Ruler::hover_point_changed()
136{
137 update();
138}
139
abad24e2
JS
140std::pair<double, unsigned int> Ruler::calculate_tick_spacing(
141 QPainter& p, double scale, double offset)
142{
143 const double SpacingIncrement = 32.0f;
144 const double MinValueSpacing = 32.0f;
145
146 double min_width = SpacingIncrement, typical_width;
147
148 double tick_period;
149 unsigned int prefix;
150
151 do {
152 const double min_period = scale * min_width;
153
154 const int order = (int)floorf(log10f(min_period));
155 const double order_decimal = pow(10.0, order);
156
157 unsigned int unit = 0;
158
159 do {
160 tick_period = order_decimal * ScaleUnits[unit++];
161 } while (tick_period < min_period && unit < countof(ScaleUnits));
162
163 prefix = (order - pv::util::FirstSIPrefixPower) / 3;
164
165 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
166 AlignLeft | AlignTop, pv::util::format_time(offset,
167 prefix)).width() + MinValueSpacing;
168
169 min_width += SpacingIncrement;
170
171 } while(typical_width > tick_period / scale);
172
173 return std::make_pair(tick_period, prefix);
174}
175
ccdd3ef5
JH
176} // namespace view
177} // namespace pv