]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
ccdd3ef5 JH |
18 | */ |
19 | ||
819e2e95 | 20 | #include <extdef.h> |
ca4ec3ea | 21 | |
819e2e95 JH |
22 | #include <QApplication> |
23 | #include <QFontMetrics> | |
2543cd4e | 24 | #include <QMenu> |
819e2e95 JH |
25 | #include <QMouseEvent> |
26 | ||
4bc9230c SA |
27 | #include <pv/globalsettings.hpp> |
28 | ||
819e2e95 | 29 | #include "ruler.hpp" |
2acdb232 | 30 | #include "view.hpp" |
ccdd3ef5 | 31 | |
819f4c25 | 32 | using namespace Qt; |
f76af637 | 33 | |
6f925ba9 | 34 | using std::function; |
5408524d C |
35 | using std::max; |
36 | using std::min; | |
819e2e95 JH |
37 | using std::shared_ptr; |
38 | using std::vector; | |
39 | ||
ccdd3ef5 | 40 | namespace pv { |
f4e57597 | 41 | namespace views { |
1573bf16 | 42 | namespace trace { |
ccdd3ef5 | 43 | |
3d79f521 | 44 | const float Ruler::RulerHeight = 2.5f; // x Text Height |
ccdd3ef5 | 45 | |
3d79f521 | 46 | const float Ruler::HoverArrowSize = 0.5f; // x Text Height |
b3a7c013 | 47 | |
ccdd3ef5 | 48 | Ruler::Ruler(View &parent) : |
e456e8e1 | 49 | MarginWidget(parent) |
ccdd3ef5 | 50 | { |
b3a7c013 JH |
51 | setMouseTracking(true); |
52 | ||
581724de SA |
53 | connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)), |
54 | this, SLOT(on_hover_point_changed(const QWidget*, QPoint))); | |
4b0af0b6 JS |
55 | connect(&view_, SIGNAL(offset_changed()), |
56 | this, SLOT(invalidate_tick_position_cache())); | |
57 | connect(&view_, SIGNAL(scale_changed()), | |
58 | this, SLOT(invalidate_tick_position_cache())); | |
59 | connect(&view_, SIGNAL(tick_prefix_changed()), | |
60 | this, SLOT(invalidate_tick_position_cache())); | |
61 | connect(&view_, SIGNAL(tick_precision_changed()), | |
62 | this, SLOT(invalidate_tick_position_cache())); | |
63 | connect(&view_, SIGNAL(tick_period_changed()), | |
64 | this, SLOT(invalidate_tick_position_cache())); | |
65 | connect(&view_, SIGNAL(time_unit_changed()), | |
66 | this, SLOT(invalidate_tick_position_cache())); | |
ccdd3ef5 JH |
67 | } |
68 | ||
a6c1726e JH |
69 | QSize Ruler::sizeHint() const |
70 | { | |
249229ec JH |
71 | const int text_height = calculate_text_height(); |
72 | return QSize(0, RulerHeight * text_height); | |
a6c1726e JH |
73 | } |
74 | ||
819e2e95 JH |
75 | QSize Ruler::extended_size_hint() const |
76 | { | |
f5b833c6 | 77 | QRectF max_rect; |
6f925ba9 | 78 | vector< shared_ptr<TimeItem> > items(view_.time_items()); |
f5b833c6 JH |
79 | for (auto &i : items) |
80 | max_rect = max_rect.united(i->label_rect(QRect())); | |
81 | return QSize(0, sizeHint().height() - max_rect.top() / 2 + | |
82 | ViewItem::HighlightRadius); | |
819e2e95 JH |
83 | } |
84 | ||
3ccf0f7f JS |
85 | QString Ruler::format_time_with_distance( |
86 | const pv::util::Timestamp& distance, | |
87 | const pv::util::Timestamp& t, | |
88 | pv::util::SIPrefix prefix, | |
89 | pv::util::TimeUnit unit, | |
90 | unsigned precision, | |
66279897 | 91 | bool sign) |
3ccf0f7f JS |
92 | { |
93 | const unsigned limit = 60; | |
94 | ||
95 | if (t.is_zero()) | |
96 | return "0"; | |
97 | ||
98 | // If we have to use samples then we have no alternative formats | |
99 | if (unit == pv::util::TimeUnit::Samples) | |
66279897 SA |
100 | return pv::util::format_time_si_adjusted(t, prefix, precision, "sa", sign); |
101 | ||
102 | QString unit_string; | |
103 | if (unit == pv::util::TimeUnit::Time) | |
104 | unit_string = "s"; | |
105 | // Note: In case of pv::util::TimeUnit::None, unit_string remains empty | |
3ccf0f7f JS |
106 | |
107 | // View zoomed way out -> low precision (0), big distance (>=60s) | |
108 | // -> DD:HH:MM | |
109 | if ((precision == 0) && (distance >= limit)) | |
110 | return pv::util::format_time_minutes(t, 0, sign); | |
111 | ||
112 | // View in "normal" range -> medium precision, medium step size | |
113 | // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds | |
114 | // View zoomed way in -> high precision (>3), low step size (<1s) | |
115 | // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds | |
116 | if (abs(t) < limit) | |
66279897 | 117 | return pv::util::format_time_si_adjusted(t, prefix, precision, unit_string, sign); |
3ccf0f7f JS |
118 | else |
119 | return pv::util::format_time_minutes(t, precision, sign); | |
120 | } | |
121 | ||
fe68068b SA |
122 | pv::util::Timestamp Ruler::get_absolute_time_from_x_pos(uint32_t x) const |
123 | { | |
124 | return view_.offset() + ((double)x + 0.5) * view_.scale(); | |
125 | } | |
126 | ||
127 | pv::util::Timestamp Ruler::get_ruler_time_from_x_pos(uint32_t x) const | |
2543cd4e SA |
128 | { |
129 | return view_.ruler_offset() + ((double)x + 0.5) * view_.scale(); | |
130 | } | |
131 | ||
4468ee42 SA |
132 | pv::util::Timestamp Ruler::get_ruler_time_from_absolute_time(const pv::util::Timestamp& abs_time) const |
133 | { | |
134 | return abs_time + view_.zero_offset(); | |
135 | } | |
136 | ||
137 | pv::util::Timestamp Ruler::get_absolute_time_from_ruler_time(const pv::util::Timestamp& ruler_time) const | |
138 | { | |
139 | return ruler_time - view_.zero_offset(); | |
140 | } | |
141 | ||
2543cd4e SA |
142 | void Ruler::contextMenuEvent(QContextMenuEvent *event) |
143 | { | |
dde5aab3 SA |
144 | MarginWidget::contextMenuEvent(event); |
145 | ||
146 | // Don't show a context menu if the MarginWidget found a widget that shows one | |
147 | if (event->isAccepted()) | |
148 | return; | |
149 | ||
2543cd4e SA |
150 | context_menu_x_pos_ = event->pos().x(); |
151 | ||
152 | QMenu *const menu = new QMenu(this); | |
153 | ||
154 | QAction *const create_marker = new QAction(tr("Create marker here"), this); | |
155 | connect(create_marker, SIGNAL(triggered()), this, SLOT(on_createMarker())); | |
156 | menu->addAction(create_marker); | |
157 | ||
e23567ed SA |
158 | QAction *const set_zero_position = new QAction(tr("Set as zero point"), this); |
159 | connect(set_zero_position, SIGNAL(triggered()), this, SLOT(on_setZeroPosition())); | |
160 | menu->addAction(set_zero_position); | |
161 | ||
4bc9230c SA |
162 | QAction *const toggle_hover_marker = new QAction(this); |
163 | connect(toggle_hover_marker, SIGNAL(triggered()), this, SLOT(on_toggleHoverMarker())); | |
164 | menu->addAction(toggle_hover_marker); | |
165 | ||
166 | GlobalSettings settings; | |
167 | const bool hover_marker_shown = | |
168 | settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool(); | |
169 | toggle_hover_marker->setText(hover_marker_shown ? | |
170 | tr("Disable mouse hover marker") : tr("Enable mouse hover marker")); | |
171 | ||
dde5aab3 | 172 | event->setAccepted(true); |
2543cd4e SA |
173 | menu->popup(event->globalPos()); |
174 | } | |
175 | ||
176 | void Ruler::resizeEvent(QResizeEvent*) | |
177 | { | |
178 | // the tick calculation depends on the width of this widget | |
179 | invalidate_tick_position_cache(); | |
180 | } | |
181 | ||
3e124bee JH |
182 | vector< shared_ptr<ViewItem> > Ruler::items() |
183 | { | |
184 | const vector< shared_ptr<TimeItem> > time_items(view_.time_items()); | |
185 | return vector< shared_ptr<ViewItem> >( | |
186 | time_items.begin(), time_items.end()); | |
187 | } | |
188 | ||
710c2a18 | 189 | void Ruler::item_hover(const shared_ptr<ViewItem> &item, QPoint pos) |
190 | { | |
191 | Q_UNUSED(pos); | |
192 | hover_item_ = dynamic_pointer_cast<TimeItem>(item); | |
193 | } | |
194 | ||
195 | shared_ptr<TimeItem> Ruler::get_reference_item() | |
196 | { | |
197 | if (mouse_modifiers_ & Qt::ShiftModifier) | |
198 | return nullptr; | |
199 | ||
200 | if (hover_item_ != nullptr) | |
201 | return hover_item_; | |
202 | ||
203 | shared_ptr<TimeItem> found(nullptr); | |
204 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
205 | for (auto i = items.rbegin(); i != items.rend(); i++) { | |
206 | if ((*i)->enabled() && (*i)->selected()) { | |
207 | if (found == nullptr) | |
208 | found = *i; | |
209 | else | |
210 | return nullptr; // Return null if multiple items are selected | |
211 | } | |
212 | } | |
213 | ||
214 | return found; | |
215 | } | |
216 | ||
6871ee9f | 217 | shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt) |
25a0d47c JH |
218 | { |
219 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
220 | for (auto i = items.rbegin(); i != items.rend(); i++) | |
221 | if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt)) | |
222 | return *i; | |
223 | return nullptr; | |
224 | } | |
225 | ||
2543cd4e SA |
226 | void Ruler::mouseDoubleClickEvent(QMouseEvent *event) |
227 | { | |
710c2a18 | 228 | hover_item_ = view_.add_flag(get_absolute_time_from_x_pos(event->x())); |
2543cd4e SA |
229 | } |
230 | ||
e314eca4 | 231 | void Ruler::paintEvent(QPaintEvent*) |
ccdd3ef5 | 232 | { |
4b0af0b6 | 233 | if (!tick_position_cache_) { |
2ad82c2e | 234 | auto ffunc = [this](const pv::util::Timestamp& t) { |
3ccf0f7f JS |
235 | return format_time_with_distance( |
236 | this->view_.tick_period(), | |
4b0af0b6 JS |
237 | t, |
238 | this->view_.tick_prefix(), | |
239 | this->view_.time_unit(), | |
240 | this->view_.tick_precision()); | |
241 | }; | |
242 | ||
7c6cbdda | 243 | tick_position_cache_ = calculate_tick_positions( |
4b0af0b6 | 244 | view_.tick_period(), |
ffc00fdd | 245 | view_.ruler_offset(), |
4b0af0b6 JS |
246 | view_.scale(), |
247 | width(), | |
4a076157 | 248 | view_.minor_tick_count(), |
7c6cbdda | 249 | ffunc); |
4b0af0b6 | 250 | } |
ccdd3ef5 | 251 | |
4b0af0b6 | 252 | const int ValueMargin = 3; |
f260e3bf | 253 | |
e456e8e1 | 254 | const int text_height = calculate_text_height(); |
249229ec | 255 | const int ruler_height = RulerHeight * text_height; |
e456e8e1 | 256 | const int major_tick_y1 = text_height + ValueMargin * 2; |
249229ec | 257 | const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2; |
f260e3bf | 258 | |
4b0af0b6 | 259 | QPainter p(this); |
ccdd3ef5 | 260 | |
4b0af0b6 JS |
261 | // Draw the tick marks |
262 | p.setPen(palette().color(foregroundRole())); | |
ccdd3ef5 | 263 | |
4b0af0b6 | 264 | for (const auto& tick: tick_position_cache_->major) { |
5408524d C |
265 | const int leftedge = 0; |
266 | const int rightedge = width(); | |
267 | const int x_tick = tick.first; | |
268 | if ((x_tick > leftedge) && (x_tick < rightedge)) { | |
269 | const int x_left_bound = QFontMetrics(font()).width(tick.second) / 2; | |
270 | const int x_right_bound = rightedge - x_left_bound; | |
271 | const int x_legend = min(max(x_tick, x_left_bound), x_right_bound); | |
272 | p.drawText(x_legend, ValueMargin, 0, text_height, | |
4b0af0b6 | 273 | AlignCenter | AlignTop | TextDontClip, tick.second); |
5408524d | 274 | p.drawLine(QPointF(x_tick, major_tick_y1), |
4b0af0b6 | 275 | QPointF(tick.first, ruler_height)); |
5408524d | 276 | } |
4b0af0b6 | 277 | } |
ccdd3ef5 | 278 | |
4b0af0b6 JS |
279 | for (const auto& tick: tick_position_cache_->minor) { |
280 | p.drawLine(QPointF(tick, minor_tick_y1), | |
281 | QPointF(tick, ruler_height)); | |
282 | } | |
ccdd3ef5 | 283 | |
b3a7c013 | 284 | // Draw the hover mark |
249229ec | 285 | draw_hover_mark(p, text_height); |
819e2e95 | 286 | |
97f71253 JS |
287 | p.setRenderHint(QPainter::Antialiasing); |
288 | ||
819e2e95 JH |
289 | // The cursor labels are not drawn with the arrows exactly on the |
290 | // bottom line of the widget, because then the selection shadow | |
291 | // would be clipped away. | |
09d5df11 | 292 | const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius); |
819e2e95 JH |
293 | |
294 | // Draw the items | |
295 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
49028d6c | 296 | for (auto &i : items) { |
803cdac4 | 297 | const bool highlight = !item_dragging_ && |
49028d6c JH |
298 | i->label_rect(r).contains(mouse_point_); |
299 | i->paint_label(p, r, highlight); | |
300 | } | |
819e2e95 JH |
301 | } |
302 | ||
2543cd4e SA |
303 | void Ruler::draw_hover_mark(QPainter &p, int text_height) |
304 | { | |
305 | const int x = view_.hover_point().x(); | |
306 | ||
307 | if (x == -1) | |
308 | return; | |
309 | ||
310 | p.setPen(QPen(Qt::NoPen)); | |
311 | p.setBrush(QBrush(palette().color(foregroundRole()))); | |
312 | ||
313 | const int b = RulerHeight * text_height; | |
314 | const float hover_arrow_size = HoverArrowSize * text_height; | |
315 | const QPointF points[] = { | |
316 | QPointF(x, b), | |
317 | QPointF(x - hover_arrow_size, b - hover_arrow_size), | |
318 | QPointF(x + hover_arrow_size, b - hover_arrow_size) | |
319 | }; | |
320 | p.drawPolygon(points, countof(points)); | |
321 | } | |
322 | ||
323 | int Ruler::calculate_text_height() const | |
324 | { | |
325 | return QFontMetrics(font()).ascent(); | |
326 | } | |
327 | ||
328 | TickPositions Ruler::calculate_tick_positions( | |
c677193d | 329 | const pv::util::Timestamp& major_period, |
4b0af0b6 JS |
330 | const pv::util::Timestamp& offset, |
331 | const double scale, | |
332 | const int width, | |
4a076157 | 333 | const unsigned int minor_tick_count, |
6f925ba9 | 334 | function<QString(const pv::util::Timestamp&)> format_function) |
4b0af0b6 JS |
335 | { |
336 | TickPositions tp; | |
337 | ||
4a076157 | 338 | const pv::util::Timestamp minor_period = major_period / minor_tick_count; |
4b0af0b6 JS |
339 | const pv::util::Timestamp first_major_division = floor(offset / major_period); |
340 | const pv::util::Timestamp first_minor_division = ceil(offset / minor_period); | |
341 | const pv::util::Timestamp t0 = first_major_division * major_period; | |
342 | ||
343 | int division = (round(first_minor_division - | |
4a076157 | 344 | first_major_division * minor_tick_count)).convert_to<int>() - 1; |
4b0af0b6 JS |
345 | |
346 | double x; | |
347 | ||
348 | do { | |
c677193d | 349 | pv::util::Timestamp t = t0 + division * minor_period; |
4b0af0b6 JS |
350 | x = ((t - offset) / scale).convert_to<double>(); |
351 | ||
4a076157 | 352 | if (division % minor_tick_count == 0) { |
2d141937 | 353 | // Recalculate 't' without using 'minor_period' which is a fraction |
4a076157 | 354 | t = t0 + division / minor_tick_count * major_period; |
4b0af0b6 JS |
355 | tp.major.emplace_back(x, format_function(t)); |
356 | } else { | |
357 | tp.minor.emplace_back(x); | |
358 | } | |
359 | ||
360 | division++; | |
361 | } while (x < width); | |
362 | ||
363 | return tp; | |
364 | } | |
365 | ||
581724de | 366 | void Ruler::on_hover_point_changed(const QWidget* widget, const QPoint &hp) |
b3a7c013 | 367 | { |
581724de | 368 | (void)widget; |
873e8035 SA |
369 | (void)hp; |
370 | ||
b3a7c013 JH |
371 | update(); |
372 | } | |
373 | ||
4b0af0b6 JS |
374 | void Ruler::invalidate_tick_position_cache() |
375 | { | |
376 | tick_position_cache_ = boost::none; | |
377 | } | |
378 | ||
2543cd4e | 379 | void Ruler::on_createMarker() |
4b0af0b6 | 380 | { |
710c2a18 | 381 | hover_item_ = view_.add_flag(get_absolute_time_from_x_pos(mouse_down_point_.x())); |
4b0af0b6 JS |
382 | } |
383 | ||
e23567ed SA |
384 | void Ruler::on_setZeroPosition() |
385 | { | |
fe68068b | 386 | view_.set_zero_position(get_absolute_time_from_x_pos(mouse_down_point_.x())); |
e23567ed SA |
387 | } |
388 | ||
4bc9230c SA |
389 | void Ruler::on_toggleHoverMarker() |
390 | { | |
391 | GlobalSettings settings; | |
392 | const bool state = settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool(); | |
393 | settings.setValue(GlobalSettings::Key_View_ShowHoverMarker, !state); | |
394 | } | |
395 | ||
1573bf16 | 396 | } // namespace trace |
f4e57597 | 397 | } // namespace views |
ccdd3ef5 | 398 | } // namespace pv |