]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
Replaced std::pair<Cursor, Cursor> with CursorPair
[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
3a6fe081
JH
62QString Ruler::format_time(double t, unsigned int prefix,
63 unsigned int precision)
64{
65 const double multiplier = pow(10.0,
66 - prefix * 3 - FirstSIPrefixPower);
67
68 QString s;
69 QTextStream ts(&s);
70 ts.setRealNumberPrecision(precision);
71 ts << fixed << forcesign << (t * multiplier) <<
72 SIPrefixes[prefix] << "s";
73 return s;
74}
75
e314eca4 76void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 77{
3af62a24
JH
78 using namespace Qt;
79
3f509c1f
JH
80 const double SpacingIncrement = 32.0f;
81 const double MinValueSpacing = 32.0f;
f260e3bf 82 const int ValueMargin = 3;
3f509c1f 83
ccdd3ef5 84 QPainter p(this);
ce6e73a8 85 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 86
3f509c1f
JH
87 double min_width = SpacingIncrement, typical_width;
88 double tick_period;
89 unsigned int prefix;
ccdd3ef5 90
3f509c1f
JH
91 // Find tick spacing, and number formatting that does not cause
92 // value to collide.
93 do
94 {
95 const double min_period = _view.scale() * min_width;
ccdd3ef5 96
3f509c1f
JH
97 const int order = (int)floorf(log10f(min_period));
98 const double order_decimal = pow(10, order);
ccdd3ef5 99
3f509c1f 100 unsigned int unit = 0;
ccdd3ef5 101
3f509c1f
JH
102 do
103 {
104 tick_period = order_decimal * ScaleUnits[unit++];
105 } while (tick_period < min_period && unit < countof(ScaleUnits));
ccdd3ef5 106
3f509c1f
JH
107 prefix = (order - FirstSIPrefixPower) / 3;
108 assert(prefix < countof(SIPrefixes));
ccdd3ef5 109
3f509c1f
JH
110
111 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
112 AlignLeft | AlignTop, format_time(_view.offset(),
3a6fe081 113 prefix)).width() + MinValueSpacing;
3f509c1f
JH
114
115 min_width += SpacingIncrement;
116
117 } while(typical_width > tick_period / _view.scale());
1b5813fe 118
ccdd3ef5 119 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
3af62a24 120 AlignLeft | AlignTop, "8").height();
ccdd3ef5
JH
121
122 // Draw the tick marks
d2caed8d 123 p.setPen(palette().color(foregroundRole()));
ccdd3ef5
JH
124
125 const double minor_tick_period = tick_period / MinorTickSubdivision;
126 const double first_major_division =
127 floor(_view.offset() / tick_period);
128 const double first_minor_division =
129 ceil(_view.offset() / minor_tick_period);
130 const double t0 = first_major_division * tick_period;
131
132 int division = (int)round(first_minor_division -
64b678ba 133 first_major_division * MinorTickSubdivision) - 1;
f260e3bf
JH
134
135 const int major_tick_y1 = text_height + ValueMargin * 2;
136 const int tick_y2 = height();
137 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
138
64b678ba 139 double x;
ccdd3ef5 140
64b678ba
JH
141 do {
142 const double t = t0 + division * minor_tick_period;
143 x = (t - _view.offset()) / _view.scale();
ccdd3ef5 144
333d5bbc 145 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
146 {
147 // Draw a major tick
f260e3bf
JH
148 p.drawText(x, ValueMargin, 0, text_height,
149 AlignCenter | AlignTop | TextDontClip,
3a6fe081 150 format_time(t, prefix));
f260e3bf
JH
151 p.drawLine(QPointF(x, major_tick_y1),
152 QPointF(x, tick_y2));
ccdd3ef5
JH
153 }
154 else
155 {
156 // Draw a minor tick
f260e3bf
JH
157 p.drawLine(QPointF(x, minor_tick_y1),
158 QPointF(x, tick_y2));
ccdd3ef5
JH
159 }
160
161 division++;
64b678ba
JH
162
163 } while (x < width());
ccdd3ef5 164
f76af637 165 // Draw the cursors
3a6fe081 166 draw_cursors(p, prefix);
f76af637 167
b3a7c013
JH
168 // Draw the hover mark
169 draw_hover_mark(p);
170
ccdd3ef5
JH
171 p.end();
172}
173
ca4ec3ea
JH
174void Ruler::mouseMoveEvent(QMouseEvent *e)
175{
333d5bbc 176 if (!_grabbed_marker)
ca4ec3ea
JH
177 return;
178
179 _grabbed_marker->set_time(_view.offset() +
180 ((double)e->x() + 0.5) * _view.scale());
181}
182
183void Ruler::mousePressEvent(QMouseEvent *e)
184{
333d5bbc 185 if (e->buttons() & Qt::LeftButton) {
ca4ec3ea
JH
186 _grabbed_marker = NULL;
187
333d5bbc 188 if (_view.cursors_shown()) {
b42d25c4
JH
189 CursorPair &cursors = _view.cursors();
190 if (cursors.first().get_label_rect(
ca4ec3ea 191 rect()).contains(e->pos()))
b42d25c4
JH
192 _grabbed_marker = &cursors.first();
193 else if (cursors.second().get_label_rect(
ca4ec3ea 194 rect()).contains(e->pos()))
b42d25c4 195 _grabbed_marker = &cursors.second();
ca4ec3ea
JH
196 }
197 }
198}
199
200void Ruler::mouseReleaseEvent(QMouseEvent *)
201{
202 _grabbed_marker = NULL;
203}
204
3a6fe081 205void Ruler::draw_cursors(QPainter &p, unsigned int prefix)
f76af637 206{
333d5bbc 207 if (!_view.cursors_shown())
f76af637
JH
208 return;
209
210 const QRect r = rect();
b42d25c4
JH
211 CursorPair &cursors = _view.cursors();
212 cursors.first().paint_label(p, r, prefix);
213 cursors.second().paint_label(p, r, prefix);
f76af637
JH
214}
215
b3a7c013
JH
216void Ruler::draw_hover_mark(QPainter &p)
217{
218 const int x = _view.hover_point().x();
333d5bbc
UH
219
220 if (x == -1 || _grabbed_marker)
b3a7c013
JH
221 return;
222
223 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 224 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013
JH
225
226 const int b = height() - 1;
227 const QPointF points[] = {
228 QPointF(x, b),
229 QPointF(x - HoverArrowSize, b - HoverArrowSize),
230 QPointF(x + HoverArrowSize, b - HoverArrowSize)
231 };
232 p.drawPolygon(points, countof(points));
233}
234
235void Ruler::hover_point_changed()
236{
237 update();
238}
239
ccdd3ef5
JH
240} // namespace view
241} // namespace pv