]>
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 | ||
f76af637 JH |
37 | using namespace std; |
38 | ||
ccdd3ef5 JH |
39 | namespace pv { |
40 | namespace view { | |
41 | ||
42 | const int Ruler::MinorTickSubdivision = 4; | |
43 | const int Ruler::ScaleUnits[3] = {1, 2, 5}; | |
44 | ||
45 | const QString Ruler::SIPrefixes[9] = | |
46 | {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"}; | |
47 | const int Ruler::FirstSIPrefixPower = -15; | |
48 | ||
b3a7c013 JH |
49 | const int Ruler::HoverArrowSize = 5; |
50 | ||
ccdd3ef5 JH |
51 | Ruler::Ruler(View &parent) : |
52 | QWidget(&parent), | |
ca4ec3ea JH |
53 | _view(parent), |
54 | _grabbed_marker(NULL) | |
ccdd3ef5 | 55 | { |
b3a7c013 JH |
56 | setMouseTracking(true); |
57 | ||
58 | connect(&_view, SIGNAL(hover_point_changed()), | |
59 | this, SLOT(hover_point_changed())); | |
ccdd3ef5 JH |
60 | } |
61 | ||
e314eca4 | 62 | void Ruler::paintEvent(QPaintEvent*) |
ccdd3ef5 | 63 | { |
3af62a24 JH |
64 | using namespace Qt; |
65 | ||
ccdd3ef5 | 66 | QPainter p(this); |
ce6e73a8 | 67 | p.setRenderHint(QPainter::Antialiasing); |
ccdd3ef5 JH |
68 | |
69 | const double MinSpacing = 80; | |
70 | ||
71 | const double min_period = _view.scale() * MinSpacing; | |
72 | ||
73 | const int order = (int)floorf(log10f(min_period)); | |
74 | const double order_decimal = pow(10, order); | |
75 | ||
cfa7293b | 76 | unsigned int unit = 0; |
ccdd3ef5 JH |
77 | double tick_period = 0.0f; |
78 | ||
79 | do | |
80 | { | |
81 | tick_period = order_decimal * ScaleUnits[unit++]; | |
333d5bbc | 82 | } while (tick_period < min_period && unit < countof(ScaleUnits)); |
ccdd3ef5 | 83 | |
cfa7293b | 84 | const unsigned int prefix = (order - FirstSIPrefixPower) / 3; |
ccdd3ef5 JH |
85 | assert(prefix < countof(SIPrefixes)); |
86 | ||
397c9541 | 87 | const double multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower); |
1b5813fe | 88 | |
ccdd3ef5 | 89 | const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX, |
3af62a24 | 90 | AlignLeft | AlignTop, "8").height(); |
ccdd3ef5 JH |
91 | |
92 | // Draw the tick marks | |
d2caed8d | 93 | p.setPen(palette().color(foregroundRole())); |
ccdd3ef5 JH |
94 | |
95 | const double minor_tick_period = tick_period / MinorTickSubdivision; | |
96 | const double first_major_division = | |
97 | floor(_view.offset() / tick_period); | |
98 | const double first_minor_division = | |
99 | ceil(_view.offset() / minor_tick_period); | |
100 | const double t0 = first_major_division * tick_period; | |
101 | ||
102 | int division = (int)round(first_minor_division - | |
103 | first_major_division * MinorTickSubdivision); | |
333d5bbc | 104 | while (1) |
ccdd3ef5 JH |
105 | { |
106 | const double t = t0 + division * minor_tick_period; | |
107 | const double x = (t - _view.offset()) / _view.scale(); | |
108 | ||
333d5bbc | 109 | if (x >= width()) |
ccdd3ef5 JH |
110 | break; |
111 | ||
333d5bbc | 112 | if (division % MinorTickSubdivision == 0) |
ccdd3ef5 JH |
113 | { |
114 | // Draw a major tick | |
115 | QString s; | |
116 | QTextStream ts(&s); | |
1b5813fe | 117 | ts << (t * multiplier) << SIPrefixes[prefix] << "s"; |
3af62a24 JH |
118 | p.drawText(x, 0, 0, text_height, AlignCenter | |
119 | AlignTop | TextDontClip, s); | |
ce6e73a8 JH |
120 | p.drawLine(QPointF(x, text_height), |
121 | QPointF(x, height())); | |
ccdd3ef5 JH |
122 | } |
123 | else | |
124 | { | |
125 | // Draw a minor tick | |
ce6e73a8 JH |
126 | p.drawLine(QPointF(x, (text_height + height()) / 2), |
127 | QPointF(x, height())); | |
ccdd3ef5 JH |
128 | } |
129 | ||
130 | division++; | |
131 | } | |
132 | ||
f76af637 JH |
133 | // Draw the cursors |
134 | draw_cursors(p); | |
135 | ||
b3a7c013 JH |
136 | // Draw the hover mark |
137 | draw_hover_mark(p); | |
138 | ||
ccdd3ef5 JH |
139 | p.end(); |
140 | } | |
141 | ||
ca4ec3ea JH |
142 | void Ruler::mouseMoveEvent(QMouseEvent *e) |
143 | { | |
333d5bbc | 144 | if (!_grabbed_marker) |
ca4ec3ea JH |
145 | return; |
146 | ||
147 | _grabbed_marker->set_time(_view.offset() + | |
148 | ((double)e->x() + 0.5) * _view.scale()); | |
149 | } | |
150 | ||
151 | void Ruler::mousePressEvent(QMouseEvent *e) | |
152 | { | |
333d5bbc | 153 | if (e->buttons() & Qt::LeftButton) { |
ca4ec3ea JH |
154 | _grabbed_marker = NULL; |
155 | ||
333d5bbc | 156 | if (_view.cursors_shown()) { |
ca4ec3ea JH |
157 | std::pair<Cursor, Cursor> &cursors = |
158 | _view.cursors(); | |
333d5bbc | 159 | if (cursors.first.get_label_rect( |
ca4ec3ea JH |
160 | rect()).contains(e->pos())) |
161 | _grabbed_marker = &cursors.first; | |
333d5bbc | 162 | else if (cursors.second.get_label_rect( |
ca4ec3ea JH |
163 | rect()).contains(e->pos())) |
164 | _grabbed_marker = &cursors.second; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | void Ruler::mouseReleaseEvent(QMouseEvent *) | |
170 | { | |
171 | _grabbed_marker = NULL; | |
172 | } | |
173 | ||
f76af637 JH |
174 | void Ruler::draw_cursors(QPainter &p) |
175 | { | |
333d5bbc | 176 | if (!_view.cursors_shown()) |
f76af637 JH |
177 | return; |
178 | ||
179 | const QRect r = rect(); | |
180 | pair<Cursor, Cursor> &cursors = _view.cursors(); | |
181 | cursors.first.paint_label(p, r); | |
182 | cursors.second.paint_label(p, r); | |
183 | } | |
184 | ||
b3a7c013 JH |
185 | void Ruler::draw_hover_mark(QPainter &p) |
186 | { | |
187 | const int x = _view.hover_point().x(); | |
333d5bbc UH |
188 | |
189 | if (x == -1 || _grabbed_marker) | |
b3a7c013 JH |
190 | return; |
191 | ||
192 | p.setPen(QPen(Qt::NoPen)); | |
193 | p.setBrush(QBrush(QColor(Qt::black))); | |
194 | ||
195 | const int b = height() - 1; | |
196 | const QPointF points[] = { | |
197 | QPointF(x, b), | |
198 | QPointF(x - HoverArrowSize, b - HoverArrowSize), | |
199 | QPointF(x + HoverArrowSize, b - HoverArrowSize) | |
200 | }; | |
201 | p.drawPolygon(points, countof(points)); | |
202 | } | |
203 | ||
204 | void Ruler::hover_point_changed() | |
205 | { | |
206 | update(); | |
207 | } | |
208 | ||
ccdd3ef5 JH |
209 | } // namespace view |
210 | } // namespace pv |