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