]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
Stopped ruler values colliding at high zoom
[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
JH
22
23#include "cursor.h"
ccdd3ef5 24#include "view.h"
b3a7c013 25#include "viewport.h"
ccdd3ef5 26
cef18fc6 27#include <extdef.h>
ccdd3ef5
JH
28
29#include <assert.h>
30#include <math.h>
c43d09e0 31#include <limits.h>
ccdd3ef5 32
b3a7c013 33#include <QMouseEvent>
ccdd3ef5
JH
34#include <QPainter>
35#include <QTextStream>
36
f76af637
JH
37using namespace std;
38
ccdd3ef5
JH
39namespace pv {
40namespace view {
41
42const int Ruler::MinorTickSubdivision = 4;
43const int Ruler::ScaleUnits[3] = {1, 2, 5};
44
45const QString Ruler::SIPrefixes[9] =
46 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
47const int Ruler::FirstSIPrefixPower = -15;
48
b3a7c013
JH
49const int Ruler::HoverArrowSize = 5;
50
ccdd3ef5
JH
51Ruler::Ruler(View &parent) :
52 QWidget(&parent),
ca4ec3ea
JH
53 _view(parent),
54 _grabbed_marker(NULL)
ccdd3ef5 55{
b3a7c013
JH
56 setMouseTracking(true);
57
58 connect(&_view, SIGNAL(hover_point_changed()),
59 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
60}
61
e314eca4 62void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 63{
3af62a24
JH
64 using namespace Qt;
65
3f509c1f
JH
66 const double SpacingIncrement = 32.0f;
67 const double MinValueSpacing = 32.0f;
68
ccdd3ef5 69 QPainter p(this);
ce6e73a8 70 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 71
3f509c1f
JH
72 double min_width = SpacingIncrement, typical_width;
73 double tick_period;
74 unsigned int prefix;
75 double multiplier;
ccdd3ef5 76
3f509c1f
JH
77 // Find tick spacing, and number formatting that does not cause
78 // value to collide.
79 do
80 {
81 const double min_period = _view.scale() * min_width;
ccdd3ef5 82
3f509c1f
JH
83 const int order = (int)floorf(log10f(min_period));
84 const double order_decimal = pow(10, order);
ccdd3ef5 85
3f509c1f 86 unsigned int unit = 0;
ccdd3ef5 87
3f509c1f
JH
88 do
89 {
90 tick_period = order_decimal * ScaleUnits[unit++];
91 } while (tick_period < min_period && unit < countof(ScaleUnits));
ccdd3ef5 92
3f509c1f
JH
93 prefix = (order - FirstSIPrefixPower) / 3;
94 assert(prefix < countof(SIPrefixes));
ccdd3ef5 95
3f509c1f
JH
96 multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
97
98 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
99 AlignLeft | AlignTop, format_time(_view.offset(),
100 multiplier, prefix)).width() + MinValueSpacing;
101
102 min_width += SpacingIncrement;
103
104 } while(typical_width > tick_period / _view.scale());
1b5813fe 105
ccdd3ef5 106 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
3af62a24 107 AlignLeft | AlignTop, "8").height();
ccdd3ef5
JH
108
109 // Draw the tick marks
d2caed8d 110 p.setPen(palette().color(foregroundRole()));
ccdd3ef5
JH
111
112 const double minor_tick_period = tick_period / MinorTickSubdivision;
113 const double first_major_division =
114 floor(_view.offset() / tick_period);
115 const double first_minor_division =
116 ceil(_view.offset() / minor_tick_period);
117 const double t0 = first_major_division * tick_period;
118
119 int division = (int)round(first_minor_division -
120 first_major_division * MinorTickSubdivision);
333d5bbc 121 while (1)
ccdd3ef5
JH
122 {
123 const double t = t0 + division * minor_tick_period;
124 const double x = (t - _view.offset()) / _view.scale();
125
333d5bbc 126 if (x >= width())
ccdd3ef5
JH
127 break;
128
333d5bbc 129 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
130 {
131 // Draw a major tick
3af62a24 132 p.drawText(x, 0, 0, text_height, AlignCenter |
3f509c1f
JH
133 AlignTop | TextDontClip,
134 format_time(t, multiplier, prefix));
ce6e73a8
JH
135 p.drawLine(QPointF(x, text_height),
136 QPointF(x, height()));
ccdd3ef5
JH
137 }
138 else
139 {
140 // Draw a minor tick
ce6e73a8
JH
141 p.drawLine(QPointF(x, (text_height + height()) / 2),
142 QPointF(x, height()));
ccdd3ef5
JH
143 }
144
145 division++;
146 }
147
f76af637
JH
148 // Draw the cursors
149 draw_cursors(p);
150
b3a7c013
JH
151 // Draw the hover mark
152 draw_hover_mark(p);
153
ccdd3ef5
JH
154 p.end();
155}
156
ca4ec3ea
JH
157void Ruler::mouseMoveEvent(QMouseEvent *e)
158{
333d5bbc 159 if (!_grabbed_marker)
ca4ec3ea
JH
160 return;
161
162 _grabbed_marker->set_time(_view.offset() +
163 ((double)e->x() + 0.5) * _view.scale());
164}
165
166void Ruler::mousePressEvent(QMouseEvent *e)
167{
333d5bbc 168 if (e->buttons() & Qt::LeftButton) {
ca4ec3ea
JH
169 _grabbed_marker = NULL;
170
333d5bbc 171 if (_view.cursors_shown()) {
ca4ec3ea
JH
172 std::pair<Cursor, Cursor> &cursors =
173 _view.cursors();
333d5bbc 174 if (cursors.first.get_label_rect(
ca4ec3ea
JH
175 rect()).contains(e->pos()))
176 _grabbed_marker = &cursors.first;
333d5bbc 177 else if (cursors.second.get_label_rect(
ca4ec3ea
JH
178 rect()).contains(e->pos()))
179 _grabbed_marker = &cursors.second;
180 }
181 }
182}
183
184void Ruler::mouseReleaseEvent(QMouseEvent *)
185{
186 _grabbed_marker = NULL;
187}
188
3f509c1f
JH
189QString Ruler::format_time(double t, double multiplier,
190 unsigned int prefix)
191{
192 QString s;
193 QTextStream ts(&s);
194 ts.setRealNumberPrecision(0);
195 ts << fixed << forcesign << (t * multiplier) <<
196 SIPrefixes[prefix] << "s";
197 return s;
198}
199
f76af637
JH
200void Ruler::draw_cursors(QPainter &p)
201{
333d5bbc 202 if (!_view.cursors_shown())
f76af637
JH
203 return;
204
205 const QRect r = rect();
206 pair<Cursor, Cursor> &cursors = _view.cursors();
207 cursors.first.paint_label(p, r);
208 cursors.second.paint_label(p, r);
209}
210
b3a7c013
JH
211void Ruler::draw_hover_mark(QPainter &p)
212{
213 const int x = _view.hover_point().x();
333d5bbc
UH
214
215 if (x == -1 || _grabbed_marker)
b3a7c013
JH
216 return;
217
218 p.setPen(QPen(Qt::NoPen));
219 p.setBrush(QBrush(QColor(Qt::black)));
220
221 const int b = height() - 1;
222 const QPointF points[] = {
223 QPointF(x, b),
224 QPointF(x - HoverArrowSize, b - HoverArrowSize),
225 QPointF(x + HoverArrowSize, b - HoverArrowSize)
226 };
227 p.drawPolygon(points, countof(points));
228}
229
230void Ruler::hover_point_changed()
231{
232 update();
233}
234
ccdd3ef5
JH
235} // namespace view
236} // namespace pv