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