]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/ruler.cpp
DecodeTrace: Removed set_view
[pulseview.git] / pv / view / ruler.cpp
... / ...
CommitLineData
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
23#include "view.h"
24#include "pv/util.h"
25
26#include <extdef.h>
27
28using namespace Qt;
29
30namespace pv {
31namespace view {
32
33const int Ruler::RulerHeight = 30;
34const int Ruler::MinorTickSubdivision = 4;
35const int Ruler::ScaleUnits[3] = {1, 2, 5};
36
37const int Ruler::HoverArrowSize = 5;
38
39Ruler::Ruler(View &parent) :
40 MarginWidget(parent)
41{
42 setMouseTracking(true);
43
44 connect(&_view, SIGNAL(hover_point_changed()),
45 this, SLOT(hover_point_changed()));
46}
47
48QSize Ruler::sizeHint() const
49{
50 return QSize(0, RulerHeight);
51}
52
53void Ruler::paintEvent(QPaintEvent*)
54{
55 const int ValueMargin = 3;
56
57 QPainter p(this);
58 p.setRenderHint(QPainter::Antialiasing);
59
60 std::pair<double, unsigned int> spacing =
61 calculate_tick_spacing(p, _view.scale(), _view.offset());
62
63 double tick_period = spacing.first;
64 unsigned int prefix = spacing.second;
65
66 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
67 AlignLeft | AlignTop, "8").height();
68
69 // Draw the tick marks
70 p.setPen(palette().color(foregroundRole()));
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 -
80 first_major_division * MinorTickSubdivision) - 1;
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
86 double x;
87
88 do {
89 const double t = t0 + division * minor_tick_period;
90 x = (t - _view.offset()) / _view.scale();
91
92 if (division % MinorTickSubdivision == 0)
93 {
94 // Draw a major tick
95 p.drawText(x, ValueMargin, 0, text_height,
96 AlignCenter | AlignTop | TextDontClip,
97 pv::util::format_time(t, prefix));
98 p.drawLine(QPointF(x, major_tick_y1),
99 QPointF(x, tick_y2));
100 }
101 else
102 {
103 // Draw a minor tick
104 p.drawLine(QPointF(x, minor_tick_y1),
105 QPointF(x, tick_y2));
106 }
107
108 division++;
109
110 } while (x < width());
111
112 // Draw the hover mark
113 draw_hover_mark(p);
114}
115
116void Ruler::draw_hover_mark(QPainter &p)
117{
118 const int x = _view.hover_point().x();
119
120 if (x == -1)
121 return;
122
123 p.setPen(QPen(Qt::NoPen));
124 p.setBrush(QBrush(palette().color(foregroundRole())));
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
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
176} // namespace view
177} // namespace pv