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