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