]>
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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <extdef.h> | |
21 | ||
22 | #include <QApplication> | |
23 | #include <QFontMetrics> | |
24 | #include <QMenu> | |
25 | #include <QMouseEvent> | |
26 | ||
27 | #include "ruler.hpp" | |
28 | #include "view.hpp" | |
29 | ||
30 | using namespace Qt; | |
31 | ||
32 | using std::function; | |
33 | using std::max; | |
34 | using std::min; | |
35 | using std::shared_ptr; | |
36 | using std::vector; | |
37 | ||
38 | namespace pv { | |
39 | namespace views { | |
40 | namespace trace { | |
41 | ||
42 | const float Ruler::RulerHeight = 2.5f; // x Text Height | |
43 | ||
44 | const float Ruler::HoverArrowSize = 0.5f; // x Text Height | |
45 | ||
46 | Ruler::Ruler(View &parent) : | |
47 | MarginWidget(parent) | |
48 | { | |
49 | setMouseTracking(true); | |
50 | ||
51 | connect(&view_, SIGNAL(hover_point_changed(QPoint)), | |
52 | this, SLOT(hover_point_changed(QPoint))); | |
53 | connect(&view_, SIGNAL(offset_changed()), | |
54 | this, SLOT(invalidate_tick_position_cache())); | |
55 | connect(&view_, SIGNAL(scale_changed()), | |
56 | this, SLOT(invalidate_tick_position_cache())); | |
57 | connect(&view_, SIGNAL(tick_prefix_changed()), | |
58 | this, SLOT(invalidate_tick_position_cache())); | |
59 | connect(&view_, SIGNAL(tick_precision_changed()), | |
60 | this, SLOT(invalidate_tick_position_cache())); | |
61 | connect(&view_, SIGNAL(tick_period_changed()), | |
62 | this, SLOT(invalidate_tick_position_cache())); | |
63 | connect(&view_, SIGNAL(time_unit_changed()), | |
64 | this, SLOT(invalidate_tick_position_cache())); | |
65 | } | |
66 | ||
67 | QSize Ruler::sizeHint() const | |
68 | { | |
69 | const int text_height = calculate_text_height(); | |
70 | return QSize(0, RulerHeight * text_height); | |
71 | } | |
72 | ||
73 | QSize Ruler::extended_size_hint() const | |
74 | { | |
75 | QRectF max_rect; | |
76 | vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
77 | for (auto &i : items) | |
78 | max_rect = max_rect.united(i->label_rect(QRect())); | |
79 | return QSize(0, sizeHint().height() - max_rect.top() / 2 + | |
80 | ViewItem::HighlightRadius); | |
81 | } | |
82 | ||
83 | QString Ruler::format_time_with_distance( | |
84 | const pv::util::Timestamp& distance, | |
85 | const pv::util::Timestamp& t, | |
86 | pv::util::SIPrefix prefix, | |
87 | pv::util::TimeUnit unit, | |
88 | unsigned precision, | |
89 | bool sign) | |
90 | { | |
91 | const unsigned limit = 60; | |
92 | ||
93 | if (t.is_zero()) | |
94 | return "0"; | |
95 | ||
96 | // If we have to use samples then we have no alternative formats | |
97 | if (unit == pv::util::TimeUnit::Samples) | |
98 | return pv::util::format_time_si_adjusted(t, prefix, precision, "sa", sign); | |
99 | ||
100 | // View zoomed way out -> low precision (0), big distance (>=60s) | |
101 | // -> DD:HH:MM | |
102 | if ((precision == 0) && (distance >= limit)) | |
103 | return pv::util::format_time_minutes(t, 0, sign); | |
104 | ||
105 | // View in "normal" range -> medium precision, medium step size | |
106 | // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds | |
107 | // View zoomed way in -> high precision (>3), low step size (<1s) | |
108 | // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds | |
109 | if (abs(t) < limit) | |
110 | return pv::util::format_time_si_adjusted(t, prefix, precision, "s", sign); | |
111 | else | |
112 | return pv::util::format_time_minutes(t, precision, sign); | |
113 | } | |
114 | ||
115 | pv::util::Timestamp Ruler::get_time_from_x_pos(uint32_t x) | |
116 | { | |
117 | return view_.ruler_offset() + ((double)x + 0.5) * view_.scale(); | |
118 | } | |
119 | ||
120 | void Ruler::contextMenuEvent(QContextMenuEvent *event) | |
121 | { | |
122 | context_menu_x_pos_ = event->pos().x(); | |
123 | ||
124 | QMenu *const menu = new QMenu(this); | |
125 | ||
126 | QAction *const create_marker = new QAction(tr("Create marker here"), this); | |
127 | connect(create_marker, SIGNAL(triggered()), this, SLOT(on_createMarker())); | |
128 | menu->addAction(create_marker); | |
129 | ||
130 | menu->popup(event->globalPos()); | |
131 | } | |
132 | ||
133 | void Ruler::resizeEvent(QResizeEvent*) | |
134 | { | |
135 | // the tick calculation depends on the width of this widget | |
136 | invalidate_tick_position_cache(); | |
137 | } | |
138 | ||
139 | vector< shared_ptr<ViewItem> > Ruler::items() | |
140 | { | |
141 | const vector< shared_ptr<TimeItem> > time_items(view_.time_items()); | |
142 | return vector< shared_ptr<ViewItem> >( | |
143 | time_items.begin(), time_items.end()); | |
144 | } | |
145 | ||
146 | shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt) | |
147 | { | |
148 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
149 | for (auto i = items.rbegin(); i != items.rend(); i++) | |
150 | if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt)) | |
151 | return *i; | |
152 | return nullptr; | |
153 | } | |
154 | ||
155 | void Ruler::mouseDoubleClickEvent(QMouseEvent *event) | |
156 | { | |
157 | view_.add_flag(get_time_from_x_pos(event->x())); | |
158 | } | |
159 | ||
160 | void Ruler::paintEvent(QPaintEvent*) | |
161 | { | |
162 | if (!tick_position_cache_) { | |
163 | auto ffunc = [this](const pv::util::Timestamp& t) { | |
164 | return format_time_with_distance( | |
165 | this->view_.tick_period(), | |
166 | t, | |
167 | this->view_.tick_prefix(), | |
168 | this->view_.time_unit(), | |
169 | this->view_.tick_precision()); | |
170 | }; | |
171 | ||
172 | tick_position_cache_ = calculate_tick_positions( | |
173 | view_.tick_period(), | |
174 | view_.ruler_offset(), | |
175 | view_.scale(), | |
176 | width(), | |
177 | view_.minor_tick_count(), | |
178 | ffunc); | |
179 | } | |
180 | ||
181 | const int ValueMargin = 3; | |
182 | ||
183 | const int text_height = calculate_text_height(); | |
184 | const int ruler_height = RulerHeight * text_height; | |
185 | const int major_tick_y1 = text_height + ValueMargin * 2; | |
186 | const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2; | |
187 | ||
188 | QPainter p(this); | |
189 | ||
190 | // Draw the tick marks | |
191 | p.setPen(palette().color(foregroundRole())); | |
192 | ||
193 | for (const auto& tick: tick_position_cache_->major) { | |
194 | const int leftedge = 0; | |
195 | const int rightedge = width(); | |
196 | const int x_tick = tick.first; | |
197 | if ((x_tick > leftedge) && (x_tick < rightedge)) { | |
198 | const int x_left_bound = QFontMetrics(font()).width(tick.second) / 2; | |
199 | const int x_right_bound = rightedge - x_left_bound; | |
200 | const int x_legend = min(max(x_tick, x_left_bound), x_right_bound); | |
201 | p.drawText(x_legend, ValueMargin, 0, text_height, | |
202 | AlignCenter | AlignTop | TextDontClip, tick.second); | |
203 | p.drawLine(QPointF(x_tick, major_tick_y1), | |
204 | QPointF(tick.first, ruler_height)); | |
205 | } | |
206 | } | |
207 | ||
208 | for (const auto& tick: tick_position_cache_->minor) { | |
209 | p.drawLine(QPointF(tick, minor_tick_y1), | |
210 | QPointF(tick, ruler_height)); | |
211 | } | |
212 | ||
213 | // Draw the hover mark | |
214 | draw_hover_mark(p, text_height); | |
215 | ||
216 | p.setRenderHint(QPainter::Antialiasing); | |
217 | ||
218 | // The cursor labels are not drawn with the arrows exactly on the | |
219 | // bottom line of the widget, because then the selection shadow | |
220 | // would be clipped away. | |
221 | const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius); | |
222 | ||
223 | // Draw the items | |
224 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
225 | for (auto &i : items) { | |
226 | const bool highlight = !item_dragging_ && | |
227 | i->label_rect(r).contains(mouse_point_); | |
228 | i->paint_label(p, r, highlight); | |
229 | } | |
230 | } | |
231 | ||
232 | void Ruler::draw_hover_mark(QPainter &p, int text_height) | |
233 | { | |
234 | const int x = view_.hover_point().x(); | |
235 | ||
236 | if (x == -1) | |
237 | return; | |
238 | ||
239 | p.setPen(QPen(Qt::NoPen)); | |
240 | p.setBrush(QBrush(palette().color(foregroundRole()))); | |
241 | ||
242 | const int b = RulerHeight * text_height; | |
243 | const float hover_arrow_size = HoverArrowSize * text_height; | |
244 | const QPointF points[] = { | |
245 | QPointF(x, b), | |
246 | QPointF(x - hover_arrow_size, b - hover_arrow_size), | |
247 | QPointF(x + hover_arrow_size, b - hover_arrow_size) | |
248 | }; | |
249 | p.drawPolygon(points, countof(points)); | |
250 | } | |
251 | ||
252 | int Ruler::calculate_text_height() const | |
253 | { | |
254 | return QFontMetrics(font()).ascent(); | |
255 | } | |
256 | ||
257 | TickPositions Ruler::calculate_tick_positions( | |
258 | const pv::util::Timestamp& major_period, | |
259 | const pv::util::Timestamp& offset, | |
260 | const double scale, | |
261 | const int width, | |
262 | const unsigned int minor_tick_count, | |
263 | function<QString(const pv::util::Timestamp&)> format_function) | |
264 | { | |
265 | TickPositions tp; | |
266 | ||
267 | const pv::util::Timestamp minor_period = major_period / minor_tick_count; | |
268 | const pv::util::Timestamp first_major_division = floor(offset / major_period); | |
269 | const pv::util::Timestamp first_minor_division = ceil(offset / minor_period); | |
270 | const pv::util::Timestamp t0 = first_major_division * major_period; | |
271 | ||
272 | int division = (round(first_minor_division - | |
273 | first_major_division * minor_tick_count)).convert_to<int>() - 1; | |
274 | ||
275 | double x; | |
276 | ||
277 | do { | |
278 | pv::util::Timestamp t = t0 + division * minor_period; | |
279 | x = ((t - offset) / scale).convert_to<double>(); | |
280 | ||
281 | if (division % minor_tick_count == 0) { | |
282 | // Recalculate 't' without using 'minor_period' which is a fraction | |
283 | t = t0 + division / minor_tick_count * major_period; | |
284 | tp.major.emplace_back(x, format_function(t)); | |
285 | } else { | |
286 | tp.minor.emplace_back(x); | |
287 | } | |
288 | ||
289 | division++; | |
290 | } while (x < width); | |
291 | ||
292 | return tp; | |
293 | } | |
294 | ||
295 | void Ruler::hover_point_changed(const QPoint &hp) | |
296 | { | |
297 | (void)hp; | |
298 | ||
299 | update(); | |
300 | } | |
301 | ||
302 | void Ruler::invalidate_tick_position_cache() | |
303 | { | |
304 | tick_position_cache_ = boost::none; | |
305 | } | |
306 | ||
307 | void Ruler::on_createMarker() | |
308 | { | |
309 | view_.add_flag(get_time_from_x_pos(context_menu_x_pos_)); | |
310 | } | |
311 | ||
312 | } // namespace trace | |
313 | } // namespace views | |
314 | } // namespace pv |