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