]> sigrok.org Git - pulseview.git/blame - pv/views/trace/ruler.cpp
Backport recent changes from mainline.
[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
JH
22#include <QApplication>
23#include <QFontMetrics>
3782d860 24#include <QMenu>
819e2e95
JH
25#include <QMouseEvent>
26
3782d860
UH
27#include <pv/globalsettings.hpp>
28
819e2e95 29#include "ruler.hpp"
2acdb232 30#include "view.hpp"
ccdd3ef5 31
819f4c25 32using namespace Qt;
f76af637 33
6f925ba9 34using std::function;
3782d860
UH
35using std::max;
36using std::min;
819e2e95
JH
37using std::shared_ptr;
38using std::vector;
39
ccdd3ef5 40namespace pv {
f4e57597 41namespace views {
1573bf16 42namespace trace {
ccdd3ef5 43
3d79f521 44const float Ruler::RulerHeight = 2.5f; // x Text Height
ccdd3ef5 45
3d79f521 46const float Ruler::HoverArrowSize = 0.5f; // x Text Height
b3a7c013 47
ccdd3ef5 48Ruler::Ruler(View &parent) :
e456e8e1 49 MarginWidget(parent)
ccdd3ef5 50{
b3a7c013
JH
51 setMouseTracking(true);
52
3782d860
UH
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
69QSize 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
75QSize 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
85QString 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,
91 bool sign)
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)
100 return pv::util::format_time_si_adjusted(t, prefix, precision, "sa", sign);
101
102 // View zoomed way out -> low precision (0), big distance (>=60s)
103 // -> DD:HH:MM
104 if ((precision == 0) && (distance >= limit))
105 return pv::util::format_time_minutes(t, 0, sign);
106
107 // View in "normal" range -> medium precision, medium step size
108 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds
109 // View zoomed way in -> high precision (>3), low step size (<1s)
110 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds
111 if (abs(t) < limit)
112 return pv::util::format_time_si_adjusted(t, prefix, precision, "s", sign);
113 else
114 return pv::util::format_time_minutes(t, precision, sign);
115}
116
3782d860
UH
117pv::util::Timestamp Ruler::get_time_from_x_pos(uint32_t x) const
118{
119 return view_.ruler_offset() + ((double)x + 0.5) * view_.scale();
120}
121
122void Ruler::contextMenuEvent(QContextMenuEvent *event)
123{
124 MarginWidget::contextMenuEvent(event);
125
126 // Don't show a context menu if the MarginWidget found a widget that shows one
127 if (event->isAccepted())
128 return;
129
130 context_menu_x_pos_ = event->pos().x();
131
132 QMenu *const menu = new QMenu(this);
133
134 QAction *const create_marker = new QAction(tr("Create marker here"), this);
135 connect(create_marker, SIGNAL(triggered()), this, SLOT(on_createMarker()));
136 menu->addAction(create_marker);
137
138 QAction *const set_zero_position = new QAction(tr("Set as zero point"), this);
139 connect(set_zero_position, SIGNAL(triggered()), this, SLOT(on_setZeroPosition()));
140 menu->addAction(set_zero_position);
141
142 QAction *const toggle_hover_marker = new QAction(this);
143 connect(toggle_hover_marker, SIGNAL(triggered()), this, SLOT(on_toggleHoverMarker()));
144 menu->addAction(toggle_hover_marker);
145
146 GlobalSettings settings;
147 const bool hover_marker_shown =
148 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
149 toggle_hover_marker->setText(hover_marker_shown ?
150 tr("Disable mouse hover marker") : tr("Enable mouse hover marker"));
151
152 event->setAccepted(true);
153 menu->popup(event->globalPos());
154}
155
156void Ruler::resizeEvent(QResizeEvent*)
157{
158 // the tick calculation depends on the width of this widget
159 invalidate_tick_position_cache();
160}
161
3e124bee
JH
162vector< shared_ptr<ViewItem> > Ruler::items()
163{
164 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
165 return vector< shared_ptr<ViewItem> >(
166 time_items.begin(), time_items.end());
167}
168
6871ee9f 169shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
25a0d47c
JH
170{
171 const vector< shared_ptr<TimeItem> > items(view_.time_items());
172 for (auto i = items.rbegin(); i != items.rend(); i++)
173 if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt))
174 return *i;
175 return nullptr;
176}
177
3782d860
UH
178void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
179{
180 view_.add_flag(get_time_from_x_pos(event->x()));
181}
182
e314eca4 183void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 184{
4b0af0b6 185 if (!tick_position_cache_) {
2ad82c2e 186 auto ffunc = [this](const pv::util::Timestamp& t) {
3ccf0f7f
JS
187 return format_time_with_distance(
188 this->view_.tick_period(),
4b0af0b6
JS
189 t,
190 this->view_.tick_prefix(),
191 this->view_.time_unit(),
192 this->view_.tick_precision());
193 };
194
7c6cbdda 195 tick_position_cache_ = calculate_tick_positions(
4b0af0b6 196 view_.tick_period(),
3782d860 197 view_.ruler_offset(),
4b0af0b6
JS
198 view_.scale(),
199 width(),
3782d860 200 view_.minor_tick_count(),
7c6cbdda 201 ffunc);
4b0af0b6 202 }
ccdd3ef5 203
4b0af0b6 204 const int ValueMargin = 3;
f260e3bf 205
e456e8e1 206 const int text_height = calculate_text_height();
249229ec 207 const int ruler_height = RulerHeight * text_height;
e456e8e1 208 const int major_tick_y1 = text_height + ValueMargin * 2;
249229ec 209 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
f260e3bf 210
4b0af0b6 211 QPainter p(this);
ccdd3ef5 212
4b0af0b6
JS
213 // Draw the tick marks
214 p.setPen(palette().color(foregroundRole()));
ccdd3ef5 215
4b0af0b6 216 for (const auto& tick: tick_position_cache_->major) {
3782d860
UH
217 const int leftedge = 0;
218 const int rightedge = width();
219 const int x_tick = tick.first;
220 if ((x_tick > leftedge) && (x_tick < rightedge)) {
221 const int x_left_bound = QFontMetrics(font()).width(tick.second) / 2;
222 const int x_right_bound = rightedge - x_left_bound;
223 const int x_legend = min(max(x_tick, x_left_bound), x_right_bound);
224 p.drawText(x_legend, ValueMargin, 0, text_height,
4b0af0b6 225 AlignCenter | AlignTop | TextDontClip, tick.second);
3782d860 226 p.drawLine(QPointF(x_tick, major_tick_y1),
4b0af0b6 227 QPointF(tick.first, ruler_height));
3782d860 228 }
4b0af0b6 229 }
ccdd3ef5 230
4b0af0b6
JS
231 for (const auto& tick: tick_position_cache_->minor) {
232 p.drawLine(QPointF(tick, minor_tick_y1),
233 QPointF(tick, ruler_height));
234 }
ccdd3ef5 235
b3a7c013 236 // Draw the hover mark
249229ec 237 draw_hover_mark(p, text_height);
819e2e95 238
97f71253
JS
239 p.setRenderHint(QPainter::Antialiasing);
240
819e2e95
JH
241 // The cursor labels are not drawn with the arrows exactly on the
242 // bottom line of the widget, because then the selection shadow
243 // would be clipped away.
09d5df11 244 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
819e2e95
JH
245
246 // Draw the items
247 const vector< shared_ptr<TimeItem> > items(view_.time_items());
49028d6c 248 for (auto &i : items) {
803cdac4 249 const bool highlight = !item_dragging_ &&
49028d6c
JH
250 i->label_rect(r).contains(mouse_point_);
251 i->paint_label(p, r, highlight);
252 }
819e2e95
JH
253}
254
3782d860
UH
255void Ruler::draw_hover_mark(QPainter &p, int text_height)
256{
257 const int x = view_.hover_point().x();
258
259 if (x == -1)
260 return;
261
262 p.setPen(QPen(Qt::NoPen));
263 p.setBrush(QBrush(palette().color(foregroundRole())));
264
265 const int b = RulerHeight * text_height;
266 const float hover_arrow_size = HoverArrowSize * text_height;
267 const QPointF points[] = {
268 QPointF(x, b),
269 QPointF(x - hover_arrow_size, b - hover_arrow_size),
270 QPointF(x + hover_arrow_size, b - hover_arrow_size)
271 };
272 p.drawPolygon(points, countof(points));
273}
274
275int Ruler::calculate_text_height() const
276{
277 return QFontMetrics(font()).ascent();
278}
279
280TickPositions Ruler::calculate_tick_positions(
c677193d 281 const pv::util::Timestamp& major_period,
4b0af0b6
JS
282 const pv::util::Timestamp& offset,
283 const double scale,
284 const int width,
3782d860 285 const unsigned int minor_tick_count,
6f925ba9 286 function<QString(const pv::util::Timestamp&)> format_function)
4b0af0b6
JS
287{
288 TickPositions tp;
289
3782d860 290 const pv::util::Timestamp minor_period = major_period / minor_tick_count;
4b0af0b6
JS
291 const pv::util::Timestamp first_major_division = floor(offset / major_period);
292 const pv::util::Timestamp first_minor_division = ceil(offset / minor_period);
293 const pv::util::Timestamp t0 = first_major_division * major_period;
294
295 int division = (round(first_minor_division -
3782d860 296 first_major_division * minor_tick_count)).convert_to<int>() - 1;
4b0af0b6
JS
297
298 double x;
299
300 do {
c677193d 301 pv::util::Timestamp t = t0 + division * minor_period;
4b0af0b6
JS
302 x = ((t - offset) / scale).convert_to<double>();
303
3782d860 304 if (division % minor_tick_count == 0) {
2d141937 305 // Recalculate 't' without using 'minor_period' which is a fraction
3782d860 306 t = t0 + division / minor_tick_count * major_period;
4b0af0b6
JS
307 tp.major.emplace_back(x, format_function(t));
308 } else {
309 tp.minor.emplace_back(x);
310 }
311
312 division++;
313 } while (x < width);
314
315 return tp;
316}
317
3782d860 318void Ruler::on_hover_point_changed(const QWidget* widget, const QPoint &hp)
819e2e95 319{
3782d860
UH
320 (void)widget;
321 (void)hp;
333d5bbc 322
3782d860 323 update();
b3a7c013
JH
324}
325
3782d860 326void Ruler::invalidate_tick_position_cache()
819e2e95 327{
3782d860 328 tick_position_cache_ = boost::none;
819e2e95
JH
329}
330
3782d860 331void Ruler::on_createMarker()
b3a7c013 332{
3782d860 333 view_.add_flag(get_time_from_x_pos(mouse_down_point_.x()));
b3a7c013
JH
334}
335
3782d860 336void Ruler::on_setZeroPosition()
4b0af0b6 337{
3782d860 338 view_.set_zero_position(get_time_from_x_pos(mouse_down_point_.x()));
4b0af0b6
JS
339}
340
3782d860 341void Ruler::on_toggleHoverMarker()
4b0af0b6 342{
3782d860
UH
343 GlobalSettings settings;
344 const bool state = settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
345 settings.setValue(GlobalSettings::Key_View_ShowHoverMarker, !state);
4b0af0b6
JS
346}
347
1573bf16 348} // namespace trace
f4e57597 349} // namespace views
ccdd3ef5 350} // namespace pv