]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
Added header time hover arrow
[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"
22#include "view.h"
b3a7c013 23#include "viewport.h"
ccdd3ef5 24
cef18fc6 25#include <extdef.h>
ccdd3ef5
JH
26
27#include <assert.h>
28#include <math.h>
29
b3a7c013 30#include <QMouseEvent>
ccdd3ef5
JH
31#include <QPainter>
32#include <QTextStream>
33
34namespace pv {
35namespace view {
36
37const int Ruler::MinorTickSubdivision = 4;
38const int Ruler::ScaleUnits[3] = {1, 2, 5};
39
40const QString Ruler::SIPrefixes[9] =
41 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
42const int Ruler::FirstSIPrefixPower = -15;
43
b3a7c013
JH
44const int Ruler::HoverArrowSize = 5;
45
ccdd3ef5
JH
46Ruler::Ruler(View &parent) :
47 QWidget(&parent),
48 _view(parent)
49{
b3a7c013
JH
50 setMouseTracking(true);
51
52 connect(&_view, SIGNAL(hover_point_changed()),
53 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
54}
55
56void Ruler::paintEvent(QPaintEvent *event)
57{
58 QPainter p(this);
ce6e73a8 59 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5
JH
60
61 const double MinSpacing = 80;
62
63 const double min_period = _view.scale() * MinSpacing;
64
65 const int order = (int)floorf(log10f(min_period));
66 const double order_decimal = pow(10, order);
67
cfa7293b 68 unsigned int unit = 0;
ccdd3ef5
JH
69 double tick_period = 0.0f;
70
71 do
72 {
73 tick_period = order_decimal * ScaleUnits[unit++];
74 } while(tick_period < min_period && unit < countof(ScaleUnits));
75
cfa7293b 76 const unsigned int prefix = (order - FirstSIPrefixPower) / 3;
ccdd3ef5
JH
77 assert(prefix >= 0);
78 assert(prefix < countof(SIPrefixes));
79
397c9541 80 const double multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
1b5813fe 81
ccdd3ef5
JH
82 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
83 Qt::AlignLeft | Qt::AlignTop, "8").height();
84
85 // Draw the tick marks
86 p.setPen(Qt::black);
87
88 const double minor_tick_period = tick_period / MinorTickSubdivision;
89 const double first_major_division =
90 floor(_view.offset() / tick_period);
91 const double first_minor_division =
92 ceil(_view.offset() / minor_tick_period);
93 const double t0 = first_major_division * tick_period;
94
95 int division = (int)round(first_minor_division -
96 first_major_division * MinorTickSubdivision);
97 while(1)
98 {
99 const double t = t0 + division * minor_tick_period;
100 const double x = (t - _view.offset()) / _view.scale();
101
102 if(x >= width())
103 break;
104
105 if(division % MinorTickSubdivision == 0)
106 {
107 // Draw a major tick
108 QString s;
109 QTextStream ts(&s);
1b5813fe 110 ts << (t * multiplier) << SIPrefixes[prefix] << "s";
ccdd3ef5
JH
111 p.drawText(x, 0, 0, text_height, Qt::AlignCenter |
112 Qt::AlignTop | Qt::TextDontClip, s);
ce6e73a8
JH
113 p.drawLine(QPointF(x, text_height),
114 QPointF(x, height()));
ccdd3ef5
JH
115 }
116 else
117 {
118 // Draw a minor tick
ce6e73a8
JH
119 p.drawLine(QPointF(x, (text_height + height()) / 2),
120 QPointF(x, height()));
ccdd3ef5
JH
121 }
122
123 division++;
124 }
125
b3a7c013
JH
126 // Draw the hover mark
127 draw_hover_mark(p);
128
ccdd3ef5
JH
129 p.end();
130}
131
b3a7c013
JH
132void Ruler::draw_hover_mark(QPainter &p)
133{
134 const int x = _view.hover_point().x();
135 if(x == -1)
136 return;
137
138 p.setPen(QPen(Qt::NoPen));
139 p.setBrush(QBrush(QColor(Qt::black)));
140
141 const int b = height() - 1;
142 const QPointF points[] = {
143 QPointF(x, b),
144 QPointF(x - HoverArrowSize, b - HoverArrowSize),
145 QPointF(x + HoverArrowSize, b - HoverArrowSize)
146 };
147 p.drawPolygon(points, countof(points));
148}
149
150void Ruler::hover_point_changed()
151{
152 update();
153}
154
ccdd3ef5
JH
155} // namespace view
156} // namespace pv