]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
Added TimeMarker popups
[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
a28a212c
JH
37#include <pv/widgets/popup.h>
38
58864c5c 39using namespace boost;
f76af637
JH
40using namespace std;
41
ccdd3ef5
JH
42namespace pv {
43namespace view {
44
45const int Ruler::MinorTickSubdivision = 4;
46const int Ruler::ScaleUnits[3] = {1, 2, 5};
47
48const QString Ruler::SIPrefixes[9] =
49 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
50const int Ruler::FirstSIPrefixPower = -15;
51
b3a7c013
JH
52const int Ruler::HoverArrowSize = 5;
53
ccdd3ef5 54Ruler::Ruler(View &parent) :
4030e03d
JH
55 MarginWidget(parent),
56 _dragging(false)
ccdd3ef5 57{
b3a7c013
JH
58 setMouseTracking(true);
59
60 connect(&_view, SIGNAL(hover_point_changed()),
61 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
62}
63
a2ae0205
JH
64void Ruler::clear_selection()
65{
66 CursorPair &cursors = _view.cursors();
58864c5c
JH
67 cursors.first()->select(false);
68 cursors.second()->select(false);
a2ae0205
JH
69 update();
70}
71
3a6fe081
JH
72QString Ruler::format_time(double t, unsigned int prefix,
73 unsigned int precision)
74{
75 const double multiplier = pow(10.0,
76 - prefix * 3 - FirstSIPrefixPower);
77
78 QString s;
79 QTextStream ts(&s);
80 ts.setRealNumberPrecision(precision);
81 ts << fixed << forcesign << (t * multiplier) <<
82 SIPrefixes[prefix] << "s";
83 return s;
84}
85
e314eca4 86void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 87{
3af62a24
JH
88 using namespace Qt;
89
3f509c1f
JH
90 const double SpacingIncrement = 32.0f;
91 const double MinValueSpacing = 32.0f;
f260e3bf 92 const int ValueMargin = 3;
3f509c1f 93
ccdd3ef5 94 QPainter p(this);
ce6e73a8 95 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 96
3f509c1f
JH
97 double min_width = SpacingIncrement, typical_width;
98 double tick_period;
99 unsigned int prefix;
ccdd3ef5 100
3f509c1f
JH
101 // Find tick spacing, and number formatting that does not cause
102 // value to collide.
103 do
104 {
105 const double min_period = _view.scale() * min_width;
ccdd3ef5 106
3f509c1f
JH
107 const int order = (int)floorf(log10f(min_period));
108 const double order_decimal = pow(10, order);
ccdd3ef5 109
3f509c1f 110 unsigned int unit = 0;
ccdd3ef5 111
3f509c1f
JH
112 do
113 {
114 tick_period = order_decimal * ScaleUnits[unit++];
115 } while (tick_period < min_period && unit < countof(ScaleUnits));
ccdd3ef5 116
3f509c1f
JH
117 prefix = (order - FirstSIPrefixPower) / 3;
118 assert(prefix < countof(SIPrefixes));
ccdd3ef5 119
3f509c1f
JH
120
121 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
122 AlignLeft | AlignTop, format_time(_view.offset(),
3a6fe081 123 prefix)).width() + MinValueSpacing;
3f509c1f
JH
124
125 min_width += SpacingIncrement;
126
127 } while(typical_width > tick_period / _view.scale());
1b5813fe 128
ccdd3ef5 129 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
3af62a24 130 AlignLeft | AlignTop, "8").height();
ccdd3ef5
JH
131
132 // Draw the tick marks
d2caed8d 133 p.setPen(palette().color(foregroundRole()));
ccdd3ef5
JH
134
135 const double minor_tick_period = tick_period / MinorTickSubdivision;
136 const double first_major_division =
137 floor(_view.offset() / tick_period);
138 const double first_minor_division =
139 ceil(_view.offset() / minor_tick_period);
140 const double t0 = first_major_division * tick_period;
141
142 int division = (int)round(first_minor_division -
64b678ba 143 first_major_division * MinorTickSubdivision) - 1;
f260e3bf
JH
144
145 const int major_tick_y1 = text_height + ValueMargin * 2;
146 const int tick_y2 = height();
147 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
148
64b678ba 149 double x;
ccdd3ef5 150
64b678ba
JH
151 do {
152 const double t = t0 + division * minor_tick_period;
153 x = (t - _view.offset()) / _view.scale();
ccdd3ef5 154
333d5bbc 155 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
156 {
157 // Draw a major tick
f260e3bf
JH
158 p.drawText(x, ValueMargin, 0, text_height,
159 AlignCenter | AlignTop | TextDontClip,
3a6fe081 160 format_time(t, prefix));
f260e3bf
JH
161 p.drawLine(QPointF(x, major_tick_y1),
162 QPointF(x, tick_y2));
ccdd3ef5
JH
163 }
164 else
165 {
166 // Draw a minor tick
f260e3bf
JH
167 p.drawLine(QPointF(x, minor_tick_y1),
168 QPointF(x, tick_y2));
ccdd3ef5
JH
169 }
170
171 division++;
64b678ba
JH
172
173 } while (x < width());
ccdd3ef5 174
f76af637 175 // Draw the cursors
332afa74
JH
176 if (_view.cursors_shown())
177 _view.cursors().draw_markers(p, rect(), prefix);
f76af637 178
b3a7c013
JH
179 // Draw the hover mark
180 draw_hover_mark(p);
181
ccdd3ef5
JH
182 p.end();
183}
184
ca4ec3ea
JH
185void Ruler::mouseMoveEvent(QMouseEvent *e)
186{
4030e03d
JH
187 if (!(e->buttons() & Qt::LeftButton))
188 return;
189
190 if ((e->pos() - _mouse_down_point).manhattanLength() <
191 QApplication::startDragDistance())
192 return;
193
194 _dragging = true;
195
58864c5c
JH
196 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
197 m->set_time(_view.offset() +
198 ((double)e->x() + 0.5) * _view.scale());
ca4ec3ea
JH
199}
200
201void Ruler::mousePressEvent(QMouseEvent *e)
202{
4030e03d
JH
203 if (e->buttons() & Qt::LeftButton)
204 {
205 _mouse_down_point = e->pos();
206
58864c5c 207 _grabbed_marker.reset();
ca4ec3ea 208
17348f85
JH
209 clear_selection();
210
333d5bbc 211 if (_view.cursors_shown()) {
b42d25c4 212 CursorPair &cursors = _view.cursors();
58864c5c 213 if (cursors.first()->get_label_rect(
ca4ec3ea 214 rect()).contains(e->pos()))
58864c5c
JH
215 _grabbed_marker = cursors.first();
216 else if (cursors.second()->get_label_rect(
ca4ec3ea 217 rect()).contains(e->pos()))
58864c5c 218 _grabbed_marker = cursors.second();
ca4ec3ea 219 }
b2a53645 220
58864c5c
JH
221 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
222 m->select();
17348f85 223
b2a53645 224 selection_changed();
ca4ec3ea
JH
225 }
226}
227
228void Ruler::mouseReleaseEvent(QMouseEvent *)
229{
a28a212c
JH
230 using pv::widgets::Popup;
231
232 if (!_dragging)
233 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock()) {
234 Popup *const p = m->create_popup(&_view);
235 p->set_position(mapToGlobal(QPoint(m->get_x(),
236 height())), Popup::Bottom);
237 p->show();
238 }
239
4030e03d 240 _dragging = false;
58864c5c 241 _grabbed_marker.reset();
ca4ec3ea
JH
242}
243
b3a7c013
JH
244void Ruler::draw_hover_mark(QPainter &p)
245{
246 const int x = _view.hover_point().x();
333d5bbc 247
4030e03d 248 if (x == -1 || _dragging)
b3a7c013
JH
249 return;
250
251 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 252 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013
JH
253
254 const int b = height() - 1;
255 const QPointF points[] = {
256 QPointF(x, b),
257 QPointF(x - HoverArrowSize, b - HoverArrowSize),
258 QPointF(x + HoverArrowSize, b - HoverArrowSize)
259 };
260 p.drawPolygon(points, countof(points));
261}
262
263void Ruler::hover_point_changed()
264{
265 update();
266}
267
ccdd3ef5
JH
268} // namespace view
269} // namespace pv