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