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