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