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