]> sigrok.org Git - pulseview.git/blame_incremental - pv/views/trace/ruler.cpp
Fixes
[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{
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
117pv::util::Timestamp Ruler::get_absolute_time_from_x_pos(uint32_t x) const
118{
119 return view_.offset() + ((double)x + 0.5) * view_.scale();
120}
121
122pv::util::Timestamp Ruler::get_ruler_time_from_x_pos(uint32_t x) const
123{
124 return view_.ruler_offset() + ((double)x + 0.5) * view_.scale();
125}
126
127void Ruler::contextMenuEvent(QContextMenuEvent *event)
128{
129 MarginWidget::contextMenuEvent(event);
130
131 // Don't show a context menu if the MarginWidget found a widget that shows one
132 if (event->isAccepted())
133 return;
134
135 context_menu_x_pos_ = event->pos().x();
136
137 QMenu *const menu = new QMenu(this);
138
139 QAction *const create_marker = new QAction(tr("Create marker here"), this);
140 connect(create_marker, SIGNAL(triggered()), this, SLOT(on_createMarker()));
141 menu->addAction(create_marker);
142
143 QAction *const set_zero_position = new QAction(tr("Set as zero point"), this);
144 connect(set_zero_position, SIGNAL(triggered()), this, SLOT(on_setZeroPosition()));
145 menu->addAction(set_zero_position);
146
147 QAction *const toggle_hover_marker = new QAction(this);
148 connect(toggle_hover_marker, SIGNAL(triggered()), this, SLOT(on_toggleHoverMarker()));
149 menu->addAction(toggle_hover_marker);
150
151 GlobalSettings settings;
152 const bool hover_marker_shown =
153 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
154 toggle_hover_marker->setText(hover_marker_shown ?
155 tr("Disable mouse hover marker") : tr("Enable mouse hover marker"));
156
157 event->setAccepted(true);
158 menu->popup(event->globalPos());
159}
160
161void Ruler::resizeEvent(QResizeEvent*)
162{
163 // the tick calculation depends on the width of this widget
164 invalidate_tick_position_cache();
165}
166
167vector< shared_ptr<ViewItem> > Ruler::items()
168{
169 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
170 return vector< shared_ptr<ViewItem> >(
171 time_items.begin(), time_items.end());
172}
173
174shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
175{
176 const vector< shared_ptr<TimeItem> > items(view_.time_items());
177 for (auto i = items.rbegin(); i != items.rend(); i++)
178 if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt))
179 return *i;
180 return nullptr;
181}
182
183void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
184{
185 view_.add_flag(get_ruler_time_from_x_pos(event->x()));
186}
187
188void Ruler::paintEvent(QPaintEvent*)
189{
190 if (!tick_position_cache_) {
191 auto ffunc = [this](const pv::util::Timestamp& t) {
192 return format_time_with_distance(
193 this->view_.tick_period(),
194 t,
195 this->view_.tick_prefix(),
196 this->view_.time_unit(),
197 this->view_.tick_precision());
198 };
199
200 tick_position_cache_ = calculate_tick_positions(
201 view_.tick_period(),
202 view_.ruler_offset(),
203 view_.scale(),
204 width(),
205 view_.minor_tick_count(),
206 ffunc);
207 }
208
209 const int ValueMargin = 3;
210
211 const int text_height = calculate_text_height();
212 const int ruler_height = RulerHeight * text_height;
213 const int major_tick_y1 = text_height + ValueMargin * 2;
214 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
215
216 QPainter p(this);
217
218 // Draw the tick marks
219 p.setPen(palette().color(foregroundRole()));
220
221 for (const auto& tick: tick_position_cache_->major) {
222 const int leftedge = 0;
223 const int rightedge = width();
224 const int x_tick = tick.first;
225 if ((x_tick > leftedge) && (x_tick < rightedge)) {
226 const int x_left_bound = QFontMetrics(font()).width(tick.second) / 2;
227 const int x_right_bound = rightedge - x_left_bound;
228 const int x_legend = min(max(x_tick, x_left_bound), x_right_bound);
229 p.drawText(x_legend, ValueMargin, 0, text_height,
230 AlignCenter | AlignTop | TextDontClip, tick.second);
231 p.drawLine(QPointF(x_tick, major_tick_y1),
232 QPointF(tick.first, ruler_height));
233 }
234 }
235
236 for (const auto& tick: tick_position_cache_->minor) {
237 p.drawLine(QPointF(tick, minor_tick_y1),
238 QPointF(tick, ruler_height));
239 }
240
241 // Draw the hover mark
242 draw_hover_mark(p, text_height);
243
244 p.setRenderHint(QPainter::Antialiasing);
245
246 // The cursor labels are not drawn with the arrows exactly on the
247 // bottom line of the widget, because then the selection shadow
248 // would be clipped away.
249 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
250
251 // Draw the items
252 const vector< shared_ptr<TimeItem> > items(view_.time_items());
253 for (auto &i : items) {
254 const bool highlight = !item_dragging_ &&
255 i->label_rect(r).contains(mouse_point_);
256 i->paint_label(p, r, highlight);
257 }
258}
259
260void Ruler::draw_hover_mark(QPainter &p, int text_height)
261{
262 const int x = view_.hover_point().x();
263
264 if (x == -1)
265 return;
266
267 p.setPen(QPen(Qt::NoPen));
268 p.setBrush(QBrush(palette().color(foregroundRole())));
269
270 const int b = RulerHeight * text_height;
271 const float hover_arrow_size = HoverArrowSize * text_height;
272 const QPointF points[] = {
273 QPointF(x, b),
274 QPointF(x - hover_arrow_size, b - hover_arrow_size),
275 QPointF(x + hover_arrow_size, b - hover_arrow_size)
276 };
277 p.drawPolygon(points, countof(points));
278}
279
280int Ruler::calculate_text_height() const
281{
282 return QFontMetrics(font()).ascent();
283}
284
285TickPositions Ruler::calculate_tick_positions(
286 const pv::util::Timestamp& major_period,
287 const pv::util::Timestamp& offset,
288 const double scale,
289 const int width,
290 const unsigned int minor_tick_count,
291 function<QString(const pv::util::Timestamp&)> format_function)
292{
293 TickPositions tp;
294
295 const pv::util::Timestamp minor_period = major_period / minor_tick_count;
296 const pv::util::Timestamp first_major_division = floor(offset / major_period);
297 const pv::util::Timestamp first_minor_division = ceil(offset / minor_period);
298 const pv::util::Timestamp t0 = first_major_division * major_period;
299
300 int division = (round(first_minor_division -
301 first_major_division * minor_tick_count)).convert_to<int>() - 1;
302
303 double x;
304
305 do {
306 pv::util::Timestamp t = t0 + division * minor_period;
307 x = ((t - offset) / scale).convert_to<double>();
308
309 if (division % minor_tick_count == 0) {
310 // Recalculate 't' without using 'minor_period' which is a fraction
311 t = t0 + division / minor_tick_count * major_period;
312 tp.major.emplace_back(x, format_function(t));
313 } else {
314 tp.minor.emplace_back(x);
315 }
316
317 division++;
318 } while (x < width);
319
320 return tp;
321}
322
323void Ruler::on_hover_point_changed(const QWidget* widget, const QPoint &hp)
324{
325 (void)widget;
326 (void)hp;
327
328 update();
329}
330
331void Ruler::invalidate_tick_position_cache()
332{
333 tick_position_cache_ = boost::none;
334}
335
336void Ruler::on_createMarker()
337{
338 view_.add_flag(get_absolute_time_from_x_pos(mouse_down_point_.x()));
339}
340
341void Ruler::on_setZeroPosition()
342{
343 view_.set_zero_position(get_absolute_time_from_x_pos(mouse_down_point_.x()));
344}
345
346void Ruler::on_toggleHoverMarker()
347{
348 GlobalSettings settings;
349 const bool state = settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
350 settings.setValue(GlobalSettings::Key_View_ShowHoverMarker, !state);
351}
352
353} // namespace trace
354} // namespace views
355} // namespace pv