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