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