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