]> sigrok.org Git - pulseview.git/blame - pv/views/trace/cursor.cpp
Adjust trace view header width when signal names change
[pulseview.git] / pv / views / trace / cursor.cpp
CommitLineData
cabce982
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
cabce982
JH
18 */
19
2acdb232 20#include "cursor.hpp"
cabce982 21
aca9aa83 22#include "pv/util.hpp"
3ccf0f7f 23#include "ruler.hpp"
2acdb232 24#include "view.hpp"
cabce982 25
4fabd61a 26#include <QApplication>
cabce982
JH
27#include <QBrush>
28#include <QPainter>
34c1ef02 29#include <QPointF>
cabce982
JH
30#include <QRect>
31#include <QRectF>
32
f9abf97e
JH
33#include <cassert>
34#include <cstdio>
e899d8bd 35#include <limits>
34c1ef02 36
20f59e95 37using std::abs; // NOLINT. Force usage of std::abs() instead of C's abs().
f9abf97e 38using std::shared_ptr;
58864c5c 39
cabce982 40namespace pv {
f4e57597 41namespace views {
1573bf16 42namespace trace {
cabce982 43
cabce982 44const QColor Cursor::FillColour(52, 101, 164);
34c1ef02 45
8debe10d 46Cursor::Cursor(View &view, double time) :
4fabd61a 47 TimeMarker(view, FillColour, time)
cabce982
JH
48{
49}
50
096fb584
JH
51bool Cursor::enabled() const
52{
53 return view_.cursors_shown();
54}
55
3b84fd6d
JH
56QString Cursor::get_text() const
57{
3ccf0f7f
JS
58 const shared_ptr<Cursor> other = get_other_cursor();
59 const pv::util::Timestamp& diff = abs(time_ - other->time_);
60
61 return Ruler::format_time_with_distance(
62 diff, time_, view_.tick_prefix(), view_.time_unit(), view_.tick_precision());
3b84fd6d
JH
63}
64
689dea92 65QRectF Cursor::label_rect(const QRectF &rect) const
cabce982 66{
58864c5c
JH
67 const shared_ptr<Cursor> other(get_other_cursor());
68 assert(other);
69
cb5a1216 70 const float x = get_x();
34c1ef02 71
4fabd61a 72 QFontMetrics m(QApplication::font());
3b84fd6d 73 QSize text_size = m.boundingRect(get_text()).size();
4fabd61a 74
34c1ef02 75 const QSizeF label_size(
ec39632d
JH
76 text_size.width() + LabelPadding.width() * 2,
77 text_size.height() + LabelPadding.height() * 2);
199441e4 78 const float top = rect.height() - label_size.height() -
6abffa97 79 TimeMarker::ArrowSize - 0.5f;
5139748b 80 const float height = label_size.height();
199441e4 81
60d9b99a
JS
82 const pv::util::Timestamp& other_time = other->time();
83
e899d8bd 84 if (time_ > other_time ||
60d9b99a 85 (abs(time_ - other_time).is_zero() && this > other.get()))
199441e4
JH
86 return QRectF(x, top, label_size.width(), height);
87 else
60d9b99a 88 return QRectF(x - label_size.width(), top, label_size.width(), height);
ca4ec3ea
JH
89}
90
58864c5c
JH
91shared_ptr<Cursor> Cursor::get_other_cursor() const
92{
5c5ce757
JH
93 const shared_ptr<CursorPair> cursors(view_.cursors());
94 assert(cursors);
95 return (cursors->first().get() == this) ?
96 cursors->second() : cursors->first();
58864c5c
JH
97}
98
1573bf16 99} // namespace trace
f4e57597 100} // namespace views
cabce982 101} // namespace pv