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